Unit tests
Philosophy
Unit tests focus on verifying the functionality of individual units or components of our software. A unit would be the smallest testable part of a software, such as a function, method or class. Unit tests in Fluid Attacks must be:
- Repeatable: Regardless of where they are executed, the result must be the same.
- Fast: Unit tests should take little time to execute because, being the first level of testing, where you have isolated functions/methods and classes, the answers should be immediate. A unit test should take at most two (2) seconds.
- Independent: The functions or classes to be tested should be isolated, no secondary effect behaviors should be validated, and, if possible, we should avoid calls to external resources such as databases; for this, we use mocks.
- Descriptive: For any developer, it should be evident what is being tested in the unit test, what the result should be, and in case of an error, what is the source of the error.
Architecture
- Location: Unit tests for a given file are located right next to the file to be tested. For example, tests for mailmap/create.py are located at mailmap/create_test.py.
- Utilities: It provides vital utilities for populating DynamoDB, comfortably mocking, and local reproducibility. This with the purpose of allowing developers to focus on actually testing the code.
- Coverage: Current coverage for a given module con be found at
<module-path>/coverage
. For example, malmap/coverage.
Writing tests
Here’s an example test:
Key aspects of the provided tests are:
- As we’re testing
integrates/back/integrates/organizations/utils.py::get_organization
, its corresponding test function isintegrates/back/integrates/organizations/utils_test.py::get_organization_test
. This naming convention should be followed for all tests. - These tests will increase coverage for the
integrates/back/integrates/organizations
module. aws_populate
decorator allows to populate the database with the necessary data for the test to execute. A clean database will be created and populated for each parameter provided via@tag.parametrize
.- Make sure you provide each test with its own data to avoid conflicts.
- A
test_get_organization_mock
test is provided to demonstrate how to mock a function. This is useful when you want to test a function that connects to an external service that you don’t want to connect to in your tests.
Running tests
Running tests for specific modules
You can run tests for specific modules with the following command:
where [moduleN]
can be any Integrates module.
For each provided module, this command will:
- Run all tests for the given module.
- Fail if any of the tests fail.
- Generate a coverage report.
- Fail if the new coverage is below the current one for the given module (Developer must add tests to at least keep the same coverage).
- Fail if the new coverage is above the current one for the given module (Developer must add new coverage to their commit).
- Pass if new and current coverage are the same.
Running tests for all modules
You can run tests for all modules with the following command:
Old unit tests
You can run tests using the following command:
To run the ones that modify the mock database:
Currently, every time our unit tests run, we launch a mock stack that is populated with the necessary data required for our tests to execute. We utilize mocking to prevent race conditions and dependencies within the tests.
When writing unit tests, you can follow these steps to ensure that the test is repeatable, fast, independent, and descriptive:
- Test file:
We store our tests using the same structure
as our repository. Inside
universe/integrates/back/test/unit/src
you can find our unit tests. Look for thetest_module_to_test.py
file or add it if missing. - Write the test: Once the file is ready, you can start writing the test. Consider the purpose of the function, method, or class that you want to test. Think about its behavior when different inputs are provided. Also, identify extreme scenarios to test within the test. These will form our test cases and are important for writing our assertions. We use the parametrize decorator if possible to declare different test cases.
- Mocks:
What do you mock? A general guideline is to look for the
await
statement inside the function, method or class that you want to test. In most cases,await
indicates that the awaited function requires an external resource, such as a database. To learn more about mocks, you can refer to the official documentation. - Mock data:
When using mocks, you need to provide the data required for
your unit test to run. We accomplish this by using
pytest fixtures
, which allow us to have mock data available fromconftest.py
files. - Assertions: Test the expected behavior. We use assertions to validate results, the number of function or mock calls, and the arguments used in mocks.