Where should I put my test code for my class?

So, I wrote a class, and I have code to test it, but where should I put this code? I could make a static Test () method for the class, but it doesn't have to be at production time and clutter the class declaration. A bit of searching told me to put the test code in a separate project, but what would be the format of this project? One static class with a method for each of the classes, so if my class is called Randomizer, will this method be called testRandomizer?

What are some guidelines for organizing test code?

EDIT: I initially put the question in various languages, which I thought was appropriate, but it seems that the general answer to the question might be “use a testing framework” that is language specific: D

+4
source share
10 answers

If you use a test environment (I highly recommend doing this) or not, the best place for unit tests is in a separate assembly (C / C ++ / C #) or in a package (Java).

You will only have access to public and protected classes and methods, however unit testing usually only checks public APIs.

I recommend that you add a separate test project / assembly / package for each existing project / assembly / package.

The format of the project depends on the test environment - for the .NET test project, use VSs built into the test project template, or NUnit in your version of VS does not support unit testing, to use Java JUnit for C / C ++, possibly CppUnit (I have not tried this).

Test projects typically contain one static class initialization method, one static class method, one non-static init method for all tests, one non-static break method for all tests, and one non-static method for test + any other methods you add.

Static methods allow you to copy DLLs, configure the test environment and clear the test environment; non-static general methods are designed to reduce duplicate code and real test methods for preparing test input, expected result and comparing them.

+4
source

Where you put your test code depends on what you intend to do with the code. If this is a separate class, which, for example, you intend to provide to other users for download and use, then the test code should be a project within the solution. The test code, in addition to verifying that the class does what you wanted to do, is an example for users of your class, so it should be well documented and extremely clear.

If, on the other hand, your class is part of a library or DLL and is designed to work only within the ecosystem of this library or DLL, then there should be a test program or framework that uses the DLL as an organization. Code coverage tools will demonstrate that test code actually implements code. In my experience, these test programs, like one class program, are built as a project as part of a solution that creates a DLL or library.

Please note that in both of the above cases, the test project is not built as part of the standard build process. You must create it specifically.

Finally, if your class should be part of a larger project, your test code should be part of any infrastructure or process flow that has been defined for your large team. In my current project, for example, tests of developer modules are supported in a separate source control tree, which has a structure parallel to the structure of the delivery code. Unit tests are required to pass a code review by both developers and the testing team. During the build process (every day right now) we create a delivery code, then test the unit, then set the test QA code. Unit tests run before the QA code, and everything should pass. This is pretty much a smoke test, to make sure we don't violate the lowest level of functionality. Unit tests are required to report a failure and exit with a negative status code. Our processes are probably more formal than many.

+3
source

In Java, you should use Junit4, either on your own, or (I think better) with an IDE. We used three environments: Eclipse, NetBeans, and Maven (with and without an IDE). There may be a slight incompatibility between them if the system is not deployed systematically.

As a rule, all tests are in one project, but in a different directory / folder. Thus, the class:

org.foo.Bar.java 

will have a test

 org.foo.BarTest.java 

They are in the same package (org.foo), but will be organized in directories:

 src/main/java/org/foo/Bar.java 

and

 src/test/java/org/foo/BarTest.java 

These directories are recognized by Eclipse, NetBeans, and Maven. Maven is the most picky, while Eclipse does not always provide rigor.

You should probably avoid calling the other TestPlugh or XyzzyTest classes, as some (old) tools will select them as containing tests, even if they do not.

Even if you have only one test for your method (and most testing authorities would have expected more extreme cases), you should organize this type of structure.

EDIT Please note that Maven can create distributions without tests, even if they are in the same package. By default, Maven also requires that all tests pass before the project is deployed.

+2
source

Most installations I have seen or used have a separate project in which there are tests. This simplifies work and improves work. As a standalone project, it’s easy to deploy your code without worrying that tests are part of a live system.

As testing progressed, I saw separate projects for unit tests, integration tests, and regression tests. One of the main ideas for this is to have your device tests run as quickly as possible. Integration and regression tests usually take longer due to the nature of their tests (connecting to databases, etc.)

+1
source

I usually create a parallel package structure in a separate source tree in the same project. Thus, your tests have access to open, protected, and even closed packages to members of the tested class, which is often useful to have.

For example, I may have

 myproject src main com.acme.myapp.model User com.acme.myapp.web RegisterController test com.acme.myapp.model UserTest com.acme.myapp.web RegisterControllerTest 

Maven does this, but the approach is not particularly tied to Maven.

+1
source

This will depend on the testing scheme you use. Unit, Nunit, some other? Each of them documents how to organize the test code. In addition, if you use continuous integration, it will also affect where and how you conduct the test. For example, this article discusses some options.

0
source

Create a new project in the same solution as your code.

If you work with C #, then Visual Studio will do it for you, if you choose Test> New Test ... It has a wizard that guides you through the process.

0
source

hmm. you want to check the random number generator ... maybe it is better to create a strong mathematical proof of the correctness of the algorithm. Because otherwise you must be sure that every sequence ever created has the desired distribution

0
source

In the case of C # and Visual Studio 2010, you can create a test project from templates that will be included in your design solution. Then you can specify which tests should be run during the construction of your project. All tests will take place in a separate assembly.

Otherwise, you can use NUnit Assembly , import it into your solution and start creating methods for the whole object that you need for testing. For large projects, I prefer to find these tests inside a separate assembly.

You can create your own tests, but I highly recommend using the existing infrastructure.

0
source

Create separate projects for unit tests, integration tests, and functional tests. Even if your "real" code has several projects, you can probably do one project for each type of test, but it is important to distinguish between each type of test.

For unit tests, you must create a parallel namespace hierarchy. Therefore, if you have crazy.juggler.drummer.Customer, you should test it in crazy.juggler.drummer.CustomerTest. Thus, it is easy to see which classes are correctly tested.

Functional and integration tests can be more difficult to place, but usually you can find a suitable place. Database level tests probably belong somewhere like my.app.database.DatabaseIntegrationTest. Function tests can guarantee their own namespace: my.app.functionaltests.CustomerCreationWorkflowTest.

But tip # 1: be extremely tough about separating the different types of tests. Especially make sure that the collection of individual tests is separate from the integration tests.

0
source

All Articles