当调用方法有 softassert 失败时 @Test 失败
@Test failed when calling method has softassert fails
软断言无法继续来自父级的测试用例class:
在下面的测试中,第二种方法 AssertTesting.softassert()
以失败的软断言完成,但 运行 在第二种方法之后停止。我想从父 class 执行第三个方法 AssertTesting.softassert2()
。我可以使用软断言的任何其他方式,所以我可以继续执行
package Assertion;
import org.testng.annotations.Test;
public class AssertTestingAll
{
@Test
public void callallAssert()
{
AssertTesting.Hardassert();
AssertTesting.softassert();
AssertTesting.softassert2();
}
}
package Assertion;
import org.junit.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class AssertTesting
{
static SoftAssert sa=new SoftAssert();
public static void Hardassert()
{
// Assert.fail("Assertion fail");
Assert.assertEquals("strings dont match:", "Test", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
Assert.assertFalse("numbers does not matches ", 2<2);
Assert.assertSame("jeeva", "jeeva");
}
public static void softassert()
{
sa.assertEquals("strings dont match:", "Test1", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
System.out.println("execution continued");
sa.assertFalse(2<2,"numbers does not matches ");
sa.assertSame("jeeva", "jeeva1");
sa.assertAll();
}
public static void softassert2()
{
sa.assertEquals("strings dont match:", "Test1", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
System.out.println("execution continued");
sa.assertFalse(2<2,"numbers does not matches ");
sa.assertSame("jeeva", "jeeva1");
sa.assertAll();
}
}
SoftAssert
的方法如 assertEquals
或 assertFalse
只记录条件是否正常,它们不会向 TestNG
抛出任何表明测试失败的异常运行纳尔。实际的检查和异常抛出发生在 assertAll
方法中。
这解释了为什么会出现此错误。 softassert
方法 运行s assertAll
抛出异常并且测试在该点停止执行。
如果您希望测试继续,您需要对所有测试使用相同的 SoftAssert
实例,并在整个测试的最后使用 运行 assertAll
。
softassert()
中的断言之一,因此当您在方法末尾使用 sa.assertAll();
时,将抛出异常。来自 docs
When an assertion fails, don't throw an exception but record the
failure. Calling assertAll() will cause an exception to be thrown if
at least one assertion failed.
如果您希望在任何断言失败时测试失败,请在测试结束时调用 assertAll();
。
软断言无法继续来自父级的测试用例class:
在下面的测试中,第二种方法 AssertTesting.softassert()
以失败的软断言完成,但 运行 在第二种方法之后停止。我想从父 class 执行第三个方法 AssertTesting.softassert2()
。我可以使用软断言的任何其他方式,所以我可以继续执行
package Assertion;
import org.testng.annotations.Test;
public class AssertTestingAll
{
@Test
public void callallAssert()
{
AssertTesting.Hardassert();
AssertTesting.softassert();
AssertTesting.softassert2();
}
}
package Assertion;
import org.junit.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class AssertTesting
{
static SoftAssert sa=new SoftAssert();
public static void Hardassert()
{
// Assert.fail("Assertion fail");
Assert.assertEquals("strings dont match:", "Test", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
Assert.assertFalse("numbers does not matches ", 2<2);
Assert.assertSame("jeeva", "jeeva");
}
public static void softassert()
{
sa.assertEquals("strings dont match:", "Test1", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
System.out.println("execution continued");
sa.assertFalse(2<2,"numbers does not matches ");
sa.assertSame("jeeva", "jeeva1");
sa.assertAll();
}
public static void softassert2()
{
sa.assertEquals("strings dont match:", "Test1", "Test");
{
String [] name1= {"jeeva","balan","madhu"};
String [] name2= {"jeeva","balan","madhu"};
Assert.assertArrayEquals("name does not match", name1, name2);
}
System.out.println("execution continued");
sa.assertFalse(2<2,"numbers does not matches ");
sa.assertSame("jeeva", "jeeva1");
sa.assertAll();
}
}
SoftAssert
的方法如 assertEquals
或 assertFalse
只记录条件是否正常,它们不会向 TestNG
抛出任何表明测试失败的异常运行纳尔。实际的检查和异常抛出发生在 assertAll
方法中。
这解释了为什么会出现此错误。 softassert
方法 运行s assertAll
抛出异常并且测试在该点停止执行。
如果您希望测试继续,您需要对所有测试使用相同的 SoftAssert
实例,并在整个测试的最后使用 运行 assertAll
。
softassert()
中的断言之一,因此当您在方法末尾使用 sa.assertAll();
时,将抛出异常。来自 docs
When an assertion fails, don't throw an exception but record the failure. Calling assertAll() will cause an exception to be thrown if at least one assertion failed.
如果您希望在任何断言失败时测试失败,请在测试结束时调用 assertAll();
。