- when implementing a new feature or changing code, make sure your PR also contains tests that cover the changes.
- always think about edge cases, go over each Solidity source code line that was changed in your PR and make sure a test covers it if needed.
- don’t only test the happy case. If you don't have
with pytest.raises():
in your tests, something is wrong
- create a
test_contract_name.py
test file for testing the constructor & all public variables for that contract (including the version) - see TokenNetwork tests - if the contract is big - create multiple
test_contract/[contract]_contract_function_name.py
test files for testing each contract function separately. E.g.test_channel_open.py
(we don't have the contract name here, but we havechannel
, which is specific enough)
- have a version test. E.g. test_version
- test
TypeErrors
- make sure your constructor receives the intended number of arguments, of the intended type. E.g. test_constructor_call for theTokenNetwork
contract. - check that all public contract variables indeed have
public
access. E.g. test_constructor_call_state from theTokenNetworkRegistry
contract
Tests usually follow this order when writing them in a file:
- test
TypeErrors
- make sure your function receives the intended number of arguments, of the intended type. E.g. test_open_channel_call - go through each line of code and see whether a test can be written to cover it. E.g. for this line
channel_counter += 1;
in TokenNetwork.openChannel, this test_counter was added test_function_name_state
- always exists and tests the contract state for when the function is successful (nopytest.raises
, but muchassert
):- pre-call tests for all contract state variables that are related, all related getter functions
- post-call tests for all contract state variables that are related, all related getter functions, comparing to the pre-call ones
- E.g. test_open_channel_state
- some of these checks are written as fixtures and reused by tests. You can find them in fixtures/channel.py E.g. common_settle_state_tests
- tests for each event. E.g. test_open_channel_event
- usually, for testing some contract functions, the contract needs to be in a specific state. There are fixtures that provide the state that we need. E.g. for testing channel deposits, we first need to create_channel. These helper fixtures can be found in fixtures/channel.py