Changeset View
Changeset View
Standalone View
Standalone View
dev-tools/python/krita-mock/src/krita/__init__.py
- This file was added.
1 | """This is a mock krita module for Python unit tests. | ||||
---|---|---|---|---|---|
2 | | ||||
3 | This module returns a mock object for any attribute name and thus | ||||
4 | prevents any errors surrounding the krita module in unit tests. This | ||||
5 | makes it possible to write unit tests for Krita-independent code | ||||
6 | units. | ||||
7 | | ||||
8 | Caveats: | ||||
9 | | ||||
10 | Will only work with proper imports: | ||||
11 | | ||||
12 | import krita | ||||
13 | krita.Krita.instance() # no-op on a mock object | ||||
14 | | ||||
15 | Not with wildcard imports: | ||||
16 | | ||||
17 | from krita import * | ||||
18 | Krita.instance() # error | ||||
19 | | ||||
20 | (Wildcard imports should be avoided anyway.) | ||||
21 | | ||||
22 | """ | ||||
23 | | ||||
24 | import builtins | ||||
25 | import sys | ||||
26 | from unittest.mock import MagicMock | ||||
27 | | ||||
28 | | ||||
29 | sys.modules['krita'] = MagicMock() | ||||
30 | | ||||
31 | builtins.i18n = lambda s: s |