• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 告訴你什么叫單元測試?

    發表于:2016-06-01來源:簡書作者作者:我叫武大郎點擊數: 標簽:單元測試
    什么是單元測試? 是一個工具.通常情況下,我們并不會用到它.

    什么是單元測試?

     

    是一個工具.通常情況下,我們并不會用到它.

    創建一個包含單元測試的項目:

     


    Snip20160508_1.png

    Snip20160508_5.png


    單元測試文件夾中包含一個繼承自XCTestCase的類.如下:

    #import <XCTest/XCTest.h>
    @interface UnitTestDemoTests : XCTestCase
    @end
    @implementation UnitTestDemoTests
    - (void)setUp {
        [super setUp];
        // Put setup code here. This method is called before the invocation of each test method in the class.
    //請將準備性的代碼,初始化的代碼寫在這里.這個方法在所有以Test開頭的方法之前調用
    }
    - (void)tearDown {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    // Put teardown code here.(什么是tearDown代碼?).這個方法在所有以Test開頭的方法之后調用
        [super tearDown];
    }
    - (void)testExample {
        // This is an example of a functional test case.
    //這是一個功能測試的例子,
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    //使用XCTAssert斷言和相關的函數來檢測你的測試代碼是否可以輸出正確的結果.
    }
    - (void)testPerformanceExample {
        // This is an example of a performance test case.
    
        [self measureBlock:^{
            // Put the code you want to measure the time of here.
    //需要測試其運行時間的代碼請放在這里.
        }];
    }
    @end

    單元測試有什么用?

     

    用于測試邏輯代碼的運行結果是否是準確的,是否是我們所期待的.

    怎么用?

     

    比如我有一個工具類,其中有一段非常核心的代碼,不希望程序運行起來測試是否有問題

    #import "YFTool.h"
    @implementation YFTool
    /**
     *  需要測試的牛逼的核心代碼,而不是通過運行整個程序來測試.
    */
    +(BOOL)checkPassword:(NSInteger)password{
        if (password == 123456) {
            return YES;
        }
        return NO;
    }
    @end

    通常并不直接在系統提供的Test文件中測試代碼,而是新建一個繼承自XCTestCase的類,在其中完成測試.比如我要測試YFTool這個類中的代碼,你需要這樣做:


    Snip20160508_9.png

    單元測試的使用.

     

    #import "YFToolTest.h"
    #import "YFTool.h"
    @implementation YFToolTest
    - (void)testCheckPassWord {
        BOOL result = [YFTool checkPassword:123];
    //這是一個斷言,左邊是結果表達式,如果不能獲得左邊的結果,彈出右邊的提示.
        XCTAssert(result == YES,@"親,輸入的密碼有誤哦!");
    }
    @end
    • 傳入123,是這樣的:

      Snip20160508_11.png
    • 傳入123456,是這樣的:

      Snip20160508_12.png
    • 注意:
      • 測試方法一定要以test開頭,Cmd + B,才會生成左側的菱形

        Snip20160508_10.png
      • 點擊菱形,運行測試代碼.
      • 點擊每個方法前面的菱形,測試這一個方法;點擊@implementation前面的菱形,測試該類中所有的方法.

      • Snip20160508_13.png

    什么時候用?

     

      • Matt哥們說,通常在需求大于一切的企業環境下面,單元測試基本上沒有用武之地,但是對于如果你想要你的項目更加健壯,邏輯更加縝密,那么單元測試就是你所需要的.

    原文轉自:http://www.jianshu.com/p/fa99b9a26d0a

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>