IOS development-OCUnit test method-logic test method

Source: Internet
Author: User

We mentioned the Application Testing and Logic Testing concepts when we added OCUnit to the project. They are not OCUnit concepts, it is a concept in unit testing. Application testing is a test of the entire application. When designing test cases, you must consider the running environment and other factors. For example, when testing JavaEE, you must consider environment issues such as Web containers and EJB containers. The logic test is lightweight and only tests the correctness of methods or algorithms of a business logic object. Write OCUnit Test Methods Each unit test case corresponds to a method in the test class. Therefore, the test classes are divided into logic test classes and Application Test classes. When designing test cases, logical test and application test are also different. The OCUnit test method should also be divided into logic test and application test. Next we will introduce them through personal income tax calculation. Their compilation process will not be introduced in the testing ViewController compilation process. 1. Logic test method the logic test should test the business logic for calculating personal income tax, that is, to test the calculate in the ViewController class: Method LogicTest. the code for h is as follows: [cpp] # import <SenTestingKit/SenTestingKit. h> # import "ViewController. h "@ interface LogicTest: SenTestCase @ property (nonatomic, strong) ViewController * viewController; @ end defines the viewController attribute in the h file. Be sure to set the attribute parameter to strong. LogicTest. the Code of m is as follows: # import "LogicTest. h "@ implementation LogicTest-(void) setUp {[super setUp]; self. viewController = [[ViewController alloc] init];}-(void) tearDown {self. viewController = nil; [super tearDown];} // Test Case 1-(void) testCalculateLevel1 {double dbRevenue = 1500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController Calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 45, @ "Expected Value: 45 actual value: % @", strTax );} // Test Case 2-(void) testCalculateLevel2 {double dbRevenue = 1500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 345, @ "Expected Value: 345 actual value: % @", strTax );} // The tax payable for the test month exceeds 4500 yuan to 9 yuan 000 Yuan case 3-(void) testCalculateLevel3 {double dbRevenue = 12500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 1245, @ "Expected Value: 1245 actual value: % @", strTax );} // test case 4-(void) testCalculateLevel4 {double dbRevenue = 9000; NSString * strRevenue = [NSString stringWithFo Rmat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 7745, @ "Expected Value: 7745 actual value: % @", strTax );} // test case 5-(void) testCalculateLevel5 {double dbRevenue = 35000; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax dou BleValue] = 13745, @ "Expected Value: 13745 actual value: % @", strTax);} // test case 6-(void) where the taxable amount in the month exceeds 55000 RMB to 80000 RMB) testCalculateLevel6 {double dbRevenue = 83500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 22495, @ "Expected Value: 22495 actual value: % @", strTax );} // test case 7-(void) testCalculateLevel7 {d Ouble dbRevenue = 103500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 31495, @ "Expected Value: 31495 actual value: % @", strTax );} @ end # import <SenTestingKit/SenTestingKit. h> # import "ViewController. h "@ interface LogicTest: SenTestCase @ property (nonatomic, strong) ViewController * viewController; @ End defines the viewController attribute in the H file. Note that the attribute parameter is set to strong. LogicTest. the Code of m is as follows: # import "LogicTest. h "@ implementation LogicTest-(void) setUp {[super setUp]; self. viewController = [[ViewController alloc] init];}-(void) tearDown {self. viewController = nil; [super tearDown];} // Test Case 1-(void) testCalculateLevel1 {double dbRevenue = 1500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController Calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 45, @ "Expected Value: 45 actual value: % @", strTax );} // Test Case 2-(void) testCalculateLevel2 {double dbRevenue = 1500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 345, @ "Expected Value: 345 actual value: % @", strTax );} // The tax payable for the test month exceeds 4500 yuan to 9 yuan 000 Yuan case 3-(void) testCalculateLevel3 {double dbRevenue = 12500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 1245, @ "Expected Value: 1245 actual value: % @", strTax );} // test case 4-(void) testCalculateLevel4 {double dbRevenue = 9000; NSString * strRevenue = [NSString stringWithFo Rmat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 7745, @ "Expected Value: 7745 actual value: % @", strTax );} // test case 5-(void) testCalculateLevel5 {double dbRevenue = 35000; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax dou BleValue] = 13745, @ "Expected Value: 13745 actual value: % @", strTax);} // test case 6-(void) where the taxable amount in the month exceeds 55000 RMB to 80000 RMB) testCalculateLevel6 {double dbRevenue = 83500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 22495, @ "Expected Value: 22495 actual value: % @", strTax );} // test case 7-(void) testCalculateLevel7 {d Ouble dbRevenue = 103500; NSString * strRevenue = [NSString stringWithFormat: @ "% f", dbRevenue]; NSString * strTax = [self. viewController calculate: strRevenue]; STAssertTrue ([strTax doubleValue] = 31495, @ "Expected Value: 31495 actual value: % @", strTax );} @ end initialize viewController in the setUp method and release the viewController attribute in the tearDown method. Test method: testCalculateLevel1 ~ TestCalculateLevel7 corresponds to test case 1 ~ 7. In the test method, STAssertTrue is the macro assertions of the OCUnit framework. OCUnit framework assertions macro framework indicates that STAssertEqualObjects fails to assert when two objects are not equal or one of them is nil. STAssertEquals fails to assert when parameter 1 is not equal to parameter 2, used for basic data testing in C. STAssertNil fails assertion when the parameter is not nil; STAssertNotNil fails assertion when the parameter is nil; STAssertTrue fails asserttrue when the expression is false; STAssertFalse: The assertion fails when the expression is true. STAssertThrows fails if the expression does not throw an exception. STAssertNoThrow fails when the expression throws an exception;

Related Article

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.