軟體測試學習筆記:Junit入門

來源:互聯網
上載者:User

標籤:

軟體測試的第一次上機課上,第一次使用JUint對項目進行測試。

安裝是最開始要進行的工作,JUint的安裝是比較容易的,只需將需要的jar包引入到項目中即可

最開始的Triangle代碼如下:

package com.tju.scs;public class Triangle {    int a, b, c;    public Triangle(){        a = 0;        b = 0;        c = 0;    }    public Triangle(int a, int b, int c){        if(a > b)        {            int temp = a;            a = b;            b = temp;        }        if(a > c)        {            int temp = a;            a = c;            c = temp;        }        if(b > c)        {            int temp = c;            c = b;            b = temp;        }                this.a = a;        this.b = b;        this.c = c;    }    int test(){        int x = 0;        if(a + b < c || a <=0 || b <= 0 || c <= 0)            x = 0;        else if(a == b && b == c)        {            x = 1;        }        else if(a == b || b == c)        {            x = 2;        }        else            x = 3;        return x;    }    int legal(){        if(this.test() != 0)            return 1;        else            return 0;    }    int isoscelesTriangle()    {        if(this.test() == 1 || this.test() == 2)            return 1;        else            return 0;    }    int equilateralTriangle()    {        if(this.test() == 1)            return 1;        else            return 0;    }    int normalTriangle()    {        if(this.test() == 3)            return 1;        else            return 0;    }}

Junit產生的單元測試代碼如下

package com.tju.scs;import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;public class TriangleTest {        Triangle tr = null;    @Before    public void setUp() throws Exception {        int a = 0;        int b = 0;        int c = 0;        tr = new Triangle(a, b, c);    }    @Test    public void testLegal() {        assertEquals(0, tr.legal());    }    @Test    public void testIsoscelesTriangle() {        assertEquals(0, tr.isoscelesTriangle());    }    @Test    public void testEquilateralTriangle() {        assertEquals(0, tr.equilateralTriangle());    }    @Test    public void testNormalTriangle() {        assertEquals(0, tr.normalTriangle());    }}

用 a = 0, b = 0, c = 0 作為三邊後,上述測試代碼結果如下:

即此三角形不合法,非等腰,非等邊,非普通三角形。  

 

軟體測試學習筆記:Junit入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.