我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用pytest.org()。
def _data_dir_copy( # The name of the subdirectory located in data/name to copy. subdir_name, # The tmpdir object for this test. See # https://pytest.org/latest/tmpdir.html. tmpdir): # Form the source and tmp paths. source_data_dir = py.path.local(_DATA_DIR).join(subdir_name) tmp_data_dir = tmpdir.join('data', subdir_name) # Copy the data. shutil.copytree(source_data_dir.strpath, tmp_data_dir.strpath) # Return the temporary data directory, so that the copied data can now be # used. return tmp_data_dir # Define a fixure for the DataDir object.
def filter_traceback(entry): """Return True if a TracebackEntry instance should be removed from tracebacks: * dynamically generated code (no code to show up for it); * internal traceback from pytest or its internal libraries, py and pluggy. """ # entry.path might sometimes return a str object when the entry # points to dynamically generated code # see https://bitbucket.org/pytest-dev/py/issues/71 raw_filename = entry.frame.code.raw.co_filename is_generated = '<' in raw_filename and '>' in raw_filename if is_generated: return False # entry.path might point to an inexisting file, in which case it will # alsso return a str object. see #1133 p = py.path.local(entry.path) return p != cutdir1 and not p.relto(cutdir2) and not p.relto(cutdir3)
def __exit__(self, *tp): __tracebackhide__ = True if tp[0] is None: pytest.fail(self.message) if sys.version_info < (2, 7): # py26: on __exit__() exc_value often does not contain the # exception value. # http://bugs.python.org/issue7853 if not isinstance(tp[1], BaseException): exc_type, value, traceback = tp tp = exc_type, exc_type(value), traceback self.excinfo.__init__(tp) suppress_exception = issubclass(self.excinfo.type, self.expected_exception) if sys.version_info[0] == 2 and suppress_exception: sys.exc_clear() return suppress_exception # builtin pytest.approx helper
def yield_fixture(scope="function", params=None, autouse=False, ids=None): """ (return a) decorator to mark a yield-fixture factory function (EXPERIMENTAL). This takes the same arguments as :py:func:`pytest.fixture` but expects a fixture function to use a ``yield`` instead of a ``return`` statement to provide a fixture. See http://pytest.org/en/latest/yieldfixture.html for more info. """ if callable(scope) and params is None and autouse == False: # direct decoration return FixtureFunctionMarker( "function", params, autouse, yieldctx=True)(scope) else: return FixtureFunctionMarker(scope, params, autouse, yieldctx=True, ids=ids)
def __exit__(self, *tp): __tracebackhide__ = True if tp[0] is None: pytest.fail("DID NOT RAISE") if sys.version_info < (2, 7): # py26: on __exit__() exc_value often does not contain the # exception value. # http://bugs.python.org/issue7853 if not isinstance(tp[1], BaseException): exc_type, value, traceback = tp tp = exc_type, exc_type(value), traceback self.excinfo.__init__(tp) return issubclass(self.excinfo.type, self.expected_exception) # # the basic pytest Function item #
def pytest_configure(config): if config.option.runxfail: old = pytest.xfail config._cleanup.append(lambda: setattr(pytest, "xfail", old)) def nop(*args, **kwargs): pass nop.Exception = XFailed setattr(pytest, "xfail", nop) config.addinivalue_line("markers", "skipif(condition): skip the given test function if eval(condition) " "results in a True value. Evaluation happens within the " "module global context. Example: skipif('sys.platform == \"win32\"') " "skips the test if we are on the win32 platform. see " "http://pytest.org/latest/skipping.html" ) config.addinivalue_line("markers", "xfail(condition, reason=None, run=True, raises=None): mark the the test function " "as an expected failure if eval(condition) has a True value. " "Optionally specify a reason for better reporting and run=False if " "you don't even want to execute the test function. If only specific " "exception(s) are expected, you can list them in raises, and if the test fails " "in other ways, it will be reported as a true failure. " "See http://pytest.org/latest/skipping.html" )
def filter_traceback(entry): # entry.path might sometimes return a str object when the entry # points to dynamically generated code # see https://bitbucket.org/pytest-dev/py/issues/71 raw_filename = entry.frame.code.raw.co_filename is_generated = '<' in raw_filename and '>' in raw_filename if is_generated: return False # entry.path might point to an inexisting file, in which case it will # alsso return a str object. see #1133 p = py.path.local(entry.path) return p != cutdir1 and not p.relto(cutdir2)
def pytest_configure(config): config.addinivalue_line("markers", "parametrize(argnames, argvalues): call a test function multiple " "times passing in different arguments in turn. argvalues generally " "needs to be a list of values if argnames specifies only one name " "or a list of tuples of values if argnames specifies multiple names. " "Example: @parametrize('arg1', [1,2]) would lead to two calls of the " "decorated test function, one with arg1=1 and another with arg1=2." "see http://pytest.org/latest/parametrize.html for more info and " "examples." ) config.addinivalue_line("markers", "usefixtures(fixturename1, fixturename2, ...): mark tests as needing " "all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures " )
def __exit__(self, *tp): __tracebackhide__ = True if tp[0] is None: pytest.fail(self.message) if sys.version_info < (2, 7): # py26: on __exit__() exc_value often does not contain the # exception value. # http://bugs.python.org/issue7853 if not isinstance(tp[1], BaseException): exc_type, value, traceback = tp tp = exc_type, exc_type(value), traceback self.excinfo.__init__(tp) return issubclass(self.excinfo.type, self.expected_exception) # builtin pytest.approx helper
def pytest_configure(config): if config.option.runxfail: old = pytest.xfail config._cleanup.append(lambda: setattr(pytest, "xfail", old)) def nop(*args, **kwargs): pass nop.Exception = XFailed setattr(pytest, "xfail", nop) config.addinivalue_line("markers", "skip(reason=None): skip the given test function with an optional reason. " "Example: skip(reason=\"no way of currently testing this\") skips the " "test." ) config.addinivalue_line("markers", "skipif(condition): skip the given test function if eval(condition) " "results in a True value. Evaluation happens within the " "module global context. Example: skipif('sys.platform == \"win32\"') " "skips the test if we are on the win32 platform. see " "http://pytest.org/latest/skipping.html" ) config.addinivalue_line("markers", "xfail(condition, reason=None, run=True, raises=None, strict=False): " "mark the the test function as an expected failure if eval(condition) " "has a True value. Optionally specify a reason for better reporting " "and run=False if you don't even want to execute the test function. " "If only specific exception(s) are expected, you can list them in " "raises, and if the test fails in other ways, it will be reported as " "a true failure. See http://pytest.org/latest/skipping.html" )
def _test(): """Run the unit tests. :return: exit code """ # Make sure to import pytest in this function. For the reason, see here: # <http://pytest.org/latest/goodpractises.html#integration-with-setuptools-test-commands> # NOPEP8 import pytest # This runs the unit tests. # It also runs doctest, but only on the modules in TESTS_DIRECTORY. return pytest.main(PYTEST_FLAGS + [TESTS_DIRECTORY])
def _test_all(): """Run lint and tests. :return: exit code """ return _lint() + _test() # The following code is to allow tests to be run with `python setup.py test'. # The main reason to make this possible is to allow tests to be run as part of # Setuptools' automatic run of 2to3 on the source code. The recommended way to # run tests is still `paver test_all'. # See <http://pythonhosted.org/setuptools/python3.html> # Code based on <http://pytest.org/latest/goodpractises.html#integration-with-setuptools-test-commands> # NOPEP8
def data_dir( # The request object for this test. See # https://pytest.org/latest/builtin.html#_pytest.python.FixtureRequest # and # https://pytest.org/latest/fixture.html#fixtures-can-introspect-the-requesting-test-context. request, # The tmpdir object for this test. See # https://pytest.org/latest/tmpdir.html. tmpdir): # Strip the leading 'test_' from the test's name. name = request.function.__name__[5:] # Copy to tmpdir and return the path. return _data_dir_copy(name, tmpdir)
def test_storage_provider_azure(config_azure, tmpdir): exercise_storage_provider(tmpdir, 'azure_block_blob', config_azure) # TODO(cmaloney): Add skipping when not run under CI with the environment variables # So devs without the variables don't see expected failures https://pytest.org/latest/skipping.html
def pytest_report_header(config): if config.option.prtl: return "forcing RTL language to be pymtl" elif config.option.vrtl: return "forcing RTL language to be verilog" # From: # https://pytest.org/latest/example/simple.html#detect-if-running-from-within-a-pytest-run
def run_tests(self): """Taken from http://pytest.org/latest/goodpractises.html.""" # have to import here, outside the eggs aren't loaded import pytest errno = pytest.main(self.test_args) raise SystemExit(errno)