chapter eleven

11 Testing your Rust application

 

This chapter covers:

  • Assessing the testing needs for our web service.
  • Using Rusts built-in testing capabilities to create unit tests.
  • Setting up a conditional testing environment.
  • Creating a mock-server which we can remotely shutdown again.
  • Using warps built-in testing framework to test our filters.
  • Writing integration tests against our running web service.

This last chapter of the book is for some developers the most important aspect of writing applications: Testing. You can practice test-driven development, where you write tests first. You can choose to write the tests directly after having implemented the business logic, or you wait until a large part of the application logic is written before testing it. There is no one-size-fits-all, and it depends on the size of the application and the circumstances you are writing it in.

The same goes for what types of tests you are writing and if you want to cover 100% of your codebase or not. The most important point is that you have tests, and that they cover the most critical workflow of your application. Another aspect you should have in mind is: Test the most complex code thoroughly. If you make a change on a piece of code you barely understand or that has many side-effects throughout your application, that’s the area you want to have many tests which ideally cover all possible outcomes. 

11.1 Unit testing our business logic

11.1.1 Testing the pagination logic and dealing with custom Errors

11.1.2 Testing the Config module with environment variables

11.1.3 Testing the profanity module with a newly created mock server

11.2 Testing our warp filters

11.3 Creating an integration testing setup

11.3.1 Splitting up the codebase into a lib.rs and a binary

11.3.2 Creating the integration-test crate and the oneshot server implementation

11.3.3 Adding the registration test

11.3.4 Unwinding in case of an error

11.3.5 Testing the login and posting questions

11.4 Summary