Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Make your tests more readable

by fragi
May 23, 2021
in TDD
Share on FacebookShare on Twitter

In the FizzBuzz code challenge, we had to write the following test method:

Swift
1
2
3
4
5
6
7
func test_fizzBuzz_whenInputIsMultipleOf3_returnsFizz() {
    let fizzBuzzCalculator = FizzBuzzCalculator()
    
    let result = fizzBuzzCalculator.fizzBuzz(number: 2 * 3)
    
    XCTAssertEqual("Fizz", result)
}

Although the test is easy to read and understand, we can do this a bit better. We used as a multiple of 3 the number 2 * 3. To increase the test’s clarity we can write the number 2 as anyIntAboveOne.

Swift
1
2
3
4
5
6
7
8
9
10
11
private func anyIntAboveOne() -> Int {
    return 2
}
 
func test_fizzBuzz_whenInputIsMultipleOf3_returnsFizz() {
    let fizzBuzzCalculator = FizzBuzzCalculator()
    
    let result = fizzBuzzCalculator.fizzBuzz(number: anyIntAboveOne() * 3)
    
    XCTAssertEqual("Fizz", result)
}

In this way, we communicate our intention more clearly, as we are not interested in the number 2 * 3 but any multiple of 3. This makes sense when we need to set up more complex objects.

Tags: TDDUnit Testing
fragi

fragi

Related Posts

What is TDD?
TDD

XCTestCase lifecycle

May 25, 2021

XCTestCase has many methods as part of its lifecycle. It has a class setup method that executes only once before...

What is TDD?
TDD

@testable

May 16, 2021

At the beginning of the FizzBuzz code challenge , we deleted the @testable line of code. What does this @testable...

What is TDD?
TDD

What is SUT

May 16, 2021

In many articles about TDD, you will find the term SUT, which refers to the System Under Test. Using SUT...

What is TDD?
TDD

Why unit testing is valuable

May 16, 2021

There are many benefits of writing unit tests. The test we write can serve as a documentation of what the...

What is TDD?
TDD

Cyclomatic complexity

November 11, 2021

According to Wikipedia (https://en.wikipedia.org/wiki/Cyclomatic_complexity): Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is...

What is TDD?
TDD

Easier navigation through test methods

May 13, 2021

In the FizzBuzz code challenge ,we end up having eight test methods; in more complex classes, we may have more....

Next Post
What is TDD?

XCTestCase lifecycle

Accessibility by Example

Accessibility by Example

What is TDD?

Write better Unit tests with XCTUnwrap

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.