Skip to content

Commit 2d403bd

Browse files
authored
Use pytest on CI and prepare switch to full pytest style tests (#83)
* Use pytest on CI * Cache pip files on travis * Remove self.expected_std_err * Introduce call_with_scorep to reduce code duplication Also get rid of class variables
1 parent 2e8c136 commit 2d403bd

3 files changed

Lines changed: 59 additions & 83 deletions

File tree

.github/workflows/unit_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ jobs:
4747
- name: Install Python packages
4848
run: |
4949
pip install --upgrade pip
50-
pip install numpy mpi4py
50+
pip install numpy mpi4py pytest
5151
5252
- name: Build python bindings
5353
run: pip install .
5454
- name: Run tests
5555
working-directory: test
56-
run: ./test_scorep.py
56+
run: pytest

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dist: bionic
22

33
language: python
4+
cache: pip
45
python:
56
- "2.7"
67
- "3.6"
@@ -17,7 +18,7 @@ addons:
1718
- libopenmpi-dev
1819

1920
install:
20-
- pip install mpi4py numpy
21+
- pip install mpi4py numpy pytest
2122

2223
script:
23-
- pip install ./ && cd test && python test_scorep.py
24+
- pip install ./ && cd test && pytest

test/test_scorep.py

Lines changed: 54 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env pytest
22

33
import unittest
44
import subprocess
@@ -35,10 +35,21 @@ def call(arguments, env=os.environ.copy()):
3535
return result
3636

3737

38-
class TestScorepBindingsPython(unittest.TestCase):
39-
maxDiff = None
40-
python = sys.executable
38+
def call_with_scorep(file, scorep_arguments=None, env=None):
39+
"""
40+
Shortcut for running a python file with the scorep module
41+
42+
@return triple (returncode, stdout, stderr) from the call to subprocess
43+
"""
44+
arguments = [sys.executable, "-m", "scorep"]
45+
if scorep_arguments:
46+
arguments.extend(scorep_arguments)
47+
if env is None:
48+
env = os.environ
49+
return call(arguments + [file], env=env)
4150

51+
52+
class TestScorepBindingsPython(unittest.TestCase):
4253
def assertRegex(self, in1, in2):
4354
if sys.version_info > (3, 5):
4455
super().assertRegex(in1, in2)
@@ -53,8 +64,6 @@ def setUp(self):
5364
self.env["SCOREP_TOTAL_MEMORY"] = "3G"
5465
self.env["SCOREP_EXPERIMENT_DIRECTORY"] = "test_bindings_dir"
5566

56-
self.expected_std_err = ""
57-
5867
shutil.rmtree(
5968
self.env["SCOREP_EXPERIMENT_DIRECTORY"],
6069
ignore_errors=True)
@@ -69,16 +78,13 @@ def test_user_regions(self):
6978
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_user_regions"
7079
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
7180

72-
out = call([self.python,
73-
"-m",
74-
"scorep",
75-
"--nopython",
76-
"cases/user_regions.py"],
77-
env=env)
81+
out = call_with_scorep("cases/user_regions.py",
82+
["--nopython"],
83+
env=env)
7884
std_out = out[1]
7985
std_err = out[2]
8086

81-
self.assertEqual(std_err, self.expected_std_err)
87+
self.assertEqual(std_err, "")
8288
self.assertEqual(
8389
std_out,
8490
"hello world\nhello world\nhello world3\nhello world4\n")
@@ -109,16 +115,13 @@ def test_context(self):
109115
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_context"
110116
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
111117

112-
out = call([self.python,
113-
"-m",
114-
"scorep",
115-
"--noinstrumenter",
116-
"cases/context.py"],
117-
env=env)
118+
out = call_with_scorep("cases/context.py",
119+
["--noinstrumenter"],
120+
env=env)
118121
std_out = out[1]
119122
std_err = out[2]
120123

121-
self.assertEqual(std_err, self.expected_std_err)
124+
self.assertEqual(std_err, "")
122125
self.assertEqual(std_out, "hello world\nhello world\nhello world\n")
123126

124127
out = call(["otf2-print", trace_path])
@@ -138,13 +141,13 @@ def test_user_regions_no_scorep(self):
138141
env = self.env
139142
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_user_regions_no_scorep"
140143

141-
out = call([self.python,
144+
out = call([sys.executable,
142145
"cases/user_regions.py"],
143146
env=env)
144147
std_out = out[1]
145148
std_err = out[2]
146149

147-
self.assertEqual(std_err, self.expected_std_err)
150+
self.assertEqual(std_err, "")
148151
self.assertEqual(
149152
std_out,
150153
"hello world\nhello world\nhello world3\nhello world4\n")
@@ -154,15 +157,11 @@ def test_user_rewind(self):
154157
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_user_rewind"
155158
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
156159

157-
out = call([self.python,
158-
"-m",
159-
"scorep",
160-
"cases/user_rewind.py"],
161-
env=env)
160+
out = call_with_scorep("cases/user_rewind.py", env=env)
162161
std_out = out[1]
163162
std_err = out[2]
164163

165-
self.assertEqual(std_err, self.expected_std_err)
164+
self.assertEqual(std_err, "")
166165
self.assertEqual(std_out, "hello world\nhello world\n")
167166

168167
out = call(["otf2-print", trace_path])
@@ -179,16 +178,13 @@ def test_oa_regions(self):
179178
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_oa_regions"
180179
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
181180

182-
out = call([self.python,
183-
"-m",
184-
"scorep",
185-
"--nopython",
186-
"cases/oa_regions.py"],
187-
env=env)
181+
out = call_with_scorep("cases/oa_regions.py",
182+
["--nopython"],
183+
env=env)
188184
std_out = out[1]
189185
std_err = out[2]
190186

191-
self.assertEqual(std_err, self.expected_std_err)
187+
self.assertEqual(std_err, "")
192188
self.assertEqual(std_out, "hello world\n")
193189

194190
out = call(["otf2-print", trace_path])
@@ -206,16 +202,13 @@ def test_instrumentation(self):
206202
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_instrumentation"
207203
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
208204

209-
out = call([self.python,
210-
"-m",
211-
"scorep",
212-
"--nocompiler",
213-
"cases/instrumentation.py"],
214-
env=env)
205+
out = call_with_scorep("cases/instrumentation.py",
206+
["--nocompiler"],
207+
env=env)
215208
std_out = out[1]
216209
std_err = out[2]
217210

218-
self.assertEqual(std_err, self.expected_std_err)
211+
self.assertEqual(std_err, "")
219212
self.assertEqual(std_out, "hello world\nbaz\nbar\n")
220213

221214
out = call(["otf2-print", trace_path])
@@ -233,17 +226,13 @@ def test_user_instrumentation(self):
233226
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_user_instrumentation"
234227
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
235228

236-
out = call([self.python,
237-
"-m",
238-
"scorep",
239-
"--nocompiler",
240-
"--noinstrumenter",
241-
"cases/user_instrumentation.py"],
242-
env=env)
229+
out = call_with_scorep("cases/user_instrumentation.py",
230+
["--nocompiler", "--noinstrumenter"],
231+
env=env)
243232
std_out = out[1]
244233
std_err = out[2]
245234

246-
self.assertEqual(std_err, self.expected_std_err)
235+
self.assertEqual(std_err, "")
247236
self.assertEqual(std_out, "hello world\nbaz\nbar\n")
248237

249238
out = call(["otf2-print", trace_path])
@@ -261,13 +250,9 @@ def test_error_region(self):
261250
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_error_region"
262251
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
263252

264-
out = call([self.python,
265-
"-m",
266-
"scorep",
267-
"--nocompiler",
268-
"--noinstrumenter",
269-
"cases/error_region.py"],
270-
env=env)
253+
out = call_with_scorep("cases/error_region.py",
254+
["--nocompiler", "--noinstrumenter"],
255+
env=env)
271256
std_out = out[1]
272257
std_err = out[2]
273258

@@ -308,7 +293,7 @@ def test_mpi(self):
308293
"-mca",
309294
"btl",
310295
"^openib",
311-
self.python,
296+
sys.executable,
312297
"-m",
313298
"scorep",
314299
"--mpp=mpi",
@@ -328,12 +313,9 @@ def test_mpi(self):
328313
def test_call_main(self):
329314
env = self.env
330315
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_call_main"
331-
out = call([self.python,
332-
"-m",
333-
"scorep",
334-
"--nocompiler",
335-
"cases/call_main.py"],
336-
env=env)
316+
out = call_with_scorep("cases/call_main.py",
317+
["--nocompiler"],
318+
env=env)
337319
std_out = out[1]
338320
std_err = out[2]
339321

@@ -346,16 +328,13 @@ def test_dummy(self):
346328
env = self.env
347329
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_dummy"
348330

349-
out = call([self.python,
350-
"-m",
351-
"scorep",
352-
"--instrumenter-type=dummy",
353-
"cases/instrumentation.py"],
354-
env=env)
331+
out = call_with_scorep("cases/instrumentation.py",
332+
["--instrumenter-type=dummy"],
333+
env=env)
355334
std_out = out[1]
356335
std_err = out[2]
357336

358-
self.assertEqual(std_err, self.expected_std_err)
337+
self.assertEqual(std_err, "")
359338
self.assertEqual(std_out, "hello world\nbaz\nbar\n")
360339
self.assertTrue(
361340
os.path.exists(
@@ -368,18 +347,14 @@ def test_numpy_dot(self):
368347
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_numpy_dot"
369348
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
370349

371-
out = call([self.python,
372-
"-m",
373-
"scorep",
374-
"--nocompiler",
375-
"--noinstrumenter",
376-
"cases/numpy_dot.py"],
377-
env=env)
350+
out = call_with_scorep("cases/numpy_dot.py",
351+
["--nocompiler", "--noinstrumenter"],
352+
env=env)
378353
std_out = out[1]
379354
std_err = out[2]
380355

381356
self.assertEqual(std_out, "[[ 7 10]\n [15 22]]\n")
382-
self.assertEqual(std_err, self.expected_std_err)
357+
self.assertEqual(std_err, "")
383358

384359
out = call(["otf2-print", trace_path])
385360
std_out = out[1]

0 commit comments

Comments
 (0)