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

Unit tests in playground

by fragi
May 24, 2023
in Uncategorized
Share on FacebookShare on Twitter

In this short article we will run unit tests on a Playground.

Lets create a Playground and add the code we want to test:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public struct FizzBuzzCalculator {
    public init() { }
 
    public func fizzBuzz(number: Int) -> String {
        if isDivisibleBy(numerator: number, denominator: 3) && isDivisibleBy(numerator: number, denominator: 5) {
            return "FizzBuzz"
        } else if isDivisibleBy(numerator: number, denominator: 5) {
            return "Buzz"
        } else if isDivisibleBy(numerator: number, denominator: 3) {
            return "Fizz"
        }
        return "\(number)"
    }
 
    private func isDivisibleBy(numerator: Int, denominator: Int) -> Bool {
        numerator % denominator == 0
    }
}

Now lets import the XCTest and the test (just for number 1 for now)

Swift
1
2
3
4
5
6
7
class FizzBuzzCalculatorTests: XCTestCase {
    func test_fizzBuzz_whenInputIs1_returns1() {
        let sut = FizzBuzzCalculator()
        let result = sut.fizzBuzz(number: 1)
        XCTAssertEqual(result, "1")
    }
}

Finally in order to run the tests all we have to do is to add the following line:

Swift
1
XCTAssertEqual(result, "1")

fragi

fragi

Related Posts

Uncategorized

C Union equivalent in Swift

May 24, 2023

In C we can declare unions as follows: So the same storage is used for the int and uint....

Uncategorized

Equal arrays

May 24, 2023

In this short article we explore when two array are equal in Swift. Let's start we the obvious cases: ...

Next Post

Equal arrays

C Union equivalent in Swift

Leave a Reply Cancel reply

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

  • 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.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.