Skip to main content
Post Reopened by candied_orange, Greg Burghardt, Thomas Owens
added 97 characters in body
Source Link

Lets say I have a class with two public methods and one private method.
The private method is used by both public methods and unit tests that are written against either of the public methods, could cover all lines of the private method.
Also the private method is complex and to cover all its behaviors many test cases are needed.

enter image description here

I could now:

Test the private method with only one of the public methods and leave it out for the other one.

Disadvantages:

  • No clear answer, which of the public methods should be used
  • If I chose public method 1 to cover the private method, method 2 is not covered at all. So if method 1 changes in some point in future and is not covering all lines of the private method anymore (but method 2 still does) this test gap could not be noticed.

Write double tests - for each public method the same tests

Disadvantage:

  • To hard to maintain the tests

Make the private method public

Disadvantage:

  • Hurting the encapsulation

How do you treat situations like this?

Lets say I have a class with two public methods and one private method.
The private method is used by both public methods and unit tests that are written against either of the public methods, could cover all lines of the private method.

enter image description here

I could now:

Test the private method with only one of the public methods and leave it out for the other one.

Disadvantages:

  • No clear answer, which of the public methods should be used
  • If I chose public method 1 to cover the private method, method 2 is not covered at all. So if method 1 changes in some point in future and is not covering all lines of the private method anymore (but method 2 still does) this test gap could not be noticed.

Write double tests - for each public method the same tests

Disadvantage:

  • To hard to maintain the tests

Make the private method public

Disadvantage:

  • Hurting the encapsulation

How do you treat situations like this?

Lets say I have a class with two public methods and one private method.
The private method is used by both public methods and unit tests that are written against either of the public methods, could cover all lines of the private method.
Also the private method is complex and to cover all its behaviors many test cases are needed.

enter image description here

I could now:

Test the private method with only one of the public methods and leave it out for the other one.

Disadvantages:

  • No clear answer, which of the public methods should be used
  • If I chose public method 1 to cover the private method, method 2 is not covered at all. So if method 1 changes in some point in future and is not covering all lines of the private method anymore (but method 2 still does) this test gap could not be noticed.

Write double tests - for each public method the same tests

Disadvantage:

  • To hard to maintain the tests

Make the private method public

Disadvantage:

  • Hurting the encapsulation

How do you treat situations like this?

edited title
Link
candied_orange
  • 119.7k
  • 27
  • 233
  • 369

How to unit test private method that is used by several public methods without duping the tests

Post Closed as "Needs details or clarity" by gnat, Doc Brown, Greg Burghardt
Source Link

How to unit test private method that is used by several public methods

Lets say I have a class with two public methods and one private method.
The private method is used by both public methods and unit tests that are written against either of the public methods, could cover all lines of the private method.

enter image description here

I could now:

Test the private method with only one of the public methods and leave it out for the other one.

Disadvantages:

  • No clear answer, which of the public methods should be used
  • If I chose public method 1 to cover the private method, method 2 is not covered at all. So if method 1 changes in some point in future and is not covering all lines of the private method anymore (but method 2 still does) this test gap could not be noticed.

Write double tests - for each public method the same tests

Disadvantage:

  • To hard to maintain the tests

Make the private method public

Disadvantage:

  • Hurting the encapsulation

How do you treat situations like this?