Test-driven iOS development. These two days have looked at the basic knowledge of this area. Sweep the blinds.
测试驱动开发,英文Test-Driven Development 简称TDD。他是一种新型的软件开发方法。他要求在写代码之前先编写测试代码,然后之编写使测试通过的代码,通过测试来推动整个开发的进行。有助于编写简洁可用和高质量的代码。并加速开发过程。
Traditional development: Writing program--design test Case--write test Case Program--unit test--write test report
Test-Driven Development: Design unit test Cases, write test case programs, program codes, unit tests, write test reports
In contrast to the two methods, the traditional method is to write the project code, then write the data, write the program (or manual) to test, and test-driven development is to write the test data and test program, and then write the project code.
It is said that the benefits are big.
单元测试是测试驱动的核心
The main iOS unit testing frameworks are: Ocunit, Ghunit, Xctest.
Ocunit and Ghunit are open-source testing frameworks.
Xctest is the default unit test framework for Xcode.
Apple has been working on test-driven development, so the xctest framework has been added by default in each project.
To tell the truth before this thing just know a name, what, how to use do not know. I haven't touched it.
Look at the following:
The project name is followed by a tests file. It contains a. m file and a info.plist file. In the targets also has his figure.
So how do we use it?
Let's do a page like this:
The Text field control is then connected to the Viewcontroller, and the Button's touch up inside event connects Viewcontroller.
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weaknonatomicIBOutlet UITextField *txtRevenue;- (IBAction)onClick:(id)sender;@end
Then we write-(ibaction) OnClick: (ID) sender;
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];}- (IBAction)onClick:(id)sender { _txtRevenue.text = @"100";}@end
If you click on the button we let the Text field control display 100; it's that simple.
This is the function we write.
Then we will use unit tests to test whether the write is correct.
We'll open the. m file inside the _tests file.
@interfacePitaxtests:xctestcase@end@implementationpitaxtests-(void) SetUp {[SuperSETUP];//Put Setup code here. This method isCalled before the invocation ofEach test methodinchThe class.}- (void) TearDown {//Put teardown code here. This method isCalled after the invocation ofEach test methodinchThe class.[SuperTearDown];} - (void) Testexample {//This isAn example ofA functional test Case. Xctassert (YES, @"Pass");} - (void) Testperformanceexample {//This isAn example ofA performance test Case. [SelfMeasureblock:^{//Put the code want to measure the time ofHere. }];}
- (void) SetUp; The method is the method that is called at the beginning of the test, in which some variables can be initialized.
- (void) TearDown; method is the method that is called after the test completes.
The other two methods are test methods.
We see that our test class inherits from Xctestcase, and at run time, the Xctest framework runs a test method (such as) Testexample in the subclass of the xctestcase that starts with the tests.
So let's use it for testing.
We just wrote a function, that is, click the Calculation button on the interface, and then the text input box appears 100 this function.
Look at the code, you can understand it at a glance.
#import <UIKit/UIKit.h> #import <XCTest/XCTest.h> #import "ViewController.h" @interface pitaxtests : xctestcase @property(nonatomic,Strong) Viewcontroller *viewcontroller;@end @implementation pitaxtests - (void) SetUp {[SuperSETUP];uiapplication*application= [uiapplicationSharedapplication];UIWindow*window = [[Application delegate] window]; _viewcontroller = (Viewcontroller *) window. Rootviewcontroller;} - (void) TearDown {//Put teardown code here. This method was called after the invocation for each test method in the class.[SuperTearDown];} - (void) Testexample {example of a functional test case.Xctassertnotnil (_viewcontroller, @"Viewcontroller is nil");//Here is the assertion below said[_viewcontroller OnClick:Nil]; Xctassertequalobjects (_viewcontroller. Txtrevenue. Text, @" the",@"100 returned result error");//Here is also asserted}@end
The assertion above is used to verify that the result is correct, and if the error is to output the following prompt message. Describes the test case error.
Just record the learning unit test and not write it as a test-driven method.
Finally, add the assertion.
The following assertion comes from the blog assertion
Ii. Various assertion tests:
Xctfail (Format ...) generates a failed test;
Xctassertnil (A1, Format ...) For the null judgment, A1 is empty when passed, and vice versa;
Xctassertnotnil (A1, Format ...) Not for empty judgment, A1 is not empty when passed, and vice versa;
Xctassert (expression, format ...) Passed when expression evaluates to true;
Xctasserttrue (expression, format ...) Passed when expression evaluates to true;
Xctassertfalse (expression, format ...) Passed when expression evaluates to false;
Xctassertequalobjects (A1, A2, Format ...) Judgment is equal, [A1 ISEQUAL:A2] value is true when passed, one of which is not empty when not passed;
Xctassertnotequalobjects (A1, A2, Format ...) Judgment unequal, [A1 ISEQUAL:A2] value is false when passed,
Xctassertequal (A1, A2, Format ...) Judge Equality (when A1 and A2 are C scalar, struct, or union, the actual test finds that nsstring can also);
Xctassertnotequal (A1, A2, Format ...) Judgment unequal (used when A1 and A2 are C scalar, struct, or union);
Xctassertequalwithaccuracy (A1, a2, accuracy, Format ...) The judgment is equal, (double or float type) provides an error range, and passes the test when equal within the error range (+/-accuracy);
Xctassertnotequalwithaccuracy (A1, a2, accuracy, Format ...) The judgment is unequal, (double or float type) provides an error range, which passes the test when the error range is not equal;
Xctassertthrows (expression, format ...) Exception test, passed when expression has an exception; (Very sick)
Xctassertthrowsspecific (expression, specificexception, format ...) exception test, when expression occurs specificexception exception ; the other exception or no exception occurs;
xctassertthrowsspecificnamed (expression, specificexception, Exception_name, Format ...) Exception test, when expression has a specific exception, the exception of the specific exception name passed the test, and vice versa;
Xctassertnothrow (expression, format ...) An exception test, which passes the test when expression does not occur abnormally;
Xctassertnothrowspecific (expression, specificexception, format ...) Exception test, when expression does not have a specific exception, the specific exception name of the exception passed the test, and vice versa;
xctassertnothrowspecificnamed (expression, specificexception, Exception_name, Format ...) An exception test that passes the test when expression does not have an exception that has a specific exception, a specific exception name, and vice versa.
Recommended Daniel Blog: TDD iOS Development preliminary and Kiwi use getting Started
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Reading Notes-unit test xctest