Write ocunit test method-Application Test Method

Source: Internet
Author: User

Application Testing Method

Application testing is to test some functions of an application. This function triggers an event by clicking a button. Therefore, it is mainly used to test the presentation layer. Let's take a look at the methods in View Controller viewcontroller. m that need to be tested, and then design the test cases.

@ Implementation viewcontroller-(void) viewdidload {[Super viewdidload];}-(void) didreceivemorywarning {[Super didreceivemorywarning];}-(ibaction) onclick :( ID) sender {// shut down the keyboard using self.txt revenue resignfirstresponder]; self. lbltax. TEXT = [self calculate: self.txt revenue. text];} // Calculate personal income tax-(nsstring *) Calculate :( nsstring *) Revenue {... ...} @ End

 

 

Whether viewdidload and didreceivememorywarning need to be tested depends on whether there are some self-written code in this method. For now, we do not need to test them. Onclick: The method to respond to the user clicking the computing button. It needs to be tested. Calculate: The method is the business logic method. We have tested it in the logic test. Do you need to test it again? Generally, you can test the logic only. However, if the method requires an external environment (dependent on other classes or special runtime environments ), if the logic test cannot be provided, application test is required. This is the application test that can run on the device. It can provide a real test environment.

Here we implement onclick: The application test of the method. We need to simulate button event processing. Its input conditions are input through the text box control, and the output result is displayed through the label control. Design a test case to select common values and boundary values as input values. The keyboard of the text box is limited to a numeric keyboard.

Input verification does not need to be considered too much. We only need to consider null cases. We have designed six use cases.

Onclick: Method Application Test Case

Test Cases

Input Conditions

Total Monthly Income (RMB)

Output result

Monthly Personal Income Tax payable (RMB)

Description

1

Blank 0.00 If the test is not entered, click the compute button.

2

8000 345.00 Test integer

3

8000.59 345.12 Test decimal number

4

08000.59 345.12 Testing has leading 0 Data

5

40000.50.56 8195.15 Test two decimal places

6

40000. 50 .. 56 8195.15 Test two decimal points connected together

Let's take a look at the application test class appllicationtest. H code:

# Import <sentestingkit/sentestingkit. h> # import "appdelegate. H "# import" viewcontroller. h "@ interface appllicationtest: sentestcase @ property (nonatomic, strong) viewcontroller * viewcontroller; @ end application test class appllicationtest. code of setup and teardown methods in M:-(void) setup {[Super setup]; appdelegate * appdelegate = [[uiapplication sharedapplication] Delegate]; uiwindow * window = [appdelegate window]; uinavigationcontroller * navcontroller = (uinavigationcontroller *) window. rootviewcontroller; self. viewcontroller = (viewcontroller *) navcontroller. topviewcontroller;}-(void) teardown {self. viewcontroller = nil; [Super teardown];}

 

In the setup method, we need to initialize the viewcontroller attribute. viewcontroller represents a view controller, which is created by the IOS system through the storyboard file, rather than simply instantiating through the following statement:

Self. viewcontroller = [[viewcontroller alloc] init];

We can use the application to delegate the object appdelegate to obtain the window object. Each window object can use the attribute rootviewcontroller to obtain a Root View Controller. In this example, the Root View Controller is uinavigationcontroller instead of viewcontroller, therefore, we also need to use the topviewcontroller attribute of uinavigationcontroller to obtain the viewcontroller object.

Test method code in appllicationtest. M:

// If you do not enter a value for the test, click the compute button (void) testonclickinputblank {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value ."); // Set the input value self.viewcontroller.txt revenue. TEXT = @ ""; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // asserted stassertequalobjects (strtax, @ "0.00", @ "Expected Value: 0.00 actual value: % @", strtax);} // test integer-(void) testonclickinputintegernumber {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value. "); // Set the input value self.viewcontroller.txt revenue. TEXT = @ "8000"; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // assert stassertequalobjects (strtax, @ "345.00", @ "Expected Value: 345.00 actual value: % @", strtax);} // test the decimal-(void) testonclickinputonedot {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value. "); // Set the input value self.viewcontroller.txt revenue. TEXT = @ "8000.59"; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // asserted stassertequalobjects (strtax, @ "345.12", @ "Expected Value: 345.12 actual value: % @", strtax );} // test two decimal places-(void) testonclickinputtwodot {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value. "); // Set the input value self.viewcontroller.txt revenue. TEXT = @ "40000.50.56"; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // asserted stassertequalobjects (strtax, @ "8195.15", @ "Expected Value: 8195.15 actual value: % @", strtax );} // test with leading 0 data-(void) testonclickinputprefixzero {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value. "); // Set the input value self.viewcontroller.txt revenue. TEXT = @ "08000.59"; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // asserted stassertequalobjects (strtax, @ "345.12", @ "Expected Value: 345.12 actual value: % @", strtax );} // test the two decimal points connected together-(void) testonclickinputlinkdot {stassertnotnil (self. viewcontroller, @ "viewcontroller is not assigned a value. "); // Set the input value self.viewcontroller.txt revenue. TEXT = @ "40000. 50 .. 56 "; // call oncclick to test [self. viewcontroller onclick: Nil]; // obtain the output result nsstring * strtax = self. viewcontroller. lbltax. text; // asserted stassertequalobjects (strtax, @ "8195.15", @ "Expected Value: 8195.15 actual value: % @", strtax );}

 

 

These test methods are very similar. First, you need to use the stassertnotnil macro to judge self. whether the viewcontroller is nil, and then self.viewcontroller.txt revenue. text sets the text box value. When running, we enter the value through the text box control. Statement [self. viewcontroller onclick: Nil] is the core purpose of the test. The parameter is that the Button Object Pointer onclick: is not used. You can pass nil. The output result is obtained from the lbltax label control. Use stassertequalobjects macro assertions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.