If we look now at the tests, we can see that all the tests start with the same instruction, which adds a 2.
Let's refactor the tests removing the common instruction from each test. In the XCTestCase class, a function named setup() is called before each test, and it looks like we can move that instruction in that function:
override func setUp() {
super.setUp()
rpnCalculator.new(element: "2")
}
However, now the test names are a bit misleading and it would be better to express the fact that all these tests have one thing in common. The thing they have in common is that the first element is the number 2: this defines the context of the suite. It could be helpful to use a trick to name the class to express that context:
class RpnCalculator_WithTwo: XCTestCase {
var rpnCalculator: RpnCalculator = FloatRpnCalculator()
override func setUp() {
super.setUp()
rpnCalculator.new(element: "2")
}
func test_TwoOnTopMostPlace() {
XCTAssertEqual(rpnCalculator.line0, "2")
}
func test_Enter__TwoOnSecondPlace() {
rpnCalculator.new(element: "e")
XCTAssertEqual(rpnCalculator.line0, "")
XCTAssertEqual(rpnCalculator.line1, "2")
}
func test_EnterFour__FourTwoOnTheStack() {
rpnCalculator.new(element: "e")
rpnCalculator.new(element: "4")
XCTAssertEqual(rpnCalculator.line0, "4")
XCTAssertEqual(rpnCalculator.line1, "2")
}
}
Now the Tree Test View is even more expressive:
It probably doesn't help much in this simple app, however, it's a nice trick to know when you have hundreds of tests in the same area.