Basic Concepts swift unit test (a)

1, Why unit test?

A: Unit testing is to avoid your app becomes a full bug in the software, in the development process so that we can better detect defects, improve code quality, but also to ensure timely detection of problems caused by changes in the code refactoring.

 

2, unit test should measure what?

A: 1) core functions: model classes and methods, as well as their interaction and controller

        2) The most common UI operations

        3) marginal conditions

        4) bug fix

 

3, unit testing what principles you need to follow that?

A: FIRST principles - tested best practices, follow the principle of FIRST will let you test more clearly helpful

        1) Fast: tests run faster, so that people will not mind if you run them

        2) Independent / Isolated: a test should not be dependent on another test

        3) Repeatable: the same test, each should get the same results. External data providers and concurrency problems can cause intermittent error

        4) Self-validating: Testing should be fully automated, the output is either pass or fail, rather than relying on programmers to interpret log files

         Before Ideally, test preparation, should be tested in the preparation Product code: 5 Timely)

 

4, unit testing a little what?

A: 1) allow developers to be more confident

        2) code does not degenerate. It will not change the bug caused by another bug

        3) In case of a good unit tests, the reconstructed code can be assured

        4) good unit test itself is to use instructions, sometimes more useful than documents

 

At present, many open-source libraries, open source projects have joined the unit tests, for example, swift version of AFN - Alamofire, you write a lot of test code, unit test is currently divided into two kinds of thinking BDD and TDD modes.

 

5. What is TDD?

A: TDD is Test Drive Development, refers to a test-driven development, relative to the normal mode of thinking, it is a more extreme approach we are generally the first to write production code, write test code, while TDD is just the opposite, that the idea is to write test code, then write the corresponding product code.

        TDD一般遵循 red->green->refactor 的步骤,即(报错->通过->重构),因为是先写了测试代码,而还未添加产品代码,所以编译器会给出红色报警,当把相应的产品代码添加完善后,并让单元测试用例通过测试,通过的状态为绿色,如此反复直到各种边界和测试都进行完毕,此时我们就可以得到一个稳定的产品,所以可以大胆的对产品代码进行重构,只要保证项目最后是绿色状态,就说嘛重构的代码没问题。

       TDD的过程,类似于脚本语言的交互式编程,写几行代码,就可以检查运行结果,如果结果有误,则要把最近的代码充血,知道单元测试结果正确为止。

 

6、什么是BDD?

答:BDD是Behavior Drive Development ,指的是行为驱动开发,常用于敏捷开发中使用的测试方法,其主要是为了解决XCTest苹果官方测试框架测试时难以mock和stub的问题。

        BDD提倡使用Given...When...Then 这种类似自然语言的描述来编写测试代码,在objc中,现在比较流行的BDD框架有specta、Kiwi、ceder,github上start较多的是Kiwi,在swift中,专用的 BDD 测试框架是QuickSleipnir

        例如Alamofire中下载的测试:

 

7、什么是Stub?

答:Stub是指人为地让一个对象对某个方法返回我们事先规定好的值。

        Stub运用的主要场景是你需要和别的开发人员协同开发时,别人的模块尚未完成,而你的模块需要用到别人的模块,这时就需要Stub。例如,后端的接口未完成,你的代码已经完成了,Stub可以伪造一个调用的返回。

        ojbc下可以使用OHHTTPStubs来伪造网络的数据返回。swift下,仍要手动写stub。

 

8、什么是Mock?

答:Mock是一个非常容易和stub混淆的概念,简单来说,我们可以将Mock看做是一种更全面和更智能的Stub。

        明确来说,Mock其实是一个对象,它是对现有类行为的一种模拟(或是岁现有接口实现的模拟)。

        Mock和Stub最大的区别在于Stub只是简单的方法替换,不涉及新的对象,被stub的对象可以是业务代码中真正的对象,而Mock行为本身产生新的(不可能在业务代码中出现)的对象,并遵循类的定义响应某些方法。

       Mock让你可以检查某种情况下,一个方法是否被调用,或者一个属性是否被正确设值。objc下可以使用OCMock来mock对象。但是,由于swift的runtime比较弱,所以,swift上一般要手动写mock。

Guess you like

Origin blog.csdn.net/lin1109221208/article/details/91946838