软件测试st1

一、JUnit的安装和使用

  Eclipse下JUnit的使用,因为现在版本的Eclipse和JDK下自带了JUnit的包,所以不再需要再去手动引进JUnit的包和hamcrest的包。

  首先创建一个需要去测试的类,Calculate.java代码如下:

  

public class Calculate {
    
    public int add(int a,int b){
        return a+b;
    }
    
    public int sub(int a,int b){
        return a-b;
    }
    
    public int mul(int a,int b){
        return a*b;
        
    }
    
    public int divide(int a,int b){
        return a/b;
    }

}

  之后建立一个JUnit Test Case,右键点击包名->New->Junit Test Case 即可,Eclipse会提示缺少JUnit的包,点击确认后可以自动添加。

  TestCalculate.java的代码:

  

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class TestCalculate1 {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    //@Test(timeout=800)
    @Test(expected=ArithmeticException.class)
    public void test() {
        //fail("Not yet implemented");
        Calculate cal = new Calculate();
        int result = cal.add(3, 5);
        assertEquals(7,result);
        
        
    }

}

运行结果为:

期待结果为7,但是结果为8.

 

二、Annotation注解

@Test后可加两个参数(expected)(timeout)

例如:

@Test(expected=ArithmeticException.class)
    public void test() {

        Calculate cal2 = new Calculate();
        int result2 = cal2.divide(1, 0);
        assertEquals(1,result2);
    
    }

测试中期待的结果为:发生一个算数的异常,而代码中确实用1/0,发生了算数的异常,所以不会抛出错误。

如果为发生期待的异常,则会抛出错误。如改成 int result2 = cal2.divide(1,2);

结果为:

 

对于timeout,如果程序运行的时间小于或等于timeout里面的时间,则不会抛出错误。反之,则会抛出错误。

例如:

@Test(timeout=300)

    public void test() {
        try {
            Thread.sleep(200);
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
    }


@Before:初始化方法   对于每一个测试方法都要执行一次(注意与BeforeClass区别,后者是对于所有方法执行一次)
@After:释放资源  对于每一个测试方法都要执行一次(注意与AfterClass区别,后者是对于所有方法执行一次)
@Test:测试方法,在这里可以测试期望异常和超时时间 
@Ignore:忽略的测试方法 
@BeforeClass:针对所有测试,只执行一次,且必须为static void 
@AfterClass:针对所有测试,只执行一次,且必须为static void 
一个JUnit4的单元测试用例执行顺序为: 
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass; 
每一个测试方法的调用顺序为: 
@Before -> @Test -> @After; 


三、Assertions断言

常用的Assert(断言):1、assertEquals(a, b) Asserts that two primitive values are equal.

                                    测试a是否等于b(a和b是原始类型数值(primitive value)或者必须为实现比较而具有equal方法)

                               2、assertFalse(a) Asserts that a condition (a) is false.

                                    测试a是否为false(假),a是一个Boolean数值。

                               3、assertTrue(a) Asserts that a condition is true.

                                    测试a是否为true(真),a是一个Boolean数值

                               4、assertNotNull(a) Asserts that an object isn't null.

                                    测试a是否非空,a是一个对象或者null。

                               5、assertNull(a) Asserts that an object is null.

                                    测试a是否为null,a是一个对象或者null。

                               6、assertNotSame(a, b) Asserts that two objects do not refer to the same object.

                                    测试a和b是否没有都引用同一个对象。

                               7、assertSame(a, b) Asserts that two objects refer to the same object.

                                    测试a和b是否都引用同一个对象。

 

 



posted @ 2016-03-17 16:20  prog123  阅读(253)  评论(0编辑  收藏  举报