junit4单元测试基础

it2022-05-05  231

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

 

 

新建测试用例

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

 

 

 

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

 

 

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

 

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

[java]  view plain  copy   package JUnitTest;    import static org.junit.Assert.*;    import org.junit.After;  import org.junit.AfterClass;  import org.junit.Before;  import org.junit.BeforeClass;  import org.junit.Ignore;  import org.junit.Test;    public class TestJava {        @Test      public void testMain() {          fail("Not yet implemented");      }        @Test      public void testTest() {          System.out.println("@Test");//调用自己要测试的方法      }            @Test      public void testAssert() {          assertEquals("chenleixing","chenlei");      }            @Test(timeout=1)      public void testTimeout() {          System.out.println("超时测试");      }        @Before      public void testBefore(){          System.out.println("@Before");      }            @BeforeClass      public static void testBeforeClass(){//必须为静态方法          System.out.println("@BeforeClass");      }            @After      public void testAfter(){          System.out.println("@After");      }            @AfterClass      public static void testAfterClass(){//必须为静态方法          System.out.println("@AfterClass");      }            @Ignore      public void testIgnore(){          System.out.println("@Ignore");      }  }  

JUnit4注解解释

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

转载于:https://www.cnblogs.com/panxuejun/p/7088990.html


最新回复(0)