问题描述
我有一个使用 Appium 和 TestNG 开发的脚本设置.TestNG xml 包含多个类的执行,每个类有多个测试用例.
I have a script setup developed using Appium and TestNG. The TestNG xml contains execution of multiple classes and each class has multiple test cases.
示例:
Class1:
-Class1_Test1
-Class1_Test2
-Class1_Test3
Class2:
-Class2_Test1
-Class2_Test2
-Class2_Test3
我尝试集成 IRetryAnalyzer,但这只会调用失败的测试用例.要求是在 Class1_Test2 失败的情况下执行完整的 Class1,然后 Class1 失败,然后再继续执行 Class2?
I tried integrating IRetryAnalyzer but that just calls the failed test case. The requirement is to execute the complete Class1 in case Class1_Test2 fails as soon as Class1 fails before it proceeds to Class2?
询问的原因是该应用程序是媒体播放器,如果由于网络/服务器问题导致媒体播放失败,则不需要执行下一个前进和后退测试用例,并且需要重新启动应用并在执行进一步测试之前重试所有步骤.
The reason for the ask is the app is a media player and if in case media playback fails due to network/server issues, next test cases of forward and rewind will not be required to be executed and it will need to relaunch the app and retry all steps before performing further tests.
推荐答案
终于找到了重新运行整个类的解决方法.我将其称为解决方法,因为从技术上讲,TestNG 不提供重新执行 @BeforeTest 的方法,以防在任何时间点发生故障.
Finally found a workaround to rerun the whole class. I would call it a workaround since technically TestNG does not provide a way to re-execute @BeforeTest in case a failure occurs at any point of time.
我发现最好的方法是没有@BeforeTest 部分,只有一个@Test 部分,并且将所有测试用例作为函数,在定义的单个@Test 中调用.因此,如果在任何时间点发生故障,@Test 将被召回,其中包含按所需顺序排列的所有功能,包括设置功能.一旦观察到失败,重试逻辑就会重新运行整个@Test 部分.
The best possible method I found was to have no @BeforeTest section and have just one @Test section and have all Test cases as functions which would be called within the single @Test defined. So in case a failure occured at any point of time, the @Test would be recalled which contains all the functions in the order required including setting up the capabilities. The retry logic reruns the entire @Test section as soon as a failure is observed.
示例:
变更前:
package <yourpackagename>;
<import required packages>
public class Home {
private AppiumDriver<?> driver;
private static final String url = "http://0.0.0.0:4723/wd/hub";
<define your variables>
@Parameters({"deviceOS", "DSN"})
@BeforeTest
public void setUp(String deviceOS, String DSN) throws InterruptedException, MalformedURLException, ParseException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "FireTVStick");
capabilities.setCapability("platformVersion", deviceOS);
capabilities.setCapability("udid", DSN);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("fullReset", false);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);
capabilities.setCapability("appPackage", "<your app package>");
capabilities.setCapability("appActivity", "<your launcher activity>");
driver = new AndroidDriver<>(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//End of Launch Code
System.out.println("-Testing Home Section-");
}
@Parameters({"DSN"})
@Test
public void Test1_VideoPlaybackVerification(String DSN) throws InterruptedException, ParseException{
//Video playback verification code starts
.
.
//End of code for Video playback verification
}
@Test //Test Case for Pause verification
public void Test2_PauseVerification() throws InterruptedException, ParseException{
//Video pause verification code starts
.
.
//End of code for Video pause verification
}
@AfterTest
public void End() {
driver.quit();
}
}
更改后:
package <yourpackagename>;
<import required packages>
@Listeners(MyTestListenerAdapter.class)
public class Home {
private AppiumDriver<?> driver;
<define your variables>
public void setUp(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformVersion", deviceOS);
capabilities.setCapability("deviceName", deviceName);
capabilities.setCapability("udid", DSN);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("fullReset", false);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 1200);
capabilities.setCapability("appPackage", "<your app package>");
capabilities.setCapability("appActivity", "<your launcher activity>");
final String url = "http://127.0.0.1:"+port+"/wd/hub";
driver = new AndroidDriver<>(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
public void HomeVerification(String DSN, String deviceName) throws InterruptedException, ParseException
{
System.out.println(deviceName+": Testing Home Section-");
<--Your code to perform any additional task before execution-->
}
public void Test1_VideoPlaybackVerification(String DSN, String deviceName) throws InterruptedException, ParseException
{
//Video playback verification code starts
.
.
//End of code for Video playback verification
}
public void Test2_PauseVerification(String deviceName) throws InterruptedException, ParseException
{
//Video pause verification code starts
.
.
//End of code for Video pause verification
}
@Parameters({"port", "deviceOS", "DSN", "deviceName"})
@Test (retryAnalyzer = Retry.class)
public void TestRun(String port, String deviceOS, String DSN, String deviceName) throws InterruptedException, ParseException, MalformedURLException {
try {
setUp(port, deviceOS, DSN, deviceName);
HomeVerification(DSN, deviceName);
Test1_VideoPlaybackVerification(DSN, deviceName);
Test2_PauseVerification(deviceName);
} catch (WebDriverException e) {
// TODO Auto-generated catch block
Reporter.log(deviceName+": Error observed while executing script!", true);
Assert.assertTrue(false); //Fails the test case
}
}
@AfterTest
public void End() {
driver.quit();
}
}
这篇关于如果使用 TestNG 测试用例失败,请重新运行整个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!