Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit e842f3c

Browse files
committed
Add more main unittests
Signed-off-by: Andrea Cervesato <[email protected]>
1 parent b0b8b97 commit e842f3c

File tree

1 file changed

+343
-22
lines changed

1 file changed

+343
-22
lines changed

ltp/tests/test_main.py

Lines changed: 343 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,351 @@
11
"""
22
Unittests for main module.
33
"""
4+
import os
5+
import pwd
6+
import json
7+
import pytest
48
import ltp.main
59

610

7-
def test_sut_plugins(tmpdir):
11+
class TestMain:
812
"""
9-
Test if SUT implementations are correctly loaded.
13+
The the main module entry point.
1014
"""
11-
suts = []
12-
suts.append(tmpdir / "sutA.py")
13-
suts.append(tmpdir / "sutB.py")
14-
suts.append(tmpdir / "sutC.txt")
15-
16-
for index in range(0, len(suts)):
17-
suts[index].write(
18-
"from ltp.sut import SUT\n\n"
19-
f"class SUT{index}(SUT):\n"
20-
" @property\n"
21-
" def name(self) -> str:\n"
22-
f" return 'mysut{index}'\n"
23-
)
24-
25-
ltp.main._discover_sut(str(tmpdir))
26-
27-
assert len(ltp.main.LOADED_SUT) == 2
28-
29-
for index in range(0, len(ltp.main.LOADED_SUT)):
30-
assert ltp.main.LOADED_SUT[index].name == f"mysut{index}"
15+
# number of tests created inside temporary folder
16+
TESTS_NUM = 6
17+
18+
@pytest.fixture(autouse=True)
19+
def prepare_tmpdir(self, tmpdir):
20+
"""
21+
Prepare the temporary directory adding runtest folder.
22+
"""
23+
# create simple testing suites
24+
content = ""
25+
for i in range(self.TESTS_NUM):
26+
content += f"test0{i} echo ciao\n"
27+
28+
tmpdir.mkdir("testcases").mkdir("bin")
29+
runtest = tmpdir.mkdir("runtest")
30+
31+
for i in range(3):
32+
suite = runtest / f"suite{i}"
33+
suite.write(content)
34+
35+
# create a suite that is executing slower than the others
36+
content = ""
37+
for i in range(self.TESTS_NUM, self.TESTS_NUM * 2):
38+
content += f"test0{i} sleep 1\n"
39+
40+
suite = runtest / f"slow_suite"
41+
suite.write(content)
42+
43+
# enable parallelization for 'slow_suite'
44+
tests = {}
45+
for index in range(self.TESTS_NUM, self.TESTS_NUM * 2):
46+
name = f"test0{index}"
47+
tests[name] = {}
48+
49+
metadata_d = {"tests": tests}
50+
metadata = tmpdir.mkdir("metadata") / "ltp.json"
51+
metadata.write(json.dumps(metadata_d))
52+
53+
# create a suite printing environment variables
54+
suite = runtest / f"env_suite"
55+
suite.write("test_env echo -n $VAR0:$VAR1:$VAR2")
56+
57+
def test_sut_plugins(self, tmpdir):
58+
"""
59+
Test if SUT implementations are correctly loaded.
60+
"""
61+
suts = []
62+
suts.append(tmpdir / "sutA.py")
63+
suts.append(tmpdir / "sutB.py")
64+
suts.append(tmpdir / "sutC.txt")
65+
66+
for index in range(0, len(suts)):
67+
suts[index].write(
68+
"from ltp.sut import SUT\n\n"
69+
f"class SUT{index}(SUT):\n"
70+
" @property\n"
71+
" def name(self) -> str:\n"
72+
f" return 'mysut{index}'\n"
73+
)
74+
75+
ltp.main._discover_sut(str(tmpdir))
76+
77+
assert len(ltp.main.LOADED_SUT) == 2
78+
79+
for index in range(0, len(ltp.main.LOADED_SUT)):
80+
assert ltp.main.LOADED_SUT[index].name == f"mysut{index}"
81+
82+
def read_report(self, temp, tests_num) -> dict:
83+
"""
84+
Check if report file contains the given number of tests.
85+
"""
86+
name = pwd.getpwuid(os.getuid()).pw_name
87+
report = str(temp / f"runltp.{name}" / "latest" / "results.json")
88+
assert os.path.isfile(report)
89+
90+
# read report and check if all suite's tests have been executed
91+
report_d = None
92+
with open(report, 'r') as report_f:
93+
report_d = json.loads(report_f.read())
94+
95+
assert len(report_d["results"]) == tests_num
96+
97+
return report_d
98+
99+
def test_wrong_options(self):
100+
"""
101+
Test wrong options.
102+
"""
103+
cmd_args = [
104+
"--run-cmd1234", "ls"
105+
]
106+
107+
with pytest.raises(SystemExit) as excinfo:
108+
ltp.main.run(cmd_args=cmd_args)
109+
110+
assert excinfo.value.code == 2
111+
112+
def test_run_command(self, tmpdir):
113+
"""
114+
Test --run-cmd option.
115+
"""
116+
temp = tmpdir.mkdir("temp")
117+
cmd_args = [
118+
"--ltp-dir", str(tmpdir),
119+
"--tmp-dir", str(temp),
120+
"--run-cmd", "ls"
121+
]
122+
123+
with pytest.raises(SystemExit) as excinfo:
124+
ltp.main.run(cmd_args=cmd_args)
125+
126+
assert excinfo.value.code == ltp.main.RC_OK
127+
128+
def test_run_command_timeout(self, tmpdir):
129+
"""
130+
Test --run-cmd option with timeout.
131+
"""
132+
temp = tmpdir.mkdir("temp")
133+
cmd_args = [
134+
"--ltp-dir", str(tmpdir),
135+
"--tmp-dir", str(temp),
136+
"--run-cmd", "sleep 1",
137+
"--exec-timeout", "0"
138+
]
139+
140+
with pytest.raises(SystemExit) as excinfo:
141+
ltp.main.run(cmd_args=cmd_args)
142+
143+
assert excinfo.value.code == ltp.main.RC_ERROR
144+
145+
def test_run_suite(self, tmpdir):
146+
"""
147+
Test --run-suite option.
148+
"""
149+
temp = tmpdir.mkdir("temp")
150+
cmd_args = [
151+
"--ltp-dir", str(tmpdir),
152+
"--tmp-dir", str(temp),
153+
"--run-suite", "suite0", "suite1", "suite2"
154+
]
155+
156+
with pytest.raises(SystemExit) as excinfo:
157+
ltp.main.run(cmd_args=cmd_args)
158+
159+
assert excinfo.value.code == ltp.main.RC_OK
160+
161+
self.read_report(temp, self.TESTS_NUM * 3)
162+
163+
def test_run_suite_timeout(self, tmpdir):
164+
"""
165+
Test --run-suite option with timeout.
166+
"""
167+
temp = tmpdir.mkdir("temp")
168+
cmd_args = [
169+
"--ltp-dir", str(tmpdir),
170+
"--tmp-dir", str(temp),
171+
"--run-suite", "slow_suite",
172+
"--suite-timeout", "0"
173+
]
174+
175+
with pytest.raises(SystemExit) as excinfo:
176+
ltp.main.run(cmd_args=cmd_args)
177+
178+
assert excinfo.value.code == ltp.main.RC_TIMEOUT
179+
180+
report_d = self.read_report(temp, self.TESTS_NUM)
181+
for param in report_d["results"]:
182+
assert param["test"]["passed"] == 0
183+
assert param["test"]["failed"] == 0
184+
assert param["test"]["broken"] == 0
185+
assert param["test"]["warnings"] == 0
186+
assert param["test"]["skipped"] == 1
187+
188+
def test_run_suite_verbose(self, tmpdir, capsys):
189+
"""
190+
Test --run-suite option with --verbose.
191+
"""
192+
temp = tmpdir.mkdir("temp")
193+
cmd_args = [
194+
"--ltp-dir", str(tmpdir),
195+
"--tmp-dir", str(temp),
196+
"--run-suite", "suite0",
197+
"--verbose",
198+
]
199+
200+
with pytest.raises(SystemExit) as excinfo:
201+
ltp.main.run(cmd_args=cmd_args)
202+
203+
assert excinfo.value.code == ltp.main.RC_OK
204+
205+
captured = capsys.readouterr()
206+
assert "ciao\n" in captured.out
207+
208+
@pytest.mark.xfail(reason="This test passes if run alone. capsys bug?")
209+
def test_run_suite_no_colors(self, tmpdir, capsys):
210+
"""
211+
Test --run-suite option with --no-colors.
212+
"""
213+
temp = tmpdir.mkdir("temp")
214+
cmd_args = [
215+
"--ltp-dir", str(tmpdir),
216+
"--tmp-dir", str(temp),
217+
"--run-suite", "suite0",
218+
"--no-colors",
219+
]
220+
221+
with pytest.raises(SystemExit) as excinfo:
222+
ltp.main.run(cmd_args=cmd_args)
223+
224+
assert excinfo.value.code == ltp.main.RC_OK
225+
226+
out, _ = capsys.readouterr()
227+
assert "test00: pass" in out
228+
229+
def test_json_report(self, tmpdir):
230+
"""
231+
Test --json-report option.
232+
"""
233+
temp = tmpdir.mkdir("temp")
234+
report = str(tmpdir / "report.json")
235+
cmd_args = [
236+
"--ltp-dir", str(tmpdir),
237+
"--tmp-dir", str(temp),
238+
"--run-suite", "suite1",
239+
"--json-report", report
240+
]
241+
242+
with pytest.raises(SystemExit) as excinfo:
243+
ltp.main.run(cmd_args=cmd_args)
244+
245+
assert excinfo.value.code == ltp.main.RC_OK
246+
assert os.path.isfile(report)
247+
248+
report_a = self.read_report(temp, self.TESTS_NUM)
249+
report_b = None
250+
with open(report, 'r') as report_f:
251+
report_b = json.loads(report_f.read())
252+
253+
assert report_a == report_b
254+
255+
def test_skip_tests(self, tmpdir):
256+
"""
257+
Test --skip-tests option.
258+
"""
259+
temp = tmpdir.mkdir("temp")
260+
cmd_args = [
261+
"--ltp-dir", str(tmpdir),
262+
"--tmp-dir", str(temp),
263+
"--run-suite", "suite0", "suite2",
264+
"--skip-tests", "test0[01]"
265+
]
266+
267+
with pytest.raises(SystemExit) as excinfo:
268+
ltp.main.run(cmd_args=cmd_args)
269+
270+
assert excinfo.value.code == ltp.main.RC_OK
271+
272+
self.read_report(temp, (self.TESTS_NUM - 2) * 2)
273+
274+
def test_skip_file(self, tmpdir):
275+
"""
276+
Test --skip-file option.
277+
"""
278+
skipfile = tmpdir / "skipfile"
279+
skipfile.write("test01\ntest02")
280+
281+
temp = tmpdir.mkdir("temp")
282+
cmd_args = [
283+
"--ltp-dir", str(tmpdir),
284+
"--tmp-dir", str(temp),
285+
"--run-suite", "suite0", "suite2",
286+
"--skip-file", str(skipfile)
287+
]
288+
289+
with pytest.raises(SystemExit) as excinfo:
290+
ltp.main.run(cmd_args=cmd_args)
291+
292+
assert excinfo.value.code == ltp.main.RC_OK
293+
294+
self.read_report(temp, (self.TESTS_NUM - 2) * 2)
295+
296+
def test_skip_tests_and_file(self, tmpdir):
297+
"""
298+
Test --skip-file option with --skip-tests.
299+
"""
300+
skipfile = tmpdir / "skipfile"
301+
skipfile.write("test02\ntest03")
302+
303+
temp = tmpdir.mkdir("temp")
304+
cmd_args = [
305+
"--ltp-dir", str(tmpdir),
306+
"--tmp-dir", str(temp),
307+
"--run-suite", "suite0", "suite2",
308+
"--skip-tests", "test0[01]",
309+
"--skip-file", str(skipfile)
310+
]
311+
312+
with pytest.raises(SystemExit) as excinfo:
313+
ltp.main.run(cmd_args=cmd_args)
314+
315+
assert excinfo.value.code == ltp.main.RC_OK
316+
317+
self.read_report(temp, (self.TESTS_NUM - 4) * 2)
318+
319+
def test_sut_help(self):
320+
"""
321+
Test "--sut help" command and check if SUT class(es) are loaded.
322+
"""
323+
cmd_args = [
324+
"--sut", "help"
325+
]
326+
327+
with pytest.raises(SystemExit) as excinfo:
328+
ltp.main.run(cmd_args=cmd_args)
329+
330+
assert excinfo.value.code == ltp.main.RC_OK
331+
assert len(ltp.main.LOADED_SUT) > 0
332+
333+
def test_env(self, tmpdir):
334+
"""
335+
Test --env option.
336+
"""
337+
temp = tmpdir.mkdir("temp")
338+
cmd_args = [
339+
"--ltp-dir", str(tmpdir),
340+
"--tmp-dir", str(temp),
341+
"--run-suite", "env_suite",
342+
"--env", "VAR0=0:VAR1=1:VAR2=2"
343+
]
344+
345+
with pytest.raises(SystemExit) as excinfo:
346+
ltp.main.run(cmd_args=cmd_args)
347+
348+
assert excinfo.value.code == ltp.main.RC_OK
349+
350+
report_d = self.read_report(temp, 1)
351+
assert report_d["results"][0]["test"]["log"] == "0:1:2"

0 commit comments

Comments
 (0)