Technology & Design

Writing Unit Test cases in iOS Unit Testing

Two Types of Testing in iOS

  1. Unit Testing
  2. UI Testing

In this Article, we will see Unit Testing.

First Step

Create Simple Project with Single View Application. We don’t need any UI Portion for this unit testing.

Second Step

Add following function in Fresh ViewController.swift File. we will generally test that function is working properly or not.
func getResult(_ value: Float) -> Float {
        return (value + 10)
}

So, Unit testing basically testing our logic functionalities

Third Step

Here we add core part of Unit Testing.

Click on ‘New Unit Test Target…’

Here every function will work as a separate testing functionality, Here you can run every function individual so you can test every diff functionality individual.

Here you can make your own functions, There will be one default function that you can use.

Fourth Step

Here in the first line importing @testable import UnitTesting Project name, cause I want to use its ViewControllers.

Then I made one Object from ViewController and assign its value to a functionsetup(). now I can use any methods of that particular ViewController.

Fifth Step

I’ve Created sample functiontestAddition(), that will test our ViewController’s logic Functionality

we’ve got a result from the functiongetResult().

now XCTAssert() this line will test that our expected answer is right or wrong

 

if your testing will run properly it will show

Else

 

Next is UI Testing.

 

Here you can download full Project source with Unit Testing & UI Testing.

Published by
Janki Thaker

Recent Posts

  • Data & Analytics

Databricks Lakehouse vs Traditional Data Warehouse: Which Offers Better ROI?

Databricks, an advanced solution that uses the Lakehouse architecture…

2 weeks ago
  • Data & Analytics

Data Democratization: How Databricks Simplifies Data Access Across Teams

To stay ahead of the curve, modern companies that…

3 weeks ago
  • AI/ML

Ambient AI: The Invisible Assistant

An invisible agent—sounds too good to be true, doesn’t…

3 weeks ago
  • Data & Analytics

Solving the ‘Data Silo’: An Approach with Databricks Lakehouse

Getting rid of data silos involves more than just…

1 month ago
  • AI/ML

Maximizing ROI with Agentic AI and Why Your Business Needs It

As we enter a new era of intelligent automation,…

1 month ago
  • Data & Analytics

From Data Chaos to Data Clarity: The True Value of Data Analytics

Businesses can break through the chaos of data using…

1 month ago