Skip to content

Commit ced4ecf

Browse files
authored
Merge pull request #50 from dajose/master
2 parents fb333e0 + 5e9adfe commit ced4ecf

8 files changed

Lines changed: 71 additions & 8 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333

3434
install:
3535
- sudo apt-get update
36-
- sudo apt-get install libboost-test-dev
36+
- sudo apt-get install libboost-test-dev valgrind
3737
- wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz
3838
- tar -zxvf release-1.8.0.tar.gz
3939
- "cd googletest-release-1.8.0/googletest && cmake . && sudo make install; cd -"

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.3.0
2+
3+
- New `cpp_harness` configuration option allows users to add prefix arguments when running the C++ test runner, allowing to use tools like `valgrind`. Thanks to @dajose for contributing!
4+
15
# 1.2.1
26

37
- Remove `from_parent()`-related warnings in pytest 5.4.2+.

README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ Set it to ``False`` if you have C++ executable files that end with the ``*.py``
9898
[pytest]
9999
cpp_ignore_py_files = False
100100
101+
**cpp_harness**
102+
103+
*New in version 1.3*.
104+
105+
This option allows the usage of tools that are used by invoking them on the console
106+
wrapping the test binary, like valgrind and memcheck:
107+
108+
.. code-block:: ini
109+
110+
[pytest]
111+
cpp_harness = valgrind --tool=memcheck
112+
101113
Install
102114
=======
103115

pytest_cpp/boost.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def list_tests(self, executable):
3030
# inside the executable, so the test_id is a dummy placeholder :(
3131
return [os.path.basename(os.path.splitext(executable)[0])]
3232

33-
def run_test(self, executable, test_id, test_args=()):
33+
def run_test(self, executable, test_id, test_args=(), harness=None):
34+
harness = harness or []
35+
3436
def read_file(name):
3537
try:
3638
with io.open(name) as f:
@@ -41,7 +43,7 @@ def read_file(name):
4143
temp_dir = tempfile.mkdtemp()
4244
log_xml = os.path.join(temp_dir, "log.xml")
4345
report_xml = os.path.join(temp_dir, "report.xml")
44-
args = [
46+
args = harness + [
4547
executable,
4648
"--output_format=XML",
4749
"--log_sink=%s" % log_xml,

pytest_cpp/google.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ def strip_comment(x):
5757
result.append(test_suite + strip_comment(line).strip())
5858
return result
5959

60-
def run_test(self, executable, test_id, test_args=()):
60+
def run_test(self, executable, test_id, test_args=(), harness=None):
61+
harness = harness or []
6162
xml_filename = self._get_temp_xml_filename()
62-
args = [
63+
args = harness + [
6364
executable,
6465
"--gtest_filter=" + test_id,
6566
"--gtest_output=xml:%s" % xml_filename,

pytest_cpp/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def pytest_addoption(parser):
7878
default=True,
7979
help='ignore *.py files that otherwise match "cpp_files" patterns',
8080
)
81+
parser.addini(
82+
"cpp_harness",
83+
type="args",
84+
default=(),
85+
help="command that wraps the cpp binary",
86+
)
8187

8288

8389
class CppFile(pytest.File):
@@ -120,7 +126,12 @@ def from_parent(cls, parent, name, facade, arguments):
120126
)
121127

122128
def runtest(self):
123-
failures = self.facade.run_test(str(self.fspath), self.name, self._arguments)
129+
failures = self.facade.run_test(
130+
str(self.fspath),
131+
self.name,
132+
self._arguments,
133+
harness=self.config.getini("cpp_harness"),
134+
)
124135
if failures:
125136
raise CppFailureError(failures)
126137

tests/test_pytest_cpp.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pytest_cpp.boost import BoostTestFacade
66
from pytest_cpp.error import CppTestFailure, CppFailureRepr
77
from pytest_cpp.google import GoogleTestFacade
8+
from distutils.spawn import find_executable
89

910

1011
def assert_outcomes(result, expected_outcomes):
@@ -390,6 +391,21 @@ def test_argument_option_priority(testdir, exes):
390391
assert_outcomes(result, [("ArgsTest.one_argument", "passed")])
391392

392393

394+
@pytest.mark.skipif(
395+
not find_executable("valgrind") or not find_executable("catchsegv"),
396+
reason="Environment does not have required tools",
397+
)
398+
def test_google_cpp_harness_via_option(testdir, exes):
399+
result = testdir.inline_run(
400+
exes.get("gtest"),
401+
"-k",
402+
"FooTest.test_success",
403+
"-o",
404+
"cpp_harness=catchsegv valgrind --tool=memcheck",
405+
)
406+
assert_outcomes(result, [("FooTest.test_success", "passed")])
407+
408+
393409
def test_boost_one_argument(testdir, exes):
394410
testdir.makeini(
395411
"""
@@ -428,6 +444,22 @@ def test_boost_two_arguments_via_option(testdir, exes):
428444
assert_outcomes(result, [("boost_two_arguments", "passed")])
429445

430446

447+
@pytest.mark.skipif(
448+
not find_executable("valgrind") or not find_executable("catchsegv"),
449+
reason="Environment does not have required tools",
450+
)
451+
def test_boost_cpp_harness_via_option(testdir, exes):
452+
result = testdir.inline_run(
453+
exes.get("boost_success"),
454+
"-s",
455+
"-k",
456+
"boost_success",
457+
"-o",
458+
"cpp_harness=catchsegv valgrind --tool=memcheck",
459+
)
460+
assert_outcomes(result, [("boost_success", "passed")])
461+
462+
431463
def test_passing_files_directly_in_command_line(testdir, exes):
432464
f = exes.get("boost_success")
433465
result = testdir.runpytest(f)

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ envlist = py{27,35,36,37,38,38}-pytestlatest,py38-pytest53
44
[testenv]
55
deps=
66
pytestlatest: pytest
7-
pytest53: pytest ~=5.3
7+
pytestlatest: pytest-xdist
8+
pytest53: pytest~=5.3
9+
pytest53: pytest-xdist<2
810
pytest-mock
9-
pytest-xdist
1011
coverage
1112
commands=
1213
coverage run --source=pytest_cpp -m pytest tests

0 commit comments

Comments
 (0)