Python unit test and code check setup

Authored by rbreu on Jan 16 2019, 9:25 PM.


Group Reviewers

Right now, it's impossible to write unit tests for Python plugins since the import of the krita module throws errors outside of Krita. This patch provides a quick mock for the krita module that can be used in unit tests. Opinions? (
See also the forum discussion here.)

Th mock krita module will return mock objects for any attribute names so that importing krita and registering Plugins etc. become no-ops. For now it works with Python 3 only, but Python 2 import would be possible with a bit more work. Of course, krita-related stuff can't be tested with this setup (or would need a lot of mocking work in the individual tests), but independent code units can be easily tested. Another caveat is that it doesn't work with wildcard imports, but those are bad code practice anyway. Other than that, I haven't found any drawbacks, and for such a simple solution that doesn't need any further maintenance on API changes, it gives us quite a bit of testing possibilities.

This patch also contains a couple of example unit tests (I plan to write more for the plugin importer) to be run with the pytest test runner, and a setup for running code checks.

[The following info will go into the wiki once/if this is commited]


To set up a Python environment for running code checks, unit tests etc, it is recommended to use a Python virtual environment to install the needed Python packages. For this, install virtualenwrapper from your package manager or follow the installation instructions. It is also possible to install the needed Python packages directly into your system.

Create a virtual environment:

mkvirtualenv krita -p /usr/bin/python3

This will create a virtual environment called *krita* and activate it.

Now install the needed dependencies into the environment:

pip install --upgrade -r dev-tools/python/dev-requirements.txt

Rerun the command whenever the version numbers in the requirement file get updated.

To get out of the virtual environment:


To get back into the virtual environment:

workon krita

Code checks

The code checker follows Python's official style guide, PEP8.

To run codechecks on all Python files:

flake8 .

Or limit to a specific directory or file:

flake8 plugins/python/plugin_importer/

Unit tests

To run all Python unit tests:

pytest .

Or limit to a specific directory, file, or test:

pytest plugins/python/plugin_importer/tests/

Diff Detail

R37 Krita
Lint Skipped
Unit Tests Skipped
rbreu created this revision.Jan 16 2019, 9:25 PM
Restricted Application added a reviewer: Krita. · View Herald TranscriptJan 16 2019, 9:25 PM
Restricted Application added a project: Krita. · View Herald Transcript
rbreu requested review of this revision.Jan 16 2019, 9:25 PM
rbreu added a comment.Jan 16 2019, 9:32 PM

Btw, it's also possible to provide an additional version of the mock krita module on github. Since pip can install Python packages from directly from github, that would be an easy way for developers of external plugins to write unit tests.

rempt accepted this revision.Jan 17 2019, 12:17 PM
rempt added a subscriber: rempt.

Cool! We also would need to have a page for the contributors manual explaining how to use this, though. I also think it's time for you to apply for a dev account:

This revision is now accepted and ready to land.Jan 17 2019, 12:17 PM
rbreu added a comment.Jan 18 2019, 9:03 PM

OK, I submitted my developer application.

Will do the manual update. The README also mentions this, is that still used and what's supposed to go where...?

rempt added a comment.Jan 18 2019, 9:27 PM

We're trying to deprecate the wiki in favor of the official manual; the commuunity wiki is more useful for things like sprint planning, not so much for documentation.

rbreu closed this revision.Jan 19 2019, 12:12 PM