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:
The code checker follows Python's official style guide, PEP8.
To run codechecks on all Python files:
Or limit to a specific directory or file:
To run all Python unit tests:
Or limit to a specific directory, file, or test: