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.
This PR adds the pytest unit testing framework to mautrix-telegram. It does so by exemplarily adding test cases for the module
mautrix_telegram.commands.handler
.Tests can be run by executing
python3 setup.py test
,python3 -m pytest
orpytest
. For the last two options to work, pytest and the required plugins (see Dependecies) must be installed.Changes
In this PR
setup.py
,setup.cfg
).mautrix_telegram.commands.handler
.CommandHandler
class is changed to unify the appearance of newline characters at the end of HTML messages.Reasoning
Adding unit testing to a project improves its overall reliability as it limits the time that is needed for hunting down and fixing bugs. This is done by
I chose the commands handler module as a good starting point for implementing unit testing in mautrix telegram, because it it was already isolated enough to be able to quickly write tests for it. But at the same time the module was complex enough to use a broad range of testing techniques including fixtures, parametrization, mocking and testing of asynchronous methods.
The pytest framework was chosen over the unittest module of the standard lib as it allows a more powerful but also more elegant way of creating test cases and allows for frictionless handling of a async methods.
Dependencies
New dependencies (for testing only) are:
Testing
The code change was tested on my private Matrix HS and all test cases pass (obviously 😏). I tried to be Python 3.5 compatible, but I only did verify Python 3.6 support as my tooling (pylint, mypy) does not work together with the future-fstrings package.
Note: I chose to add all tests in a separate folder called
tests
and make it and all of its subfolders packages. Pytest in this case strongly recommends to move all source code into its ownsrc
directory. Not doing so prevents the tests from runnning against an installed version. I didn't incorporate the addition of asrc
directory into this PR as moving all source files is a more invasive change.