aspose file tools
The moose likes Testing and the fly likes JUnit. How to pass parameters into Test classes from suite() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit. How to pass parameters into Test classes from suite() ?" Watch "JUnit. How to pass parameters into Test classes from suite() ?" New topic

JUnit. How to pass parameters into Test classes from suite() ?

Andrey Frolov

Joined: Apr 16, 2011
Posts: 1
Hi everybody.

What do I have now (example code, just fore more clear explanation).

Class which is used for running all tests:

Test classes (FirstTest, SecondTest) uses field "id", which is declared in their common super class somehow like this:

So, what do I need.
Want to write class AllMultiIdsTests, which also will be responsible for running test classes FirstTest and SecondTest.
But unlike AllTests, it would be do it for ids collection.

Something like this:

Or it's also possible to rewrite FirstTest, SecondTest in a way when they will use single id or ids collection in depend on some new field. But I also need to pass value of this field from AllMultiIdsTests and AllTests... How can I do it?

For now I haven't found anything related to configuring Test classes from class which run them all...

PS: Sorry for any errors in English if there are some. Thanks.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32299

Welcome to CodeRanch! This is an interesting question. I can think of two approaches.

Option 1
If you add the same test multiple times, JUnit will run it. This means you can create a dummy test where the the test itself sets the property.
1) The suite is still a JUnit test.
2) You get one set of output
1) You are relying on JUnit continuing to run the tests in the same order as specified in the test suite. It is documented that the order isn't guaranteed so you would be introducing code that relies on a side effect.
2) It isn't apparent which test has failed. (You can mitigate this by having DummyTestToSetProperty output what the current property is.)

Option 2
Run the test suite itself multiple times from a main method.
1) Cleaner and not subject to a side effect
1) Runs multiple suites with separate output. Could be a problem for your build tool

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Consider Paul's rocket mass heater.
subject: JUnit. How to pass parameters into Test classes from suite() ?
It's not a secret anymore!