Going from Swift to Go: Testing

Going from Swift to Go: Testing

Unit testing is crucial for any project; but the two types of testing found in Swift and Go are fundamentally different. In Swift, the testing is assertion-based, while the testing in Go is output-based. However, these two things are very easy to switch between.

Testing in Swift

In a separate target (usually a target with ‘test’ in the name), we create a class that is a subclass of ‘XCTestCase’, and import XCTest at the top. To write the actual test, we declare ‘func’ followed by a function name that begins with ‘test’. In the function, we can run the desired code, and then add an XCTAssert function. There are several variations on this function, such as asserting true or false; the bottom line is, you can use it to inform XCode what conditions are needed to pass. Example:

import XCTests
class sampleTests: XCTestCase {


func testAddition() {
var x = 3
x += 4
XCTAssertTrue(x == 7)
}


}

Testing in Go

In Go, you don’t have to deal with targets, just create a new file that has the word ‘test’ in its name; for example, ‘sample_test.go’. At the top, include ‘package main’ and import ‘testing’. In this file, write a function that starts with ‘Test’ and takes (t *testing.T) as a parameter. Then run the desired code in this function. To determine if the test passes, first run a statement that checks the conditions needed to pass, then run a t.Fatal() function. The t.Fatal tells the program that the test has failed; the programmer must create conditions that determine if this line if programming is run. Example:

package main
import (
"testing"
)

func TestAddition(t *testing.T) {
x := 3
x += 4
if x != 7 {
t.Fatal("Incorrect Sum").
}
}

The Difference

So what is the fundamental difference between these two testing procedures? In Swift, we write SucceedsOrFails(condition), while in Go, we write Condition(succeedsOrFails). Note that the above testing functions (XCTAssertTrue and t.Fatal) are just a few of the many testing functions.

Running Tests

In Swift, presumably you are using XCode, in which case you can simply press the diamond next to your test function to run the test, or at the top of the class to run every test in the class. You can also go to the test section in the left window menu (the icon is a diamond with a dash in the middle) and run any and all test in the project.

In Go, simply use the command line to reach the desired folder, then run ‘go test’ to run every test file, or run ‘go test addition_test.go’ to run tests in a specific file, in this case called ‘addition_test.go’.

Setup Tests

In both Swift and Go, you can add a function that will run before every test in a class/file; this is useful if you would otherwise run the same block of code at the start of every test. In Swift, this is accomplished with ‘override func setUp() { }’, while in Go, we use ‘func init() { }’. These are just a couple of examples of helper functions that can be used to make the testing process easier.