iOS Testing with Xcode 閱讀筆記

來源:互聯網
上載者:User

標籤:nbsp   until   least   running   table   roc   原創   實值型別   ever   

 

 

 

官方文檔直通車

 

 

Performance Testing

A baseline is a combination of the average time performance in ten runs of the test method with a measure of the standard deviation of each run.

執行一次效能測試時,measureBlock內的代碼會被執行十次!

 

 

Where to Start When Testing

When you start to create tests, keep the following ideas in mind:

  • When creating unit tests, focus on testing the most basic foundations of your code, the Model classes and methods, which interact with the Controller.  單元測試需要對每個細小的代碼單元進行測試,單元的劃分需要合理。

  • When creating UI tests, start by considering the most common workflows. Think of what the user does when getting started using the app and what UI is exercised immediately in that process. Using the UI recording feature is a great way to capture a sequence of user actions into a UI test method that can be expanded upon to implement tests for correctness and/or performance. 

    UI tests of this type tend to start with a relatively coarse-grained focus and might cut across several subsystems; they can return a lot of information that can be hard to analyze at first. As you work with your UI test suite, you can refine the testing granularity and focus UI tests to reflect specific subsystem behaviors more clearly.UI測試,按照最平常的操作流程寫測試代碼,考慮介面應該如何正確響應,多次測試之後再慢慢去細化測試的每個部分。

 

 

Flow of Test Execution

For each class, testing starts by running the class setup method. For each test method, a new instance of the class is allocated and its instance setup method executed. After that it runs the test method, and after that the instance teardown method. This sequence repeats for all the test methods in the class. After the last test method teardown in the class has been run, Xcode executes the class teardown method and moves on to the next class. This sequence repeats until all the test methods in all test classes have been run.

 

每個測試類別開始執行時,都會先執行setUp類方法,然後開始執行每個測試方法。

執行每個測試方法時,都會先執行這個測試類別執行個體對象的setUp方法,然後開始執行這個測試方法,在這個測試方法執行結束後,會執行這個測試類別執行個體對象的tearDown方法。

這個類裡面的所有測試方法執行結束之後,會執行這個測試類別的tearDown類方法。

然後一直重複這個過程,直到所有的測試類別都執行完測試方法位置。

 

簡單來說,這個順序從前往後就是:class func setUp() ,  func setUp() ,  func testFunc()  , func tearDown()  , class func tesrDown() 。

 

 

 

 

Writing Tests with Swift 

The Swift access control model, as described in the Access Control section of The Swift Programming Language (Swift 4), prevents an external entity from accessing anything declared as internal in an app or framework. By default, to be able to access these items from your test code, you would need to elevate their access level to at least public, reducing the benefits of Swift’s type safety.

Xcode provides a two-part solution to this problem:

  1. When you set the Enable Testability build setting to Yes, which is true by default for test builds in new projects, Xcode includes the -enable-testing flag during compilation. This makes the Swift entities declared in the compiled module eligible for a higher level of access.

  2. When you add the @testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.

Note: @testable provides access only for internal functions; file-private and private declarations are not visible outside of their usual scope when using @testable.

 

對於Swfit代碼的測試存在讀取控制許可權問題,所以蘋果提供了兩步解決辦法來提升代碼當前的存取控制許可權。

第一步是去Xcode裡面設定Enable Testability,第二步是在測試代碼裡添加@testable。對於fileprivate和private,@testable無法提升他們的存取權限。

 

 

 

Using Assertions with Objective-C and Swift
  • For Objective-C, assertions marked for scalar types can be used with the types that can be used with the equality comparison operators: ==!=<=<>=, and >. If the expression resolves to any C type, struct, or array comparison that works with these operators, it is considered a scalar.  使用Objc寫測試代碼時,要注意實值型別和參考型別的區別,而Swift不用。

  • Using XCTest assertions in your tests also differs between Objective-C and Swift because of how the languages differ in treating data types and implicit conversions.

    • For Objective-C, the use of implicit conversions in the XCTest implementation allows the comparisons to operate independent of the expressions’ data types, and no check is made of the input data types.

    • For Swift, implicit conversions are not allowed because Swift is stricter about type safety; both parameters to a comparison must be of the same type. Type mismatches are flagged at compile time and in the source editor.

      Swift對於類型不符會報編譯錯誤,進行Assert比較的變數必須類型匹配;Objc不會檢查類型!

 

 

 

Test Debugging Workflow

Here are some common issues to keep in mind:

  • Is the logic of the test correct? Is the implementation correct? 邏輯是否正確?實現是否正確?字面量有沒有打錯?

    It’s always a good idea to check for typos and incorrect literal values that you might be using as the reference standard that the test method is using as a basis of comparison.

  • What are the assumptions? 多定義一些錯誤類型,寫測試的時候也有可能傳錯資料

    For example, you might be using the wrong data type in the test method, creating a range error for the code you’re testing.

  • Are you using the correct assertion to report the pass/fail status? 有沒有用錯Assetion?

    For example, perhaps the condition of the test needs XTCAssertTrue rather than XCTAssertFalse. It’s sometimes easy to make this error.

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

Ficow原創,轉載請註明出處:http://www.cnblogs.com/ficow/p/7859646.html

iOS Testing with Xcode 閱讀筆記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.