Where do you place unit tests for private methods?

Where do you put unit tests for private functions in C # classes?

Wikipedia article suggests:

  • Setting tests in the same class as those they test.
  • Using partial classes

Personally, none of these methods work, and I prefer the unit tests to be in a separate project in general.

Any thoughts on this?

(I know that there is quite a bit of discussion about whether private methods should be checked at all. I would like to hear both sides of the argument.)

+4
source share
8 answers

Private methods do not have to be tested directly. You can determine their validity based on tests of public methods that use these private methods.

However, you should make sure that your public methods can easily introduce false dependencies into your private methods to facilitate testing and modeling of all reasonable scenarios.

Edit: As for where your other tests should be located, I suggest a separate subdirectory in your project to handle your tests. When writing tests for PHP applications, I have the tests directory in the root directory of my project, the directory structure of which matches the structure of my real application directory structure. In it, I have a test class for each real class.

Just don’t compile your test classes with the rest of your project during production release (or in the case of an interpreted language such as PHP, do not deploy test classes to a web server).

+14
source

Do not close personal unit test methods. Unit tests are designed to test the visible (so public and secure) class interface. Private methods are implementation details, and they do not need to perform unit tests for them (their behavior must be implicitly tested on tests for visible methods), leads to fragile tests (since implementation details can change) and is an obstacle to refactoring.

If you have a private method that you think you need unit test for, this is a big hint that you should probably include it in an open method in another class.

Where do you put unit tests for private functions in C # classes?

Nowhere. They do not exist.

In general, my unit tests are in separate projects.

+9
source

I also personally like to have unit tests in a separate project. If you want unit test a private method, you can make the private method internal. You can then make the internal methods visible to your unit tests for a direct call by adding the following to AssemblyInfo.cs:

[assembly: InternalsVisibleTo("MyAssembly.UnitTests")] 
+9
source

You must do what works for you; here is what works for me:

My module is a class : this is what I am trying to verify. Not a method. I am trying to do object oriented programming, so I paid attention to objects.

If I am tempted to test a private method, I need refactoring . I just want to test the private method directly, because there is too much other code between it and the tests, and because the private method itself is complex enough to require attention checking. So, I usually take out a class to pull this private method and other related members into a new class.

My classes are usually pretty small . They are easy to read and understand. My methods, of course, are also very small and understandable.

Switching to this way of working required me to rethink many of my assumptions and programming habits. What once seemed radical now seems commonplace.

+3
source

I agree that private methods should not be tested, in general, because you should only test public interfaces.

There are reasons why you can test private methods:

  • You are using TDD and you need to develop a complex private method. Creating test methods for a private method may be required in order to keep the write-write code of the write code in the correct granularity.

  • You can be part of a team where others may need to change a private method, and you want the tests to ensure that the changes do not cause problems.

Some solutions:

  • Declare some public methods that pass to the private method and are used only for testing purposes. They can be a prefix, for example. with TestFoo1, TestFoo2, etc.

  • Use internal

http://msdn.microsoft.com/en-us/library/7c5ka91b(VS.80).aspx

+2
source

The title of the question and the first sentence are different. :)

I am not sure where to place your tests. It will depend on the language, and C # is not what I am familiar with, but I believe that a significant part of your code is inside private methods. I would be uncomfortable if this had not been verified. Code coverage will drop quite a bit.

+1
source

absolutely yes. private methods should be tested anyway. and mbunit unit test framework can access a private member.

see this blog: testing private methods

+1
source

We use private accessors in Visual Studio to test private methods. This means that test classes can live in a separate project.

However, we are trying to really limit their number, because:

  • a private method that can stand alone can be pulled out as a public method in another class
  • private methods that do nothing particularly useful outside the class can be tested using the public methods of this class.
+1
source

All Articles