Creating Test Suite in Webdriver

2019-05-23 15:01发布

问题:

Apologies if this is an obvious question. I have written some tests in Java using webdriver backed selenium - these tests all run perfectly when ran individually. I would now like a way of running a selection of these together, in a testSuite. From what I have found so far the suggestion has been the following:

package my.package;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class My_TestSuite

public static Test suite() {
suite.addTestSuite(US298.class);
suite.addTestSuite(US111.class);
//etc
return suite;
}
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
}

I got this from attempting to export an existing test suite from the Selenium IDE, to try and give me an idea as to how to do this but it has not helped! Any help greatly appreciated - many thanks.

回答1:

Following your comment about using JUnit 4, the problem is that you're actually using a JUnit 3 style test suite with JUnit 4 tests. You need to use the JUnit 4 Suite. This has the following format:

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class AllTestsSuite {
}

The junit.framework.* classes are JUnit 3, and the org.junit.* classes are JUnit 4.



回答2:

I use TestNG Suites see the doc at 3.textng.xml

It s easy to use

Example:

<test name="init">
    <classes>
        <class name="com.example.Setup" />
        <class name="com.example.Login"/>
    </classes>
</test>
<test name="myTests">
    <packages>
        <package name="com.example.tests"/>
    </packages>
</test> 

You would have to adjust your tests to testng of course, but thats easily done. I also suggest that you use webdriver tests and not webdriver backed but thats just my opinion.



回答3:

@tarken

When you use testNG what code do you write for executing and invoking test suite considering this as an example from above mentioned piece of code

package my.package;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class My_TestSuite

public static Test suite() {
suite.addTestSuite(US298.class);
suite.addTestSuite(US111.class);
//etc
return suite;
}
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
}