Unit Testing in iOS using swift (Part 1)

Updated 26 October 2021

Save

Unit Testing in iOS

Unit testing is a basic way to check the first phase of your code , it will help to check your model classes & Network Call in the App . so it helps & saves lot of time

of manual testing. In this part we will provide , how to setup the unit testing in the App.

1: Setup in App:

there is various way to add the Unit testing in the new App or Existing App.

a: In New App : Check on Unit test.

b: In Existing App:

You can take the Unit test File in various way

b.1 : Go to file section & take a Unit Test File

.

b.2 : Click on test navigator option then click on bottom + sign & take a Unit test file.

After Set the file now its time to write the code.

2: Unit test Class structure:

When you open the file you will get

func setUp() : This method will call at first when this class will load so any kind of setup you can do in this class like variable  initialisation or model initialisation.

func tearDown(): This method will call ,when All your test method is executed so in this section you can free-up the memory .

func testExample() : This is Test method that will run after the setUp method , you will create your own but you need to append test on start of any method name

ex: testHomeAPI(), testMySample()

func testPerformanceExample(): this will give the total time of your test classes.

 

3: How to use :

For Accessing the project in Test Class file you need to import your project module like this.

1: select your project target -> Build setting – > Product Module name

 

Write this way in your Test Class file:

Now you can access all the classes of your project ,

Here i have taken one example of my project model class .

 

Here we are accessing the model class of our project & do some checking .

4: How to Run & Observe

Before running there is some method you need to know first here we have taken some method for example.

1: XCTAssertNil(responseError,“Message in String”) : if nil is passed then no issue other wise it show the error

2: XCTFail(“Error: \(error.localizedDescription)”) : if you want to fail the test use this method.

3:XCTAssertTrue(true) : if there is no issue then use this.

4:XCTAssertEqual(statusCode, 200,”Message write here”)   : it will compare the value if equal then no issue other wise show the error.

Here we have taken few method

Now run the test cases

there is various way to run the test classes.

1: Long press on project run button then it will show test option

2: Go to Test App navigator option

here you can see All the test classes & their method as well so select which classes you have to run just tap on run button

 

This is the introduction of  Unit test Class in next Blog we will cover the Async Network call & Some other tips to test the Model classes.

Next Blog Link: https://mobikul.com/unit-testing-in-ios-using-swift-part-2/

 

 

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home