Friday, April 8, 2016

Unit Testing in iOS


Hey Folks,

I enjoyed working with Unit Testing. So decided to make a demonstration for you all.
Hope you find it helpful.

What is Unit testing
    • The smallest part of any software development task in which the units are tested for proper and smooth workout. 
    • It can be done manually and automated too.

    Why should I write Test Cases or do Unit testing
    • Helps to define a problem and find all its positive & negative solutions.
    • Gives better understanding of each small task or code.

    Things that should definitely be covered or tested in a Unit Test
    • Boundary Values
    • Expected Values
    • Stress Testing for Loops
    • Out of Bound values
    • Nil / NULL check

    Working with OCUnit in XCode
    • OCUnit is built in XCode
    • When you check Use Unit Testing on New Project template wizard the setup is already done for you.

    Lets start by Adding Unit Test Target to an existing project
    • Right Click on Target
    • iOS
    • Test
    • Choose iOS Unit Testing Bundle


    Working on New Project with Test Targets
    • Start xCode
    • Choose a Single View Application
    • Choose options for your new project
    • Remember to check on "Include Unit Tests"

    • This is the Project Navigator



    • This is the sample code structure available for Test file 



    XCTestCase Class
    • It is a subclass of XCTest and is the Override points for writing test cases
    • This class can have multiple methods & supports the Setup & TearDown methods
    • Setup & TearDown methods execute BEFORE & AFTER every test method
    • Test methods are instance methods
    • Test methods accept NO PARAMETERS & have NO RETURN VALUE
    • Test methods are prefixed with "test" keyword
    • Being prefixed they are automatically recognized as test methods by the framework

    How to add XCTestCase Class Files
    • Right Click on Tests
    • New File
    • iOS
    • Source
    • Unit Test Case Class 
    • Provide a Name, must be suffixed with keyword Tests

    Some Testing Code
    • To compare Objects
                             XCTAssertEqualObjects(String to compare, my string, @"comments");
    • To check date accuracy or precision of values
                            XCTAssertEqualWithAccuracy(Date to compare, date that I set , 0.001);
    • To check if dictionary has nil value
                            XCTAssertNotNil(dict);
    • To Check true / false value
                            XCTAssertTrue(BOOL value returned from any method,
                                                      @”Can be used for conformsToProtocols”);

                            XCTAssertFalse(BOOL value returned form  method,
                                                      @"Generates a failure when ((a expression) != false)");
    • To test performance or execution Time
                            [self measureBlock:^ { [self anyMethod]; } ] ;


    How to Execute Tests
    • Command + U
    • OR Product > Test

    View status of all Tests executed
    • The Green Tick indicates the Success.
    • If it is Red then it indicates an Error.
    • Same images appear in reader view before each method


    Enable XCode Code Coverage Reports
    • Product > Scheme > Edit Scheme
    • Choose Test Scheme > Tick "Gather Coverage Data"


    View Reports in XCode
    • Command + 8
    • OR View > Navigators > Report Navigators










    Some Info...
    • What happens if i remove Setup and tear down?? 
    • No issue arises
    • Can I run single test?? 
    • YES, you can :  Control-Option-Command-U
    • Running xCode Tests from Terminal
    • xcodebuild test -scheme OpenPage2 -project OpenPage2.xcodeproj -configuration Debug -sdk iphonesimulator
    • It clearly states that  Unit Tests are not supported by the Simulator. To avoid this add TEST_HOST='' to the end of the xcodebuild command. 
    • Run the tests from the terminal again. 

    Hope you find it helpful :)

    No comments:

    Post a Comment