The first test

Now that the project is compilable, let's move to the test file, called something similar to RpnCalculatorTests.
As you can see, Xcode already implemented a few tests using a template; however we don't need any of those, so we delete all the template code and start from scratch.
The first test we want to write is to check that when we insert the number 2, we have that number in the line0 property of the model. An important piece of information to know when using XCTest is that all the tests must start with the test prefix; this is the way Xcode understands how to run the tests using inspection it searches for all the functions in a subclass of XCTestCase whose names start with test. Apart from that, we are free to call the test functions what we want.

There are several testing naming conventions, and it is, in general, a matter of taste. We'll use one where we specify the context of the test and the expected result, such as a short story of the test:

However, some words don't give any information, such as WHEN and THEN, so we can omit them and assume they are there. Every time we are reading a test description, we can assume there is a WHEN after test_ and a THEN after the double underscore, for example:

func test_TwoIsAddedToTwo__FourIsTheResult()

The preceding code can be read as testing that WHEN Two Is Added To Two THEN Four Is The Result.
However, this is only one possible convention on writing test names, and there are several others, but describing them is beyond the scope of this chapter.
The only important thing is to select one method and then stick to it so that all the test names are uniformly named and can be read in the same way.

Getting back to our first test, we can write it as follows:

import XCTest
@testable import RpnCalculator

class RpnCalculatorTests: XCTestCase {
var rpnCalculator: RpnCalculator = FloatRpnCalculator()

func test_Two__TwoOnTopMostPlace() {
rpnCalculator.new(element: "2")
XCTAssertEqual(rpnCalculator.line0, "2")
}
}

We introduce here the function that XCTest uses to verify whether a value is something we expect: XCTAssert.

There are many of those, and we'll describe them in another section; however, the most used is XCTAssertEqual(par1, par2), which passes if both the parameters are equal, and fails if not. We'll now run the tests, either via the Product | Test menu, or with the ⌘ + u keyboard shortcut, and we get a failure.
Following the rules of TDD, let's make it pass in the simplest way possible:

class FloatRpnCalculator: RpnCalculator {
// ...
func new(element: String) {
line0 = element
}
}

When we run the test now, it passes. Congratulations on your first green test!

I know that it looks like cheating, since this code cannot be part of the production, but the first trivial test is important to implement the structure and infrastructure that we'll use to build the real code.