日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

测试===JUnit单元测试

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试===JUnit单元测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

測試

    • 一,測試分類
    • 二,單元測試Junit,你以為的junit只是@Test注解嗎,shallow..
      • 優(yōu)點:
      • 規(guī)范:
      • 斷言:
      • 案例demo:
        • junit test case測試類創(chuàng)建,執(zhí)行測試,結果反饋
        • junit test suite 套娃測試,suite套suite,suite套case
        • 當參數(shù)和結果有沖突時,測試具體某個方法

一,測試分類

測試細致分類:

  • 白盒測試 具體的跑代碼,結合功能。
  • 黑盒測試,測試人員用,從界面來,看界面,功能有沒達到要求,需要寫文檔,每個細節(jié)部分要記錄到。
  • 交叉測試,開發(fā)人員開發(fā)時,相互測試對方的功能。
  • 功能測試:
    1. 包括白盒,黑盒
    2. 準備測試數(shù)據(jù)
    3. 多環(huán)境測試:(測試環(huán)境(局域網數(shù)據(jù)),預發(fā)布環(huán)境(外網數(shù)據(jù)),正式環(huán)境)

  • 相同操作系統(tǒng)、相同版本、相同的軟件環(huán)境(運行環(huán)境、代碼、jdk、tomcat、mysql…)
  • 數(shù)據(jù)庫數(shù)據(jù)不同(預發(fā)布環(huán)境的數(shù)據(jù)一般取是最近正式環(huán)境的數(shù)據(jù))

  • 自動化測試
    使用工具,來測試產品。

    性能測試(jmeter)、壓力測試
    響應速度、主要是模擬高并發(fā)場景

    編寫測試報告
    各個具體業(yè)務流程,截圖,具體時間,哪些地方有bug,哪些error,哪些failure記錄下來!

    bug跟蹤系統(tǒng)
    用來記錄并跟蹤bug,當前bug的數(shù)量,當前bug經過多長時間才被解決掉,等… 是一套這樣的系統(tǒng),有第三方的,也有公司自己研發(fā)的。

    對bug數(shù)據(jù)進行統(tǒng)計,分析,解決。

    二,單元測試Junit,你以為的junit只是@Test注解嗎,shallow…

    優(yōu)點:

    優(yōu)點:junit包括junit case和junit suite。能夠一次性的測試多個方法,或者多個單元測試類,并設置預期的結果。運行的結果是測試run了多少個方法,哪些error, 哪些failure了。

    規(guī)范:

    1.測試方法上必須使用@Test進行修飾
    2.測試方法必須使用public void進行修飾,不能帶任何的參數(shù)
    3.新建一個源代碼目錄用來存放測試代碼
    4.測試類的包應該和被測試類保持一致
    5.測試單元中的每個方法必須獨立測試,測試方法間不能有任何的依賴
    6.測試類使用Test作為類的后綴
    7.測試方法使用test作為方法名的前綴

    斷言:

    測試結果與設置預期的結果對比。

    斷言 //查看兩個數(shù)組是否相等。 assertArrayEquals(expecteds, actuals) //查看兩個對象是否相等。類似于字符串比較使用的equals()方法 assertEquals(expected, actual) //查看兩個對象是否不相等。 assertNotEquals(first, second) //查看對象是否為空。 assertNull(object) //查看對象是否不為空。 assertNotNull(object) //查看兩個對象的引用是否相等。類似于使用“==”比較兩個對象 assertSame(expected, actual) //查看兩個對象的引用是否不相等。類似于使用“!=”比較兩個對象 assertNotSame(unexpected, actual) //查看運行結果是否為true。 assertTrue(condition) //查看運行結果是否為false。 assertFalse(condition) //查看實際值是否滿足指定的條件 assertThat(actual, matcher) fail() 讓測試失敗

    案例demo:

    junit test case測試類創(chuàng)建,執(zhí)行測試,結果反饋

    這里使用eclipse,java8

  • 新建普通Java ee項目,并導入junit測試包,build path
  • 導入



  • 開始編寫德莫,求和,除法

    package cn.bitqian.demo;/*** @author echo lovely* @date 2020年11月13日 下午6:55:22*/public class MyMath {// sumpublic int add(int a, int b) {return a + b;}// dividepublic int division(int a, int b) {if (b == 0)return b;return a / b;}}

    創(chuàng)建junit測試類,包名和src下面的一樣,類名在原類名上+Test



    上面的下一步

    package cn.bitqian.demo;// 靜態(tài)導入類,可直接用里面的靜態(tài)方法 import static org.junit.Assert.*;import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test;/*** junit test class* @author echo lovely* @date 2020年11月13日 下午6:59:03*/public class MyMathTest {private MyMath math = new MyMath();/*** 1. 測試方法 無參無返* 2. 方法前面加上@Test注解* * 運行測試方法,選中方法,右鍵run as Junit test 或者 debug as Junit test* 直接右鍵,會運行所有有@Test注解的方法* Runs 測試了幾個方法* Errors 測試方法有問題* Failures 測試方法沒達到預期的要求*/// 類加載時執(zhí)行@BeforeClasspublic static void beforeClass() {System.out.println("init...");}// 每個有@Test方法執(zhí)行前執(zhí)行@Beforepublic void before() {// 如獲取mybatis的session工廠System.out.println("method before...");}// 每個方法執(zhí)行之后執(zhí)行@Afterpublic void after() {// 如關閉mybatis的session工廠System.out.println("method after...");}// 類所有的方法執(zhí)行完后執(zhí)行@AfterClasspublic static void afterClass() {System.out.println("all is done...");}// 1秒 內必須出結果,否則測試失敗@Test(timeout=1000)public void testAdd() {System.out.println("測試了math類的加法");// 預測值int expected = 2 + 3;// 實際值int actual = math.add(2, 3);// 斷言Assert.assertEquals(expected, actual);}@Testpublic void testDivision() {System.out.println("測試了math類的除法");// Errorsint expected = 5 / 0;int actual = math.division(5, 2);assertEquals(expected, actual);}}

    右鍵運行測試

    控制臺

    junit test suite 套娃測試,suite套suite,suite套case

    這個能一次性的測試更多方法。


    結構

    package cn.bitqian.suite;import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses;import cn.bitqian.demo.MyMathTest;/*** suite 測試套件 套娃* 測試套件就是組織測試類一起運行* 寫一個測試套件的入口類,這個類不包含其他的方法* @author echo lovely* @date 2020年11月13日 下午7:16:57*/// 運行器 @RunWith(Suite.class) // 哪些測試類要包含, 會運行對應類的內容 @SuiteClasses({MyMathTest.class}) public class MyMathTestSuite {} package cn.bitqian.suite;import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses;import cn.bitqian.demo.MyMathTest2;/*** 測試套件2 套測試類 MyMathTest2* @author echo lovely* @date 2020年11月13日 下午7:16:57*/@RunWith(Suite.class) @SuiteClasses({MyMathTest2.class}) public class MyMathTestSuite2 {} package cn.bitqian.suite;import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses;/*** 總套件,套娃,套兩個Suite* @author echo lovely* @date 2020年11月13日 下午7:24:08*/@RunWith(Suite.class) @SuiteClasses({ MyMathTestSuite.class, MyMathTestSuite2.class }) public class AllTests {}

    運行 AllTests

    當參數(shù)和結果有沖突時,測試具體某個方法

    package cn.bitqian.demo;import java.util.Arrays; import java.util.Collection;import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters;/*** 測試MyMath類中的add方法* @author echo lovely* @date 2020年11月13日 下午8:08:17*/@RunWith(Parameterized.class) public class MyMathAddTest {// 預期值int excepted = 0;// 參數(shù)1int input1 = 0;// 參數(shù)2int input2 = 0;public MyMathAddTest(int excepted, int input1, int input2) {super();this.excepted = excepted;this.input1 = input1;this.input2 = input2;}@Parameterspublic static Collection<Object[]> t(){return Arrays.asList(new Object[][]{{4,2,2},{11,9,2},{8,6,2},{1,-6,7}// res v1 v2});}@Testpublic void testAdd(){MyMath myMath = new MyMath();Assert.assertEquals(this.excepted,myMath.add(this.input1, this.input2));}}

    總結

    以上是生活随笔為你收集整理的测试===JUnit单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。