Building nunit tests without exporting them using api

I am new to unit testing using nunit (and Java development in general). When creating unit tests for private methods in classes, it looks like the test file should be in the same package as the class under test. What is the typical way to avoid exporting unit test APIs? Can I make my test classes / methods package protected? Or do developers usually have a separate release build that excludes unit test files?

0


source to share


5 answers


The test file does not need to be in the same package as the class under test. In fact, it is good practice to keep the test files in a completely separate package, allowing them to test the open API without worrying about package-level implementation details.



Alternatively, you can tweak your script construct (eg Nant) to ignore files containing "Test" when you create the release executable.

0


source


I can tell IntelliJ or Ant not to package JUnit tests into deployment. I have tests in a separate directory from source, which is what makes it possible.



Don't mix source and test classes together. Keep them separate to make it easier to use the tool / script you are using to deploy.

+1


source


Personally, my approach is only to test open functionality, so you can only fully test the encapsulated parts.

This usually results in my design containing small classes with well-defined functionality that are easier to test.

Generally, when unit testing, you shouldn't care about the internals of what you are testing, so I believe this is the best way to approach it.

I also agree that it is best to separate test and production code.

0


source


Store your test source code outside your application source code. In general, check only functionality. If you really need to test the private behavior, create a test object that extends the real object and allows publec access to the private behavior.

0


source


I find it a mistake to move your test code out of the CUT (Class Under Test) package. At some point, you might test a protected method or class, and having the test code in another package makes it difficult or impossible.

The best solution is to create a separate directory for your test code that simply reflects the package structure of your production code. That's what I'm doing:

src/main/java/com/example/Foo.java
src/test/java/com/example/FooTest.java

      

Then your build script can very easily be ignored src/test/**

when it's time to package and deploy.

0


source







All Articles