java TestNG 失败后不继续执行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38900090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TestNG does not continue execute tests after failure
提问by Anton Anton
I have test automation framework with a page object model. All my test are located in separate classes in same package.
我有一个带有页面对象模型的测试自动化框架。我所有的测试都位于同一个包中的不同类中。
In testng.xml i have
在 testng.xml 我有
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test">
<test name="SmokeTest">
<classes>
<class name="name.test1"/>
<class name="name.test2"/>
<class name="name.test3"/>
</classes>
</test>
</suite>
Problem is that after running TestNG.xml if the 1st test will fail, it will stop test execution. But i want to continue executing of all test cases.
问题是在运行 TestNG.xml 之后,如果第一个测试失败,它将停止测试执行。但我想继续执行所有测试用例。
I use Jenkins on my project and if one of the tests are failed it stops execution immediately.
我在我的项目中使用 Jenkins,如果其中一项测试失败,它会立即停止执行。
Example of test
测试示例
public class LoginTestTest {
public AndroidDriver<AndroidElement> driver;
public AOWebClient aoWebClient;
AOWebClient aoWeb;
public LoginTestTest(AndroidDriver<AndroidElement> driver, AOWebClient aoWeb){
this.driver = driver;
this.aoWeb = aoWeb;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
public LoginTestTest() {
}
SoftAssert softAssert = new SoftAssert();
@BeforeClass
public void setUp() throws Exception {
driver = DesiredCapabilitiesSetup.startAppiumServer();
aoWebClient = DesiredCapabilitiesSetup.getAOWeb();
LogIn logIn = new LogIn(driver,aoWebClient);
logIn.logIn();
}
@org.testng.annotations.Test
public void goToSettings() throws InterruptedException {
HeaderMenu header = new HeaderMenu(driver,aoWeb);
HamburgerMenuList ham = new HamburgerMenuList(driver);
header.clickHamburgerButton();
header.clickHamburgerButton();
header.editButtonClick();
softAssert.assertAll();
}
@AfterClass
public void tearDown(ITestResult result) throws Exception {
if (result.getStatus() == ITestResult.FAILURE) {
TakeScreenshot screenshot = new TakeScreenshot();
screenshot.TakeScreenshot("screenshots/");
}
LogOut logOut = new LogOut(driver,aoWeb);
logOut.logOut();
}
}
If my test will fail in @Test
it will never continue to @AfterClass
method.
And I want that if @Test
fail it will continue to @AfterClass
method and After This Class continue executes test from other classes from testng.xml
.
如果我的测试失败,@Test
它将永远不会继续@AfterClass
方法。我希望如果@Test
失败它会继续@AfterClass
方法并且在这个类之后继续从testng.xml
.
回答by Jerrod Anderson
Your suite tag in your xml should include configfailurepolicy="continue". This tells testng that even though a config failed in one class, you still want to run other classes in that suite. See "configfailurepolicy" in the documentation.
您的 xml 中的套件标记应包括 configfailurepolicy="continue"。这告诉 testng 即使一个类中的配置失败,您仍然希望在该套件中运行其他类。请参阅文档中的“configfailurepolicy” 。
So your xml would become:
所以你的 xml 会变成:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test" configfailurepolicy="continue">
<test name="SmokeTest">
<classes>
<class name="name.test1"/>
<class name="name.test2"/>
<class name="name.test3"/>
</classes>
</test>
</suite>
回答by juherr
From the documentation:
从文档:
alwaysRun: For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
alwaysRun:对于after方法(afterSuite,afterClass,...):如果设置为true,则即使先前调用的一个或多个方法失败或被跳过,此配置方法也将运行。
Then, just replace your @AfterClass
by @AfterClass(alwaysRun = true)
.
然后,只需替换您的@AfterClass
by @AfterClass(alwaysRun = true)
。