1
1
from subprocess import run
2
2
from math import factorial , log
3
3
4
+ from pytest import fixture
4
5
5
- def test_positional_args (capfd ):
6
+
7
+ def test_positional_args (capfd , datadir ):
6
8
x , y = 12 , 34
7
9
8
- run (f'python basicmath.py add { x } { y } ' , shell = True )
10
+ run (f'python { datadir / " basicmath.py" } add { x } { y } ' , shell = True )
9
11
assert int (capfd .readouterr ().out ) == x + y
10
12
11
13
12
- def test_optional_args (capfd ):
14
+ def test_optional_args (capfd , datadir ):
13
15
x , power = 12 , 3
14
16
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 )
16
18
assert int (capfd .readouterr ().out ) == x ** power
17
19
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 )
19
21
assert int (capfd .readouterr ().out ) == x ** power
20
22
21
- run (f'python basicmath.py power { x } ' , shell = True )
23
+ run (f'python { datadir / " basicmath.py" } power { x } ' , shell = True )
22
24
assert int (capfd .readouterr ().out ) == x ** 2
23
25
24
26
25
- def test_no_args (capfd ):
27
+ def test_no_args (capfd , datadir ):
26
28
from math import pi
27
29
28
- run ('python basicmath.py pi' , shell = True )
30
+ run (f 'python { datadir / " basicmath.py" } pi' , shell = True )
29
31
assert float (capfd .readouterr ().out ) == pi
30
32
31
- run ('python basicmath.py avg' , shell = True )
33
+ run (f 'python { datadir / " basicmath.py" } avg' , shell = True )
32
34
assert float (capfd .readouterr ().out ) == sum ((1 , 2 , 3 ))/ 3
33
35
34
36
35
- def test_root_command (capfd ):
37
+ def test_root_command (capfd , datadir ):
36
38
version = '0.1.0'
37
39
38
- run ('python basicmath.py' , shell = True )
40
+ run (f 'python { datadir / " basicmath.py" } ' , shell = True )
39
41
assert capfd .readouterr ().out .strip () == 'Welcome to math!'
40
42
41
- run ('python basicmath.py --version' , shell = True )
43
+ run (f 'python { datadir / " basicmath.py" } --version' , shell = True )
42
44
assert capfd .readouterr ().out .strip () == version
43
45
44
- run ('python basicmath.py -v' , shell = True )
46
+ run (f 'python { datadir / " basicmath.py" } -v' , shell = True )
45
47
assert capfd .readouterr ().out .strip () == version
46
48
47
49
48
- def test_nargs (capfd ):
50
+ def test_nargs (capfd , datadir ):
49
51
numbers = 1 , 2 , 42 , 101
50
52
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 )
52
54
assert int (capfd .readouterr ().out ) == sum (numbers )
53
55
54
56
numbers = 1 , 2 , 42.2 , 101.1
55
57
56
58
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 )} ' ,
58
60
shell = True
59
61
)
60
62
assert float (capfd .readouterr ().out ) == sum (numbers )/ len (numbers )
61
63
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 )
63
65
assert float (capfd .readouterr ().out ) == sum (numbers )/ len (numbers )
64
66
65
67
66
- def test_negative_numbers (capfd ):
68
+ def test_negative_numbers (capfd , datadir ):
67
69
x , y = 12 , - 34
68
70
69
- run (f'python basicmath.py add { x } { y } ' , shell = True )
71
+ run (f'python { datadir / " basicmath.py" } add { x } { y } ' , shell = True )
70
72
assert int (capfd .readouterr ().out ) == x + y
71
73
72
74
73
- def test_type_casting (capfd ):
75
+ def test_type_casting (capfd , datadir ):
74
76
error = "argument {arg}: invalid int value: '{value}'"
75
77
x , y = 12 , 34.0
76
78
77
- run (f'python basicmath.py add { x } { y } ' , shell = True )
79
+ run (f'python { datadir / " basicmath.py" } add { x } { y } ' , shell = True )
78
80
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (arg = 'y' , value = y ))
79
81
80
82
x , y = 12.0 , 34.0
81
83
82
- run (f'python basicmath.py add { x } { y } ' , shell = True )
84
+ run (f'python { datadir / " basicmath.py" } add { x } { y } ' , shell = True )
83
85
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (arg = 'x' , value = x ))
84
86
85
87
x , y = 'foo' , 42
86
88
87
- run (f'python basicmath.py add { x } { y } ' , shell = True )
89
+ run (f'python { datadir / " basicmath.py" } add { x } { y } ' , shell = True )
88
90
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (arg = 'x' , value = x ))
89
91
90
92
91
- def test_missing_args (capfd ):
93
+ def test_missing_args (capfd , datadir ):
92
94
error = 'the following arguments are required: {args}'
93
95
x = 12
94
96
95
- run (f'python basicmath.py add { x } ' , shell = True )
97
+ run (f'python { datadir / " basicmath.py" } add { x } ' , shell = True )
96
98
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (args = 'y' ))
97
99
98
- run (f'python basicmath.py add' , shell = True )
100
+ run (f'python { datadir / " basicmath.py" } add' , shell = True )
99
101
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (args = 'x, y' ))
100
102
101
103
102
- def test_redundant_args (capfd ):
104
+ def test_redundant_args (capfd , datadir ):
103
105
error = 'unrecognized arguments: {args}'
104
106
x , y , z = 12 , 34 , 56
105
107
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 )
107
109
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (args = z ))
108
110
109
111
x , y , z = 12 , 34 , '-f'
110
112
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 )
112
114
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (args = z ))
113
115
114
116
x , y , z , q = 12 , 34 , '--flag' , 'value'
115
117
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 )
117
119
assert capfd .readouterr ().err .splitlines ()[- 1 ].endswith (error .format (args = f'{ z } { q } ' ))
118
120
119
121
120
- def test_open (capfd ):
121
- filename = 'numbers.txt'
122
+ def test_open (capfd , datadir ):
123
+ filename = datadir / 'numbers.txt'
122
124
with open (filename ) as file :
123
125
numbers = (float (line ) for line in file .readlines ())
124
126
125
- run ('python basicmath.py sumfile numbers.txt ' , shell = True )
127
+ run (f 'python { datadir / " basicmath.py" } sumfile { filename } ' , shell = True )
126
128
assert float (capfd .readouterr ().out ) == sum (numbers )
127
129
128
130
129
- def test_aliases (capfd ):
131
+ def test_aliases (capfd , datadir ):
130
132
x , y = 12 , 34
131
133
132
- run (f'python basicmath.py sum { x } { y } ' , shell = True )
134
+ run (f'python { datadir / " basicmath.py" } sum { x } { y } ' , shell = True )
133
135
assert int (capfd .readouterr ().out ) == x + y
134
136
135
- run (f'python basicmath.py plus { x } { y } ' , shell = True )
137
+ run (f'python { datadir / " basicmath.py" } plus { x } { y } ' , shell = True )
136
138
assert int (capfd .readouterr ().out ) == x + y
137
139
138
140
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 )
144
143
assert 'Basic math operations.' in capfd .readouterr ().out
145
144
146
- run ('python noroot .py' , shell = True )
145
+ run (f 'python { datadir / "basicmath .py" } -h ' , shell = True )
147
146
assert 'Basic math operations.' in capfd .readouterr ().out
148
147
149
- run ('python basicmath.py add --help' , shell = True )
148
+ run (f 'python { datadir / " basicmath.py" } add --help' , shell = True )
150
149
help_message = capfd .readouterr ().out
151
150
assert 'Add two numbers.' in help_message
152
151
assert 'First operand' in help_message
153
152
assert 'Second operand' in help_message
154
153
155
- run ('python basicmath.py add -h' , shell = True )
154
+ run (f 'python { datadir / " basicmath.py" } add -h' , shell = True )
156
155
help_message = capfd .readouterr ().out
157
156
assert 'Add two numbers.' in help_message
158
157
assert 'First operand' in help_message
159
158
assert 'Second operand' in help_message
160
159
161
160
162
- def test_ignore (capfd ):
161
+ def test_ignore (capfd , datadir ):
163
162
n = 6
164
163
165
- run (f'python basicmath.py calculate-factorial { n } ' , shell = True )
164
+ run (f'python { datadir / " basicmath.py" } calculate-factorial { n } ' , shell = True )
166
165
assert "invalid choice: 'calculate-factorial'" in capfd .readouterr ().err .splitlines ()[- 1 ]
167
166
168
167
169
- def test_pseudonym (capfd ):
168
+ def test_pseudonym (capfd , datadir ):
170
169
n = 6
171
170
172
- run (f'python basicmath.py fac 6' , shell = True )
171
+ run (f'python { datadir / " basicmath.py" } fac 6' , shell = True )
173
172
assert int (capfd .readouterr ().out ) == factorial (n )
174
173
175
174
176
- def test_arg_map (capfd ):
175
+ def test_arg_map (capfd , datadir ):
177
176
x , base = 1000 , 10
178
177
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 )
180
179
assert float (capfd .readouterr ().out ) == log (x , base )
181
180
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 )
183
182
assert float (capfd .readouterr ().out ) == log (x , base )
184
183
185
184
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 )
188
187
assert '-t BASE, --to BASE' in capfd .readouterr ().out
189
188
190
- run ('python basicmath.py log -h' , shell = True )
189
+ run (f 'python { datadir / " basicmath.py" } log -h' , shell = True )
191
190
assert '-t BASE, --to BASE' in capfd .readouterr ().out
192
191
193
192
@@ -205,7 +204,7 @@ def _(self):
205
204
assert 'Command name cannot be empty' in str (excinfo .value )
206
205
207
206
208
- def test_str_arg (capfd ):
207
+ def test_str_arg (capfd , datadir ):
209
208
message = 'Hello Cliar'
210
- run (f'python basicmath.py echo "{ message } "' , shell = True )
209
+ run (f'python { datadir / " basicmath.py" } echo "{ message } "' , shell = True )
211
210
assert capfd .readouterr ().out .strip () == message
0 commit comments