Reputation: 297155
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:
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
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:
Upvotes: 0
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
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