美文网首页
junit学习小结

junit学习小结

作者: HamletSunS | 来源:发表于2019-07-25 23:58 被阅读0次

    junit

    1. 什么是junit
      这是一个Java测试框架,可以通过它来编写测试案例,对写好的代码进行测试
    2. 如何使用
      在junit 4.0以上的版本中已经可以通过注解来实现测试。

    常用的注解:
    @Before
    @After
    @Test
    上述3个注解可以用于编写测试案列,如下

    package junit;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import junit.framework.Assert;
    
    public class TestCase2 {
    
        @Before
        public void before() {
            System.out.println("测试前的准备工作,比如链接数据库等等");
        }
        @After
        public void after() {
            System.out.println("测试结束后的工作,比如关闭链接等等");
        }
        
        @Test
        public void testSum1() {
            int result = SumUtil.sum1(1, 2);
            Assert.assertEquals(result, 3);
        }
    
        @Test
        public void testSum2() {
            int result = SumUtil.sum2(1, 2,3);
            Assert.assertEquals(result, 5);
        }
    }
    
    

    @RunWith
    @Suite
    上述2个注解可以编写一个测试套件,一次性运行多个测试类,如下

    package junit;
    
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({TestCase1.class,TestCase2.class})
    public class TestSuite {
    
    }
    

    相关的包:
    org.junit.After/Before/Test
    junit.framework.Assert
    org.junit.runner.RunWith
    org.junit.runners.Suite

    相关文章

      网友评论

          本文标题:junit学习小结

          本文链接:https://www.haomeiwen.com/subject/uarlrctx.html