Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pytest unit testing framework #290

Merged
merged 2 commits into from Mar 16, 2019

Conversation

Projects
None yet
2 participants
@V02460
Copy link
Contributor

commented Mar 8, 2019

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 or pytest. For the last two options to work, pytest and the required plugins (see Dependecies) must be installed.

Changes

In this PR

  • unit testing via pytest is enabled (setup.py, setup.cfg).
  • test cases are added for mautrix_telegram.commands.handler.
  • doc comments are added to the tested module, classes and methods.
  • the 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

  • avoiding regressions
  • giving direct feedback when new bugs are introduced
  • enabling automated testing of new commits

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:

  • pytest
  • pytest-asyncio
  • pytest-mock
  • pytest-runner

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 own src directory. Not doing so prevents the tests from runnning against an installed version. I didn't incorporate the addition of a src directory into this PR as moving all source files is a more invasive change.

@tulir

tulir approved these changes Mar 16, 2019

@tulir tulir merged commit 7c82580 into tulir:master Mar 16, 2019

1 check failed

codeclimate 3 issues to fix
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can鈥檛 perform that action at this time.