在使用 TestNG 失败的测试用例的情况下重新运行整个班级

我有一个使用 Appium 和 TestNG 开发的脚本设置。TestNG xml 包含多个类的执行,每个类都有多个测试用例。


例子:


Class1:

-Class1_Test1

-Class1_Test2

-Class1_Test3

Class2:

-Class2_Test1

-Class2_Test2

-Class2_Test3

我尝试集成 IRetryAnalyzer 但这只是调用失败的测试用例。要求是执行完整的 Class1,以防 Class1_Test2 在 Class1 失败之前立即失败,然后再进入 Class2?


询问的原因是应用程序是媒体播放器,如果由于网络/服务器问题导致媒体播放失败,则不需要执行下一个前进和后退测试用例,需要重新启动应用程序并重试进行进一步测试之前的所有步骤。


呼如林
浏览 197回答 3
3回答

FFIVE

根据TestNg文档没有办法实现这一点,可能下面的答案可以帮助你

撒科打诨

我正在使用小组测试。即使课堂上的某些测试失败,它也会继续测试。在您的类文件中,您可以定义如下组。public class myClass(){&nbsp; &nbsp; @Test(groups = {"abc"}, priority = 1)&nbsp; &nbsp; public void test1(){&nbsp; &nbsp; }&nbsp; &nbsp; @Test(groups = {"abc"}, priority = 2)&nbsp; &nbsp; public void test2(){&nbsp; &nbsp; }&nbsp; &nbsp; @Test(groups = {"abc"}, priority = 3)&nbsp; &nbsp; public void test3(){&nbsp; &nbsp; }}同样,您可以定义具有相同组名或不同组名的第二类。优先级决定了测试用例运行的顺序。您的 Testng.xml 文件将如下所示:<test name="any name">&nbsp; <groups>&nbsp; &nbsp; <run>&nbsp; &nbsp; &nbsp; <include name="abc"&nbsp; />&nbsp; &nbsp; &nbsp; <include name="any other group name"&nbsp; />&nbsp; &nbsp; </run>&nbsp; </groups>&nbsp; <classes>&nbsp; &nbsp; <class name="packageName.myClass"/>&nbsp; &nbsp; <class name="your_packageName.class2"/>&nbsp; </classes></test>packageName 是您的测试类所在的路径。如果您的测试类和 testng.xml 文件在同一个包中,则不需要 packageName。有关 Testng 中的测试方法的更多信息,请参阅此链接。

眼眸繁星

终于找到了重新运行整个班级的解决方法。我将其称为一种解决方法,因为从技术上讲,TestNG 不提供在任何时间点发生故障时重新执行 @BeforeTest 的方法。我发现最好的方法是没有@BeforeTest 部分,只有一个@Test 部分,并将所有测试用例作为函数,在定义的单个@Test 中调用。因此,如果在任何时间点发生故障,将调用@Test,其中包含按所需顺序包含的所有功能,包括设置功能。一旦观察到失败,重试逻辑就会重新运行整个 @Test 部分。示例:改动前:package <yourpackagename>;<import required packages>public class Home {&nbsp; &nbsp; private AppiumDriver<?> driver;&nbsp; &nbsp; private static final String url = "http://0.0.0.0:4723/wd/hub";&nbsp; &nbsp; <define your variables>&nbsp; &nbsp; @Parameters({"deviceOS", "DSN"})&nbsp; &nbsp; @BeforeTest&nbsp; &nbsp; public void setUp(String deviceOS, String DSN) throws InterruptedException, MalformedURLException, ParseException {&nbsp; &nbsp; &nbsp; &nbsp; DesiredCapabilities capabilities = new DesiredCapabilities();&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability(CapabilityType.BROWSER_NAME, "");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("deviceName", "FireTVStick");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("platformVersion", deviceOS);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("udid", DSN);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("platformName", "Android");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("noReset", true);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("fullReset", false);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("appPackage", "<your app package>");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("appActivity", "<your launcher activity>");&nbsp; &nbsp; &nbsp; &nbsp; driver = new AndroidDriver<>(new URL(url), capabilities);&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);&nbsp; &nbsp; &nbsp; &nbsp; //End of Launch Code&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-Testing Home Section-");&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; @Parameters({"DSN"})&nbsp; &nbsp; @Test&nbsp; &nbsp; public void Test1_VideoPlaybackVerification(String DSN) throws InterruptedException, ParseException{&nbsp; &nbsp; &nbsp; &nbsp; //Video playback verification code starts&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; //End of code for Video playback verification&nbsp; &nbsp; }&nbsp; &nbsp; @Test //Test Case for Pause verification&nbsp; &nbsp; public void Test2_PauseVerification() throws InterruptedException, ParseException{&nbsp; &nbsp; &nbsp; &nbsp; //Video pause verification code starts&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; //End of code for Video pause verification&nbsp; &nbsp; }&nbsp; &nbsp; @AfterTest&nbsp; &nbsp; public void End() {&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();&nbsp; &nbsp; }}更改后:package <yourpackagename>;<import required packages>@Listeners(MyTestListenerAdapter.class)public class Home {&nbsp; &nbsp; private AppiumDriver<?> driver;&nbsp; &nbsp; <define your variables>&nbsp; &nbsp; public void setUp(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, MalformedURLException {&nbsp; &nbsp; &nbsp; &nbsp; DesiredCapabilities capabilities = new DesiredCapabilities();&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability(CapabilityType.BROWSER_NAME, "");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("platformVersion", deviceOS);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("deviceName", deviceName);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("udid", DSN);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("platformName", "Android");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("noReset", true);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("fullReset", false);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("appPackage", "<your app package>");&nbsp; &nbsp; &nbsp; &nbsp; capabilities.setCapability("appActivity", "<your launcher activity>");&nbsp; &nbsp; &nbsp; &nbsp; final String url = "http://127.0.0.1:"+port+"/wd/hub";&nbsp; &nbsp; &nbsp; &nbsp; driver = new AndroidDriver<>(new URL(url), capabilities);&nbsp; &nbsp; &nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);&nbsp; &nbsp; }&nbsp; &nbsp; public void HomeVerification(String DSN, String deviceName) throws InterruptedException, ParseException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(deviceName+": Testing Home Section-");&nbsp; &nbsp; &nbsp; &nbsp; <--Your code to perform any additional task before execution-->&nbsp; &nbsp; }&nbsp; &nbsp; public void Test1_VideoPlaybackVerification(String DSN, String deviceName) throws InterruptedException, ParseException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Video playback verification code starts&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; //End of code for Video playback verification&nbsp; &nbsp; }&nbsp; &nbsp; public void Test2_PauseVerification(String deviceName) throws InterruptedException, ParseException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Video pause verification code starts&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; //End of code for Video pause verification&nbsp; &nbsp; }&nbsp; &nbsp; @Parameters({"port", "deviceOS", "DSN", "deviceName"})&nbsp; &nbsp; @Test (retryAnalyzer = Retry.class)&nbsp; &nbsp; public void TestRun(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, ParseException, MalformedURLException {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setUp(port, deviceOS, DSN, deviceName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HomeVerification(DSN, deviceName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test1_VideoPlaybackVerification(DSN, deviceName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test2_PauseVerification(deviceName);&nbsp; &nbsp; &nbsp; &nbsp; } catch (WebDriverException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reporter.log(deviceName+": Error observed while executing script!", true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.assertTrue(false); //Fails the test case&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @AfterTest&nbsp; &nbsp; public void End() {&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java