Writing tests for server-side Swift projects
Writing tests is an important part in server-side Swift development. A server-side test is different from that of a client side test because server-side code involves a lot of querying different APIs by clients, or writing calls to other backend cloud services or microservices. You need to write tests that intercept calls dynamically. To do that, you instantiate and run a server instance, then you simulate the runtime environment using known data and match the output with the expected result.
If the data required for a test is not available, you can make up something using fake data or simulated data. A test is passed only if the results are validated and matched with your expectation. You don't just test if a feature works correctly when presented with valid data. You should also check if a feature fails gracefully and consistently with invalid data. Graceful failure is part of the overall user experience for your server product.
When writing each test, keep in mind that each test is independent from each other. One test cannot rely on the result from another test. This approach is called unit testing. Sometimes it is helpful to construct helper methods to handle repetitive steps common to all unit tests. It is okay to repeat some operations in every unit test so that each test is totally decoupled from each other. You'll see shortly that there are good coding techniques that promote code re-using in writing tests in Swift.
Besides unit tests, there is another kind of test called an integration test. Integration testing is useful when you want to validate how different methods work correctly together. Since more business logic is involved in integration testing, you can expect to have more complicated test cases. Therefore, it is beneficial for you to have good coverage in unit tests. Unit testing helps you identify problems earlier and reduce the need for complicated debugging efforts for the defects discovered later in integration testing.