7

I've got four junit cases and I need to pass them a parameter, that is the same for all of them, but this parameter is created in a dynamic way in the test suite. How can I pass a parameter from the test suite to all the tests in the test case?

James DW
  • 1,815
  • 16
  • 21

3 Answers3

1

Instead of use system property, try to use a static class, store a class with all info that you want in memory.

gonella
  • 186
  • 1
  • 6
1

If its just a string parameter, you can set the System Property and access it in test cases.

If you want to programmatically do it, you can do it at one place System.setProperty("x","123"); otherwise you can always pass System properties from command line as -Dx=123.

kdabir
  • 9,623
  • 3
  • 43
  • 45
  • Thanks for you reply, but can you explain that, i mean if i have this class @RunWith(Suite.class) @SuiteClasses({ TestAddTag.class }) public class AllTests { } How pass to the test TestAddTag a string parameter? – Alexander Herrera Sep 21 '11 at 14:30
  • Can you try this in the static initializer? `@RunWith(Suite.class) @SuiteClasses({ TestAddTag.class }) public class AllTests { static {System.setProperty("x","123");} } ` can then do `System.getProperty("x")` in your test case. – kdabir Sep 21 '11 at 14:35
  • the static block didn't take effect, so I went with: http://www.coderanch.com/t/534637/Testing/JUnit-pass-parameters-Test-classes – pulkitsinghal Dec 09 '13 at 22:57
  • @pulkitsinghal did you get that to work with junit 4 tests cases? – Ray Tayek Jan 14 '16 at 19:29
  • this answer (http://stackoverflow.com/a/177069/51292) worked for me, but required a suite class for each option. – Ray Tayek Jan 14 '16 at 19:46
0

Try parameterized tests. It is a built-in JUnit feature designed to pass parameters to all tests inside a test case. See below link for examples:

https://github.com/junit-team/junit4/wiki/Parameterized-tests

johnny
  • 5,077
  • 1
  • 16
  • 9