IOS Test Frameworks and Unit Testing Brief Explanation

8525 단어 iOSSwiftunittest

IOS Test Frameworks



Maybe you already developed an application that is working properly, but you might not have any test set up for it and of course you may want to test any changes when you want to extend the application functionality.

In IOS there are lots of Test Frameworks, created for developers to test their applications to make sure that all the functionalities are working fine before shipping them to the users. This article will list some of the popular Test Frameworks for IOS application.

  • Appium : is an open-source testing tool. It provides automations of application testing process on Android and IOS. It allows the user to check each individual parts of the program's source code, and systematically done during the process of programming.

  • Calabash : is an IOS automation testing tool. The testing process of this framework is done based on Cucumber (an extensive automation testing tool)

  • XCTest : is the IOS functional testing framework. This framework can be used for testing at any state of the development process.

  • EarlGrey : is the best UI testing framework. This open-source framework is easy to integrate with IOS.

  • Jest : is an automated testing framework. Using this framework, we can ensure that the user interface cannot be changed unexpectedly.

  • As mentions earlier there are lots of Test Framework available for IOS, but in this article I will walk you through the built-in IOS Unit testing itself.

    IOS Unit Testing



    To demonstrate of how we can implement Unit Test in IOS I create a sample project ArithmeticUnitTest in Github. It's basically an app to print the value of arithmetic operation to the console by giving sample values. Now clone it into your local the project in Xcode.

    Create a Unit Test Target in Xcode



    Now, open test navigator and click on the + sign at the lower left corner and then select New Unit Test Target

    To run this test class you can just simple go to Product -> Test or Command + U

    Using XCTAssert to test the Model



    On top of your test class add line below the import statement:
    @testable import UnitTestSample
    

    It will give you the unit test access to classes and methods in UnitTestSample
    Now, let's create a property of Arithmetic class in the test class
    var arithmeticUnderTest: Arithmetic!
    

    then initialize in setup(), It will create SUT (System Under Test), so that all the tests in this test class can access to the SUT's object property and methods.
    override func setUp() {
        super.setUp()
        self.arithmeticUnderTest = Arithmetic(a: 10, b: 5)
    }
    

    and then you have to release the SUT object in tearDown()
    override func tearDown() {
       self.arithmeticUnderTest = nil
       super.tearDown()
    }
    

    Now, we are ready to write our Arithmetic Unit test. Let's go and delete testExample() and add these following methods:
    func testAddition() {
        XCTAssertEqual(self.arithmeticUnderTest.addition(), 15, "Addtion Process is wrong!")
    }
    
    func testSubstraction() {
        XCTAssertEqual(self.arithmeticUnderTest.substraction(), 5, "Substraction Process is wrong!")
    }
    
    func testMultiplication() {
        XCTAssertEqual(self.arithmeticUnderTest.multiplication(), 50, "Multiplication Process is wrong!")
    }
    
    func testDivision() {
        XCTAssertEqual(self.arithmeticUnderTest.division(), 2, "Division Process is wrong!")
    }
    

    A test method's name always begin with test.
    Right in the above given methods, we give a sample value at the initialization part in setup() int a = 10 and int b = 5. Now let's run the test and see the result. If the result is passed, the diamond button on the right hand of each test functions will turn green.

    Debugging a Test



    In this testing method, you can also add Test Failure Breakpoint. By creating this the test program will stop running when it encounter any failure. You can create it by navigating to the Breakpoint Navigator and click on the + sign at the lower left corner and then select Test Failure Breakpoint

    Now go ahead and change the result of arithmetic operation to see how it look.
    func addition() -> Int {
        return self.a - self.b
    }
    

    Now that you have change the result of addition, then it will give you the error when you attempt to run test.


    Cool! That's all for the demonstration now, but there are lots more we can do with this IOS Unit Test like Asynchronous test operation..etc stuff like that.

    좋은 웹페이지 즐겨찾기