Swift – Unit Test

By | 17/07/2020

In this post, we will see how to create a Unit Test in Swift, in order to test a function called IsNumberEven used to check if a number in input is either even or odd.

First of all, we create a Swift file called Core, where we will define the function IsNumberEven:

[CORE.SWIFT]

import Foundation

class Core {
    func IsNumberEven(inputNumber: Int) -> Bool {
        if(inputNumber%2==0)
        {
            return true
        }
        else
        {
            return false
        }
    }
}



Obviously function is very easy, but it isn’t the goal of the post.

Finally, we create a Unit Test Case Class called TestCore, where we will test the function:

[TESTCORE.SWIFT]

import XCTest
// We have to import the name of the module that we want to test
@testable import SwiftPosts

class TestCore: XCTestCase {
  
    // Definiction of Core object
    private var objCore: Core!
    

    // In this method we can setup the variables used in the tests
    override func setUp() {
        super.setUp()
        objCore = Core()
    }
    

    // in order to run the test, we must add the prefix 'test' at the function's name
    func testIsNumberEven_WithInputEvenNumber_ShouldReturnTrue()
    {
        // In the classic Unit Test the AAA pattern means Arrange, Act and Assert.
        // In Swift instead we have Given, When and Then
            
        // Given
        // Definition of the input used for testing
        let lstInput = [2,4,6,12,36,90,120]
        var result:Bool = true
            
        for singleItem in lstInput
        {
            // When
            result = objCore.IsNumberEven(inputNumber: singleItem)
            if (result==false)
            {
                break
            }
        }
            
        // Then
        XCTAssertTrue(result)
    }
      
  
    // in order to run the test, we must add the prefix 'test' at the function's name
    func testIsNumberOdd_WithInputOddNubmer_ShouldReturnFalse()
    {
        // In the classic Unit Test the AAA pattern means Arrange, Act and Assert.
        // In Swift instead we have Given, When and Then
            
        // Given
        // Definition of the input used for testing
        let lstInput = [1,3,5,11,35,91,121]
        var result:Bool = false
            
        for singleItem in lstInput
        {
            // When
            result = objCore.IsNumberEven(inputNumber: singleItem)
            if (result==true)
            {
                break
            }
        }
            
        // Then
        XCTAssertFalse(result)
    }
}



Now, we can run all tests, pushing the arrow near the class name:

and at last, we will receive a result message:



7 thoughts on “Swift – Unit Test

  1. Royal CBD

    Excellent post however I was wondering if you could write a litte
    more on this subject? I’d be very thankful if you could elaborate a little bit further.
    Thank you!

    Also visit my blog post – Royal CBD

    Reply
    1. Damiano Abballe Post author

      Thanks for your comment!
      I will prepare another post on Swift – Unit Tests!

      Reply
  2. Anonymous

    hello!,I love your writing very a lot! percentage we
    keep in touch more approximately your article on AOL?
    I need an expert in this area to unravel my problem.
    Maybe that is you! Taking a look forward to peer you.

    Reply

Leave a Reply

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