Write ocunit test method-logic test method

Source: Internet
Author: User

Application testing and logic Testing

When we added ocunit to the project, we mentioned the application testing and logic testing concepts. They are not ocunit concepts, but unit testing concepts. Application testing is a test of the entire application. When designing test cases, you must consider the runtime 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.

Compile ocunit Test Method

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 Testing Method

The business logic for calculating personal income tax should be tested in the logic test, that is, the calculate in the viewcontroller class: Method

The code for logictest. H is as follows:

# 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 );} // Test Case 3-(void) testcalculatelevel3 {double dbrevenue = 4500; 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 stringwithformat: @ "% 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 doublevalue] = 13745, @ "Expected Value: 13745 actual value: % @", strtax );} // The tax payable for the test month exceeds 55000 RMB to 80000 RMB. Use Case 6-(void) 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 {double dbrevenue = 80000; 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 assertion macro

Framework

Description

Stassertequalobjects

When two objects are not equal or one of them is nil, the assertion fails;

Stassertequals

When parameter 1 is not equal to parameter 2, the assertion fails and is used for basic data testing in C;

Stassertnil

Assertion fails when the parameter is not nil;

Stassertnotnil

Assertion fails when the parameter is nil;

Stasserttrue

When the expression is false, the assertion fails;

Stassertfalse

Assertion fails when expression is true;

Stassertthrows

If the expression does not throw an exception, the assertion fails;

Stassertnothrow

If the expression throws an exception, the assertion fails;

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.