gpt4 book ai didi

java - Selenium -测试 : SoftAssert to be called inside each method or inside @afterTest

转载 作者:行者123 更新时间:2023-12-02 12:07:26 24 4
gpt4 key购买 nike

我是 Selenium 新手,使用 testNG 框架。下面是我试图理解的代码

@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}

}

@Test
public void softAssertionTest2() {
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
}

@AfterTest
public void afterTest(){
softAssert.assertAll();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}
}

Q1)我读到在配置方法中进行断言不是一个好习惯。而是在每个测试方法中进行这些操作。因此我修改代码如下

@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}
}

@Test
public void softAssertionTest2() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method2"+ verificationErrors);
}
}

public void doSoftAssert(){
softAssert.assertAll();
/*String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}*/
}

当我运行此命令时,我看到从 softAssertionTest2() 调用 softAssert.assertAll() 也会执行 softAssertionTest1() 中的断言,复制断言。我怎样才能让断言只被断言一次?

Q2) 将错误附加到 verificationErrorString 的方式是否正确,还是建议将这些错误添加到 HashMap 中?

最佳答案

How can I have assertions being asserted only once?

每个测试都应该实例化自己的SoftAssert对象,并且不应在多个 @Test 之间共享方法。

Is appending the errors to verificationErrorString the right way or is it advisable to add these errors in a HashMap?

你永远不应该用try..catch来包装你的断言。 block ,因为它可能会导致您吞噬异常,并且会在测试中添加大量样板代码。如果您的目的基本上是捕获验证消息,那么您应该查看子类 org.testng.asserts.SoftAssert您在哪里:

  • 覆盖org.testng.asserts.Assertion#onAssertSuccess (如果你想捕获传递的断言)和
  • 覆盖org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) (如果你想捕获失败的断言)

关于java - Selenium -测试 : SoftAssert to be called inside each method or inside @afterTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com