What is given-when-then testing in Swift?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

Given-When-Then (GWT) is a structured way to write unit tests. I find it is particularly useful when I need to write complex unit tests. The structure helps me to write and understand unit test faster than the traditional approach. Using this concept, we break down and write a unit test into three sections.

Given When Then
  • Given: “given” section is the pre-conditions of the tests. Preconditions can be arguments to the function call, value assertion or any test doubles.
  • When: “when” describes the actual behaviour of the test. In this section, an operation to be tested is performed.
  • Then: “then” section depicts the expected outcome due to the specified behaviour. In this section, we have used assertions statements. We can check an assertion by validating some values, nil or non-nil, true or false, two objects match or not etc.

Get ahead of the competition and ace your next iOS interview with expert-curated resources. Check out the collection of SwiftUI and Swift interview questions and answers, behavioral interview tips, and the popular custom layout contents. Plus, go through the new section for Engineering Manager to help you excel in your career!

Join my free Newsletter 🚀

Let’s think about a real world test case scenario.

Test case: Add food to a food cart. As a user, I want to add a food item to my food cart and the total food items are increased.

Given: The food cart is initially empty. Total food item = 0
When: Add three food items in the cart
Then: User should have three food.

The Given-When-Then was proposed by Dan North as part of behaviour-driven development.

func testUppercasedName() {
    // Given
    let firstName = "Jacob"
    let lastName = "Adam"

    // When
    let fullName = sut.fullName(firstName, lastName).uppercased()

    // Then
    XCTAssertEqual(fullName, "JACOB ADAM")

Benefits of having GWT approach in iOS project

Given-When-Then (GWT) testing in Swift (and in general) is a beneficial approach to testing because it provides a structured and organized way to write tests that are easy to understand and maintain. Here are some specific benefits of using GWT testing in Swift:

  1. Clarity: GWT testing forces the developer to write test cases in a clear, concise and structured manner, which makes it easier for other developers to understand what the test is trying to achieve.
  2. Improved collaboration: Because GWT testing is structured, it makes it easier for developers to collaborate and work together on testing efforts. Each test case can be broken down into individual steps, and each developer can take on a specific step to complete.
  3. Better test coverage: GWT testing ensures that all the possible scenarios are tested, which helps improve the overall test coverage of the application.
  4. Faster debugging: GWT testing makes it easier to identify the root cause of the problem if a test case fails, because each step in the test is clearly defined and can be easily traced back to the problematic code.
  5. Easier maintenance: GWT testing makes it easier to maintain the test suite over time, because it is easier to understand and update the tests when the underlying code changes.

Adopting GWT

Adopting Given-When-Then (GWT) testing in a complex Swift project can be challenging, but it is possible with some planning and effort. Here are some steps you can follow to adopt GWT in your complex Swift project:

  1. Define the scope: Start by defining the scope of the GWT testing effort. Identify the key components and features of the application that will be covered by GWT testing.
  2. Identify the scenarios: Once you have defined the scope, identify the scenarios that you want to test. Break each scenario down into individual steps using the GWT format (Given-When-Then).
  3. Write the tests: Write the tests for each scenario. Use the XCTest framework provided by Swift to write the tests. Make sure that each step in the GWT format is clearly defined and that the tests are easy to read and understand.

In conclusion, Given-When-Then (GWT) testing in Swift is a testing approach that structures test cases into three distinct parts: the given (the preconditions or initial state), the when (the action being tested), and the then (the expected outcome). This approach helps developers to write tests that are easy to understand, maintain and update. GWT testing ensures that all the possible scenarios are tested, and helps to improve test coverage, debugging, and collaboration between developers. Adopting GWT testing in a complex Swift project requires planning, effort, and collaboration, but the benefits make it a worthwhile investment for any development team.

✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn

Get Ready to Shine: Mastering the iOS Interview

Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.