如果其中一个测试失败,如何在TestNG中继续运行测试

时间:2018-03-26 03:16:17

标签: java selenium-webdriver testng

如果其中一个测试失败,如何在TestNG中继续运行测试。我已经在xml文件中将配置策略设置为continue。下面是我的xml文件。我也尝试将每个测试设置为(alwaysrun =“true”)。如果其中一个测试失败,那么所有其他测试都不会执行。它显示为失败。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression" verbose="1" configfailurepolicy="continue">


    <!-- yellowfin login credentials -->
    <parameter name="URL" value="http://10.10.5.77:8082/" />
    <parameter name="userName" value="admin" />
    <parameter name="passsword" value="test" />

    <test name="ReportFormatting">
        <classes>
            <class name="fin.bi.test.ReportFormatting">
                <methods>

                    <include name="ValidateDataSection"></include>
                    <include name="ValidateColumnandRowHeadingsandBorder"></include>
                    <include name="ValidateTitleandDescription"></include>
                    <include name="ValidateHeaderFooterandTableSort"></include>
                    <!-- <include name=""></include> -->

                </methods>
            </class>
        </classes>
    </test>
</suite>

以下是我的主要课程的测试。在这里,我将“始终运行”设置为true。我已经在stackoverflow中搜索了,否则它们似乎都没有工作。请指教。感谢

@Parameters({ "userName", "passsword", "viewName", "rf1", "rf2", "rf3", "rf4", "rf5", "fontType" ,"fontSize"})
@Test(testName = "validateDataSection", enabled = true, groups = {"Report Formatting : Data"}, alwaysRun = true, priority=1)
public void ValidateDataSection(String username, String password, String viewName, String r1, String r2, String r3, String r4, String r5, String ftype, String fsize) throws InterruptedException, AWTException {
    extentTest = extent.startTest("ValidateDataSection");

    login.loginToTenant(username, password);
    // select view from content menu button
    createContentMenuButton.setContentMenuButton();
    // choose view
    reportView.selectView(viewName);
    // create the report in report builder
    createChart.createReport(r1, r2, r3, r4, r5);

    //Checks the style "Font Type, Font Size, Bold Italic"
    reportFormattingPage.DataSection(ftype,fsize);

    // Access Row Highlight
    reportFormattingPage.RowHighlight();

    logout.performLogout();


}


@Parameters({ "userName", "passsword", "viewName", "rf1", "rf2", "rf3", "rf4", "rf5", "headerFontType", "headerFontSize", "borderWidth"})
@Test(testName = "Validate Column & Row Headings and Border", enabled = true, groups = {"Report Formatting : Column & Row Headings and Border"}, alwaysRun = true, priority=1)
public void ValidateColumnandRowHeadingsandBorder(String username, String password, String viewName, String r1, String r2, String r3,   String r4, String r5, String headerFontType, String headerFontSize, String borderWidth) throws InterruptedException {
    extentTest = extent.startTest("ValidateColumnandRowHeadingsandBorder");

    login.loginToTenant(username, password);
    // select view from content menu button
    createContentMenuButton.setContentMenuButton();
    // choose view
    reportView.selectView(viewName);
    // create the report in report builder
    createChart.createReport(r1, r2, r3, r4, r5);

    // validates the column and Row headings
    reportFormattingPage.ColumnAndRowHandling(headerFontType, headerFontSize);

    // Validates the border
    reportFormattingPage.Border(borderWidth);

    logout.performLogout();

}

1 个答案:

答案 0 :(得分:1)

引用文档:

configfailurepolicy - 是否在他们失败一次或只是跳过剩余之后继续尝试课前/课后/方法。所以这对常规测试方法没有影响,只影响配置方法。

alwaysRun - 如果设置为true,即使依赖于失败的方法,也始终会运行此测试方法。如果此测试不依赖于任何方法或组,则将忽略此属性。在您的示例中,您似乎没有方法之间的任何依赖关系。所以这个属性也将被忽略。

您分享的内容是TestNG的一个非常基本的用例。

以下是一个显示此功能的简单示例(我正在使用TestNG版本6.14.3

这是我的样本测试类

import org.testng.annotations.Test;

public class SampleTestClass {
    @Test(groups = {"Report Formatting : Data"})
    public void testMethodA() {
        throw new RuntimeException("Intentionally failing the test");
    }

    @Test(groups = {"Report Formatting : Column & Row Headings and Border"})
    public void testMethodB() {
        System.err.println("testMethodB()");
    }
}

这是我的套件xml的样子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="49483424_Suite" parallel="false" verbose="2" >
    <test name="49483424_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn49483424.SampleTestClass">
                <methods>
                    <include name="testMethodA"/>
                    <include name="testMethodB"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

这里是完整的执行输出

...
... TestNG 6.14.3 by Cédric Beust (cedric@beust.com)
...

java.lang.RuntimeException: Intentionally failing the test

    at com.rationaleemotions.stackoverflow.qn49483424.SampleTestClass.testMethodA(SampleTestClass.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

testMethodB()
PASSED: testMethodB
FAILED: testMethodA
java.lang.RuntimeException: Intentionally failing the test
    at com.rationaleemotions.stackoverflow.qn49483424.SampleTestClass.testMethodA(SampleTestClass.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)


===============================================
    49483424_test
    Tests run: 2, Failures: 1, Skips: 0
===============================================

===============================================
49483424_Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================


Process finished with exit code 0

所以我建议你首先尝试运行一个简单的测试,排除所有其他依赖项,例如扩展区报告等,并检查以确保它是否适用于普通的Java测试。 如果它仍然无法正常工作,那么我请求您在此处提交错误:https://github.com/cbeust/testng/issues并包含一个示例测试(最好是对任何外部库没有任何依赖但只使用一个一堆打印语句来显示bug)。我会看看出了什么问题。