[RFC/WIP]: Use pytest to run CoAP tasks #86
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Contribution description
This PR refines the experimental 09-CoAP tasks in #77 by using pytest to automate running the tasks. This PR also is a kind of RFC for use of pytest in RIOT. pytest provides a number of advantages for composing and running tests, and it provides an evolutionary path forward from RIOT's pexpect-based testrunner.
If you're not familiar with pytest, review the documentation or some of the presentations about it. The content of the tests in 09-coap are introduced in coap.md. I am a newbie to pytest myself, but we do use it at my employer.
01_test.py
Runs an aiocoap-based Resource Directory server as a pytest fixture in rd_server(). Also runs the gcoap CORD client as a fixture. These fixtures include a generic class to run pexpect, ExpectHost, found in conftest.py. They also use the capability for a fixture to include teardown code, which neatly packages this functionality with the setup code.
Finally, the test itself in test_register() is a single function call. Notice that the fixtures are included in the test by passing them to the test function.
02_test.py
Runs an soscoap server for its capability to short circuit responses to a confirmable request. The CoAP server is started in retry_server(), and the client is started in gcoap_example() in conftest.py, which allows for reuse in other tests.
The test functions also include an ignores parameter to vary the number of confirmable requests to ignore. The value of the parameter is specified in the 'parametrize' decorator. pytest includes a few mechanisms for parameterization, as described in the fixture documentation.
Finally, notice that the test_timeout function is able to simply modify the pexect timeout for this test. It also uses the pytest.raises() context manager function to mark that send_recv() is expected to timeout.
Other pytest features
pytest includes an extensive collection of plugins. Below you can see the output of the pytest-html plugin for a test run of 09-coap.
Thanks to @jia200x for his testutils PR #79, which inspired some of this work. Also thanks to @smlng and @MrKevinWeiss for RIOT #10241 and RIOT #10095 for Robot Framework.
Issues/PRs references
This PR improves on #77 by automating the tasks.