tox

オンラインドキュメント

https://pypi.org/project/tox/

https://tox.readthedocs.io/en/latest/

インストール

pip install virtualenv
pip install tox
1
2

使い方

テストを実行したいバージョンのPythonをインストールする。ここでは、Python2.7、Python3.6、Python3.7、Python3.8、Python 3.9 でのテスト実行を説明する。

テストファイルは、pytest で示した 成功するテストtest_sample1.py と 失敗するテストtest_sample2.py を使用する。

以下のような内容の tox.ini を作成する。envlist に テストするPythonのバージョンを記述し、deps にテスト実行に必要なライブラリを記述し、commands にテストコマンドを記述する。

[tox]
envlist = py27, py36, py37, py38, py39
 
[testenv]
deps =
    pytest
commands =
    pytest test_sample1.py test_sample2.py
1
2
3
4
5
6
7
8

以下のような内容の setup.py を作成する。

from setuptools import setup
 
setup(
    neme = "tox_sample1",
    version = "1.0",
)
1
2
3
4
5
6

コマンドプロンプトで tox -q を実行する。各バージョンのPython用の仮想環境が作成されテストが実行されて、以下のような内容の結果が表示される。

============================= test session starts =============================
platform win32 -- Python 2.7.18, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
cachedir: .tox\py27\.pytest_cache
rootdir: C:\Users\eagle_eight\tox
collected 2 items
 
test_sample1.py .                                                        [ 50%]
test_sample2.py F                                                        [100%]
 
================================== FAILURES ===================================
____________________________ test_failing_sample1 _____________________________
 
    def test_failing_sample1() :
>       assert(1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff
 
test_sample2.py:2: AssertionError
===================== 1 failed, 1 passed in 0.06 seconds ======================
ERROR: InvocationError for command 'C:\Users\eagle_eight\tox\.tox\py27\Scripts\pytest.EXE' test_sample1.py test_sample2.py (exited with code 1)
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .tox\py36\.pytest_cache
rootdir: C:\Users\eagle_eight\tox
collected 2 items
 
test_sample1.py .                                                        [ 50%]
test_sample2.py F                                                        [100%]
 
================================== FAILURES ===================================
____________________________ test_failing_sample1 _____________________________
 
    def test_failing_sample1() :
>       assert(1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff
 
test_sample2.py:2: AssertionError
=========================== short test summary info ===========================
FAILED test_sample2.py::test_failing_sample1 - assert (1, 2, 3) == (3, 2, 1)
========================= 1 failed, 1 passed in 0.06s =========================
ERROR: InvocationError for command 'C:\Users\eagle_eight\tox\.tox\py36\Scripts\pytest.EXE' test_sample1.py test_sample2.py (exited with code 1)
============================= test session starts =============================
platform win32 -- Python 3.7.9, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .tox\py37\.pytest_cache
rootdir: C:\Users\eagle_eight\tox
collected 2 items
 
test_sample1.py .                                                        [ 50%]
test_sample2.py F                                                        [100%]
 
================================== FAILURES ===================================
____________________________ test_failing_sample1 _____________________________
 
    def test_failing_sample1() :
>       assert(1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff
 
test_sample2.py:2: AssertionError
=========================== short test summary info ===========================
FAILED test_sample2.py::test_failing_sample1 - assert (1, 2, 3) == (3, 2, 1)
========================= 1 failed, 1 passed in 0.05s =========================
ERROR: InvocationError for command 'C:\Users\eagle_eight\tox\.tox\py37\Scripts\pytest.EXE' test_sample1.py test_sample2.py (exited with code 1)
============================= test session starts =============================
platform win32 -- Python 3.8.8, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .tox\py38\.pytest_cache
rootdir: C:\Users\eagle_eight\tox
collected 2 items
 
test_sample1.py .                                                        [ 50%]
test_sample2.py F                                                        [100%]
 
================================== FAILURES ===================================
____________________________ test_failing_sample1 _____________________________
 
    def test_failing_sample1() :
>       assert(1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff
 
test_sample2.py:2: AssertionError
=========================== short test summary info ===========================
FAILED test_sample2.py::test_failing_sample1 - assert (1, 2, 3) == (3, 2, 1)
========================= 1 failed, 1 passed in 0.08s =========================
ERROR: InvocationError for command 'C:\Users\eagle_eight\tox\.tox\py38\Scripts\pytest.EXE' test_sample1.py test_sample2.py (exited with code 1)
============================= test session starts =============================
platform win32 -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .tox\py39\.pytest_cache
rootdir: C:\Users\eagle_eight\tox
collected 2 items
 
test_sample1.py .                                                        [ 50%]
test_sample2.py F                                                        [100%]
 
================================== FAILURES ===================================
____________________________ test_failing_sample1 _____________________________
 
    def test_failing_sample1() :
>       assert(1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff
 
test_sample2.py:2: AssertionError
=========================== short test summary info ===========================
FAILED test_sample2.py::test_failing_sample1 - assert (1, 2, 3) == (3, 2, 1)
========================= 1 failed, 1 passed in 0.09s =========================
ERROR: InvocationError for command 'C:\Users\eagle_eight\tox\.tox\py39\Scripts\pytest.EXE' test_sample1.py test_sample2.py (exited with code 1)
___________________________________ summary ___________________________________
ERROR:   py27: commands failed
ERROR:   py36: commands failed
ERROR:   py37: commands failed
ERROR:   py38: commands failed
ERROR:   py39: commands failed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119