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?
send parameters from test suite to test case in junit 4
Asked Answered
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
.
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? –
Kaffir
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. –
Smoothen the static block didn't take effect, so I went with: coderanch.com/t/534637/Testing/… –
Cortico
@Cortico did you get that to work with junit 4 tests cases? –
Disparagement
this answer (https://mcmap.net/q/162729/-before-and-after-suite-execution-hook-in-junit-4-x) worked for me, but required a suite class for each option. –
Disparagement
Instead of use system property, try to use a static class, store a class with all info that you want in memory.
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
© 2022 - 2024 — McMap. All rights reserved.