send parameters from test suite to test case in junit 4
Asked Answered
K

3

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?

Kaffir answered 21/9, 2011 at 14:14 Comment(0)
S
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.

Smoothen answered 21/9, 2011 at 14:22 Comment(5)
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
L
1

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

Lactate answered 9/3, 2015 at 11:20 Comment(0)
F
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

Footed answered 26/4, 2016 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.