-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_pytest_cpp.py
More file actions
666 lines (531 loc) · 20.6 KB
/
test_pytest_cpp.py
File metadata and controls
666 lines (531 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import subprocess
import sys
import tempfile
from shutil import which
import pytest
from pytest_cpp import error
from pytest_cpp.boost import BoostTestFacade
from pytest_cpp.catch2 import Catch2Facade
from pytest_cpp.error import CppFailureRepr
from pytest_cpp.error import CppTestFailure
from pytest_cpp.google import GoogleTestFacade
from pytest_cpp.helpers import make_cmdline
def assert_outcomes(result, expected_outcomes):
__tracebackhide__ = True
obtained = []
for test_id, _ in expected_outcomes:
rep = result.matchreport(test_id, "pytest_runtest_logreport")
obtained.append((test_id, rep.outcome))
assert expected_outcomes == obtained
@pytest.fixture
def dummy_failure():
class DummyTestFailure(CppTestFailure):
def __init__(self):
self.lines = []
self.file_reference = "unknown", 0
def get_lines(self):
return self.lines
def get_file_reference(self):
return self.file_reference
return DummyTestFailure()
@pytest.mark.parametrize(
"facade, name, expected",
[
(
GoogleTestFacade(),
"gtest",
[
"FooTest.test_success",
"FooTest.test_failure",
"FooTest.test_error",
"FooTest.DISABLED_test_disabled",
"FooTest.test_skipped",
"FooTest.test_skipped_no_msg",
],
),
(BoostTestFacade(), "boost_success", ["boost_success"]),
(BoostTestFacade(), "boost_error", ["boost_error"]),
(BoostTestFacade(), "boost_fixture_setup_error", ["boost_fixture_setup_error"]),
(
Catch2Facade(),
"catch2_success",
["Factorials are computed", "Passed Sections"],
),
(
Catch2Facade(),
"catch2_success_v3",
["Factorials are computed", "Passed Sections"],
),
],
)
def test_list_tests(facade, name, expected, exes):
obtained = facade.list_tests(exes.get(name))
assert obtained == expected
@pytest.mark.parametrize(
"facade, name, other_name",
[
(GoogleTestFacade(), "gtest", "boost_success"),
(BoostTestFacade(), "boost_success", "gtest"),
(Catch2Facade(), "catch2_success", "gtest"),
],
)
def test_is_test_suite(facade, name, other_name, exes, tmp_path):
assert facade.is_test_suite(exes.get(name))
assert not facade.is_test_suite(exes.get(other_name))
tmp_path.joinpath("foo.txt").touch()
assert not facade.is_test_suite(str(tmp_path.joinpath("foo.txt")))
@pytest.mark.parametrize(
"facade, name, test_id",
[
(GoogleTestFacade(), "gtest", "FooTest.test_success"),
(BoostTestFacade(), "boost_success", "<unused>"),
(Catch2Facade(), "catch2_success", "Factorials are computed"),
],
)
def test_success(facade, name, test_id, exes):
assert facade.run_test(exes.get(name), test_id)[0] is None
def test_cmdline_builder_happy_flow():
arg_string = make_cmdline(["wine"], "gtest", ["--help"])
assert arg_string == ["wine", "gtest", "--help"]
def test_cmdline_builder_with_empty_harness():
arg_string = make_cmdline(
list(), "boost_test", ["--output_format=XML", "--log_sink=dummy.log"]
)
assert arg_string == ["boost_test", "--output_format=XML", "--log_sink=dummy.log"]
def test_cmdline_builder_with_empty_args():
arg_string = make_cmdline(["wine"], "gtest")
assert arg_string == ["wine", "gtest"]
def test_google_failure(exes):
facade = GoogleTestFacade()
failures, _ = facade.run_test(exes.get("gtest"), "FooTest.test_failure")
assert len(failures) == 2
colors = ("red", "bold")
assert failures[0].get_lines() == [
("Expected equality of these values:", colors),
(" 2 * 3", colors),
(" Which is: 6", colors),
(" 5", colors),
]
assert failures[0].get_file_reference() == ("gtest.cpp", 19)
assert failures[1].get_lines() == [
("Expected equality of these values:", colors),
(" 2 * 6", colors),
(" Which is: 12", colors),
(" 15", colors),
]
assert failures[1].get_file_reference() == ("gtest.cpp", 20)
def test_google_error(exes):
facade = GoogleTestFacade()
failures, _ = facade.run_test(exes.get("gtest"), "FooTest.test_error")
assert len(failures) == 1
colors = ("red", "bold")
assert failures[0].get_lines() == [
("unknown file", colors),
(
'C++ exception with description "unexpected exception"'
" thrown in the test body.",
colors,
),
]
def test_google_disabled(exes):
facade = GoogleTestFacade()
try:
facade.run_test(exes.get("gtest"), "FooTest.DISABLED_test_disabled")
except pytest.skip.Exception as disabled_message:
assert "Disabled" in str(disabled_message)
def test_google_skipped(exes):
facade = GoogleTestFacade()
try:
facade.run_test(exes.get("gtest"), "FooTest.test_skipped")
assert False, "No skipped exception found"
except pytest.skip.Exception as skipped_message:
assert "This is a skipped message" in str(skipped_message)
def test_google_skipped_no_msg(exes):
facade = GoogleTestFacade()
with pytest.raises(pytest.skip.Exception):
facade.run_test(exes.get("gtest"), "FooTest.test_skipped_no_msg")
assert False, "No skipped exception found"
def test_boost_failure(exes):
facade = BoostTestFacade()
failures, _ = facade.run_test(exes.get("boost_failure"), "<unused>")
assert len(failures) == 2
fail1, fail2 = failures
colors = ("red", "bold")
assert fail1.get_lines() == [("check 2 * 3 == 5 has failed", colors)]
assert fail1.get_file_reference() == ("boost_failure.cpp", 9)
assert fail2.get_lines() == [("check 2 - 1 == 0 has failed", colors)]
assert fail2.get_file_reference() == ("boost_failure.cpp", 15)
def test_boost_fatal_error(exes):
facade = BoostTestFacade()
failures, _ = facade.run_test(exes.get("boost_fatal_error"), "<unused>")
assert len(failures) == 1
(fail1,) = failures
colors = ("red", "bold")
assert fail1.get_lines() == [("critical check 2 * 3 == 5 has failed", colors)]
assert fail1.get_file_reference() == ("boost_fatal_error.cpp", 8)
def test_boost_error(exes):
facade = BoostTestFacade()
failures, _ = facade.run_test(exes.get("boost_error"), "<unused>")
assert len(failures) == 2
fail1, fail2 = failures
colors = ("red", "bold")
assert fail1.get_lines() == [("std::runtime_error: unexpected exception", colors)]
assert fail1.get_file_reference() == ("unknown location", 0)
assert fail2.get_lines() == [
("std::runtime_error: another unexpected exception", colors)
]
assert fail2.get_file_reference() == ("unknown location", 0)
def test_boost_fixture_setup_error(exes):
facade = BoostTestFacade()
failures, _ = facade.run_test(exes.get("boost_fixture_setup_error"), "<unused>")
assert len(failures) == 1
fail1 = failures[0]
colors = ("red", "bold")
((line, obtained_colors),) = fail1.get_lines()
assert line == "std::runtime_error: This is a global fixture init failure"
assert obtained_colors == colors
assert fail1.get_file_reference() == ("unknown location", 0)
def test_google_run(testdir, exes):
result = testdir.inline_run("-v", exes.get("gtest", "test_gtest"))
assert_outcomes(
result,
[
("FooTest.test_success", "passed"),
("FooTest.test_failure", "failed"),
("FooTest.test_error", "failed"),
("FooTest.DISABLED_test_disabled", "skipped"),
("FooTest.test_skipped", "skipped"),
("FooTest.test_skipped_no_msg", "skipped"),
],
)
def test_unknown_error(testdir, exes, mocker):
mocker.patch.object(
GoogleTestFacade, "run_test", side_effect=RuntimeError("unknown error")
)
result = testdir.inline_run("-v", exes.get("gtest", "test_gtest"))
rep = result.matchreport("FooTest.test_success", "pytest_runtest_logreport")
assert "unknown error" in str(rep.longrepr)
def test_google_internal_errors(mocker, testdir, exes, tmp_path):
mocker.patch.object(GoogleTestFacade, "is_test_suite", return_value=True)
mocker.patch.object(
GoogleTestFacade, "list_tests", return_value=["FooTest.test_success"]
)
mocked = mocker.patch.object(
subprocess, "check_output", autospec=True, return_value=""
)
def raise_error(*args, **kwargs):
raise subprocess.CalledProcessError(returncode=100, cmd="")
mocked.side_effect = raise_error
result = testdir.inline_run("-v", exes.get("gtest", "test_gtest"))
rep = result.matchreport(exes.exe_name("test_gtest"), "pytest_runtest_logreport")
assert "Internal Error: calling" in str(rep.longrepr)
mocked.side_effect = None
xml_file = tmp_path.joinpath("cpp-report.xml")
xml_file.write_text("<empty/>")
temp_mock = mocker.patch.object(tempfile, "TemporaryDirectory")
temp_mock().__enter__.return_value = str(tmp_path)
result = testdir.inline_run("-v", exes.get("gtest", "test_gtest"))
rep = result.matchreport(exes.exe_name("test_gtest"), "pytest_runtest_logreport")
assert "Internal Error: could not find test" in str(rep.longrepr)
def test_boost_run(testdir, exes):
all_names = [
"boost_success",
"boost_error",
"boost_fixture_setup_error",
"boost_failure",
]
all_files = [exes.get(n, "test_" + n) for n in all_names]
result = testdir.inline_run("-v", *all_files)
assert_outcomes(
result,
[
("test_boost_success", "passed"),
("test_boost_error", "failed"),
("test_boost_fixture_setup_error", "failed"),
("test_boost_failure", "failed"),
],
)
def mock_popen(mocker, return_code, stdout, stderr):
mocked_popen = mocker.MagicMock()
mocked_popen.__enter__ = mocked_popen
mocked_popen.communicate.return_value = stdout, stderr
mocked_popen.return_code = return_code
mocked_popen.poll.return_value = return_code
mocker.patch.object(subprocess, "Popen", return_value=mocked_popen)
return mocked_popen
def test_boost_internal_error(testdir, exes, mocker):
exe = exes.get("boost_success", "test_boost_success")
mock_popen(mocker, return_code=100, stderr=None, stdout=None)
mocker.patch.object(BoostTestFacade, "is_test_suite", return_value=True)
mocker.patch.object(GoogleTestFacade, "is_test_suite", return_value=False)
result = testdir.inline_run(exe)
rep = result.matchreport(
exes.exe_name("test_boost_success"), "pytest_runtest_logreport"
)
assert "Internal Error:" in str(rep.longrepr)
def test_cpp_failure_repr(dummy_failure):
dummy_failure.lines = [("error message", {"red"})]
dummy_failure.file_reference = "test_suite", 20
failure_repr = CppFailureRepr([dummy_failure])
assert str(failure_repr) == "error message\ntest_suite:20: C++ failure"
def test_cpp_files_option(testdir, exes):
exes.get("boost_success")
exes.get("gtest")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines("* no tests *")
testdir.makeini("""
[pytest]
cpp_files = gtest* boost*
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines(
[
"*CppFile boost_success*",
"*CppFile gtest*",
]
)
# skip to avoid dealing with exes.get appending extension
@pytest.mark.skipif(
sys.platform.startswith("win"), reason="This is not a problem on Windows"
)
def test_cpp_ignore_py_files(testdir, exes):
file_name = "cpptest_success.py"
exes.get("gtest", "cpptest_success.py")
testdir.makeini("""
[pytest]
cpp_files = cpptest_*
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines("* no tests *")
result = testdir.runpytest("--collect-only", "-o", "cpp_ignore_py_files=False")
result.stdout.fnmatch_lines("*CppFile cpptest_success.py*")
# running directly skips out machinery as well.
result = testdir.runpytest("--collect-only", file_name)
result.stdout.fnmatch_lines("*1 error in*")
result = testdir.runpytest(
"--collect-only", "-o", "cpp_ignore_py_files=False", file_name
)
result.stdout.fnmatch_lines("*CppFile cpptest_success.py*")
def test_google_one_argument(testdir, exes):
testdir.makeini("""
[pytest]
cpp_arguments = argument1
""")
result = testdir.inline_run(exes.get("gtest_args"), "-k", "ArgsTest.one_argument")
assert_outcomes(result, [("ArgsTest.one_argument", "passed")])
def test_google_two_arguments(testdir, exes):
testdir.makeini("""
[pytest]
cpp_arguments = argument1 argument2
""")
result = testdir.inline_run(exes.get("gtest_args"), "-k", "ArgsTest.two_arguments")
assert_outcomes(result, [("ArgsTest.two_arguments", "passed")])
def test_google_one_argument_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("gtest_args"),
"-k",
"ArgsTest.one_argument",
"-o",
"cpp_arguments=argument1",
)
assert_outcomes(result, [("ArgsTest.one_argument", "passed")])
def test_google_two_arguments_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("gtest_args"),
"-k",
"ArgsTest.two_arguments",
"-o",
"cpp_arguments=argument1 argument2",
)
assert_outcomes(result, [("ArgsTest.two_arguments", "passed")])
def test_argument_option_priority(testdir, exes):
testdir.makeini("""
[pytest]
cpp_arguments = argument2
""")
result = testdir.inline_run(
exes.get("gtest_args"),
"-k",
"ArgsTest.one_argument",
"-o",
"cpp_arguments=argument1",
)
assert_outcomes(result, [("ArgsTest.one_argument", "passed")])
@pytest.mark.skipif(
not which("valgrind") or not which("catchsegv"),
reason="Environment does not have required tools",
)
def test_google_cpp_harness_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("gtest"),
"-k",
"FooTest.test_success",
"-o",
"cpp_harness=catchsegv valgrind --tool=memcheck",
)
assert_outcomes(result, [("FooTest.test_success", "passed")])
def test_boost_one_argument(testdir, exes):
testdir.makeini("""
[pytest]
cpp_arguments = argument1
""")
result = testdir.inline_run(exes.get("boost_one_argument"))
assert_outcomes(result, [("boost_one_argument", "passed")])
def test_boost_two_arguments(testdir, exes):
testdir.makeini("""
[pytest]
cpp_arguments = argument1 argument2
""")
result = testdir.inline_run(exes.get("boost_two_arguments"))
assert_outcomes(result, [("boost_two_arguments", "passed")])
def test_boost_one_argument_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("boost_one_argument"), "-o", "cpp_arguments=argument1"
)
assert_outcomes(result, [("boost_one_argument", "passed")])
def test_boost_two_arguments_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("boost_two_arguments"), "-o", "cpp_arguments=argument1 argument2"
)
assert_outcomes(result, [("boost_two_arguments", "passed")])
@pytest.mark.skipif(
not which("valgrind") or not which("catchsegv"),
reason="Environment does not have required tools",
)
def test_boost_cpp_harness_via_option(testdir, exes):
result = testdir.inline_run(
exes.get("boost_success"),
"-s",
"-k",
"boost_success",
"-o",
"cpp_harness=catchsegv valgrind --tool=memcheck",
)
assert_outcomes(result, [("boost_success", "passed")])
def test_passing_files_directly_in_command_line(testdir, exes):
f = exes.get("boost_success")
result = testdir.runpytest(f)
result.stdout.fnmatch_lines(["*1 passed*"])
def test_race_condition_on_collect(tmp_path):
"""
Check that collection correctly handles when a path no longer is valid.
This might happen in some situations when xdist is collecting multiple files, and that
causes temporary .pyc files to be generated; in those situations, pytest may obtain that
filename and ask plugins if they can collect it, but by the time a plugin is called
the file may be gone already:
OSError: [Errno 2] No such file or directory:
'/../test_duplicate_filenames.cpython-27-PYTEST.pyc.21746'
"""
import pytest_cpp.plugin
assert (
pytest_cpp.plugin.pytest_collect_file(None, tmp_path / "invalid-file") is None
)
def test_exe_mask_on_windows(tmp_path, monkeypatch):
"""
Test for #45: C++ tests not collected due to '*_test' mask on Windows
"""
import pytest_cpp.plugin
monkeypatch.setattr(sys, "platform", "win32")
fn = tmp_path.joinpath("generator_demo_test.exe")
fn.touch()
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
fn = tmp_path.joinpath("test_generator_demo.exe")
fn.touch()
assert pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
fn = tmp_path.joinpath("my_generator_test_demo.exe")
fn.touch()
assert not pytest_cpp.plugin.matches_any_mask(fn, ["test_*", "*_test"])
def test_output_section(testdir, exes):
exes.get("boost_failure")
exes.get("gtest")
testdir.makeini("""
[pytest]
cpp_files = gtest* boost*
""")
result = testdir.runpytest("-k", "failure")
result.stdout.fnmatch_lines(
[
"*Captured c++ call*",
"Just saying hi from boost",
"Just saying hi from gtest",
]
)
def test_cpp_verbose(testdir, exes):
exes.get("boost_success")
exes.get("gtest")
testdir.makeini("""
[pytest]
cpp_files = gtest* boost*
""")
result = testdir.runpytest("-k", "success", "-s", "-o", "cpp_verbose=true")
result.stdout.fnmatch_lines(
["*Just saying hi from boost", "*Just saying hi from gtest"]
)
def assert_catch2_failure(line, expected_line, expected_colors):
assert expected_line in line[0]
assert line[1] == expected_colors
def test_catch2_failure(exes):
for suffix in ["", "_v3"]:
facade = Catch2Facade()
failures, _ = facade.run_test(
exes.get(f"catch2_failure{suffix}"), "Factorials are computed"
)
assert len(failures) == 1
fail1 = failures[0]
colors = ("red", "bold")
assert_catch2_failure(fail1.get_lines()[0], "Expected: ", colors)
assert_catch2_failure(fail1.get_lines()[1], "Factorial(1) == 0", colors)
assert_catch2_failure(fail1.get_lines()[3], "Actual: ", colors)
assert_catch2_failure(fail1.get_lines()[4], "1 == 0", colors)
assert fail1.get_file_reference() == (f"catch2_failure.cpp", 9)
failures, _ = facade.run_test(
exes.get(f"catch2_failure{suffix}"), "Failed Sections"
)
assert len(failures) == 2
# test exceptions
failures, _ = facade.run_test(exes.get(f"catch2_error{suffix}"), "Error")
[fail1] = failures
assert_catch2_failure(fail1.get_lines()[0], "Error: ", colors)
assert_catch2_failure(fail1.get_lines()[1], "a runtime error", colors)
@pytest.mark.parametrize("suffix", ["", "_v3"])
@pytest.mark.parametrize(
"test_id",
[
"Brackets in [test] name",
"**Star in test name**",
"~Tilde in test name",
"Comma, in, test, name",
r"Backslash\ in\ test\ name",
'"Quotes" in test name',
],
)
def test_catch2_special_chars(suffix, test_id, exes):
facade = Catch2Facade()
exe = exes.get("catch2_special_chars" + suffix)
assert facade.run_test(exe, test_id)[0] is None
class TestError:
def test_get_whitespace(self):
assert error.get_left_whitespace(" foo") == " "
assert error.get_left_whitespace("\t\t foo") == "\t\t "
def test_get_code_context_around_line(self, tmp_path):
f = tmp_path.joinpath("foo.py")
f.write_text("line1\nline2\nline3\nline4\nline5")
assert error.get_code_context_around_line(str(f), 1) == ["line1"]
assert error.get_code_context_around_line(str(f), 2) == ["line1", "line2"]
assert error.get_code_context_around_line(str(f), 3) == [
"line1",
"line2",
"line3",
]
assert error.get_code_context_around_line(str(f), 4) == [
"line2",
"line3",
"line4",
]
assert error.get_code_context_around_line(str(f), 5) == [
"line3",
"line4",
"line5",
]
invalid = str(tmp_path.joinpath("invalid"))
assert error.get_code_context_around_line(invalid, 10) == []