Testing
Whenever things are built it is important to test them. Testing can be done manually, but this is time consuming and prone to human error. It is better to automate your testing. This will allow you to test your code more frequently and ensure that it is working as expected. For this application unit tests will be used to make sure that all routes function as intended.
With our tests we can track the coverage to see if every line of code is tested and if there are any lines that are not tested. 100% coverage can be challenging to achieve as applications grow in complexity, but it is a good goal to aim for.
Unit tests
In Python the pytest framework is commonly used for unit testing. This framework allows you to write tests as functions and run them from the command line. To run the tests you can use the pytest
command. This will run all tests in the current directory and subdirectories. You can also specify a specific file or directory to run tests from. The Pytest docs contain a good getting started guide.
Setup
As our tests should run seperate from whatever is happening with our application they will need their own data which means that the tests can always be run.
With our data we can create the core config for our tests which will create the temporary database for testing and also share the application to all of the tests to use.
-
@pytest.fixture
can be used in any other test function by including it in a parameter, eg.def test_hello(client):
will use theclient
fixture. This allows us to create predefined configs that can be reused (fixtures can also use each other). -
The
app
fixture creates a temporary database and initialises it with the data fromdata.sql
. This is done by creating a temporary file and passing the path to the database to the application. TheTESTING
config is also set toTrue
to ensure that the application is in testing mode.
We also should test the factory to make sure our app is being created the correct way.
Testing the database
Our database should close once the context it is in is closed through the teardown context that was created earlier.
The create command should also be tested to ensure that it is creating the database correctly. To make sure that the function is being run the function is monkeypatched (modified) to record if it is run.
Testing the blog
All the blog routes involve reading from the database so we can test that the correct data is being returned and if data is being correctly added. As we know the contents of the database the tests can be hardcoded for the expected behaviour.
Running the tests
To make it easier to run the tests some of the settings cna be solved in the pyproject.toml
file.
The pytest
command can now be run which will find and execute all tests. To get a more verbose output the -v
flag can be used pytest -v
.
With our tests we can measure the amount of code that is covered with our tests to see if anything is missed.
Once the tests are run the report can be visualised with coverage report
.
A more in depth report with details on lines missed can be seen from coverage html
which will create a htmlcov
directory with the report.
Accessibility Testing
While it is important that your site functions correctly, it is also important that it is accessible to all users. This includes users with disabilities, users with slow internet connections, and users with older devices. There are a number of tools that can be used to test your site for accessibility.
Lighthouse
An easy way to test your site is to use the Lighthouse tool built into Chrome. This tool can be accessed by opening the developer tools and clicking on the Lighthouse tab. This tool will run a number of tests on your site and give you a score for each category. It will also give you a list of things you can do to improve your score.