DEV Community

loading...
Cover image for Getting started with Swift Unit Testing, a rant/cry for help

Getting started with Swift Unit Testing, a rant/cry for help

Javier Salcedo
Being able to create almost anything with what essentially is just an arbitrary arrangement of words is the closer I’ll get to real world magic, and that is why I love programming.
・3 min read

Photo by Andre Hunter on Unsplash

During the last week I've tried to get started with Swift, because eventually I want to learn Metal (Apple graphics API).
It's being a really frustrating experience.

At first I really liked it. I come from C++ and Rust, and Swift felt like a sweet middle ground.
A nice (and "rusty") package manager, modern but familiar syntax, safe but not as annoying as Rust...

So, after reading a bit through swift.org and doing some really simple exercises, I decided to implement a simple Ray Tracer.

I start creating a Vec3 class, and of course I decide to create a corresponding test suite.

Apparently, Swift comes with its own test framework XCTest that you can launch with swift test, and running swift package init actually creates a sample one to help you. So nice!

I see that it's contained in a Tests/ProjectNameTests dir, and that is referenced in Package.swift as a testTarget that has the project package as a dependency.
So far, so good. This looks like the way to add new test suites.

The sample test then adds every individual test (as a pair of a String and what looks like a function object) to an array called allTests, that later is passed as a parameter in a XCTestManifest.swift.
Great, what I understand from this is:

  • A Test Suite/Target has:
    • XCTestCase child classes that group individual tests
    • A manifest that runs them
  • I have to list every test in the class in an array
  • I have to put that array on the manifest

So far, everything looks reasonable.

I want to keep things clean and tidy, so I decide to add a new XCTestCase to group all my Vec3 tests. I call it Vec3Tests.swift and I add its allTests array to the manifest.

Just to make sure it works, my first test is a dummy meant to fail:

static var allTests = [
    ("dummy", dummy),
]

func dummy() throws { XCTAssert( false ) }
Enter fullscreen mode Exit fullscreen mode

I save, go to the terminal, type swift test and... nothing.
Only 1 test runs. The sample one.
It builds fine. It doesn't complain. The tests just don't run.

At the time of writing this, I've tried creating a whole new Test Target, and using XCode instead both for creating the test files and to initialise the project (I was using just VSCode).
The only thing that works is to add the test in the sample ProjectNameTests.swift file.

I looked through the official docs, but I've found almost nothing. Maybe I'm not looking where I should, because I find the swift.org docs very convoluted and hard to navigate.
Also, 99% of the blog posts, forum questions and videos are for iOS app development. I'm just trying to make a simple terminal app!

I learned Vim, Vulkan and Rust before, all infamous for their learning curves, but this feels even more confusing and frustrating somehow.
I must be missing something or understanding it completely backwards, but I just don't know what.
This is something that should be trivial after all.

Can anyone point me to any good docs or tutorials?

Discussion (0)