Suite Class不会调用@BeforeClass& @下课以后

时间:2016-03-07 05:05:48

标签: maven selenium selenium-webdriver junit surefire

我有一套测试用例。我想要@BeforeClass& @AfterClass分别在测试用例执行前后调用。当我使用JUnit运行套件类时,它正确调用方法,即在测试用例之前调用setUp(),在测试用例完成后调用tearDown()。但是当我执行maven执行测试用例并使用生成报告时确定它没有调用setUp()& TEARDOWN()

@RunWith(Suite.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SuiteClasses({test1.class, test2.class})
public class SuiteClass{
    public static WebDriver driver;
    public static String baseUrl;
    public static StringBuffer verificationErrors = new StringBuffer();

    @BeforeClass
    public static void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://localhost:7070/myproject/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

最后我找到了解决办法:)为有需要的人分享

套件类

@RunWith(Suite.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SuiteClasses({test1.class, test2.class})
public class SuiteClass{
    public static WebDriver driver;
    public static String baseUrl;
    public static StringBuffer verificationErrors = new StringBuffer();

    @BeforeClass
    public static void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://localhost:7070/myproject/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

在pom.xml中输入报告生成

<reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <outputName>TestReport</outputName>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

指定Maven目标以执行SuiteClass

site -Dtest=com.test.testcase.SuiteClass

答案 1 :(得分:1)

@RunWith(Suite.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SuiteClasses({test1.class, test2.class})
public class SuiteClass{
    public static WebDriver driver;
    public static String baseUrl;
    public static StringBuffer verificationErrors = new StringBuffer();

    @BeforeClass
    public static void setUp() throws Exception {

    public static WebDriver driver;
    public static String baseUrl;
    public static StringBuffer verificationErrors = new StringBuffer();
    }

    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
} 

尝试以上你知道你做错了什么......

答案 2 :(得分:0)

这不是因为TestSuite没有任何注释。 surefire没有选择你的套件的原因肯定只是在寻找要运行的测试时选择具有以下命名约定的类:

1.Test *

2 *测试

3 *测试用例。

如果您按照上述惯例更新套件的名称,它将正常工作。