zoukankan      html  css  js  c++  java
  • 五、testNG异常处理

    当程序出现异常或者测试中有异常测试案例可以使他抛出异常

    例如:0不可以当做除数,如果将除数设置为0会抛出异常

    在testNG上加上 expectedExceptions = ArithmeticException.class 

    运行测试案例测试通过

    package com.lc.testngYiChang;
    
    import org.testng.annotations.Test;
    
    public class testNG04 {
      @Test(expectedExceptions = ArithmeticException.class)
      public void testNG04_01() {
          int  a = 0 ; 
          int  b = 1 ; 
          System.out.println("我是testNG04类的testNG04——01方法,我在异常之前打印。");
    
          System.out.println("我是b/a  "+b/a);
          
          
          System.out.println("我是testNG04类的testNG04——01方法,我在异常之后打印。");
      }
    }

    如果程序正常,在测方法上加上 expectedExceptions = ArithmeticException.class ,会报出程序异常

    package com.lc.testngYiChang;
    
    import org.testng.annotations.Test;
    
    public class testNG04 {
      @Test(expectedExceptions = ArithmeticException.class)
      public void testNG04_01() {
          int  a = 0 ; 
          int  b = 1 ; 
          System.out.println("我是testNG04类的testNG04——01方法,我在异常之前打印。");
    
          System.out.println("我是b/a  "+b/b);
          
          
          System.out.println("我是testNG04类的testNG04——01方法,我在异常之后打印。");
      }
      
     
    }
    [TestNG] Running:
      C:UserslcAppDataLocalTemp	estng-eclipse-466935271	estng-customsuite.xml
    
    我是testNG04类的testNG04——01方法,我在异常之前打印。
    我是b/a  1
    我是testNG04类的testNG04——01方法,我在异常之后打印。
    FAILED: testNG04_01
    org.testng.TestException: 
    Method testNG04.testNG04_01()[pri:0, instance:com.lc.testngYiChang.testNG04@256216b3] should have thrown an exception of type class java.lang.ArithmeticException
        at org.testng.internal.ExpectedExceptionsHolder.noException(ExpectedExceptionsHolder.java:89)
        at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1376)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:677)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
        at org.testng.TestRunner.privateRun(TestRunner.java:774)
        at org.testng.TestRunner.run(TestRunner.java:624)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
        at org.testng.SuiteRunner.run(SuiteRunner.java:261)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
        at org.testng.TestNG.run(TestNG.java:1024)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
    
    
    ===============================================
        Default test
        Tests run: 1, Failures: 1, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    ===============================================
    
    [TestNG] Time taken by org.testng.reporters.EmailableReporter2@61064425: 11 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@16f65612: 59 ms
    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6bc168e5: 5 ms
    [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 5 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@77556fd: 27 ms
    [TestNG] Time taken by org.testng.reporters.XMLReporter@7a7b0070: 3 ms

     另外如果程序出现异常 会在项目下test-output/testng-failed.xml 汇整错误信息,方便观察错误类、方法,方便回购测试

  • 相关阅读:
    适配器模式
    快排变种
    美团面试问题
    gulp前端自动化构建工具新手入门篇
    javascript继承
    .call()和.apply()相同点与不同点
    JavaScript原型,原型链 !
    锚点链接和hash属性
    构造函数与普通函数的比较
    JS的作用域和作用域链
  • 原文地址:https://www.cnblogs.com/fanfancs/p/14097109.html
Copyright © 2011-2022 走看看