Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests must locate in the same package?

A JUnit book says " protected method ... this is one reason the test classes are located in the same package as the classes they are testing"

Can someone share their experience on how to organize the unit tests and integration tests (package/directory wise)?

like image 918
sean Avatar asked Sep 01 '10 04:09

sean


People also ask

Should unit tests be in the same package?

Unit tests can be in any package. In essence they are just separate classes used to test the behaviour of the class being tested.

Should unit tests be in a separate file?

We have a requirement that the unit testing file need to be separate with the source file in project building. It means we must not do this for testing the source file.

Where should the unit test reside in the repository?

The developers should write unit tests alongside the source code and execute them in pipelines. Separating the repositories keeps more clean environment. Repositories contain only things it's concerned about.

Should unit tests depend on each other?

Tests should never depend on each other. If your tests have to be run in a specific order, then you need to change your tests. Instead, you should make proper use of the Setup and TearDown features of your unit-testing framework to ensure each test is ready to run individually.


2 Answers

I prefer the maven directory layout. It helps you separate the test sources and test resources from your application sources in a nice way and still allow them to be part of the same package.

I use this for both maven and ant based projects.

  project
    |
    +- src
        |
        +- main
        |    |
        |    +- java // com.company.packge (sources)
        |    +- resources
        |
        +- test
             |
             +- java // com.company.package (tests)
             +- resources
like image 112
naikus Avatar answered Oct 04 '22 12:10

naikus


in my build process, the source directories are

java/src
java/test/unit
java/test/integration

The test and the source code are in different paths, but the packages are the same

java/src/com/mypackage/domain/Foo.java
java/test/unit/com/mypackage/domain/FooTest.java
java/test/integration/com/mypackage/domain/FooTest.java
like image 20
Aaron Saunders Avatar answered Oct 04 '22 12:10

Aaron Saunders