Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

Per-module test setup on Maven

Is there any way with Maven to run a piece of code as setup before testing each module? Cleanup for each module as well would be a plus.

To give an example, let's say I have modules A and B, with test classes AA, AB, BA, BB and BC, each of which containing a couple of test method. So module A would run the test classes AA and AB, the first containing the test methods AA.1 and AA.2 and the second AB.1 and AB.2.

Given that, I'd like test setups run like this:

  1. Before module A
  2. Before class AA
  3. Before method AA.1
  4. Before method AA.2
  5. Before class AB
  6. Before method AB.1
  7. Before method AB.2
  8. Before module B
  9. Before class BA
  10. and so forth

It's easy to setup most of this, but I see no way to perform steps 1 and 8, and I'd like to.

EDIT

Yeah, this is the kind of thing that ought to be integration testing, but it isn't, and we are talking about dozens of modules, let alone tests. I need to prepare these resources per-module mostly so the module testing can be made to run in parallel.

Upvotes: 0

Views: 86

Answers (3)

soru
soru

Reputation: 5526

You can do that in JUnit by defining a nested test suite along the lines outlined here, with @Before and @After methods on the sub-suites for the modules.

You then configure maven to only run your new test suite, not the individual tests.

More ambitiously, you could do this based on a naming convention, without having to specify all the individual tests involved, by:

  • setting up a dynamic AllTests top level test
  • Using Guava ClassPath (or similar) to find all visible test suites and test classes
  • adding them to the top level, or nested, test suites according to the package hierarchy

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97359

That sounds like an integration tests. This means you can use the pre-integration-test, integration-test and post-integration-test phase to define your setup etc. which is needed to do your appropriate testing. You can bind whatever plugin to the pre-integration-test phase to make a kind of setup (like database setup via sql-maven-plugin etc).

Furthermore you could use TestNG which support groups etc. and furthermore it supports things like this:

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass
@BeforeMethod

Upvotes: 1

Donald_W
Donald_W

Reputation: 1823

It's not a great solution, but you could always attach an arbitrary plugin and goal to the process-test-classes build phase, which runs by default just before test. See http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference. Obviously this code wouldn't run in the same JVM as your test code, but if you're looking to prepare some external resources, it might be suitable.

If you are preparing external resources, you are probably really performing integration tests, and therefore the pre-integration-test phase is what you'd want to attach to.

Upvotes: 1

Related Questions