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 :)

    Wednesday, April 6, 2016

    Handling Multiple Targets & Environments in iOS


    Hey Folks,

    Finally writing something that I wanted to do from long time.
    Handling multiple targets in an iOS Project that deal with multiple environments.

    Lets Start.

    1) Create a New Project or you can start with an existing Project.
        I will be using new one named HelloGradle which I will be using in Gradle integration also.

    2) So in my Project i was provided with a default target as HelloGradle. I renamed it as "A".

    3) Now lets add more Targets.

    • Click on Project.
    • Right Click on any Target.
    • Duplicate.
    • Rename it as "B".
    • Perform this task to add more Targets. I added 6 in total "A - F".




    4) Name still not changed : You will observe that Target names are still not changed at all places
    Go to Manage Schemes. It will show all targets. Select a Target press Enter change Name and Press Enter.




    5) Now rename the plist files associated with each Target.
    • Click on Target.
    • Build settings.
    • Packaging.
    • Rename plist file for all targets as A-Info.plist , B-Info.plist .......... F-Info.plist.


                                                             



    6) Build and Run : It will work fine as usual. Currently there are only 2 build Configurations namely "Debug" & "Release"

    7) Now I wish to add Multiple Build Configurations or you can say Environments. 
        Remember : Build configurations are project wise not Target wise.
    • Project
    • Go to Info Tab
    • Click Editor
    • Add Configuration
    • Duplicate Debug Configuration

    OR
    • Click + below Configurations
    • Duplicate Debug Configuration




    8) Now we have multiple Targets & multiple Build Configurations 




    9) Now the question arises how to store environment specific values. For this do this..
    • Create plist for all Environment & for all Targets. 
    • Ensure that name of plist file remains same.
    • Have a look @ the structure.
    • Remember to follow the naming conventions.
    • Inside plist file you can add values that are environment specific.
    • Remember to add one value in all plists ENVIRONMENT : TARGET_Dev / TARGET_QA / TARGET_Prod depending on Target & environment folder.


    10)  Now to establish relation between plist file and build configuration
    • Target
    • Build Settings
    • Click on + next to  ALL | Combined | Levels + 

    • Add User defined Settings
    • Name it as Build_Env
    • Provide values as shown in below image



    11) Choose any Build Configuration from Edit Scheme
    12) Build & Run your project
    13) And you are Done
    14) Now from AppDelegate read your environment.plist file and set values accordingly.



    Hope you did not find it BORING :P







    Tuesday, April 5, 2016

    Creating Profiles & Certificates for iOS


    Hey Folks,

    This tutorial gives you basic info of the Developer account for creating profile & certificates etc..

    Lets StartCreating profiles & certificates
    • Open developer.apple.com
    • Click Member Center
    • Provide Apple Developer credentials & Log in
    • Click Certificate , Identifiers , Profile








    ==================================================================

     Certificates


    ********************************* Step - 1 *********************************




    ********************************* Step - 2 *********************************

    Choose between Development & Production


    ********************************* Step - 3 *********************************



    ********************************* Step - 4 *********************************



    ********************************* Step - 5 *********************************

    Your Certificate is created and now you can download it


    =============================================================


    App ID

    ********************************* Step - 1 *********************************



    ********************************* Step - 2 *********************************


    ********************************* Step - 3 *********************************

    Click to Continue & its done


    =============================================================


    Provisioning Profile


    ***************************** Step - 1 ******************************





    ********************************* Step - 2 *********************************




    ********************************* Step - 3 *********************************




    ********************************* Step - 4 *********************************

    Click to Download


    =============================================================

    Devices

    • Provide Name, UDID and you are DONE with devices.
    • You can add a max of 100 devices. However people say that if u drop a mail to Apple they may increase your quota of devices for FREE but haven't tried for it.



    Thanks to all :)

    Friday, April 1, 2016

    Hands On with PCH File in IOS


    Hey Folks,

    All people working on iOS must have come across this file. Lets dive in it for all its pros and cons

    What is PCH File?

    • Pre-Compiled Header File
    • Prefix headers are compiled and stored in a cache, and then automatically gets included in every file during compilation & lets you include a file without adding an import statement to it. So rather than parsing the same header files over and over, these files get parsed once, ahead of time.

    Naming Convention
    • ProjectName-Prefix.pch

    Positive Face
    • Precompiling the prefix header will be most effective if the contents of the prefix header or any file it includes change rarely.
    • They make compiling faster.

    Negative Face
    • If the contents of the prefix header or any file it includes change frequently, there may be a negative impact to overall build time.
    • Avoid adding Macros.
    • If you transfer your file to any other project then it may not compile as many dependent headers that were added in PCH are not available @ different location and you need to check the actual one from PCH headers list. This could be a tedious task for Large projects.
    • People have one funda >> Adding headers in PCH saves header importing typing time.

    Can i compile my project without PCH file
    • YES, Ofcourse

    How to add PCH file if not provided
    • Right Click on Project
    • New File
    • iOS > Others > PCH file
    • Provide name as ProjectName-Prefix.pch
    • Save in ProjectName Folder
    • Target > Build Settings > Search for Prefix Header and Provide Name







      Hope you all enjoyed it :)