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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

测试===JUnit单元测试

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

測試

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

一,測試分類

測試細(xì)致分類:

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

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

  • 自動化測試
    使用工具,來測試產(chǎn)品。

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

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

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

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

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

    優(yōu)點(diǎn):

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

    規(guī)范:

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

    斷言:

    測試結(jié)果與設(shè)置預(yù)期的結(jié)果對比。

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

    案例demo:

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

    這里使用eclipse,java8

  • 新建普通Java ee項(xiàng)目,并導(dǎo)入junit測試包,build path
  • 導(dǎo)入



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

    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)導(dǎo)入類,可直接用里面的靜態(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注解* * 運(yùn)行測試方法,選中方法,右鍵run as Junit test 或者 debug as Junit test* 直接右鍵,會運(yùn)行所有有@Test注解的方法* Runs 測試了幾個(gè)方法* Errors 測試方法有問題* Failures 測試方法沒達(dá)到預(yù)期的要求*/// 類加載時(shí)執(zhí)行@BeforeClasspublic static void beforeClass() {System.out.println("init...");}// 每個(gè)有@Test方法執(zhí)行前執(zhí)行@Beforepublic void before() {// 如獲取mybatis的session工廠System.out.println("method before...");}// 每個(gè)方法執(zhí)行之后執(zhí)行@Afterpublic void after() {// 如關(guān)閉mybatis的session工廠System.out.println("method after...");}// 類所有的方法執(zhí)行完后執(zhí)行@AfterClasspublic static void afterClass() {System.out.println("all is done...");}// 1秒 內(nèi)必須出結(jié)果,否則測試失敗@Test(timeout=1000)public void testAdd() {System.out.println("測試了math類的加法");// 預(yù)測值int expected = 2 + 3;// 實(shí)際值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);}}

    右鍵運(yùn)行測試

    控制臺

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

    這個(gè)能一次性的測試更多方法。


    結(jié)構(gòu)

    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 測試套件 套娃* 測試套件就是組織測試類一起運(yùn)行* 寫一個(gè)測試套件的入口類,這個(gè)類不包含其他的方法* @author echo lovely* @date 2020年11月13日 下午7:16:57*/// 運(yùn)行器 @RunWith(Suite.class) // 哪些測試類要包含, 會運(yùn)行對應(yīng)類的內(nèi)容 @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;/*** 總套件,套娃,套兩個(gè)Suite* @author echo lovely* @date 2020年11月13日 下午7:24:08*/@RunWith(Suite.class) @SuiteClasses({ MyMathTestSuite.class, MyMathTestSuite2.class }) public class AllTests {}

    運(yùn)行 AllTests

    當(dāng)參數(shù)和結(jié)果有沖突時(shí),測試具體某個(gè)方法

    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 {// 預(yù)期值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));}}

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。