UnIT TESTING




  • What is Unit Testing?
  • Introducing STTest.
  • Writing Unit test,Running Unit tests.
Unit Test:


  • Single "unit" of functionality.
  • Pass/Fail
  • Small,Fast,Isolated.


Unit testing does not cover the following things:

  • Performance
  • UI Interaction
  • Whole system integration.

Incorporate Unit-Testing in a Project


To unit-test your code,your project must include Unit-test bundles.These bundles contain the test methods that exercise the code in an app to ensure that it behaves correctly.

Setting Up Unit-Testing in a Project


  • The most convenient way of configuring unit tests in a project or target is at the time you create it. 
  • Select the Include unit tests option when you create a project or a target,Xcode includes a unit-test target in the scheme that builds the product.
Adding a Unit-Test Target to Your Project

To add a unit-test target to a project:

  1. Open a project to which you want to add a unit test target.
  2. Choose File > New > New Target.
  3. Select the unit testing bundle template for your platform: 
  • iOS: In the iOS section,select Other,the select the Cocoa Touch Unit Testing Bundle Template.                                                               
  •  Mac: In the OS X section,select Other,then select the Cocoa Unit Testing Bundle Template.


    4.Click Next.
    5.Specify the following unit-test target options:
    • Product Name
    • Project
    6.Click Finish.

    After adding the unit-test target the project navigator shows a new group-with the same name as the target-that contains the source files that implements the temporary test case.The new target is listed in the project's target list.

    This unit-test target can perform one type of unit testing logic or application not both.

    Setting up Logic Unit Tests


    To Set up a unit-test target to perform logic tests:

    1.In the project editor select the unit-test target you want to set up,and display the build settings pane.

    2.In the Build Settings pane's scope bar,click All.

    3.In the search field enter bundle loader,and press Return.

    4.If the Bundle Loader build setting appears in bold,select it and press Delete.

    5.In the search field,enter test host and press Return.

    6.If the Test host build setting appears in bold,select it and press Delete.





    To confirm that the unit-test target is configured correctly to perform logic tests:

    1.From the Scheme toolbar menu,choose the unit-tests scheme and a run destination:

    IOS:For logic tests,the run destination can be only a simulator.


    2.Choose Product > Test.

    3.Choose View > Navigators > Issue,to display the issue navigator.

    The issue navigator lists the failed temporary test case,indicating the unit-test target is working correctly.



    1. Choose View > Navigators > Show Log Navigator to display the log navigator. 
    2. In the action list in the log navigator,select the test run named after the unit- test target. The test log run appears in the editor area.                      


    The test-run script indicates that the unit tests performed are the logic unit tests.

    Writing Test Case Methods

    • A test case method is an instance method of a test suite class that's named test..,with no parameters, and whose return type is void.
    • A test case method calls the code being tested (known as the unit) and reports whether the calls produced the expected result.
    • For  example ,whether they return the anticipated value or raise an appropriate exception.
    • Test case methods use a set of macros to check for the expected conditions and report for their findings.

    Structure of a Test case Method


    -(void)test<test_case_name> {

    ....      // Set up,call test-case subject API.

    ST...//  Report pass/fail to testing framework.

    ....     // Tear down.

    }

    • When Xcode runs unit tests,it invokes each test case method independently.
    • Add a pair of methods to a test suite class that are called before and after each test case method is invoked :setUp and tearDown.



    Example Of a setUp/tearDown method pair


    -(void)setUp {

    test_subject=[[MyClass alloc]init];

    STAssertNotNil(test_subject,@"could not create test object");

    }


    -(void)tearDown {

    [test_subject release];

    }


    Unit-Test Result Macro Reference


    • The SenTestingKit framework defines a set of test case result macros that report test case results to the framework.
    • When a test fails,the framework sends a test-failure message ,which Xcode displays in the log and issue navigators. 
    • The following test result macros you can use in your test case methods.                                 
      • UnConditional Failure
      • Equality Tests 
      • Nil Tests          
      • Boolean Tests                                                                                                                                                  
      • Exception Tests

    Unconditional Failure

    STFail

    Fails the test case.

    STFail(failure_description,...)


    Parameters

    failure_description

     Format string specifying error message.Can be nil.



    Equality Tests

    STAssertEqualObjects

    Fails the test case when two objects are different.

    Syntax:

    STAssertEqualObjects(object1,object2,failure description,..) 

     Parameters

    object_1

       An object.

    object_2

      An object.

    Failure_description

     Format string specifying error message.Can be nil.

    STAssertEquals

    Fails the test case when two values are different.

    Syntax:

     STAssertEquals(value_1,value_2,failure_description,...)

    Parameters

    value_1

     A scalar,structure or union.

    value_2

     A scalar,structure or union.

    Failure description:

     Format string specifying error message.Can be nil.


    STAssertEqualsWithAccuracy

    Fails the test case when the difference between two values is greater than the given value.

    Syntax:

    STAssertEqualsWithAccuracy(value1,value2,accuracy,failure description,..)

    Parameters

    value_1

     An integer or a floating-point value.

    value_2

     An integer or a floating-point value.

    accuracy

     An integer or a floating-point value.

    failure description

     Format string specifying error message.Can be nil.


    Nil Tests

    STAssertNil

    Fails the test case when a given expression is not nil.

    Syntax:

    STAssertNil(expression,failure description,....)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.


    STAssertNotNil

    Fails the test case when the given expression is nil.

    Syntax:

    STAssertNotNil(expression,failure_description,..)

    Parameters

    expression:

     Expression to test.

    failure_description:

     Format string specifying error message can be nil.


    Boolean Tests

    STAssertTrue

    Fails the test case when a given expression is false.

    Syntax:

     STAssertTrue(expression,failure description,...)

    Parameters:

     expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertFalse

    Fails the test case when a given expression is true.

    Syntax:

     STAssertFalse(expression,failure_description,...)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.

    Exception Tests

    STAssertThrows

    Fails the test case when an expression does not raise an exception.

    Syntax:

    STAssertThrows(expression,failure_description,...)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertThrowsSpecific

    Fails the test case when an expression does not raise an exception of a particular class.

    Syntax:

    STAssertThrowsSpecific(expression,exception_class,failure_description,...)

    Parameters

    expression

     Expression to test.

    exception_class

      An exception class.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertThrowsSpecificNamed

    Fails the test case when an expression doesn't raise an exception of a particular class with a given name.

    Syntax:

    STAssertThrowsSpecificNamed(expression,exception_class,exception_name,failure_description,..)

    Parameters

    expression

     Expression to test.

    exception_class

     An exception class.

    exception_name

     A string with name of an exception.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertNoThrow

    Fails the test case when an expression raises an exception.

    Syntax:

     STAssertNoThrow(expression,failure_description,...)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertNoThrowSpecific

    Fails the test case when an expression raises an exception of a particular class.

    Syntax:

    STAssertNoThrowSpecific(expression,exception_class,failure_description,...)

    Parameters

    expression

     Expression to test.

    exception_class

     An exception class.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertNoThrowSpecificNamed

    Fails the test case when an expression raise an exception of a particular class with a given name.

    Syntax:

    STAssertNoThrowSpecificNamed(expression,exception_class,exception_name,failure_description,...)

    Parameters

    expression

     Expression to test.

    exception_class

     An exception class.

    exception_name

     A string with the name of an exception.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertTrueNoThrow

    Fails the test case when an expression is false or raises an exception.

    Syntax:

    STAssertTrueNoThrow(expression,failure_description,...)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.

    STAssertFalseNoThrow

    Fails the test case when an expression is true or raises an exception.

    Syntax

    STAssertFalseNoThrow(expression,failure_description,...)

    Parameters

    expression

     Expression to test.

    failure_description

     Format string specifying error message.Can be nil.





    THANK YOU

    UNIT TESTING

    By Torry Harris Business Solutions