diff --git a/CHANGELOG.md b/CHANGELOG.md index 75075d0..a2c5680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 1.2.0 - `pytest-cpp` no longer supports Python 3.4. +- New `cpp_ignore_py_files` option that makes the plugin ignore `*.py` files even if they + would otherwise match the `cpp_files` option (defaults to `True`). # 1.1.0 diff --git a/README.rst b/README.rst index 5d1537c..e1bf4ba 100644 --- a/README.rst +++ b/README.rst @@ -45,27 +45,34 @@ Once installed, when py.test runs it will search and run tests found in executable files, detecting if the suites are Google or Boost tests automatically. +Configuration Options +~~~~~~~~~~~~~~~~~~~~~ + +**cpp_files** + You can configure which files are tested for suites by using the ``cpp_files`` -ini configuration: +ini configuration option: .. code-block:: ini [pytest] - cpp_files=test_suite* + cpp_files = test_suite* By default matches ``test_*`` and ``*_test`` executable files. -Additional arguments to the C++ tests can be provided with the -``cpp_arguments`` ini configuration. +**cpp_arguments** *New in version 1.1*. +Arguments to the C++ tests can be provided with the +``cpp_arguments`` ini configuration option. + For example: .. code-block:: ini [pytest] - cpp_arguments=-v --log-dir=logs + cpp_arguments =-v --log-dir=logs You can change this option directly in the command-line using pytest's ``-o`` option: @@ -74,12 +81,19 @@ pytest's ``-o`` option: $ pytest -o cpp_arguments='-v --log-dir=logs' +**cpp_ignore_py_files** + +*New in version 1.2*. -Requirements -============ +This option defaults to ``True`` and configures the plugin to ignore ``*.py`` files that +would otherwise match the ``cpp_files`` option. -* Python 2.7+, Python 3.4+ -* pytest +Set it to ``False`` if you have C++ executable files that end with the ``*.py`` extension. + +.. code-block:: ini + + [pytest] + cpp_ignore_py_files = False Install ======= diff --git a/pytest_cpp/plugin.py b/pytest_cpp/plugin.py index 0fa50b1..373bf60 100644 --- a/pytest_cpp/plugin.py +++ b/pytest_cpp/plugin.py @@ -23,10 +23,13 @@ def pytest_collect_file(parent, path): return config = parent.config - masks = config.getini('cpp_files') or DEFAULT_MASKS - - test_args = config.getini(_ARGUMENTS) or () + masks = config.getini('cpp_files') + test_args = config.getini('cpp_arguments') + cpp_ignore_py_files = config.getini('cpp_ignore_py_files') + # don't attempt to check *.py files even if they were given as explicit arguments + if cpp_ignore_py_files and path.fnmatch('*.py'): + return if not parent.session.isinitpath(path): for pat in masks: if path.fnmatch(pat): @@ -39,13 +42,18 @@ def pytest_collect_file(parent, path): def pytest_addoption(parser): - parser.addini("cpp_files", type="args", - default=DEFAULT_MASKS, - help="glob-style file patterns for C++ test module discovery") - parser.addini(_ARGUMENTS, - type='args', - default='', - help='Additional arguments for test executables') + parser.addini('cpp_files', + type='args', + default=DEFAULT_MASKS, + help="glob-style file patterns for C++ test module discovery") + parser.addini('cpp_arguments', + type='args', + default=(), + help='additional arguments for test executables') + parser.addini('cpp_ignore_py_files', + type='bool', + default=True, + help='ignore *.py files that otherwise match "cpp_files" patterns') class CppFile(pytest.File): diff --git a/tests/SConstruct b/tests/SConstruct index e29e210..9d18e54 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -8,12 +8,16 @@ else: CCFLAGS = '' LIBS = ['pthread'] -env = Environment( - CPPPATH=os.environ.get('INCLUDE'), - CCFLAGS=CCFLAGS, - LIBPATH=os.environ.get('LIBPATH'), - LIBS=LIBS, -) +kwargs = { + 'CPPPATH': os.environ.get('INCLUDE'), + 'CCFLAGS': CCFLAGS, + 'LIBPATH': os.environ.get('LIBPATH'), + 'LIBS': LIBS, +} +if 'CXX' in os.environ: + kwargs['CXX'] = os.environ['CXX'] + +env = Environment(**kwargs) genv = env.Clone(LIBS=['gtest'] + LIBS) Export('env genv') diff --git a/tests/test_pytest_cpp.py b/tests/test_pytest_cpp.py index 8a2c523..9207232 100644 --- a/tests/test_pytest_cpp.py +++ b/tests/test_pytest_cpp.py @@ -1,3 +1,4 @@ +import sys import pytest import subprocess from pytest_cpp import error @@ -260,6 +261,30 @@ def test_cpp_files_option(testdir, exes): ]) +# 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 ran *') + + 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]