Skip to content

Commit 41ef4f0

Browse files
author
Vasileios Karakasis
authored
Merge pull request #1188 from teojgo/code_quality/convert2pytest
[refactor] Convert unittests to pytest syntax
2 parents bc3b571 + 8c3df25 commit 41ef4f0

24 files changed

+1601
-1560
lines changed

unittests/test_argparser.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# SPDX-License-Identifier: BSD-3-Clause
55

6+
import pytest
67
import unittest
78

89
from reframe.frontend.argparse import ArgumentParser
@@ -27,36 +28,37 @@ def setUp(self):
2728
self.foo_options.add_argument('--barfoo', action='store_true')
2829

2930
def test_arguments(self):
30-
self.assertRaises(ValueError, self.foo_options.add_argument,
31-
action='store', default='FOO')
31+
with pytest.raises(ValueError):
32+
self.foo_options.add_argument(action='store', default='FOO')
33+
3234
self.foo_options.add_argument('--foo-bar', action='store_true')
3335
self.foo_options.add_argument('--alist', action='append', default=[])
3436
options = self.parser.parse_args(['--foobar', '--foo-bar'])
35-
self.assertTrue(options.foobar)
36-
self.assertTrue(options.foo_bar)
37+
assert options.foobar
38+
assert options.foo_bar
3739

3840
def test_parsing(self):
3941
options = self.parser.parse_args(
4042
'--foo name --foolist gag --barfoo --unfoo'.split()
4143
)
42-
self.assertEqual('name', options.foo)
43-
self.assertEqual(['gag'], options.foolist)
44-
self.assertTrue(options.barfoo)
45-
self.assertFalse(options.unfoo)
44+
assert 'name' == options.foo
45+
assert ['gag'] == options.foolist
46+
assert options.barfoo
47+
assert not options.unfoo
4648

4749
# Check the defaults now
48-
self.assertFalse(options.foobar)
49-
self.assertEqual('BAR', options.bar)
50-
self.assertEqual([], options.barlist)
50+
assert not options.foobar
51+
assert 'BAR' == options.bar
52+
assert [] == options.barlist
5153

5254
# Reparse based on the already loaded options
5355
options = self.parser.parse_args(
5456
'--bar beer --foolist any'.split(), options
5557
)
56-
self.assertEqual('name', options.foo)
57-
self.assertEqual(['any'], options.foolist)
58-
self.assertFalse(options.foobar)
59-
self.assertFalse(options.unfoo)
60-
self.assertEqual('beer', options.bar)
61-
self.assertEqual([], options.barlist)
62-
self.assertTrue(options.barfoo)
58+
assert 'name' == options.foo
59+
assert ['any'] == options.foolist
60+
assert not options.foobar
61+
assert not options.unfoo
62+
assert 'beer' == options.bar
63+
assert [] == options.barlist
64+
assert options.barfoo

unittests/test_buildsystems.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ def test_emit_from_env(self):
5555
'CFLAGS="-Wall -std=c99" CXXFLAGS="-Wall -std=c++11" '
5656
'FCFLAGS="-Wall" LDFLAGS="-dynamic" FOO=1'
5757
]
58-
self.assertEqual(expected,
59-
self.build_system.emit_build_commands(self.environ))
58+
assert expected == self.build_system.emit_build_commands(self.environ)
6059

6160
def test_emit_from_buildsystem(self):
6261
super().setup_base_buildsystem()
@@ -70,13 +69,12 @@ def test_emit_from_buildsystem(self):
7069
'CXXFLAGS="-Wall -std=c++11 -O3" FCFLAGS="-Wall -O3" '
7170
'LDFLAGS="-static" FOO=1'
7271
]
73-
self.assertEqual(expected,
74-
self.build_system.emit_build_commands(self.environ))
72+
assert expected == self.build_system.emit_build_commands(self.environ)
7573

7674
def test_emit_no_env_defaults(self):
7775
self.build_system.flags_from_environ = False
78-
self.assertEqual(['make -j 1'],
79-
self.build_system.emit_build_commands(self.environ))
76+
assert (['make -j 1'] ==
77+
self.build_system.emit_build_commands(self.environ))
8078

8179

8280
class TestCMake(_BuildSystemTest, unittest.TestCase):
@@ -103,8 +101,7 @@ def test_emit_from_env(self):
103101
'make -j 32 install'
104102

105103
]
106-
self.assertEqual(expected,
107-
self.build_system.emit_build_commands(self.environ))
104+
assert expected == self.build_system.emit_build_commands(self.environ)
108105

109106
def test_emit_from_buildsystem(self):
110107
super().setup_base_buildsystem()
@@ -124,13 +121,12 @@ def test_emit_from_buildsystem(self):
124121

125122
]
126123
print(self.build_system.emit_build_commands(self.environ))
127-
self.assertEqual(expected,
128-
self.build_system.emit_build_commands(self.environ))
124+
assert expected == self.build_system.emit_build_commands(self.environ)
129125

130126
def test_emit_no_env_defaults(self):
131127
self.build_system.flags_from_environ = False
132-
self.assertEqual(['cmake .', 'make -j 1'],
133-
self.build_system.emit_build_commands(self.environ))
128+
assert (['cmake .', 'make -j 1'] ==
129+
self.build_system.emit_build_commands(self.environ))
134130

135131

136132
class TestAutotools(_BuildSystemTest, unittest.TestCase):
@@ -154,8 +150,7 @@ def test_emit_from_env(self):
154150
'make -j 32 check'
155151

156152
]
157-
self.assertEqual(expected,
158-
self.build_system.emit_build_commands(self.environ))
153+
assert expected == self.build_system.emit_build_commands(self.environ)
159154

160155
def test_emit_from_buildsystem(self):
161156
super().setup_base_buildsystem()
@@ -173,13 +168,12 @@ def test_emit_from_buildsystem(self):
173168

174169
]
175170
print(self.build_system.emit_build_commands(self.environ))
176-
self.assertEqual(expected,
177-
self.build_system.emit_build_commands(self.environ))
171+
assert expected == self.build_system.emit_build_commands(self.environ)
178172

179173
def test_emit_no_env_defaults(self):
180174
self.build_system.flags_from_environ = False
181-
self.assertEqual(['./configure', 'make -j 1'],
182-
self.build_system.emit_build_commands(self.environ))
175+
assert (['./configure', 'make -j 1'] ==
176+
self.build_system.emit_build_commands(self.environ))
183177

184178

185179
class TestSingleSource(_BuildSystemTest, unittest.TestCase):
@@ -224,8 +218,8 @@ def test_emit_from_env(self):
224218
'-o foo.e %s' % (comp, flags,
225219
self.build_system.srcfile, ldflags)
226220
]
227-
self.assertEqual(expected,
228-
self.build_system.emit_build_commands(self.environ))
221+
assert (expected ==
222+
self.build_system.emit_build_commands(self.environ))
229223

230224
def test_emit_no_env(self):
231225
super().setup_base_buildsystem()
@@ -256,5 +250,5 @@ def test_emit_no_env(self):
256250
'-o foo.e %s' % (comp, flags,
257251
self.build_system.srcfile, ldflags)
258252
]
259-
self.assertEqual(expected,
260-
self.build_system.emit_build_commands(self.environ))
253+
assert (expected ==
254+
self.build_system.emit_build_commands(self.environ))

unittests/test_check_filters.py

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# SPDX-License-Identifier: BSD-3-Clause
55

6+
import pytest
67
import unittest
78

89
import reframe.core.runtime as rt
@@ -47,67 +48,56 @@ def count_checks(self, filter_fn):
4748
return sn.count(filter(filter_fn, self.checks))
4849

4950
def test_have_name(self):
50-
self.assertEqual(1, self.count_checks(filters.have_name('check1')))
51-
self.assertEqual(3, self.count_checks(filters.have_name('check')))
52-
self.assertEqual(2, self.count_checks(filters.have_name(r'\S*1|\S*3')))
53-
self.assertEqual(0, self.count_checks(filters.have_name('Check')))
54-
self.assertEqual(3, self.count_checks(filters.have_name('(?i)Check')))
55-
self.assertEqual(
56-
2, self.count_checks(filters.have_name('(?i)check1|CHECK2'))
57-
)
51+
assert 1 == self.count_checks(filters.have_name('check1'))
52+
assert 3 == self.count_checks(filters.have_name('check'))
53+
assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3'))
54+
assert 0 == self.count_checks(filters.have_name('Check'))
55+
assert 3 == self.count_checks(filters.have_name('(?i)Check'))
56+
assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2'))
5857

5958
def test_have_not_name(self):
60-
self.assertEqual(2, self.count_checks(filters.have_not_name('check1')))
61-
self.assertEqual(
62-
1, self.count_checks(filters.have_not_name('check1|check3'))
63-
)
64-
self.assertEqual(
65-
0, self.count_checks(filters.have_not_name('check1|check2|check3'))
66-
)
67-
self.assertEqual(3, self.count_checks(filters.have_not_name('Check1')))
68-
self.assertEqual(
69-
2, self.count_checks(filters.have_not_name('(?i)Check1'))
70-
)
59+
assert 2 == self.count_checks(filters.have_not_name('check1'))
60+
assert 1 == self.count_checks(filters.have_not_name('check1|check3'))
61+
assert 0 == self.count_checks(filters.have_not_name(
62+
'check1|check2|check3'))
63+
assert 3 == self.count_checks(filters.have_not_name('Check1'))
64+
assert 2 == self.count_checks(filters.have_not_name('(?i)Check1'))
7165

7266
def test_have_tags(self):
73-
self.assertEqual(2, self.count_checks(filters.have_tag('a|c')))
74-
self.assertEqual(0, self.count_checks(filters.have_tag('p|q')))
75-
self.assertEqual(2, self.count_checks(filters.have_tag('z')))
67+
assert 2 == self.count_checks(filters.have_tag('a|c'))
68+
assert 0 == self.count_checks(filters.have_tag('p|q'))
69+
assert 2 == self.count_checks(filters.have_tag('z'))
7670

7771
def test_have_prgenv(self):
78-
self.assertEqual(
79-
1, self.count_checks(filters.have_prgenv('env1|env2'))
80-
)
81-
self.assertEqual(2, self.count_checks(filters.have_prgenv('env3')))
82-
self.assertEqual(1, self.count_checks(filters.have_prgenv('env4')))
83-
self.assertEqual(
84-
3, self.count_checks(filters.have_prgenv('env1|env3'))
85-
)
72+
assert 1 == self.count_checks(filters.have_prgenv('env1|env2'))
73+
assert 2 == self.count_checks(filters.have_prgenv('env3'))
74+
assert 1 == self.count_checks(filters.have_prgenv('env4'))
75+
assert 3 == self.count_checks(filters.have_prgenv('env1|env3'))
8676

8777
@rt.switch_runtime(fixtures.TEST_SITE_CONFIG, 'testsys')
8878
def test_partition(self):
8979
p = rt.runtime().system.partition('gpu')
90-
self.assertEqual(2, self.count_checks(filters.have_partition([p])))
80+
assert 2 == self.count_checks(filters.have_partition([p]))
9181
p = rt.runtime().system.partition('login')
92-
self.assertEqual(0, self.count_checks(filters.have_partition([p])))
82+
assert 0 == self.count_checks(filters.have_partition([p]))
9383

9484
def test_have_gpu_only(self):
95-
self.assertEqual(2, self.count_checks(filters.have_gpu_only()))
85+
assert 2 == self.count_checks(filters.have_gpu_only())
9686

9787
def test_have_cpu_only(self):
98-
self.assertEqual(1, self.count_checks(filters.have_cpu_only()))
88+
assert 1 == self.count_checks(filters.have_cpu_only())
9989

10090
def test_invalid_regex(self):
10191
# We need to explicitly call `evaluate` to make sure the exception
10292
# is triggered in all cases
103-
with self.assertRaises(ReframeError):
93+
with pytest.raises(ReframeError):
10494
self.count_checks(filters.have_name('*foo')).evaluate()
10595

106-
with self.assertRaises(ReframeError):
96+
with pytest.raises(ReframeError):
10797
self.count_checks(filters.have_not_name('*foo')).evaluate()
10898

109-
with self.assertRaises(ReframeError):
99+
with pytest.raises(ReframeError):
110100
self.count_checks(filters.have_tag('*foo')).evaluate()
111101

112-
with self.assertRaises(ReframeError):
102+
with pytest.raises(ReframeError):
113103
self.count_checks(filters.have_prgenv('*foo')).evaluate()

0 commit comments

Comments
 (0)