End-to-end tests

An end-to-end test will check that the whole system works as expected from the end-user point of view. The test needs to behave like a real client, and call the system through the same User Interface (UI).

Depending on the type of application you are writing, a simple HTTP client might not be enough to simulate a real user. For instance, if the visible part of the system through which users are interacting is a web application with HTML pages that gets rendered on a client-side, you will need to use a tool like Selenium (http://docs.seleniumhq.org/). It will automate your browser in order to make sure that the client requests every CSS and JavaScript files and then renders correctly every page.

JavaScript frameworks are now doing a lot of work on the client side to produce pages. Some of them have completely removed server-side rendering of templates, and just grab data from the server to generate the HTML page by manipulating the Document Object Model (DOM) through the browser APIs. Calls to the server, in that case, consist of getting all the static JavaScript files needed for rendering a given URL, plus the data.

Writing end-to-end tests is outside the scope of this book, but you can refer to Selenium Testing Tools Cookbook from the same editor to learn how to write some.

The following points summarize what we've learned in this section:

  • Functional tests are the most important tests to write, and it's easy to do it in Flask by instantiating the app in the tests and interacting with it
  • Unit tests are a good complement, but don't abuse mocks
  • Integration tests are like functional tests, but against a real deployment
  • Load tests are useful to learn about your microservice bottlenecks and plan for the next steps
  • End-to-end tests require using the same UI that the client would normally use

Knowing when you will need to write integration, load, or end-to-end tests depends on how your project is managed--but both unit and functional tests should be written every time you change something. Ideally, each change you make in your code should include a new test or modify an existing test.

Unit tests can be written using vanilla Python, thanks to the excellent unittest package included in the standard library--and we will see later how the pytest (http://docs.pytest.org) library adds awesomeness on the top of it.

For functional tests, we'll look in the next section at WebTest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset