DEV Community

ayelishaamoah
ayelishaamoah

Posted on

4

Test Driving FizzBuzz

What is TDD?

Test Driven Development is a development approach that involves writing your test first and then writing the simplest code that allows you to pass that test, allowing tests to guide your application code. This process is repeated as many times as it is required.

Unit testing

Unit tests should test a single unit of code - generally a method/ function and should only be concerned with the output of that unit. We can use tools such as mocks, stubs and testing frameworks to make unit testing easier.

Generally unit testing consists of four parts:

  • setup - the system under test (usually a class, object, or method) is set up e.g account = Account.new
  • exercise - the system under test is executed account.deposit(1000)
  • verify - check the result against the expectationexpect(account.show_balance).to eq 1000
  • teardown - reset the system under test (usually handled by the language framework)

TDD process

A common approach to TDD is the Red-Green-Refactor lifecycle:

  • Red - write a failing test
  • Green - write the simplest code to pass the test
  • Refactor - clean up the code that was written

TDD in swift

As I'm beginning my journey with swift I am interested in gaining exposure to the XCTest framework. In order to do this I will attempt to TDD a basic FizzBuzz app. This involves starting a project in XCode.

Fizzbuzz is a programming Kata that can be used to learn TDD and is a great way to explore TDD in a new language.

Process

  • Create a file for the tests
  • Write the first test e.g
    func testIsDivisibleByThree() {
        let brain = Brain()
        let result = brain.isDivisibleByThree(number: 3)
        XCTAssertEqual(result, true)
    }
Enter fullscreen mode Exit fullscreen mode

XCode will throw some errors that you will want to resolve step-by-step.

  • Import the app into your test file by placing@testable import FizzBuzzat the top of file underimport XCTest
  • Write the simplest code to pass the tests
class Brain {
    func isDivisibleByThree(number: Int) -> Bool {
        return true
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Run the tests using ⌘ + U

This process is repeated for each piece of functionality with refactors when code starts becoming repetitive.

Here is a great article on Medium Getting Started with TDD in Swift that I followed in order to build FizzBuzz in Swift.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay