调用方法的软断言失败时,@ Test失败

时间:2018-08-13 08:05:40

标签: java selenium testng assertions

软断言无法从父类继续测试用例:

在下面的测试中,第二种方法AssertTesting.softassert()的软断言失败,但是在第二种方法之后停止运行。我想从父类执行第三个方法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();

      }

    }

2 个答案:

答案 0 :(得分:0)

诸如SoftAssertassertEquals之类的assertFalse方法仅记录条件是否良好,并且不会向TestNG运行者抛出任何表明测试失败的异常。实际的检查和异常抛出发生在assertAll方法中。

这说明了为什么会出现此错误。 softassert方法运行assertAll,该方法会引发异常,并且测试将在此时停止执行。

如果要继续进行测试,则需要对所有测试使用相同的SoftAssert实例,并在整个测试的最后运行assertAll

答案 1 :(得分:0)

softassert()中的一个断言之一,因此,在方法末尾使用sa.assertAll();时,将引发异常。从docs

  

断言失败时,请勿引发异常,而应记录   失败。调用assertAll()会在以下情况下引发异常   至少一个断言失败。

如果您想使测试失败,如果任何断言失败,请在测试结束时调用assertAll();