Skip to content

Commit 82e7c86

Browse files
committed
Move the tests imto a separate directory.
1 parent ca0bd34 commit 82e7c86

File tree

7 files changed

+76
-57
lines changed

7 files changed

+76
-57
lines changed

pyproject.lock

+14-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ platform = "*"
208208
coverage = ">=3.7.1"
209209
pytest = ">=2.6.0"
210210
[[package]]
211+
name = "pytest-datadir"
212+
version = "1.0.1"
213+
description = "pytest plugin for test data directories and files"
214+
category = "dev"
215+
optional = false
216+
python-versions = "*"
217+
platform = "*"
218+
219+
[package.dependencies]
220+
pytest = ">=2.7.0"
221+
pathlib = "*"
222+
[[package]]
211223
name = "requests"
212224
version = "2.18.4"
213225
description = "Python HTTP for Humans."
@@ -260,7 +272,7 @@ platform = "*"
260272
[metadata]
261273
python-versions = "^3.6"
262274
platform = "*"
263-
content-hash = "7ce2c03447eb45ba59dcdf80124b6e5462a7d84d881a638ce132cfb1662b1f2d"
275+
content-hash = "523ec577d845a8cb3ea985f5d255ebed924269e3b0eeda7fb6cef8fc3ba00b6c"
264276

265277
[metadata.hashes]
266278
astroid = [ "032f6e09161e96f417ea7fad46d3fac7a9019c775f202182c22df0e4f714cb1c", "dea42ae6e0b789b543f728ddae7ddb6740ba33a49fb52c4a4d9cb7bb4aa6ec09",]
@@ -282,6 +294,7 @@ pygments = [ "78f3f434bcc5d6ee09020f92ba487f95ba50f1e3ef83ae96b9d5ffa1bab25c5d",
282294
pylint = [ "aa519865f8890a5905fa34924fed0f3bfc7d84fc9f9142c16dac52ffecd25a39", "c353d8225195b37cc3aef18248b8f3fe94c5a6a95affaf885ae21a24ca31d8eb",]
283295
pytest = [ "c76e93f3145a44812955e8d46cdd302d8a45fbfc7bf22be24fe231f9d8d8853a", "39555d023af3200d004d09e51b4dd9fdd828baa863cded3fd6ba2f29f757ae2d",]
284296
pytest-cov = [ "890fe5565400902b0c78b5357004aab1c814115894f4f21370e2433256a3eeec", "03aa752cf11db41d281ea1d807d954c4eda35cfa1b21d6971966cc041bbf6e2d",]
297+
pytest-datadir = [ "68226fbb6780366950a9409bb705cd4d05b11bff07a4569d4cc589c69c259def", "3e5d1892c68d0dfb6863e87d6a1f683e67be60802fee798a3ccae5dbda1ab378",]
285298
requests = [ "6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b", "9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e",]
286299
rope = [ "a09edfd2034fd50099a67822f9bd851fbd0f4e98d3b87519f6267b60e50d80d1",]
287300
six = [ "832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb", "70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",]

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ pytest-cov = "^2.5"
2020
rope = "^0.10.7"
2121
codecov = "^2.0"
2222
pygments = "^2.2"
23+
pytest-datadir = "^1.0"
+55-56
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,192 @@
11
from subprocess import run
22
from math import factorial, log
33

4+
from pytest import fixture
45

5-
def test_positional_args(capfd):
6+
7+
def test_positional_args(capfd, datadir):
68
x, y = 12, 34
79

8-
run(f'python basicmath.py add {x} {y}', shell=True)
10+
run(f'python {datadir/"basicmath.py"} add {x} {y}', shell=True)
911
assert int(capfd.readouterr().out) == x + y
1012

1113

12-
def test_optional_args(capfd):
14+
def test_optional_args(capfd, datadir):
1315
x, power = 12, 3
1416

15-
run(f'python basicmath.py power {x} --power {power}', shell=True)
17+
run(f'python {datadir/"basicmath.py"} power {x} --power {power}', shell=True)
1618
assert int(capfd.readouterr().out) == x ** power
1719

18-
run(f'python basicmath.py power {x} -p {power}', shell=True)
20+
run(f'python {datadir/"basicmath.py"} power {x} -p {power}', shell=True)
1921
assert int(capfd.readouterr().out) == x ** power
2022

21-
run(f'python basicmath.py power {x}', shell=True)
23+
run(f'python {datadir/"basicmath.py"} power {x}', shell=True)
2224
assert int(capfd.readouterr().out) == x ** 2
2325

2426

25-
def test_no_args(capfd):
27+
def test_no_args(capfd, datadir):
2628
from math import pi
2729

28-
run('python basicmath.py pi', shell=True)
30+
run(f'python {datadir/"basicmath.py"} pi', shell=True)
2931
assert float(capfd.readouterr().out) == pi
3032

31-
run('python basicmath.py avg', shell=True)
33+
run(f'python {datadir/"basicmath.py"} avg', shell=True)
3234
assert float(capfd.readouterr().out) == sum((1, 2, 3))/3
3335

3436

35-
def test_root_command(capfd):
37+
def test_root_command(capfd, datadir):
3638
version = '0.1.0'
3739

38-
run('python basicmath.py', shell=True)
40+
run(f'python {datadir/"basicmath.py"}', shell=True)
3941
assert capfd.readouterr().out.strip() == 'Welcome to math!'
4042

41-
run('python basicmath.py --version', shell=True)
43+
run(f'python {datadir/"basicmath.py"} --version', shell=True)
4244
assert capfd.readouterr().out.strip() == version
4345

44-
run('python basicmath.py -v', shell=True)
46+
run(f'python {datadir/"basicmath.py"} -v', shell=True)
4547
assert capfd.readouterr().out.strip() == version
4648

4749

48-
def test_nargs(capfd):
50+
def test_nargs(capfd, datadir):
4951
numbers = 1, 2, 42, 101
5052

51-
run(f'python basicmath.py summ {" ".join(str(number) for number in numbers)}', shell=True)
53+
run(f'python {datadir/"basicmath.py"} summ {" ".join(str(number) for number in numbers)}', shell=True)
5254
assert int(capfd.readouterr().out) == sum(numbers)
5355

5456
numbers = 1, 2, 42.2, 101.1
5557

5658
run(
57-
f'python basicmath.py avg --numbers {" ".join(str(number) for number in numbers)}',
59+
f'python {datadir/"basicmath.py"} avg --numbers {" ".join(str(number) for number in numbers)}',
5860
shell=True
5961
)
6062
assert float(capfd.readouterr().out) == sum(numbers)/len(numbers)
6163

62-
run(f'python basicmath.py avg -n {" ".join(str(number) for number in numbers)}', shell=True)
64+
run(f'python {datadir/"basicmath.py"} avg -n {" ".join(str(number) for number in numbers)}', shell=True)
6365
assert float(capfd.readouterr().out) == sum(numbers)/len(numbers)
6466

6567

66-
def test_negative_numbers(capfd):
68+
def test_negative_numbers(capfd, datadir):
6769
x, y = 12, -34
6870

69-
run(f'python basicmath.py add {x} {y}', shell=True)
71+
run(f'python {datadir/"basicmath.py"} add {x} {y}', shell=True)
7072
assert int(capfd.readouterr().out) == x + y
7173

7274

73-
def test_type_casting(capfd):
75+
def test_type_casting(capfd, datadir):
7476
error = "argument {arg}: invalid int value: '{value}'"
7577
x, y = 12, 34.0
7678

77-
run(f'python basicmath.py add {x} {y}', shell=True)
79+
run(f'python {datadir/"basicmath.py"} add {x} {y}', shell=True)
7880
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(arg='y', value=y))
7981

8082
x, y = 12.0, 34.0
8183

82-
run(f'python basicmath.py add {x} {y}', shell=True)
84+
run(f'python {datadir/"basicmath.py"} add {x} {y}', shell=True)
8385
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(arg='x', value=x))
8486

8587
x, y = 'foo', 42
8688

87-
run(f'python basicmath.py add {x} {y}', shell=True)
89+
run(f'python {datadir/"basicmath.py"} add {x} {y}', shell=True)
8890
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(arg='x', value=x))
8991

9092

91-
def test_missing_args(capfd):
93+
def test_missing_args(capfd, datadir):
9294
error = 'the following arguments are required: {args}'
9395
x = 12
9496

95-
run(f'python basicmath.py add {x}', shell=True)
97+
run(f'python {datadir/"basicmath.py"} add {x}', shell=True)
9698
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(args='y'))
9799

98-
run(f'python basicmath.py add', shell=True)
100+
run(f'python {datadir/"basicmath.py"} add', shell=True)
99101
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(args='x, y'))
100102

101103

102-
def test_redundant_args(capfd):
104+
def test_redundant_args(capfd, datadir):
103105
error = 'unrecognized arguments: {args}'
104106
x, y, z = 12, 34, 56
105107

106-
run(f'python basicmath.py add {x} {y} {z}', shell=True)
108+
run(f'python {datadir/"basicmath.py"} add {x} {y} {z}', shell=True)
107109
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(args=z))
108110

109111
x, y, z = 12, 34, '-f'
110112

111-
run(f'python basicmath.py add {x} {y} {z}', shell=True)
113+
run(f'python {datadir/"basicmath.py"} add {x} {y} {z}', shell=True)
112114
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(args=z))
113115

114116
x, y, z, q = 12, 34, '--flag', 'value'
115117

116-
run(f'python basicmath.py add {x} {y} {z} {q}', shell=True)
118+
run(f'python {datadir/"basicmath.py"} add {x} {y} {z} {q}', shell=True)
117119
assert capfd.readouterr().err.splitlines()[-1].endswith(error.format(args=f'{z} {q}'))
118120

119121

120-
def test_open(capfd):
121-
filename = 'numbers.txt'
122+
def test_open(capfd, datadir):
123+
filename = datadir / 'numbers.txt'
122124
with open(filename) as file:
123125
numbers = (float(line) for line in file.readlines())
124126

125-
run('python basicmath.py sumfile numbers.txt', shell=True)
127+
run(f'python {datadir/"basicmath.py"} sumfile {filename}', shell=True)
126128
assert float(capfd.readouterr().out) == sum(numbers)
127129

128130

129-
def test_aliases(capfd):
131+
def test_aliases(capfd, datadir):
130132
x, y = 12, 34
131133

132-
run(f'python basicmath.py sum {x} {y}', shell=True)
134+
run(f'python {datadir/"basicmath.py"} sum {x} {y}', shell=True)
133135
assert int(capfd.readouterr().out) == x + y
134136

135-
run(f'python basicmath.py plus {x} {y}', shell=True)
137+
run(f'python {datadir/"basicmath.py"} plus {x} {y}', shell=True)
136138
assert int(capfd.readouterr().out) == x + y
137139

138140

139-
def test_help(capfd):
140-
run('python basicmath.py --help', shell=True)
141-
assert 'Basic math operations.' in capfd.readouterr().out
142-
143-
run('python basicmath.py -h', shell=True)
141+
def test_help(capfd, datadir):
142+
run(f'python {datadir/"basicmath.py"} --help', shell=True)
144143
assert 'Basic math operations.' in capfd.readouterr().out
145144

146-
run('python noroot.py', shell=True)
145+
run(f'python {datadir/"basicmath.py"} -h', shell=True)
147146
assert 'Basic math operations.' in capfd.readouterr().out
148147

149-
run('python basicmath.py add --help', shell=True)
148+
run(f'python {datadir/"basicmath.py"} add --help', shell=True)
150149
help_message = capfd.readouterr().out
151150
assert 'Add two numbers.' in help_message
152151
assert 'First operand' in help_message
153152
assert 'Second operand' in help_message
154153

155-
run('python basicmath.py add -h', shell=True)
154+
run(f'python {datadir/"basicmath.py"} add -h', shell=True)
156155
help_message = capfd.readouterr().out
157156
assert 'Add two numbers.' in help_message
158157
assert 'First operand' in help_message
159158
assert 'Second operand' in help_message
160159

161160

162-
def test_ignore(capfd):
161+
def test_ignore(capfd, datadir):
163162
n = 6
164163

165-
run(f'python basicmath.py calculate-factorial {n}', shell=True)
164+
run(f'python {datadir/"basicmath.py"} calculate-factorial {n}', shell=True)
166165
assert "invalid choice: 'calculate-factorial'" in capfd.readouterr().err.splitlines()[-1]
167166

168167

169-
def test_pseudonym(capfd):
168+
def test_pseudonym(capfd, datadir):
170169
n = 6
171170

172-
run(f'python basicmath.py fac 6', shell=True)
171+
run(f'python {datadir/"basicmath.py"} fac 6', shell=True)
173172
assert int(capfd.readouterr().out) == factorial(n)
174173

175174

176-
def test_arg_map(capfd):
175+
def test_arg_map(capfd, datadir):
177176
x, base = 1000, 10
178177

179-
run(f'python basicmath.py log {x} --to {base}', shell=True)
178+
run(f'python {datadir/"basicmath.py"} log {x} --to {base}', shell=True)
180179
assert float(capfd.readouterr().out) == log(x, base)
181180

182-
run(f'python basicmath.py log {x} -t {base}', shell=True)
181+
run(f'python {datadir/"basicmath.py"} log {x} -t {base}', shell=True)
183182
assert float(capfd.readouterr().out) == log(x, base)
184183

185184

186-
def test_metavars(capfd):
187-
run('python basicmath.py log --help', shell=True)
185+
def test_metavars(capfd, datadir):
186+
run(f'python {datadir/"basicmath.py"} log --help', shell=True)
188187
assert '-t BASE, --to BASE' in capfd.readouterr().out
189188

190-
run('python basicmath.py log -h', shell=True)
189+
run(f'python {datadir/"basicmath.py"} log -h', shell=True)
191190
assert '-t BASE, --to BASE' in capfd.readouterr().out
192191

193192

@@ -205,7 +204,7 @@ def _(self):
205204
assert 'Command name cannot be empty' in str(excinfo.value)
206205

207206

208-
def test_str_arg(capfd):
207+
def test_str_arg(capfd, datadir):
209208
message = 'Hello Cliar'
210-
run(f'python basicmath.py echo "{message}"', shell=True)
209+
run(f'python {datadir/"basicmath.py"} echo "{message}"', shell=True)
211210
assert capfd.readouterr().out.strip() == message
File renamed without changes.
File renamed without changes.

tests/test_noroot.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from subprocess import run
2+
3+
4+
def test_help(capfd, datadir):
5+
run(f'python {datadir/"noroot.py"}', shell=True)
6+
assert 'Basic math operations.' in capfd.readouterr().out
File renamed without changes.

0 commit comments

Comments
 (0)