Unit Testing Vapor (Toolbox v.1.0.1) Projects

As iOS Developer and former backend developer I’ve always dreamt to have the opportunity to write the backend by my self without the need of “Context switching” between Objective-c/Swift to “AnyOtherBackendLanguage” and, when some days ago I stumbled upon Vapor, it has been an instant crush! Writing backend code in Swift, in Xcode, deploying it wherever through docker containers, database connectivity and ORM, crazy performances, Vapor is simply Incredible!! Probably is the first time that I fall so much in love for a technology to write a tutorial on that.

Actually everything works more or less “out-of-the-box”, the guys are doing an amazing job on continuously improving the documentation and the toolbox, but to deploy a Vapor project in production is mandatory to include unit tests on the project and I found that this is the less documented and at the same time the most tricky part (so far 😁) on setting up a Vapor project. I’ll try to cover this small gap in the best way possible sharing what I’ve learnt in some hours of struggle. Let’s start from the scratch:

vapor new UnitTestTutorial

After a while a brand new project will be created containing some boilerplate code. It’s important to note that Vapor will create the project structure, but not the Xcode project.

I don’t get exactly why, but actually (toolbox v1.0.1) running the command to create the Xcode project doesn’t include the test target and to do that you have to follow this procedure:

  1. Open Terminal
  2. cd into the project directory e.g cd ~/Documents/UnitTestTutorial
  3. create a folder Tests containing a folder ProjectNameTests so in our example mkdir -p Tests/UnitTestTutorialTests
  4. create a swift file into it (even empty is fine) touch ./Tests/UnitTestTutorialTests/Test.swift

Now let’s edit the file Package.swift on the project root and let’s remove the line Tests from the exclude array (in my screenshot line 14)

Remove the Tests line

Now let’s run the vapor command for the Xcode project creation

vapor xcode

Now lets create a new class in the project just to verify that the unit test will work later. So let’s create a new class on the project under the App Group (only the default declaration is fine we’ll test only that after the init it won’t be nil), and let’s call it HelloWorld.swift.

IT’S EXTREMELY IMPORTANT THAT EVERY CLASS THAT WE WANT TO TEST IS INCLUDED BOTH IN THE APP AND IN THE TEST TARGETS OTHERWISE WILL NOT BE FOUND THIS DETAIL DROVE ME CRAZY FOR HOURS SO KEEP IT IN MIND

I’ve deselected Core that was selected by default and was actually not interesting.

It can be done after the creation of the file as well by selecting the file on the file tree and selecting the targets on the right column

Now let’s create a new UnitTest Test case class into Tests/UnitTestTutorialTests Group. The one that we created at the very beginning can be now deleted (in my case I created a file called test.swift).

and let’s call it HelloWorldTests.swift.

Now let’s try to test the brand new HelloWorld class initialization including the following code into testExample function :

func testExample() {
XCTAssert(HelloWorld() != nil)
}

If everything has been done properly, the test should succeed (⌘ + u is the shortcut to run the tests, if you see some errors don’t worry they will disappear the project needs to be ricompiled, eventually clean it. There will be a warning about the result of the expression it’s just an example you will unleash your fantasy once everything will be up and running) and you’ve all the elements to start testing on your amazing Vapor Project.

I hope that this guide will help you on doing the right choice, I’m pretty sure that server side Swift and Vapor will be the next big things in Backend development, for every question please join the Slack channel and refer to the Documentation.

Happy Testing!!!