Is there an equivalent testNG @BeforeSuite in JUnit 4?

I'm new to the test automation script, so forgive me if this is a stupid question, but Google failed me this time. Or at least everything I read just embarrassed me.

I am using JUnit 4 and Selenium Webdriver in Eclipse. I have several tests that I need to run both a set and individually. At the moment, these tests work fine when they run on their own. At the beginning of the test, the user is given an input window in which the tester first asks which server they should be tested on (this is a string variable that becomes part of the URL) and with which browser they want to test. At the time of running the tests in the package, the user is asked this at the beginning of each test, because, obviously, this is encoded into each of their @Before methods.

How can I take these values ​​once and pass them to each of the testing methods?

So, if server = "server1" and browser = "firefox", then firefox is the browser I want to use selenium and the URL I want to open is http://server1.blah.com/ for all of the following testing methods . The reason I used separate @Before methods is because the required URL is slightly different for each testing method. each method checks a different page, such as server1.blah.com/something and server1.blah.com/somethingElse

Tests run fine, I just don’t want to continue to enter values, because the number of testing methods will ultimately be quiet.

I could also convert my tests to testNG if testNG has an easier way to do this. I thought the @BeforeSuite annotation might work, but now I'm not sure.

( )

+5
4

JUnit 4 Test invocation.

, Suite MySuite. /, . . , . , . , .

, :

public class MySuite extends Suite {
    public static String url;

    /**
     * Called reflectively on classes annotated with <code>@RunWith(Suite.class)</code>
     * 
     * @param klass the root class
     * @param builder builds runners for classes in the suite
     * @throws InitializationError
     */
    public MySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
        this(builder, klass, getAnnotatedClasses(klass));
        // put your global setup here
        MySuite.url = getUrlFromUser();
    }
}

Suite :

@RunWith(MySuite.class)
@SuiteClasses({FooTest.class, BarTest.class, BazTest.class});

- @Before/@After, TestRule, , ExternalResource. ExternalResource :

public static class FooTest {
    private String url;

    @Rule
    public ExternalResource resource= new ExternalResource() {
        @Override
        protected void before() throws Throwable {
            url = (MySuite.url != null) ? MySuite.url : getUrlFromUser();
        };

        @Override
        protected void after() {
            // if necessary
        };
    };

    @Test
    public void testFoo() {
        // something which uses resource.url
    }
}

, , ExternalResource .

+1

, TestNG, , - @BeforeSuite, @DataProviders, ( , ).

TestNG, - , , BeanShell.

+1

, , @Before, .

, url , , , , .

0

If you use @RunWith(Suite.class), you can add static methods with @BeforeClass(and @AfterClass) that will run before (and after) the entire Suite you have selected. See this question .

This, of course, will not help if you reference the entire set of classes found dynamically and do not use Suite runner .

0
source

All Articles