3 回答
TA贡献1934条经验 获得超2个赞
我正在使用小组测试。即使课堂上的某些测试失败,它也会继续测试。
在您的类文件中,您可以定义如下组。
public class myClass(){
@Test(groups = {"abc"}, priority = 1)
public void test1(){
}
@Test(groups = {"abc"}, priority = 2)
public void test2(){
}
@Test(groups = {"abc"}, priority = 3)
public void test3(){
}
}
同样,您可以定义具有相同组名或不同组名的第二类。优先级决定了测试用例运行的顺序。
您的 Testng.xml 文件将如下所示:
<test name="any name">
<groups>
<run>
<include name="abc" />
<include name="any other group name" />
</run>
</groups>
<classes>
<class name="packageName.myClass"/>
<class name="your_packageName.class2"/>
</classes>
</test>
packageName 是您的测试类所在的路径。如果您的测试类和 testng.xml 文件在同一个包中,则不需要 packageName。
有关 Testng 中的测试方法的更多信息,请参阅此链接。
TA贡献1873条经验 获得超9个赞
终于找到了重新运行整个班级的解决方法。我将其称为一种解决方法,因为从技术上讲,TestNG 不提供在任何时间点发生故障时重新执行 @BeforeTest 的方法。
我发现最好的方法是没有@BeforeTest 部分,只有一个@Test 部分,并将所有测试用例作为函数,在定义的单个@Test 中调用。因此,如果在任何时间点发生故障,将调用@Test,其中包含按所需顺序包含的所有功能,包括设置功能。一旦观察到失败,重试逻辑就会重新运行整个 @Test 部分。
示例:
改动前:
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();
}
}
添加回答
举报