zoukankan      html  css  js  c++  java
  • junit4单元测试基础

      导入方法看如下截图就明白了:

     

    新建测试用例

           右击包名,点击新建,或者新建里的others,选择JUnit test case,如下图所示:

     

    接下来,给测试类起名字和选择要测试的类,如下图所示:

     

    然后点击【Next】,选择要被测试类中的测试方法,如下图所示:

     

    最后新建完成,如下(第一个方法是生成的方法,后边几个都是我自己手动写上去的):

    [java] view plain copy
     
    1. package JUnitTest;  
    2.   
    3. import static org.junit.Assert.*;  
    4.   
    5. import org.junit.After;  
    6. import org.junit.AfterClass;  
    7. import org.junit.Before;  
    8. import org.junit.BeforeClass;  
    9. import org.junit.Ignore;  
    10. import org.junit.Test;  
    11.   
    12. public class TestJava {  
    13.   
    14.     @Test  
    15.     public void testMain() {  
    16.         fail("Not yet implemented");  
    17.     }  
    18.   
    19.     @Test  
    20.     public void testTest() {  
    21.         System.out.println("@Test");//调用自己要测试的方法  
    22.     }  
    23.       
    24.     @Test  
    25.     public void testAssert() {  
    26.         assertEquals("chenleixing","chenlei");  
    27.     }  
    28.       
    29.     @Test(timeout=1)  
    30.     public void testTimeout() {  
    31.         System.out.println("超时测试");  
    32.     }  
    33.   
    34.     @Before  
    35.     public void testBefore(){  
    36.         System.out.println("@Before");  
    37.     }  
    38.       
    39.     @BeforeClass  
    40.     public static void testBeforeClass(){//必须为静态方法  
    41.         System.out.println("@BeforeClass");  
    42.     }  
    43.       
    44.     @After  
    45.     public void testAfter(){  
    46.         System.out.println("@After");  
    47.     }  
    48.       
    49.     @AfterClass  
    50.     public static void testAfterClass(){//必须为静态方法  
    51.         System.out.println("@AfterClass");  
    52.     }  
    53.       
    54.     @Ignore  
    55.     public void testIgnore(){  
    56.         System.out.println("@Ignore");  
    57.     }  
    58. }  


    JUnit4注解解释

    1. @Test : 测试方法,测试程序会运行的方法,后边可以跟参数代表不同的测试,如(expected=XXException.class) 异常测试,(timeout=xxx)超时测试
    2. @Ignore : 被忽略的测试方法
    3. @Before: 每一个测试方法之前运行
    4. @After : 每一个测试方法之后运行
    5. @BeforeClass: 所有测试开始之前运行
    6. @AfterClass: 所有测试结束之后运行

  • 相关阅读:
    2017年系统架构设计师论文范文
    在SQL Server 2008中执行透明数据加密(转自IT专家网)
    开发笔记
    [置顶] GO-Gin框架快速指南
    [置顶] JS-逆向爬虫
    [置顶] ES篇
    [置顶] GO
    [置顶] 爬虫入狱指南
    [置顶] websocket
    [置顶] Linux篇
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7088990.html
Copyright © 2011-2022 走看看