返回介绍

Python Developer Tools

发布于 2024-06-23 21:36:11 字数 3035 浏览 0 评论 0 收藏 0

For working with Krita’s Python code, there are a couple of tools for running unit tests and code checks.

Setup

To set up a Python environment for running code checks, unit tests etc, it is recommended to use a Python virtual environment for installing 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.

To create a virtual environment for Python 3 with virtualenwrapper, run:

mkvirtualenv krita -p /usr/bin/python3

This will create a virtual environment called krita and activate it. You will see that your command line now starts with (krita) to indicate the active virtual environment.

Now change into your Krita git repository install the needed dependencies into the environment:

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

You can rerun this command to update the packages should the version numbers in the requirement file get updated.

To get out of the virtual environment, type:

deactivate

And to get back into the virtual environment, type:

workon krita

Code Checks

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

To run codechecks on all Python files, type:

flake8 .

Or limit to a specific directory or file:

flake8 plugins/python/plugin_importer/

Unit Tests

To run all Python unit tests, type:

pytest .

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

pytest plugins/python/plugin_importer/tests/test_plugin_importer.py::PluginImporterTestCase::test_zipfile_doesnt_exist

See Pytest’s Getting Started documentation for more information about pytest in general.

Unit tests for Python plugins should reside in a tests folder inside the plugin’s folder. See the plugin_importer plugin for example unit tests. Note that in order to be able to import Krita’s Python plugins outside of Krita in the unit test setup, there is a mock krita module that will return mock objects for any attribute names so that importing krita and registering plugins etc. become no-ops. Thus, it’s only possible to test code units that are independent of Krita-related functions. Another caveat is that the mock krita module doesn’t work with wildcard imports (from krita import *), but those should be avoided anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文