pytest-4.6.9/0000775000372000037200000000000013604173421013725 5ustar travistravis00000000000000pytest-4.6.9/azure-pipelines.yml0000664000372000037200000000617413604173400017571 0ustar travistravis00000000000000trigger: - master - features variables: PYTEST_ADDOPTS: "--junitxml=build/test-results/$(tox.env).xml -vv" python.needs_vc: False COVERAGE_FILE: "$(Build.Repository.LocalPath)/.coverage" COVERAGE_PROCESS_START: "$(Build.Repository.LocalPath)/.coveragerc" PYTEST_COVERAGE: '0' jobs: - job: 'Test' pool: vmImage: "vs2017-win2016" strategy: matrix: py27: python.version: '2.7' tox.env: 'py27' py27-nobyte-lsof-numpy: python.version: '2.7' tox.env: 'py27-lsof-nobyte-numpy' # Coverage for: # - test_supports_breakpoint_module_global # - test_terminal_reporter_writer_attr (without xdist) # - "if write" branch in _pytest.assertion.rewrite # - numpy # - pytester's LsofFdLeakChecker (being skipped) PYTEST_COVERAGE: '1' py27-twisted: python.version: '2.7' tox.env: 'py27-twisted' python.needs_vc: True py27-pluggymaster-xdist: python.version: '2.7' tox.env: 'py27-pluggymaster-xdist' # Coverage for: # - except-IOError in _attempt_to_close_capture_file for py2. # Also seen with py27-nobyte (using xdist), and py27-xdist. # But no exception with py27-pexpect,py27-twisted,py27-numpy. PYTEST_COVERAGE: '1' # -- pypy2 and pypy3 are disabled for now: #5279 -- # pypy: # python.version: 'pypy2' # tox.env: 'pypy' # pypy3: # python.version: 'pypy3' # tox.env: 'pypy3' py35-xdist: python.version: '3.5' tox.env: 'py35-xdist' # Coverage for: # - test_supports_breakpoint_module_global PYTEST_COVERAGE: '1' py36-xdist: python.version: '3.6' tox.env: 'py36-xdist' py37: python.version: '3.7' tox.env: 'py37' # Coverage for: # - _py36_windowsconsoleio_workaround (with py36+) # - test_request_garbage (no xdist) PYTEST_COVERAGE: '1' py37-linting/docs/doctesting: python.version: '3.7' tox.env: 'linting,docs,doctesting' py37-twisted/numpy: python.version: '3.7' tox.env: 'py37-twisted,py37-numpy' py37-pluggymaster-xdist: python.version: '3.7' tox.env: 'py37-pluggymaster-xdist' maxParallel: 10 steps: - task: UsePythonVersion@0 inputs: versionSpec: '$(python.version)' architecture: 'x64' - script: choco install vcpython27 condition: eq(variables['python.needs_vc'], True) displayName: 'Install VC for py27' - script: python -m pip install tox displayName: 'Install tox' - script: | call scripts/setup-coverage-vars.bat || goto :eof python -m tox -e $(tox.env) displayName: 'Run tests' - task: PublishTestResults@2 inputs: testResultsFiles: 'build/test-results/$(tox.env).xml' testRunTitle: '$(tox.env)' condition: succeededOrFailed() - script: call scripts\upload-coverage.bat displayName: 'Report and upload coverage' condition: eq(variables['PYTEST_COVERAGE'], '1') env: CODECOV_TOKEN: $(CODECOV_TOKEN) PYTEST_CODECOV_NAME: $(tox.env) pytest-4.6.9/.github/0000775000372000037200000000000013604173421015265 5ustar travistravis00000000000000pytest-4.6.9/.github/config.yml0000664000372000037200000000002713604173400017251 0ustar travistravis00000000000000rtd: project: pytest pytest-4.6.9/.github/PULL_REQUEST_TEMPLATE.md0000664000372000037200000000157513604173400021073 0ustar travistravis00000000000000 - [ ] Target the `master` branch for bug fixes, documentation updates and trivial changes. - [ ] Target the `features` branch for new features and removals/deprecations. - [ ] Include documentation when adding new features. - [ ] Include new tests or update existing tests when applicable. Unless your change is trivial or a small documentation fix (e.g., a typo or reword of a small section) please: - [ ] Create a new changelog file in the `changelog` folder, with a name like `..rst`. See [changelog/README.rst](https://github.com/pytest-dev/pytest/blob/master/changelog/README.rst) for details. - [ ] Add yourself to `AUTHORS` in alphabetical order; pytest-4.6.9/.github/ISSUE_TEMPLATE.md0000664000372000037200000000044213604173400017767 0ustar travistravis00000000000000 - [ ] a detailed description of the bug or suggestion - [ ] output of `pip list` from the virtual environment you are using - [ ] pytest and operating system versions - [ ] minimal example if possible pytest-4.6.9/.github/FUNDING.yml0000664000372000037200000000027713604173400017105 0ustar travistravis00000000000000# info: # * https://help.github.com/en/articles/displaying-a-sponsor-button-in-your-repository # * https://tidelift.com/subscription/how-to-connect-tidelift-with-github tidelift: pypi/pytest pytest-4.6.9/.github/labels.toml0000664000372000037200000000715213604173400017426 0ustar travistravis00000000000000["os: cygwin"] color = "006b75" description = "cygwin platform-specific problem" name = "os: cygwin" ["os: linux"] color = "1d76db" description = "linux platform-specific problem" name = "os: linux" ["os: mac"] color = "bfdadc" description = "mac platform-specific problem" name = "os: mac" ["os: windows"] color = "fbca04" description = "windows platform-specific problem" name = "os: windows" ["plugin: argcomplete"] color = "d4c5f9" description = "related to the argcomplete builtin plugin" name = "plugin: argcomplete" ["plugin: cache"] color = "c7def8" description = "related to the cache builtin plugin" name = "plugin: cache" ["plugin: capture"] color = "1d76db" description = "related to the capture builtin plugin" name = "plugin: capture" ["plugin: debugging"] color = "dd52a8" description = "related to the debugging builtin plugin" name = "plugin: debugging" ["plugin: doctests"] color = "fad8c7" description = "related to the doctests builtin plugin" name = "plugin: doctests" ["plugin: junitxml"] color = "c5def5" description = "related to the junitxml builtin plugin" name = "plugin: junitxml" ["plugin: logging"] color = "ff5432" description = "related to the logging builtin plugin" name = "plugin: logging" ["plugin: monkeypatch"] color = "0e8a16" description = "related to the monkeypatch builtin plugin" name = "plugin: monkeypatch" ["plugin: nose"] color = "bfdadc" description = "related to the nose integration builtin plugin" name = "plugin: nose" ["plugin: pastebin"] color = "bfd4f2" description = "related to the pastebin builtin plugin" name = "plugin: pastebin" ["plugin: pytester"] color = "c5def5" description = "related to the pytester builtin plugin" name = "plugin: pytester" ["plugin: tmpdir"] color = "bfd4f2" description = "related to the tmpdir builtin plugin" name = "plugin: tmpdir" ["plugin: unittest"] color = "006b75" description = "related to the unittest integration builtin plugin" name = "plugin: unittest" ["plugin: warnings"] color = "fef2c0" description = "related to the warnings builtin plugin" name = "plugin: warnings" ["plugin: xdist"] color = "5319e7" description = "related to the xdist external plugin" name = "plugin: xdist" ["status: critical"] color = "e11d21" description = "grave problem or usability issue that affects lots of users" name = "status: critical" ["status: easy"] color = "bfe5bf" description = "easy issue that is friendly to new contributor" name = "status: easy" ["status: help wanted"] color = "159818" description = "developers would like help from experts on this topic" name = "status: help wanted" ["status: needs information"] color = "5319e7" description = "reporter needs to provide more information; can be closed after 2 or more weeks of inactivity" name = "status: needs information" ["topic: collection"] color = "006b75" description = "related to the collection phase" name = "topic: collection" ["topic: config"] color = "006b75" description = "related to config handling, argument parsing and config file" name = "topic: config" ["topic: fixtures"] color = "5319e7" description = "anything involving fixtures directly or indirectly" name = "topic: fixtures" ["topic: marks"] color = "b60205" description = "related to marks, either the general marks or builtin" name = "topic: marks" ["topic: parametrize"] color = "fbca04" description = "related to @pytest.mark.parametrize" name = "topic: parametrize" ["topic: reporting"] color = "fef2c0" description = "related to terminal output and user-facing messages and errors" name = "topic: reporting" ["topic: rewrite"] color = "0e8a16" description = "related to the assertion rewrite mechanism" name = "topic: rewrite" pytest-4.6.9/CHANGELOG.rst0000664000372000037200000105061713604173400015755 0ustar travistravis00000000000000================= Changelog history ================= Versions follow `Semantic Versioning `_ (``..``). Backward incompatible (breaking) changes will only be introduced in major versions with advance notice in the **Deprecations** section of releases. .. You should *NOT* be adding new change log entries to this file, this file is managed by towncrier. You *may* edit previous change logs to fix problems like typo corrections or such. To add a new change log entry, please see https://pip.pypa.io/en/latest/development/#adding-a-news-entry we named the news folder changelog .. towncrier release notes start pytest 4.6.9 (2020-01-04) ========================= Bug Fixes --------- - `#6301 `_: Fix assertion rewriting for egg-based distributions and ``editable`` installs (``pip install --editable``). pytest 4.6.8 (2019-12-19) ========================= Features -------- - `#5471 `_: JUnit XML now includes a timestamp and hostname in the testsuite tag. Bug Fixes --------- - `#5430 `_: junitxml: Logs for failed test are now passed to junit report in case the test fails during call phase. Trivial/Internal Changes ------------------------ - `#6345 `_: Pin ``colorama`` to ``0.4.1`` only for Python 3.4 so newer Python versions can still receive colorama updates. pytest 4.6.7 (2019-12-05) ========================= Bug Fixes --------- - `#5477 `_: The XML file produced by ``--junitxml`` now correctly contain a ```` root element. - `#6044 `_: Properly ignore ``FileNotFoundError`` (``OSError.errno == NOENT`` in Python 2) exceptions when trying to remove old temporary directories, for instance when multiple processes try to remove the same directory (common with ``pytest-xdist`` for example). pytest 4.6.6 (2019-10-11) ========================= Bug Fixes --------- - `#5523 `_: Fixed using multiple short options together in the command-line (for example ``-vs``) in Python 3.8+. - `#5537 `_: Replace ``importlib_metadata`` backport with ``importlib.metadata`` from the standard library on Python 3.8+. - `#5806 `_: Fix "lexer" being used when uploading to bpaste.net from ``--pastebin`` to "text". - `#5902 `_: Fix warnings about deprecated ``cmp`` attribute in ``attrs>=19.2``. Trivial/Internal Changes ------------------------ - `#5801 `_: Fixes python version checks (detected by ``flake8-2020``) in case python4 becomes a thing. pytest 4.6.5 (2019-08-05) ========================= Bug Fixes --------- - `#4344 `_: Fix RuntimeError/StopIteration when trying to collect package with "__init__.py" only. - `#5478 `_: Fix encode error when using unicode strings in exceptions with ``pytest.raises``. - `#5524 `_: Fix issue where ``tmp_path`` and ``tmpdir`` would not remove directories containing files marked as read-only, which could lead to pytest crashing when executed a second time with the ``--basetemp`` option. - `#5547 `_: ``--step-wise`` now handles ``xfail(strict=True)`` markers properly. - `#5650 `_: Improved output when parsing an ini configuration file fails. pytest 4.6.4 (2019-06-28) ========================= Bug Fixes --------- - `#5404 `_: Emit a warning when attempting to unwrap a broken object raises an exception, for easier debugging (`#5080 `__). - `#5444 `_: Fix ``--stepwise`` mode when the first file passed on the command-line fails to collect. - `#5482 `_: Fix bug introduced in 4.6.0 causing collection errors when passing more than 2 positional arguments to ``pytest.mark.parametrize``. - `#5505 `_: Fix crash when discovery fails while using ``-p no:terminal``. pytest 4.6.3 (2019-06-11) ========================= Bug Fixes --------- - `#5383 `_: ``-q`` has again an impact on the style of the collected items (``--collect-only``) when ``--log-cli-level`` is used. - `#5389 `_: Fix regressions of `#5063 `__ for ``importlib_metadata.PathDistribution`` which have their ``files`` attribute being ``None``. - `#5390 `_: Fix regression where the ``obj`` attribute of ``TestCase`` items was no longer bound to methods. pytest 4.6.2 (2019-06-03) ========================= Bug Fixes --------- - `#5370 `_: Revert unrolling of ``all()`` to fix ``NameError`` on nested comprehensions. - `#5371 `_: Revert unrolling of ``all()`` to fix incorrect handling of generators with ``if``. - `#5372 `_: Revert unrolling of ``all()`` to fix incorrect assertion when using ``all()`` in an expression. pytest 4.6.1 (2019-06-02) ========================= Bug Fixes --------- - `#5354 `_: Fix ``pytest.mark.parametrize`` when the argvalues is an iterator. - `#5358 `_: Fix assertion rewriting of ``all()`` calls to deal with non-generators. pytest 4.6.0 (2019-05-31) ========================= Important --------- The ``4.6.X`` series will be the last series to support **Python 2 and Python 3.4**. For more details, see our `Python 2.7 and 3.4 support plan `__. Features -------- - `#4559 `_: Added the ``junit_log_passing_tests`` ini value which can be used to enable or disable logging of passing test output in the Junit XML file. - `#4956 `_: pytester's ``testdir.spawn`` uses ``tmpdir`` as HOME/USERPROFILE directory. - `#5062 `_: Unroll calls to ``all`` to full for-loops with assertion rewriting for better failure messages, especially when using Generator Expressions. - `#5063 `_: Switch from ``pkg_resources`` to ``importlib-metadata`` for entrypoint detection for improved performance and import time. - `#5091 `_: The output for ini options in ``--help`` has been improved. - `#5269 `_: ``pytest.importorskip`` includes the ``ImportError`` now in the default ``reason``. - `#5311 `_: Captured logs that are output for each failing test are formatted using the ColoredLevelFormatter. - `#5312 `_: Improved formatting of multiline log messages in Python 3. Bug Fixes --------- - `#2064 `_: The debugging plugin imports the wrapped ``Pdb`` class (``--pdbcls``) on-demand now. - `#4908 `_: The ``pytest_enter_pdb`` hook gets called with post-mortem (``--pdb``). - `#5036 `_: Fix issue where fixtures dependent on other parametrized fixtures would be erroneously parametrized. - `#5256 `_: Handle internal error due to a lone surrogate unicode character not being representable in Jython. - `#5257 `_: Ensure that ``sys.stdout.mode`` does not include ``'b'`` as it is a text stream. - `#5278 `_: Pytest's internal python plugin can be disabled using ``-p no:python`` again. - `#5286 `_: Fix issue with ``disable_test_id_escaping_and_forfeit_all_rights_to_community_support`` option not working when using a list of test IDs in parametrized tests. - `#5330 `_: Show the test module being collected when emitting ``PytestCollectionWarning`` messages for test classes with ``__init__`` and ``__new__`` methods to make it easier to pin down the problem. - `#5333 `_: Fix regression in 4.5.0 with ``--lf`` not re-running all tests with known failures from non-selected tests. Improved Documentation ---------------------- - `#5250 `_: Expand docs on use of ``setenv`` and ``delenv`` with ``monkeypatch``. pytest 4.5.0 (2019-05-11) ========================= Features -------- - `#4826 `_: A warning is now emitted when unknown marks are used as a decorator. This is often due to a typo, which can lead to silently broken tests. - `#4907 `_: Show XFail reason as part of JUnitXML message field. - `#5013 `_: Messages from crash reports are displayed within test summaries now, truncated to the terminal width. - `#5023 `_: New flag ``--strict-markers`` that triggers an error when unknown markers (e.g. those not registered using the `markers option`_ in the configuration file) are used in the test suite. The existing ``--strict`` option has the same behavior currently, but can be augmented in the future for additional checks. .. _`markers option`: https://docs.pytest.org/en/latest/reference.html#confval-markers - `#5026 `_: Assertion failure messages for sequences and dicts contain the number of different items now. - `#5034 `_: Improve reporting with ``--lf`` and ``--ff`` (run-last-failure). - `#5035 `_: The ``--cache-show`` option/action accepts an optional glob to show only matching cache entries. - `#5059 `_: Standard input (stdin) can be given to pytester's ``Testdir.run()`` and ``Testdir.popen()``. - `#5068 `_: The ``-r`` option learnt about ``A`` to display all reports (including passed ones) in the short test summary. - `#5108 `_: The short test summary is displayed after passes with output (``-rP``). - `#5172 `_: The ``--last-failed`` (``--lf``) option got smarter and will now skip entire files if all tests of that test file have passed in previous runs, greatly speeding up collection. - `#5177 `_: Introduce new specific warning ``PytestWarning`` subclasses to make it easier to filter warnings based on the class, rather than on the message. The new subclasses are: * ``PytestAssertRewriteWarning`` * ``PytestCacheWarning`` * ``PytestCollectionWarning`` * ``PytestConfigWarning`` * ``PytestUnhandledCoroutineWarning`` * ``PytestUnknownMarkWarning`` - `#5202 `_: New ``record_testsuite_property`` session-scoped fixture allows users to log ```` tags at the ``testsuite`` level with the ``junitxml`` plugin. The generated XML is compatible with the latest xunit standard, contrary to the properties recorded by ``record_property`` and ``record_xml_attribute``. - `#5214 `_: The default logging format has been changed to improve readability. Here is an example of a previous logging message:: test_log_cli_enabled_disabled.py 3 CRITICAL critical message logged by test This has now become:: CRITICAL root:test_log_cli_enabled_disabled.py:3 critical message logged by test The formatting can be changed through the `log_format `__ configuration option. - `#5220 `_: ``--fixtures`` now also shows fixture scope for scopes other than ``"function"``. Bug Fixes --------- - `#5113 `_: Deselected items from plugins using ``pytest_collect_modifyitems`` as a hookwrapper are correctly reported now. - `#5144 `_: With usage errors ``exitstatus`` is set to ``EXIT_USAGEERROR`` in the ``pytest_sessionfinish`` hook now as expected. - `#5235 `_: ``outcome.exit`` is not used with ``EOF`` in the pdb wrapper anymore, but only with ``quit``. Improved Documentation ---------------------- - `#4935 `_: Expand docs on registering marks and the effect of ``--strict``. Trivial/Internal Changes ------------------------ - `#4942 `_: ``logging.raiseExceptions`` is not set to ``False`` anymore. - `#5013 `_: pytest now depends on `wcwidth `__ to properly track unicode character sizes for more precise terminal output. - `#5059 `_: pytester's ``Testdir.popen()`` uses ``stdout`` and ``stderr`` via keyword arguments with defaults now (``subprocess.PIPE``). - `#5069 `_: The code for the short test summary in the terminal was moved to the terminal plugin. - `#5082 `_: Improved validation of kwargs for various methods in the pytester plugin. - `#5202 `_: ``record_property`` now emits a ``PytestWarning`` when used with ``junit_family=xunit2``: the fixture generates ``property`` tags as children of ``testcase``, which is not permitted according to the most `recent schema `__. - `#5239 `_: Pin ``pluggy`` to ``< 1.0`` so we don't update to ``1.0`` automatically when it gets released: there are planned breaking changes, and we want to ensure pytest properly supports ``pluggy 1.0``. pytest 4.4.2 (2019-05-08) ========================= Bug Fixes --------- - `#5089 `_: Fix crash caused by error in ``__repr__`` function with both ``showlocals`` and verbose output enabled. - `#5139 `_: Eliminate core dependency on 'terminal' plugin. - `#5229 `_: Require ``pluggy>=0.11.0`` which reverts a dependency to ``importlib-metadata`` added in ``0.10.0``. The ``importlib-metadata`` package cannot be imported when installed as an egg and causes issues when relying on ``setup.py`` to install test dependencies. Improved Documentation ---------------------- - `#5171 `_: Doc: ``pytest_ignore_collect``, ``pytest_collect_directory``, ``pytest_collect_file`` and ``pytest_pycollect_makemodule`` hooks's 'path' parameter documented type is now ``py.path.local`` - `#5188 `_: Improve help for ``--runxfail`` flag. Trivial/Internal Changes ------------------------ - `#5182 `_: Removed internal and unused ``_pytest.deprecated.MARK_INFO_ATTRIBUTE``. pytest 4.4.1 (2019-04-15) ========================= Bug Fixes --------- - `#5031 `_: Environment variables are properly restored when using pytester's ``testdir`` fixture. - `#5039 `_: Fix regression with ``--pdbcls``, which stopped working with local modules in 4.0.0. - `#5092 `_: Produce a warning when unknown keywords are passed to ``pytest.param(...)``. - `#5098 `_: Invalidate import caches with ``monkeypatch.syspath_prepend``, which is required with namespace packages being used. pytest 4.4.0 (2019-03-29) ========================= Features -------- - `#2224 `_: ``async`` test functions are skipped and a warning is emitted when a suitable async plugin is not installed (such as ``pytest-asyncio`` or ``pytest-trio``). Previously ``async`` functions would not execute at all but still be marked as "passed". - `#2482 `_: Include new ``disable_test_id_escaping_and_forfeit_all_rights_to_community_support`` option to disable ascii-escaping in parametrized values. This may cause a series of problems and as the name makes clear, use at your own risk. - `#4718 `_: The ``-p`` option can now be used to early-load plugins also by entry-point name, instead of just by module name. This makes it possible to early load external plugins like ``pytest-cov`` in the command-line:: pytest -p pytest_cov - `#4855 `_: The ``--pdbcls`` option handles classes via module attributes now (e.g. ``pdb:pdb.Pdb`` with `pdb++`_), and its validation was improved. .. _pdb++: https://pypi.org/project/pdbpp/ - `#4875 `_: The `testpaths `__ configuration option is now displayed next to the ``rootdir`` and ``inifile`` lines in the pytest header if the option is in effect, i.e., directories or file names were not explicitly passed in the command line. Also, ``inifile`` is only displayed if there's a configuration file, instead of an empty ``inifile:`` string. - `#4911 `_: Doctests can be skipped now dynamically using ``pytest.skip()``. - `#4920 `_: Internal refactorings have been made in order to make the implementation of the `pytest-subtests `__ plugin possible, which adds unittest sub-test support and a new ``subtests`` fixture as discussed in `#1367 `__. For details on the internal refactorings, please see the details on the related PR. - `#4931 `_: pytester's ``LineMatcher`` asserts that the passed lines are a sequence. - `#4936 `_: Handle ``-p plug`` after ``-p no:plug``. This can be used to override a blocked plugin (e.g. in "addopts") from the command line etc. - `#4951 `_: Output capturing is handled correctly when only capturing via fixtures (capsys, capfs) with ``pdb.set_trace()``. - `#4956 `_: ``pytester`` sets ``$HOME`` and ``$USERPROFILE`` to the temporary directory during test runs. This ensures to not load configuration files from the real user's home directory. - `#4980 `_: Namespace packages are handled better with ``monkeypatch.syspath_prepend`` and ``testdir.syspathinsert`` (via ``pkg_resources.fixup_namespace_packages``). - `#4993 `_: The stepwise plugin reports status information now. - `#5008 `_: If a ``setup.cfg`` file contains ``[tool:pytest]`` and also the no longer supported ``[pytest]`` section, pytest will use ``[tool:pytest]`` ignoring ``[pytest]``. Previously it would unconditionally error out. This makes it simpler for plugins to support old pytest versions. Bug Fixes --------- - `#1895 `_: Fix bug where fixtures requested dynamically via ``request.getfixturevalue()`` might be teardown before the requesting fixture. - `#4851 `_: pytester unsets ``PYTEST_ADDOPTS`` now to not use outer options with ``testdir.runpytest()``. - `#4903 `_: Use the correct modified time for years after 2038 in rewritten ``.pyc`` files. - `#4928 `_: Fix line offsets with ``ScopeMismatch`` errors. - `#4957 `_: ``-p no:plugin`` is handled correctly for default (internal) plugins now, e.g. with ``-p no:capture``. Previously they were loaded (imported) always, making e.g. the ``capfd`` fixture available. - `#4968 `_: The pdb ``quit`` command is handled properly when used after the ``debug`` command with `pdb++`_. .. _pdb++: https://pypi.org/project/pdbpp/ - `#4975 `_: Fix the interpretation of ``-qq`` option where it was being considered as ``-v`` instead. - `#4978 `_: ``outcomes.Exit`` is not swallowed in ``assertrepr_compare`` anymore. - `#4988 `_: Close logging's file handler explicitly when the session finishes. - `#5003 `_: Fix line offset with mark collection error (off by one). Improved Documentation ---------------------- - `#4974 `_: Update docs for ``pytest_cmdline_parse`` hook to note availability liminations Trivial/Internal Changes ------------------------ - `#4718 `_: ``pluggy>=0.9`` is now required. - `#4815 `_: ``funcsigs>=1.0`` is now required for Python 2.7. - `#4829 `_: Some left-over internal code related to ``yield`` tests has been removed. - `#4890 `_: Remove internally unused ``anypython`` fixture from the pytester plugin. - `#4912 `_: Remove deprecated Sphinx directive, ``add_description_unit()``, pin sphinx-removed-in to >= 0.2.0 to support Sphinx 2.0. - `#4913 `_: Fix pytest tests invocation with custom ``PYTHONPATH``. - `#4965 `_: New ``pytest_report_to_serializable`` and ``pytest_report_from_serializable`` **experimental** hooks. These hooks will be used by ``pytest-xdist``, ``pytest-subtests``, and the replacement for resultlog to serialize and customize reports. They are experimental, meaning that their details might change or even be removed completely in future patch releases without warning. Feedback is welcome from plugin authors and users alike. - `#4987 `_: ``Collector.repr_failure`` respects the ``--tb`` option, but only defaults to ``short`` now (with ``auto``). pytest 4.3.1 (2019-03-11) ========================= Bug Fixes --------- - `#4810 `_: Logging messages inside ``pytest_runtest_logreport()`` are now properly captured and displayed. - `#4861 `_: Improve validation of contents written to captured output so it behaves the same as when capture is disabled. - `#4898 `_: Fix ``AttributeError: FixtureRequest has no 'confg' attribute`` bug in ``testdir.copy_example``. Trivial/Internal Changes ------------------------ - `#4768 `_: Avoid pkg_resources import at the top-level. pytest 4.3.0 (2019-02-16) ========================= Deprecations ------------ - `#4724 `_: ``pytest.warns()`` now emits a warning when it receives unknown keyword arguments. This will be changed into an error in the future. Features -------- - `#2753 `_: Usage errors from argparse are mapped to pytest's ``UsageError``. - `#3711 `_: Add the ``--ignore-glob`` parameter to exclude test-modules with Unix shell-style wildcards. Add the ``collect_ignore_glob`` for ``conftest.py`` to exclude test-modules with Unix shell-style wildcards. - `#4698 `_: The warning about Python 2.7 and 3.4 not being supported in pytest 5.0 has been removed. In the end it was considered to be more of a nuisance than actual utility and users of those Python versions shouldn't have problems as ``pip`` will not install pytest 5.0 on those interpreters. - `#4707 `_: With the help of new ``set_log_path()`` method there is a way to set ``log_file`` paths from hooks. Bug Fixes --------- - `#4651 `_: ``--help`` and ``--version`` are handled with ``UsageError``. - `#4782 `_: Fix ``AssertionError`` with collection of broken symlinks with packages. pytest 4.2.1 (2019-02-12) ========================= Bug Fixes --------- - `#2895 `_: The ``pytest_report_collectionfinish`` hook now is also called with ``--collect-only``. - `#3899 `_: Do not raise ``UsageError`` when an imported package has a ``pytest_plugins.py`` child module. - `#4347 `_: Fix output capturing when using pdb++ with recursive debugging. - `#4592 `_: Fix handling of ``collect_ignore`` via parent ``conftest.py``. - `#4700 `_: Fix regression where ``setUpClass`` would always be called in subclasses even if all tests were skipped by a ``unittest.skip()`` decorator applied in the subclass. - `#4739 `_: Fix ``parametrize(... ids=)`` when the function returns non-strings. - `#4745 `_: Fix/improve collection of args when passing in ``__init__.py`` and a test file. - `#4770 `_: ``more_itertools`` is now constrained to <6.0.0 when required for Python 2.7 compatibility. - `#526 `_: Fix "ValueError: Plugin already registered" exceptions when running in build directories that symlink to actual source. Improved Documentation ---------------------- - `#3899 `_: Add note to ``plugins.rst`` that ``pytest_plugins`` should not be used as a name for a user module containing plugins. - `#4324 `_: Document how to use ``raises`` and ``does_not_raise`` to write parametrized tests with conditional raises. - `#4709 `_: Document how to customize test failure messages when using ``pytest.warns``. Trivial/Internal Changes ------------------------ - `#4741 `_: Some verbosity related attributes of the TerminalReporter plugin are now read only properties. pytest 4.2.0 (2019-01-30) ========================= Features -------- - `#3094 `_: `Classic xunit-style `__ functions and methods now obey the scope of *autouse* fixtures. This fixes a number of surprising issues like ``setup_method`` being called before session-scoped autouse fixtures (see `#517 `__ for an example). - `#4627 `_: Display a message at the end of the test session when running under Python 2.7 and 3.4 that pytest 5.0 will no longer support those Python versions. - `#4660 `_: The number of *selected* tests now are also displayed when the ``-k`` or ``-m`` flags are used. - `#4688 `_: ``pytest_report_teststatus`` hook now can also receive a ``config`` parameter. - `#4691 `_: ``pytest_terminal_summary`` hook now can also receive a ``config`` parameter. Bug Fixes --------- - `#3547 `_: ``--junitxml`` can emit XML compatible with Jenkins xUnit. ``junit_family`` INI option accepts ``legacy|xunit1``, which produces old style output, and ``xunit2`` that conforms more strictly to https://github.com/jenkinsci/xunit-plugin/blob/xunit-2.3.2/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd - `#4280 `_: Improve quitting from pdb, especially with ``--trace``. Using ``q[quit]`` after ``pdb.set_trace()`` will quit pytest also. - `#4402 `_: Warning summary now groups warnings by message instead of by test id. This makes the output more compact and better conveys the general idea of how much code is actually generating warnings, instead of how many tests call that code. - `#4536 `_: ``monkeypatch.delattr`` handles class descriptors like ``staticmethod``/``classmethod``. - `#4649 `_: Restore marks being considered keywords for keyword expressions. - `#4653 `_: ``tmp_path`` fixture and other related ones provides resolved path (a.k.a real path) - `#4667 `_: ``pytest_terminal_summary`` uses result from ``pytest_report_teststatus`` hook, rather than hardcoded strings. - `#4669 `_: Correctly handle ``unittest.SkipTest`` exception containing non-ascii characters on Python 2. - `#4680 `_: Ensure the ``tmpdir`` and the ``tmp_path`` fixtures are the same folder. - `#4681 `_: Ensure ``tmp_path`` is always a real path. Trivial/Internal Changes ------------------------ - `#4643 `_: Use ``a.item()`` instead of the deprecated ``np.asscalar(a)`` in ``pytest.approx``. ``np.asscalar`` has been `deprecated `__ in ``numpy 1.16.``. - `#4657 `_: Copy saferepr from pylib - `#4668 `_: The verbose word for expected failures in the teststatus report changes from ``xfail`` to ``XFAIL`` to be consistent with other test outcomes. pytest 4.1.1 (2019-01-12) ========================= Bug Fixes --------- - `#2256 `_: Show full repr with ``assert a==b`` and ``-vv``. - `#3456 `_: Extend Doctest-modules to ignore mock objects. - `#4617 `_: Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization). - `#4631 `_: Don't rewrite assertion when ``__getattr__`` is broken Improved Documentation ---------------------- - `#3375 `_: Document that using ``setup.cfg`` may crash other tools or cause hard to track down problems because it uses a different parser than ``pytest.ini`` or ``tox.ini`` files. Trivial/Internal Changes ------------------------ - `#4602 `_: Uninstall ``hypothesis`` in regen tox env. pytest 4.1.0 (2019-01-05) ========================= Removals -------- - `#2169 `_: ``pytest.mark.parametrize``: in previous versions, errors raised by id functions were suppressed and changed into warnings. Now the exceptions are propagated, along with a pytest message informing the node, parameter value and index where the exception occurred. - `#3078 `_: Remove legacy internal warnings system: ``config.warn``, ``Node.warn``. The ``pytest_logwarning`` now issues a warning when implemented. See our `docs `__ on information on how to update your code. - `#3079 `_: Removed support for yield tests - they are fundamentally broken because they don't support fixtures properly since collection and test execution were separated. See our `docs `__ on information on how to update your code. - `#3082 `_: Removed support for applying marks directly to values in ``@pytest.mark.parametrize``. Use ``pytest.param`` instead. See our `docs `__ on information on how to update your code. - `#3083 `_: Removed ``Metafunc.addcall``. This was the predecessor mechanism to ``@pytest.mark.parametrize``. See our `docs `__ on information on how to update your code. - `#3085 `_: Removed support for passing strings to ``pytest.main``. Now, always pass a list of strings instead. See our `docs `__ on information on how to update your code. - `#3086 `_: ``[pytest]`` section in **setup.cfg** files is no longer supported, use ``[tool:pytest]`` instead. ``setup.cfg`` files are meant for use with ``distutils``, and a section named ``pytest`` has notoriously been a source of conflicts and bugs. Note that for **pytest.ini** and **tox.ini** files the section remains ``[pytest]``. - `#3616 `_: Removed the deprecated compat properties for ``node.Class/Function/Module`` - use ``pytest.Class/Function/Module`` now. See our `docs `__ on information on how to update your code. - `#4421 `_: Removed the implementation of the ``pytest_namespace`` hook. See our `docs `__ on information on how to update your code. - `#4489 `_: Removed ``request.cached_setup``. This was the predecessor mechanism to modern fixtures. See our `docs `__ on information on how to update your code. - `#4535 `_: Removed the deprecated ``PyCollector.makeitem`` method. This method was made public by mistake a long time ago. - `#4543 `_: Removed support to define fixtures using the ``pytest_funcarg__`` prefix. Use the ``@pytest.fixture`` decorator instead. See our `docs `__ on information on how to update your code. - `#4545 `_: Calling fixtures directly is now always an error instead of a warning. See our `docs `__ on information on how to update your code. - `#4546 `_: Remove ``Node.get_marker(name)`` the return value was not usable for more than a existence check. Use ``Node.get_closest_marker(name)`` as a replacement. - `#4547 `_: The deprecated ``record_xml_property`` fixture has been removed, use the more generic ``record_property`` instead. See our `docs `__ for more information. - `#4548 `_: An error is now raised if the ``pytest_plugins`` variable is defined in a non-top-level ``conftest.py`` file (i.e., not residing in the ``rootdir``). See our `docs `__ for more information. - `#891 `_: Remove ``testfunction.markername`` attributes - use ``Node.iter_markers(name=None)`` to iterate them. Deprecations ------------ - `#3050 `_: Deprecated the ``pytest.config`` global. See https://docs.pytest.org/en/latest/deprecations.html#pytest-config-global for rationale. - `#3974 `_: Passing the ``message`` parameter of ``pytest.raises`` now issues a ``DeprecationWarning``. It is a common mistake to think this parameter will match the exception message, while in fact it only serves to provide a custom message in case the ``pytest.raises`` check fails. To avoid this mistake and because it is believed to be little used, pytest is deprecating it without providing an alternative for the moment. If you have concerns about this, please comment on `issue #3974 `__. - `#4435 `_: Deprecated ``raises(..., 'code(as_a_string)')`` and ``warns(..., 'code(as_a_string)')``. See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec for rationale and examples. Features -------- - `#3191 `_: A warning is now issued when assertions are made for ``None``. This is a common source of confusion among new users, which write: .. code-block:: python assert mocked_object.assert_called_with(3, 4, 5, key="value") When they should write: .. code-block:: python mocked_object.assert_called_with(3, 4, 5, key="value") Because the ``assert_called_with`` method of mock objects already executes an assertion. This warning will not be issued when ``None`` is explicitly checked. An assertion like: .. code-block:: python assert variable is None will not issue the warning. - `#3632 `_: Richer equality comparison introspection on ``AssertionError`` for objects created using `attrs `__ or `dataclasses `_ (Python 3.7+, `backported to 3.6 `__). - `#4278 `_: ``CACHEDIR.TAG`` files are now created inside cache directories. Those files are part of the `Cache Directory Tagging Standard `__, and can be used by backup or synchronization programs to identify pytest's cache directory as such. - `#4292 `_: ``pytest.outcomes.Exit`` is derived from ``SystemExit`` instead of ``KeyboardInterrupt``. This allows us to better handle ``pdb`` exiting. - `#4371 `_: Updated the ``--collect-only`` option to display test descriptions when ran using ``--verbose``. - `#4386 `_: Restructured ``ExceptionInfo`` object construction and ensure incomplete instances have a ``repr``/``str``. - `#4416 `_: pdb: added support for keyword arguments with ``pdb.set_trace``. It handles ``header`` similar to Python 3.7 does it, and forwards any other keyword arguments to the ``Pdb`` constructor. This allows for ``__import__("pdb").set_trace(skip=["foo.*"])``. - `#4483 `_: Added ini parameter ``junit_duration_report`` to optionally report test call durations, excluding setup and teardown times. The JUnit XML specification and the default pytest behavior is to include setup and teardown times in the test duration report. You can include just the call durations instead (excluding setup and teardown) by adding this to your ``pytest.ini`` file: .. code-block:: ini [pytest] junit_duration_report = call - `#4532 `_: ``-ra`` now will show errors and failures last, instead of as the first items in the summary. This makes it easier to obtain a list of errors and failures to run tests selectively. - `#4599 `_: ``pytest.importorskip`` now supports a ``reason`` parameter, which will be shown when the requested module cannot be imported. Bug Fixes --------- - `#3532 `_: ``-p`` now accepts its argument without a space between the value, for example ``-pmyplugin``. - `#4327 `_: ``approx`` again works with more generic containers, more precisely instances of ``Iterable`` and ``Sized`` instead of more restrictive ``Sequence``. - `#4397 `_: Ensure that node ids are printable. - `#4435 `_: Fixed ``raises(..., 'code(string)')`` frame filename. - `#4458 `_: Display actual test ids in ``--collect-only``. Improved Documentation ---------------------- - `#4557 `_: Markers example documentation page updated to support latest pytest version. - `#4558 `_: Update cache documentation example to correctly show cache hit and miss. - `#4580 `_: Improved detailed summary report documentation. Trivial/Internal Changes ------------------------ - `#4447 `_: Changed the deprecation type of ``--result-log`` to ``PytestDeprecationWarning``. It was decided to remove this feature at the next major revision. pytest 4.0.2 (2018-12-13) ========================= Bug Fixes --------- - `#4265 `_: Validate arguments from the ``PYTEST_ADDOPTS`` environment variable and the ``addopts`` ini option separately. - `#4435 `_: Fix ``raises(..., 'code(string)')`` frame filename. - `#4500 `_: When a fixture yields and a log call is made after the test runs, and, if the test is interrupted, capture attributes are ``None``. - `#4538 `_: Raise ``TypeError`` for ``with raises(..., match=)``. Improved Documentation ---------------------- - `#1495 `_: Document common doctest fixture directory tree structure pitfalls pytest 4.0.1 (2018-11-23) ========================= Bug Fixes --------- - `#3952 `_: Display warnings before "short test summary info" again, but still later warnings in the end. - `#4386 `_: Handle uninitialized exceptioninfo in repr/str. - `#4393 `_: Do not create ``.gitignore``/``README.md`` files in existing cache directories. - `#4400 `_: Rearrange warning handling for the yield test errors so the opt-out in 4.0.x correctly works. - `#4405 `_: Fix collection of testpaths with ``--pyargs``. - `#4412 `_: Fix assertion rewriting involving ``Starred`` + side-effects. - `#4425 `_: Ensure we resolve the absolute path when the given ``--basetemp`` is a relative path. Trivial/Internal Changes ------------------------ - `#4315 `_: Use ``pkg_resources.parse_version`` instead of ``LooseVersion`` in minversion check. - `#4440 `_: Adjust the stack level of some internal pytest warnings. pytest 4.0.0 (2018-11-13) ========================= Removals -------- - `#3737 `_: **RemovedInPytest4Warnings are now errors by default.** Following our plan to remove deprecated features with as little disruption as possible, all warnings of type ``RemovedInPytest4Warnings`` now generate errors instead of warning messages. **The affected features will be effectively removed in pytest 4.1**, so please consult the `Deprecations and Removals `__ section in the docs for directions on how to update existing code. In the pytest ``4.0.X`` series, it is possible to change the errors back into warnings as a stop gap measure by adding this to your ``pytest.ini`` file: .. code-block:: ini [pytest] filterwarnings = ignore::pytest.RemovedInPytest4Warning But this will stop working when pytest ``4.1`` is released. **If you have concerns** about the removal of a specific feature, please add a comment to `#4348 `__. - `#4358 `_: Remove the ``::()`` notation to denote a test class instance in node ids. Previously, node ids that contain test instances would use ``::()`` to denote the instance like this:: test_foo.py::Test::()::test_bar The extra ``::()`` was puzzling to most users and has been removed, so that the test id becomes now:: test_foo.py::Test::test_bar This change could not accompany a deprecation period as is usual when user-facing functionality changes because it was not really possible to detect when the functionality was being used explicitly. The extra ``::()`` might have been removed in some places internally already, which then led to confusion in places where it was expected, e.g. with ``--deselect`` (`#4127 `_). Test class instances are also not listed with ``--collect-only`` anymore. Features -------- - `#4270 `_: The ``cache_dir`` option uses ``$TOX_ENV_DIR`` as prefix (if set in the environment). This uses a different cache per tox environment by default. Bug Fixes --------- - `#3554 `_: Fix ``CallInfo.__repr__`` for when the call is not finished yet. pytest 3.10.1 (2018-11-11) ========================== Bug Fixes --------- - `#4287 `_: Fix nested usage of debugging plugin (pdb), e.g. with pytester's ``testdir.runpytest``. - `#4304 `_: Block the ``stepwise`` plugin if ``cacheprovider`` is also blocked, as one depends on the other. - `#4306 `_: Parse ``minversion`` as an actual version and not as dot-separated strings. - `#4310 `_: Fix duplicate collection due to multiple args matching the same packages. - `#4321 `_: Fix ``item.nodeid`` with resolved symlinks. - `#4325 `_: Fix collection of direct symlinked files, where the target does not match ``python_files``. - `#4329 `_: Fix TypeError in report_collect with _collect_report_last_write. Trivial/Internal Changes ------------------------ - `#4305 `_: Replace byte/unicode helpers in test_capture with python level syntax. pytest 3.10.0 (2018-11-03) ========================== Features -------- - `#2619 `_: Resume capturing output after ``continue`` with ``__import__("pdb").set_trace()``. This also adds a new ``pytest_leave_pdb`` hook, and passes in ``pdb`` to the existing ``pytest_enter_pdb`` hook. - `#4147 `_: Add ``--sw``, ``--stepwise`` as an alternative to ``--lf -x`` for stopping at the first failure, but starting the next test invocation from that test. See `the documentation `__ for more info. - `#4188 `_: Make ``--color`` emit colorful dots when not running in verbose mode. Earlier, it would only colorize the test-by-test output if ``--verbose`` was also passed. - `#4225 `_: Improve performance with collection reporting in non-quiet mode with terminals. The "collecting …" message is only printed/updated every 0.5s. Bug Fixes --------- - `#2701 `_: Fix false ``RemovedInPytest4Warning: usage of Session... is deprecated, please use pytest`` warnings. - `#4046 `_: Fix problems with running tests in package ``__init__.py`` files. - `#4260 `_: Swallow warnings during anonymous compilation of source. - `#4262 `_: Fix access denied error when deleting stale directories created by ``tmpdir`` / ``tmp_path``. - `#611 `_: Naming a fixture ``request`` will now raise a warning: the ``request`` fixture is internal and should not be overwritten as it will lead to internal errors. - `#4266 `_: Handle (ignore) exceptions raised during collection, e.g. with Django's LazySettings proxy class. Improved Documentation ---------------------- - `#4255 `_: Added missing documentation about the fact that module names passed to filter warnings are not regex-escaped. Trivial/Internal Changes ------------------------ - `#4272 `_: Display cachedir also in non-verbose mode if non-default. - `#4277 `_: pdb: improve message about output capturing with ``set_trace``. Do not display "IO-capturing turned off/on" when ``-s`` is used to avoid confusion. - `#4279 `_: Improve message and stack level of warnings issued by ``monkeypatch.setenv`` when the value of the environment variable is not a ``str``. pytest 3.9.3 (2018-10-27) ========================= Bug Fixes --------- - `#4174 `_: Fix "ValueError: Plugin already registered" with conftest plugins via symlink. - `#4181 `_: Handle race condition between creation and deletion of temporary folders. - `#4221 `_: Fix bug where the warning summary at the end of the test session was not showing the test where the warning was originated. - `#4243 `_: Fix regression when ``stacklevel`` for warnings was passed as positional argument on python2. Improved Documentation ---------------------- - `#3851 `_: Add reference to ``empty_parameter_set_mark`` ini option in documentation of ``@pytest.mark.parametrize`` Trivial/Internal Changes ------------------------ - `#4028 `_: Revert patching of ``sys.breakpointhook`` since it appears to do nothing. - `#4233 `_: Apply an import sorter (``reorder-python-imports``) to the codebase. - `#4248 `_: Remove use of unnecessary compat shim, six.binary_type pytest 3.9.2 (2018-10-22) ========================= Bug Fixes --------- - `#2909 `_: Improve error message when a recursive dependency between fixtures is detected. - `#3340 `_: Fix logging messages not shown in hooks ``pytest_sessionstart()`` and ``pytest_sessionfinish()``. - `#3533 `_: Fix unescaped XML raw objects in JUnit report for skipped tests - `#3691 `_: Python 2: safely format warning message about passing unicode strings to ``warnings.warn``, which may cause surprising ``MemoryError`` exception when monkey patching ``warnings.warn`` itself. - `#4026 `_: Improve error message when it is not possible to determine a function's signature. - `#4177 `_: Pin ``setuptools>=40.0`` to support ``py_modules`` in ``setup.cfg`` - `#4179 `_: Restore the tmpdir behaviour of symlinking the current test run. - `#4192 `_: Fix filename reported by ``warnings.warn`` when using ``recwarn`` under python2. pytest 3.9.1 (2018-10-16) ========================= Features -------- - `#4159 `_: For test-suites containing test classes, the information about the subclassed module is now output only if a higher verbosity level is specified (at least "-vv"). pytest 3.9.0 (2018-10-15 - not published due to a release automation bug) ========================================================================= Deprecations ------------ - `#3616 `_: The following accesses have been documented as deprecated for years, but are now actually emitting deprecation warnings. * Access of ``Module``, ``Function``, ``Class``, ``Instance``, ``File`` and ``Item`` through ``Node`` instances. Now users will this warning:: usage of Function.Module is deprecated, please use pytest.Module instead Users should just ``import pytest`` and access those objects using the ``pytest`` module. * ``request.cached_setup``, this was the precursor of the setup/teardown mechanism available to fixtures. You can consult `funcarg comparison section in the docs `_. * Using objects named ``"Class"`` as a way to customize the type of nodes that are collected in ``Collector`` subclasses has been deprecated. Users instead should use ``pytest_collect_make_item`` to customize node types during collection. This issue should affect only advanced plugins who create new collection types, so if you see this warning message please contact the authors so they can change the code. * The warning that produces the message below has changed to ``RemovedInPytest4Warning``:: getfuncargvalue is deprecated, use getfixturevalue - `#3988 `_: Add a Deprecation warning for pytest.ensuretemp as it was deprecated since a while. Features -------- - `#2293 `_: Improve usage errors messages by hiding internal details which can be distracting and noisy. This has the side effect that some error conditions that previously raised generic errors (such as ``ValueError`` for unregistered marks) are now raising ``Failed`` exceptions. - `#3332 `_: Improve the error displayed when a ``conftest.py`` file could not be imported. In order to implement this, a new ``chain`` parameter was added to ``ExceptionInfo.getrepr`` to show or hide chained tracebacks in Python 3 (defaults to ``True``). - `#3849 `_: Add ``empty_parameter_set_mark=fail_at_collect`` ini option for raising an exception when parametrize collects an empty set. - `#3964 `_: Log messages generated in the collection phase are shown when live-logging is enabled and/or when they are logged to a file. - `#3985 `_: Introduce ``tmp_path`` as a fixture providing a Path object. - `#4013 `_: Deprecation warnings are now shown even if you customize the warnings filters yourself. In the previous version any customization would override pytest's filters and deprecation warnings would fall back to being hidden by default. - `#4073 `_: Allow specification of timeout for ``Testdir.runpytest_subprocess()`` and ``Testdir.run()``. - `#4098 `_: Add returncode argument to pytest.exit() to exit pytest with a specific return code. - `#4102 `_: Reimplement ``pytest.deprecated_call`` using ``pytest.warns`` so it supports the ``match='...'`` keyword argument. This has the side effect that ``pytest.deprecated_call`` now raises ``pytest.fail.Exception`` instead of ``AssertionError``. - `#4149 `_: Require setuptools>=30.3 and move most of the metadata to ``setup.cfg``. Bug Fixes --------- - `#2535 `_: Improve error message when test functions of ``unittest.TestCase`` subclasses use a parametrized fixture. - `#3057 `_: ``request.fixturenames`` now correctly returns the name of fixtures created by ``request.getfixturevalue()``. - `#3946 `_: Warning filters passed as command line options using ``-W`` now take precedence over filters defined in ``ini`` configuration files. - `#4066 `_: Fix source reindenting by using ``textwrap.dedent`` directly. - `#4102 `_: ``pytest.warn`` will capture previously-warned warnings in Python 2. Previously they were never raised. - `#4108 `_: Resolve symbolic links for args. This fixes running ``pytest tests/test_foo.py::test_bar``, where ``tests`` is a symlink to ``project/app/tests``: previously ``project/app/conftest.py`` would be ignored for fixtures then. - `#4132 `_: Fix duplicate printing of internal errors when using ``--pdb``. - `#4135 `_: pathlib based tmpdir cleanup now correctly handles symlinks in the folder. - `#4152 `_: Display the filename when encountering ``SyntaxWarning``. Improved Documentation ---------------------- - `#3713 `_: Update usefixtures documentation to clarify that it can't be used with fixture functions. - `#4058 `_: Update fixture documentation to specify that a fixture can be invoked twice in the scope it's defined for. - `#4064 `_: According to unittest.rst, setUpModule and tearDownModule were not implemented, but it turns out they are. So updated the documentation for unittest. - `#4151 `_: Add tempir testing example to CONTRIBUTING.rst guide Trivial/Internal Changes ------------------------ - `#2293 `_: The internal ``MarkerError`` exception has been removed. - `#3988 `_: Port the implementation of tmpdir to pathlib. - `#4063 `_: Exclude 0.00 second entries from ``--duration`` output unless ``-vv`` is passed on the command-line. - `#4093 `_: Fixed formatting of string literals in internal tests. pytest 3.8.2 (2018-10-02) ========================= Deprecations and Removals ------------------------- - `#4036 `_: The ``item`` parameter of ``pytest_warning_captured`` hook is now documented as deprecated. We realized only after the ``3.8`` release that this parameter is incompatible with ``pytest-xdist``. Our policy is to not deprecate features during bugfix releases, but in this case we believe it makes sense as we are only documenting it as deprecated, without issuing warnings which might potentially break test suites. This will get the word out that hook implementers should not use this parameter at all. In a future release ``item`` will always be ``None`` and will emit a proper warning when a hook implementation makes use of it. Bug Fixes --------- - `#3539 `_: Fix reload on assertion rewritten modules. - `#4034 `_: The ``.user_properties`` attribute of ``TestReport`` objects is a list of (name, value) tuples, but could sometimes be instantiated as a tuple of tuples. It is now always a list. - `#4039 `_: No longer issue warnings about using ``pytest_plugins`` in non-top-level directories when using ``--pyargs``: the current ``--pyargs`` mechanism is not reliable and might give false negatives. - `#4040 `_: Exclude empty reports for passed tests when ``-rP`` option is used. - `#4051 `_: Improve error message when an invalid Python expression is passed to the ``-m`` option. - `#4056 `_: ``MonkeyPatch.setenv`` and ``MonkeyPatch.delenv`` issue a warning if the environment variable name is not ``str`` on Python 2. In Python 2, adding ``unicode`` keys to ``os.environ`` causes problems with ``subprocess`` (and possible other modules), making this a subtle bug specially susceptible when used with ``from __future__ import unicode_literals``. Improved Documentation ---------------------- - `#3928 `_: Add possible values for fixture scope to docs. pytest 3.8.1 (2018-09-22) ========================= Bug Fixes --------- - `#3286 `_: ``.pytest_cache`` directory is now automatically ignored by Git. Users who would like to contribute a solution for other SCMs please consult/comment on this issue. - `#3749 `_: Fix the following error during collection of tests inside packages:: TypeError: object of type 'Package' has no len() - `#3941 `_: Fix bug where indirect parametrization would consider the scope of all fixtures used by the test function to determine the parametrization scope, and not only the scope of the fixtures being parametrized. - `#3973 `_: Fix crash of the assertion rewriter if a test changed the current working directory without restoring it afterwards. - `#3998 `_: Fix issue that prevented some caplog properties (for example ``record_tuples``) from being available when entering the debugger with ``--pdb``. - `#3999 `_: Fix ``UnicodeDecodeError`` in python2.x when a class returns a non-ascii binary ``__repr__`` in an assertion which also contains non-ascii text. Improved Documentation ---------------------- - `#3996 `_: New `Deprecations and Removals `_ page shows all currently deprecated features, the rationale to do so, and alternatives to update your code. It also list features removed from pytest in past major releases to help those with ancient pytest versions to upgrade. Trivial/Internal Changes ------------------------ - `#3955 `_: Improve pre-commit detection for changelog filenames - `#3975 `_: Remove legacy code around im_func as that was python2 only pytest 3.8.0 (2018-09-05) ========================= Deprecations and Removals ------------------------- - `#2452 `_: ``Config.warn`` and ``Node.warn`` have been deprecated, see ``_ for rationale and examples. - `#3936 `_: ``@pytest.mark.filterwarnings`` second parameter is no longer regex-escaped, making it possible to actually use regular expressions to check the warning message. **Note**: regex-escaping the match string was an implementation oversight that might break test suites which depend on the old behavior. Features -------- - `#2452 `_: Internal pytest warnings are now issued using the standard ``warnings`` module, making it possible to use the standard warnings filters to manage those warnings. This introduces ``PytestWarning``, ``PytestDeprecationWarning`` and ``RemovedInPytest4Warning`` warning types as part of the public API. Consult `the documentation `__ for more info. - `#2908 `_: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are now shown by default if no other warning filter is configured. This makes pytest more compliant with `PEP-0506 `_. See `the docs `_ for more info. - `#3251 `_: Warnings are now captured and displayed during test collection. - `#3784 `_: ``PYTEST_DISABLE_PLUGIN_AUTOLOAD`` environment variable disables plugin auto-loading when set. - `#3829 `_: Added the ``count`` option to ``console_output_style`` to enable displaying the progress as a count instead of a percentage. - `#3837 `_: Added support for 'xfailed' and 'xpassed' outcomes to the ``pytester.RunResult.assert_outcomes`` signature. Bug Fixes --------- - `#3911 `_: Terminal writer now takes into account unicode character width when writing out progress. - `#3913 `_: Pytest now returns with correct exit code (EXIT_USAGEERROR, 4) when called with unknown arguments. - `#3918 `_: Improve performance of assertion rewriting. Improved Documentation ---------------------- - `#3566 `_: Added a blurb in usage.rst for the usage of -r flag which is used to show an extra test summary info. - `#3907 `_: Corrected type of the exceptions collection passed to ``xfail``: ``raises`` argument accepts a ``tuple`` instead of ``list``. Trivial/Internal Changes ------------------------ - `#3853 `_: Removed ``"run all (no recorded failures)"`` message printed with ``--failed-first`` and ``--last-failed`` when there are no failed tests. pytest 3.7.4 (2018-08-29) ========================= Bug Fixes --------- - `#3506 `_: Fix possible infinite recursion when writing ``.pyc`` files. - `#3853 `_: Cache plugin now obeys the ``-q`` flag when ``--last-failed`` and ``--failed-first`` flags are used. - `#3883 `_: Fix bad console output when using ``console_output_style=classic``. - `#3888 `_: Fix macOS specific code using ``capturemanager`` plugin in doctests. Improved Documentation ---------------------- - `#3902 `_: Fix pytest.org links pytest 3.7.3 (2018-08-26) ========================= Bug Fixes --------- - `#3033 `_: Fixtures during teardown can again use ``capsys`` and ``capfd`` to inspect output captured during tests. - `#3773 `_: Fix collection of tests from ``__init__.py`` files if they match the ``python_files`` configuration option. - `#3796 `_: Fix issue where teardown of fixtures of consecutive sub-packages were executed once, at the end of the outer package. - `#3816 `_: Fix bug where ``--show-capture=no`` option would still show logs printed during fixture teardown. - `#3819 `_: Fix ``stdout/stderr`` not getting captured when real-time cli logging is active. - `#3843 `_: Fix collection error when specifying test functions directly in the command line using ``test.py::test`` syntax together with ``--doctest-modules``. - `#3848 `_: Fix bugs where unicode arguments could not be passed to ``testdir.runpytest`` on Python 2. - `#3854 `_: Fix double collection of tests within packages when the filename starts with a capital letter. Improved Documentation ---------------------- - `#3824 `_: Added example for multiple glob pattern matches in ``python_files``. - `#3833 `_: Added missing docs for ``pytester.Testdir``. - `#3870 `_: Correct documentation for setuptools integration. Trivial/Internal Changes ------------------------ - `#3826 `_: Replace broken type annotations with type comments. - `#3845 `_: Remove a reference to issue `#568 `_ from the documentation, which has since been fixed. pytest 3.7.2 (2018-08-16) ========================= Bug Fixes --------- - `#3671 `_: Fix ``filterwarnings`` not being registered as a builtin mark. - `#3768 `_, `#3789 `_: Fix test collection from packages mixed with normal directories. - `#3771 `_: Fix infinite recursion during collection if a ``pytest_ignore_collect`` hook returns ``False`` instead of ``None``. - `#3774 `_: Fix bug where decorated fixtures would lose functionality (for example ``@mock.patch``). - `#3775 `_: Fix bug where importing modules or other objects with prefix ``pytest_`` prefix would raise a ``PluginValidationError``. - `#3788 `_: Fix ``AttributeError`` during teardown of ``TestCase`` subclasses which raise an exception during ``__init__``. - `#3804 `_: Fix traceback reporting for exceptions with ``__cause__`` cycles. Improved Documentation ---------------------- - `#3746 `_: Add documentation for ``metafunc.config`` that had been mistakenly hidden. pytest 3.7.1 (2018-08-02) ========================= Bug Fixes --------- - `#3473 `_: Raise immediately if ``approx()`` is given an expected value of a type it doesn't understand (e.g. strings, nested dicts, etc.). - `#3712 `_: Correctly represent the dimensions of a numpy array when calling ``repr()`` on ``approx()``. - `#3742 `_: Fix incompatibility with third party plugins during collection, which produced the error ``object has no attribute '_collectfile'``. - `#3745 `_: Display the absolute path if ``cache_dir`` is not relative to the ``rootdir`` instead of failing. - `#3747 `_: Fix compatibility problem with plugins and the warning code issued by fixture functions when they are called directly. - `#3748 `_: Fix infinite recursion in ``pytest.approx`` with arrays in ``numpy<1.13``. - `#3757 `_: Pin pathlib2 to ``>=2.2.0`` as we require ``__fspath__`` support. - `#3763 `_: Fix ``TypeError`` when the assertion message is ``bytes`` in python 3. pytest 3.7.0 (2018-07-30) ========================= Deprecations and Removals ------------------------- - `#2639 `_: ``pytest_namespace`` has been `deprecated `_. - `#3661 `_: Calling a fixture function directly, as opposed to request them in a test function, now issues a ``RemovedInPytest4Warning``. See `the documentation for rationale and examples `_. Features -------- - `#2283 `_: New ``package`` fixture scope: fixtures are finalized when the last test of a *package* finishes. This feature is considered **experimental**, so use it sparingly. - `#3576 `_: ``Node.add_marker`` now supports an ``append=True/False`` parameter to determine whether the mark comes last (default) or first. - `#3579 `_: Fixture ``caplog`` now has a ``messages`` property, providing convenient access to the format-interpolated log messages without the extra data provided by the formatter/handler. - `#3610 `_: New ``--trace`` option to enter the debugger at the start of a test. - `#3623 `_: Introduce ``pytester.copy_example`` as helper to do acceptance tests against examples from the project. Bug Fixes --------- - `#2220 `_: Fix a bug where fixtures overridden by direct parameters (for example parametrization) were being instantiated even if they were not being used by a test. - `#3695 `_: Fix ``ApproxNumpy`` initialisation argument mixup, ``abs`` and ``rel`` tolerances were flipped causing strange comparison results. Add tests to check ``abs`` and ``rel`` tolerances for ``np.array`` and test for expecting ``nan`` with ``np.array()`` - `#980 `_: Fix truncated locals output in verbose mode. Improved Documentation ---------------------- - `#3295 `_: Correct the usage documentation of ``--last-failed-no-failures`` by adding the missing ``--last-failed`` argument in the presented examples, because they are misleading and lead to think that the missing argument is not needed. Trivial/Internal Changes ------------------------ - `#3519 `_: Now a ``README.md`` file is created in ``.pytest_cache`` to make it clear why the directory exists. pytest 3.6.4 (2018-07-28) ========================= Bug Fixes --------- - Invoke pytest using ``-mpytest`` so ``sys.path`` does not get polluted by packages installed in ``site-packages``. (`#742 `_) Improved Documentation ---------------------- - Use ``smtp_connection`` instead of ``smtp`` in fixtures documentation to avoid possible confusion. (`#3592 `_) Trivial/Internal Changes ------------------------ - Remove obsolete ``__future__`` imports. (`#2319 `_) - Add CITATION to provide information on how to formally cite pytest. (`#3402 `_) - Replace broken type annotations with type comments. (`#3635 `_) - Pin ``pluggy`` to ``<0.8``. (`#3727 `_) pytest 3.6.3 (2018-07-04) ========================= Bug Fixes --------- - Fix ``ImportWarning`` triggered by explicit relative imports in assertion-rewritten package modules. (`#3061 `_) - Fix error in ``pytest.approx`` when dealing with 0-dimension numpy arrays. (`#3593 `_) - No longer raise ``ValueError`` when using the ``get_marker`` API. (`#3605 `_) - Fix problem where log messages with non-ascii characters would not appear in the output log file. (`#3630 `_) - No longer raise ``AttributeError`` when legacy marks can't be stored in functions. (`#3631 `_) Improved Documentation ---------------------- - The description above the example for ``@pytest.mark.skipif`` now better matches the code. (`#3611 `_) Trivial/Internal Changes ------------------------ - Internal refactoring: removed unused ``CallSpec2tox ._globalid_args`` attribute and ``metafunc`` parameter from ``CallSpec2.copy()``. (`#3598 `_) - Silence usage of ``reduce`` warning in Python 2 (`#3609 `_) - Fix usage of ``attr.ib`` deprecated ``convert`` parameter. (`#3653 `_) pytest 3.6.2 (2018-06-20) ========================= Bug Fixes --------- - Fix regression in ``Node.add_marker`` by extracting the mark object of a ``MarkDecorator``. (`#3555 `_) - Warnings without ``location`` were reported as ``None``. This is corrected to now report ````. (`#3563 `_) - Continue to call finalizers in the stack when a finalizer in a former scope raises an exception. (`#3569 `_) - Fix encoding error with ``print`` statements in doctests (`#3583 `_) Improved Documentation ---------------------- - Add documentation for the ``--strict`` flag. (`#3549 `_) Trivial/Internal Changes ------------------------ - Update old quotation style to parens in fixture.rst documentation. (`#3525 `_) - Improve display of hint about ``--fulltrace`` with ``KeyboardInterrupt``. (`#3545 `_) - pytest's testsuite is no longer runnable through ``python setup.py test`` -- instead invoke ``pytest`` or ``tox`` directly. (`#3552 `_) - Fix typo in documentation (`#3567 `_) pytest 3.6.1 (2018-06-05) ========================= Bug Fixes --------- - Fixed a bug where stdout and stderr were logged twice by junitxml when a test was marked xfail. (`#3491 `_) - Fix ``usefixtures`` mark applyed to unittest tests by correctly instantiating ``FixtureInfo``. (`#3498 `_) - Fix assertion rewriter compatibility with libraries that monkey patch ``file`` objects. (`#3503 `_) Improved Documentation ---------------------- - Added a section on how to use fixtures as factories to the fixture documentation. (`#3461 `_) Trivial/Internal Changes ------------------------ - Enable caching for pip/pre-commit in order to reduce build time on travis/appveyor. (`#3502 `_) - Switch pytest to the src/ layout as we already suggested it for good practice - now we implement it as well. (`#3513 `_) - Fix if in tests to support 3.7.0b5, where a docstring handling in AST got reverted. (`#3530 `_) - Remove some python2.5 compatibility code. (`#3529 `_) pytest 3.6.0 (2018-05-23) ========================= Features -------- - Revamp the internals of the ``pytest.mark`` implementation with correct per node handling which fixes a number of long standing bugs caused by the old design. This introduces new ``Node.iter_markers(name)`` and ``Node.get_closest_marker(name)`` APIs. Users are **strongly encouraged** to read the `reasons for the revamp in the docs `_, or jump over to details about `updating existing code to use the new APIs `_. (`#3317 `_) - Now when ``@pytest.fixture`` is applied more than once to the same function a ``ValueError`` is raised. This buggy behavior would cause surprising problems and if was working for a test suite it was mostly by accident. (`#2334 `_) - Support for Python 3.7's builtin ``breakpoint()`` method, see `Using the builtin breakpoint function `_ for details. (`#3180 `_) - ``monkeypatch`` now supports a ``context()`` function which acts as a context manager which undoes all patching done within the ``with`` block. (`#3290 `_) - The ``--pdb`` option now causes KeyboardInterrupt to enter the debugger, instead of stopping the test session. On python 2.7, hitting CTRL+C again exits the debugger. On python 3.2 and higher, use CTRL+D. (`#3299 `_) - pytest no longer changes the log level of the root logger when the ``log-level`` parameter has greater numeric value than that of the level of the root logger, which makes it play better with custom logging configuration in user code. (`#3307 `_) Bug Fixes --------- - A rare race-condition which might result in corrupted ``.pyc`` files on Windows has been hopefully solved. (`#3008 `_) - Also use iter_marker for discovering the marks applying for marker expressions from the cli to avoid the bad data from the legacy mark storage. (`#3441 `_) - When showing diffs of failed assertions where the contents contain only whitespace, escape them using ``repr()`` first to make it easy to spot the differences. (`#3443 `_) Improved Documentation ---------------------- - Change documentation copyright year to a range which auto-updates itself each time it is published. (`#3303 `_) Trivial/Internal Changes ------------------------ - ``pytest`` now depends on the `python-atomicwrites `_ library. (`#3008 `_) - Update all pypi.python.org URLs to pypi.org. (`#3431 `_) - Detect `pytest_` prefixed hooks using the internal plugin manager since ``pluggy`` is deprecating the ``implprefix`` argument to ``PluginManager``. (`#3487 `_) - Import ``Mapping`` and ``Sequence`` from ``_pytest.compat`` instead of directly from ``collections`` in ``python_api.py::approx``. Add ``Mapping`` to ``_pytest.compat``, import it from ``collections`` on python 2, but from ``collections.abc`` on Python 3 to avoid a ``DeprecationWarning`` on Python 3.7 or newer. (`#3497 `_) pytest 3.5.1 (2018-04-23) ========================= Bug Fixes --------- - Reset ``sys.last_type``, ``sys.last_value`` and ``sys.last_traceback`` before each test executes. Those attributes are added by pytest during the test run to aid debugging, but were never reset so they would create a leaking reference to the last failing test's frame which in turn could never be reclaimed by the garbage collector. (`#2798 `_) - ``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword argument. (`#3348 `_) - ``pytest.raises`` now works with exception classes that look like iterables. (`#3372 `_) Improved Documentation ---------------------- - Fix typo in ``caplog`` fixture documentation, which incorrectly identified certain attributes as methods. (`#3406 `_) Trivial/Internal Changes ------------------------ - Added a more indicative error message when parametrizing a function whose argument takes a default value. (`#3221 `_) - Remove internal ``_pytest.terminal.flatten`` function in favor of ``more_itertools.collapse``. (`#3330 `_) - Import some modules from ``collections.abc`` instead of ``collections`` as the former modules trigger ``DeprecationWarning`` in Python 3.7. (`#3339 `_) - record_property is no longer experimental, removing the warnings was forgotten. (`#3360 `_) - Mention in documentation and CLI help that fixtures with leading ``_`` are printed by ``pytest --fixtures`` only if the ``-v`` option is added. (`#3398 `_) pytest 3.5.0 (2018-03-21) ========================= Deprecations and Removals ------------------------- - ``record_xml_property`` fixture is now deprecated in favor of the more generic ``record_property``. (`#2770 `_) - Defining ``pytest_plugins`` is now deprecated in non-top-level conftest.py files, because they "leak" to the entire directory tree. `See the docs `_ for the rationale behind this decision (`#3084 `_) Features -------- - New ``--show-capture`` command-line option that allows to specify how to display captured output when tests fail: ``no``, ``stdout``, ``stderr``, ``log`` or ``all`` (the default). (`#1478 `_) - New ``--rootdir`` command-line option to override the rules for discovering the root directory. See `customize `_ in the documentation for details. (`#1642 `_) - Fixtures are now instantiated based on their scopes, with higher-scoped fixtures (such as ``session``) being instantiated first than lower-scoped fixtures (such as ``function``). The relative order of fixtures of the same scope is kept unchanged, based in their declaration order and their dependencies. (`#2405 `_) - ``record_xml_property`` renamed to ``record_property`` and is now compatible with xdist, markers and any reporter. ``record_xml_property`` name is now deprecated. (`#2770 `_) - New ``--nf``, ``--new-first`` options: run new tests first followed by the rest of the tests, in both cases tests are also sorted by the file modified time, with more recent files coming first. (`#3034 `_) - New ``--last-failed-no-failures`` command-line option that allows to specify the behavior of the cache plugin's ```--last-failed`` feature when no tests failed in the last run (or no cache was found): ``none`` or ``all`` (the default). (`#3139 `_) - New ``--doctest-continue-on-failure`` command-line option to enable doctests to show multiple failures for each snippet, instead of stopping at the first failure. (`#3149 `_) - Captured log messages are added to the ```` tag in the generated junit xml file if the ``junit_logging`` ini option is set to ``system-out``. If the value of this ini option is ``system-err``, the logs are written to ````. The default value for ``junit_logging`` is ``no``, meaning captured logs are not written to the output file. (`#3156 `_) - Allow the logging plugin to handle ``pytest_runtest_logstart`` and ``pytest_runtest_logfinish`` hooks when live logs are enabled. (`#3189 `_) - Passing ``--log-cli-level`` in the command-line now automatically activates live logging. (`#3190 `_) - Add command line option ``--deselect`` to allow deselection of individual tests at collection time. (`#3198 `_) - Captured logs are printed before entering pdb. (`#3204 `_) - Deselected item count is now shown before tests are run, e.g. ``collected X items / Y deselected``. (`#3213 `_) - The builtin module ``platform`` is now available for use in expressions in ``pytest.mark``. (`#3236 `_) - The *short test summary info* section now is displayed after tracebacks and warnings in the terminal. (`#3255 `_) - New ``--verbosity`` flag to set verbosity level explicitly. (`#3296 `_) - ``pytest.approx`` now accepts comparing a numpy array with a scalar. (`#3312 `_) Bug Fixes --------- - Suppress ``IOError`` when closing the temporary file used for capturing streams in Python 2.7. (`#2370 `_) - Fixed ``clear()`` method on ``caplog`` fixture which cleared ``records``, but not the ``text`` property. (`#3297 `_) - During test collection, when stdin is not allowed to be read, the ``DontReadFromStdin`` object still allow itself to be iterable and resolved to an iterator without crashing. (`#3314 `_) Improved Documentation ---------------------- - Added a `reference `_ page to the docs. (`#1713 `_) Trivial/Internal Changes ------------------------ - Change minimum requirement of ``attrs`` to ``17.4.0``. (`#3228 `_) - Renamed example directories so all tests pass when ran from the base directory. (`#3245 `_) - Internal ``mark.py`` module has been turned into a package. (`#3250 `_) - ``pytest`` now depends on the `more-itertools `_ package. (`#3265 `_) - Added warning when ``[pytest]`` section is used in a ``.cfg`` file passed with ``-c`` (`#3268 `_) - ``nodeids`` can now be passed explicitly to ``FSCollector`` and ``Node`` constructors. (`#3291 `_) - Internal refactoring of ``FormattedExcinfo`` to use ``attrs`` facilities and remove old support code for legacy Python versions. (`#3292 `_) - Refactoring to unify how verbosity is handled internally. (`#3296 `_) - Internal refactoring to better integrate with argparse. (`#3304 `_) - Fix a python example when calling a fixture in doc/en/usage.rst (`#3308 `_) pytest 3.4.2 (2018-03-04) ========================= Bug Fixes --------- - Removed progress information when capture option is ``no``. (`#3203 `_) - Refactor check of bindir from ``exists`` to ``isdir``. (`#3241 `_) - Fix ``TypeError`` issue when using ``approx`` with a ``Decimal`` value. (`#3247 `_) - Fix reference cycle generated when using the ``request`` fixture. (`#3249 `_) - ``[tool:pytest]`` sections in ``*.cfg`` files passed by the ``-c`` option are now properly recognized. (`#3260 `_) Improved Documentation ---------------------- - Add logging plugin to plugins list. (`#3209 `_) Trivial/Internal Changes ------------------------ - Fix minor typo in fixture.rst (`#3259 `_) pytest 3.4.1 (2018-02-20) ========================= Bug Fixes --------- - Move import of ``doctest.UnexpectedException`` to top-level to avoid possible errors when using ``--pdb``. (`#1810 `_) - Added printing of captured stdout/stderr before entering pdb, and improved a test which was giving false negatives about output capturing. (`#3052 `_) - Fix ordering of tests using parametrized fixtures which can lead to fixtures being created more than necessary. (`#3161 `_) - Fix bug where logging happening at hooks outside of "test run" hooks would cause an internal error. (`#3184 `_) - Detect arguments injected by ``unittest.mock.patch`` decorator correctly when pypi ``mock.patch`` is installed and imported. (`#3206 `_) - Errors shown when a ``pytest.raises()`` with ``match=`` fails are now cleaner on what happened: When no exception was raised, the "matching '...'" part got removed as it falsely implies that an exception was raised but it didn't match. When a wrong exception was raised, it's now thrown (like ``pytest.raised()`` without ``match=`` would) instead of complaining about the unmatched text. (`#3222 `_) - Fixed output capture handling in doctests on macOS. (`#985 `_) Improved Documentation ---------------------- - Add Sphinx parameter docs for ``match`` and ``message`` args to ``pytest.raises``. (`#3202 `_) Trivial/Internal Changes ------------------------ - pytest has changed the publication procedure and is now being published to PyPI directly from Travis. (`#3060 `_) - Rename ``ParameterSet._for_parameterize()`` to ``_for_parametrize()`` in order to comply with the naming convention. (`#3166 `_) - Skip failing pdb/doctest test on mac. (`#985 `_) pytest 3.4.0 (2018-01-30) ========================= Deprecations and Removals ------------------------- - All pytest classes now subclass ``object`` for better Python 2/3 compatibility. This should not affect user code except in very rare edge cases. (`#2147 `_) Features -------- - Introduce ``empty_parameter_set_mark`` ini option to select which mark to apply when ``@pytest.mark.parametrize`` is given an empty set of parameters. Valid options are ``skip`` (default) and ``xfail``. Note that it is planned to change the default to ``xfail`` in future releases as this is considered less error prone. (`#2527 `_) - **Incompatible change**: after community feedback the `logging `_ functionality has undergone some changes. Please consult the `logging documentation `_ for details. (`#3013 `_) - Console output falls back to "classic" mode when capturing is disabled (``-s``), otherwise the output gets garbled to the point of being useless. (`#3038 `_) - New `pytest_runtest_logfinish `_ hook which is called when a test item has finished executing, analogous to `pytest_runtest_logstart `_. (`#3101 `_) - Improve performance when collecting tests using many fixtures. (`#3107 `_) - New ``caplog.get_records(when)`` method which provides access to the captured records for the ``"setup"``, ``"call"`` and ``"teardown"`` testing stages. (`#3117 `_) - New fixture ``record_xml_attribute`` that allows modifying and inserting attributes on the ```` xml node in JUnit reports. (`#3130 `_) - The default cache directory has been renamed from ``.cache`` to ``.pytest_cache`` after community feedback that the name ``.cache`` did not make it clear that it was used by pytest. (`#3138 `_) - Colorize the levelname column in the live-log output. (`#3142 `_) Bug Fixes --------- - Fix hanging pexpect test on MacOS by using flush() instead of wait(). (`#2022 `_) - Fix restoring Python state after in-process pytest runs with the ``pytester`` plugin; this may break tests using multiple inprocess pytest runs if later ones depend on earlier ones leaking global interpreter changes. (`#3016 `_) - Fix skipping plugin reporting hook when test aborted before plugin setup hook. (`#3074 `_) - Fix progress percentage reported when tests fail during teardown. (`#3088 `_) - **Incompatible change**: ``-o/--override`` option no longer eats all the remaining options, which can lead to surprising behavior: for example, ``pytest -o foo=1 /path/to/test.py`` would fail because ``/path/to/test.py`` would be considered as part of the ``-o`` command-line argument. One consequence of this is that now multiple configuration overrides need multiple ``-o`` flags: ``pytest -o foo=1 -o bar=2``. (`#3103 `_) Improved Documentation ---------------------- - Document hooks (defined with ``historic=True``) which cannot be used with ``hookwrapper=True``. (`#2423 `_) - Clarify that warning capturing doesn't change the warning filter by default. (`#2457 `_) - Clarify a possible confusion when using pytest_fixture_setup with fixture functions that return None. (`#2698 `_) - Fix the wording of a sentence on doctest flags used in pytest. (`#3076 `_) - Prefer ``https://*.readthedocs.io`` over ``http://*.rtfd.org`` for links in the documentation. (`#3092 `_) - Improve readability (wording, grammar) of Getting Started guide (`#3131 `_) - Added note that calling pytest.main multiple times from the same process is not recommended because of import caching. (`#3143 `_) Trivial/Internal Changes ------------------------ - Show a simple and easy error when keyword expressions trigger a syntax error (for example, ``"-k foo and import"`` will show an error that you can not use the ``import`` keyword in expressions). (`#2953 `_) - Change parametrized automatic test id generation to use the ``__name__`` attribute of functions instead of the fallback argument name plus counter. (`#2976 `_) - Replace py.std with stdlib imports. (`#3067 `_) - Corrected 'you' to 'your' in logging docs. (`#3129 `_) pytest 3.3.2 (2017-12-25) ========================= Bug Fixes --------- - pytester: ignore files used to obtain current user metadata in the fd leak detector. (`#2784 `_) - Fix **memory leak** where objects returned by fixtures were never destructed by the garbage collector. (`#2981 `_) - Fix conversion of pyargs to filename to not convert symlinks on Python 2. (`#2985 `_) - ``PYTEST_DONT_REWRITE`` is now checked for plugins too rather than only for test modules. (`#2995 `_) Improved Documentation ---------------------- - Add clarifying note about behavior of multiple parametrized arguments (`#3001 `_) Trivial/Internal Changes ------------------------ - Code cleanup. (`#3015 `_, `#3021 `_) - Clean up code by replacing imports and references of ``_ast`` to ``ast``. (`#3018 `_) pytest 3.3.1 (2017-12-05) ========================= Bug Fixes --------- - Fix issue about ``-p no:`` having no effect. (`#2920 `_) - Fix regression with warnings that contained non-strings in their arguments in Python 2. (`#2956 `_) - Always escape null bytes when setting ``PYTEST_CURRENT_TEST``. (`#2957 `_) - Fix ``ZeroDivisionError`` when using the ``testmon`` plugin when no tests were actually collected. (`#2971 `_) - Bring back ``TerminalReporter.writer`` as an alias to ``TerminalReporter._tw``. This alias was removed by accident in the ``3.3.0`` release. (`#2984 `_) - The ``pytest-capturelog`` plugin is now also blacklisted, avoiding errors when running pytest with it still installed. (`#3004 `_) Improved Documentation ---------------------- - Fix broken link to plugin ``pytest-localserver``. (`#2963 `_) Trivial/Internal Changes ------------------------ - Update github "bugs" link in ``CONTRIBUTING.rst`` (`#2949 `_) pytest 3.3.0 (2017-11-23) ========================= Deprecations and Removals ------------------------- - pytest no longer supports Python **2.6** and **3.3**. Those Python versions are EOL for some time now and incur maintenance and compatibility costs on the pytest core team, and following up with the rest of the community we decided that they will no longer be supported starting on this version. Users which still require those versions should pin pytest to ``<3.3``. (`#2812 `_) - Remove internal ``_preloadplugins()`` function. This removal is part of the ``pytest_namespace()`` hook deprecation. (`#2636 `_) - Internally change ``CallSpec2`` to have a list of marks instead of a broken mapping of keywords. This removes the keywords attribute of the internal ``CallSpec2`` class. (`#2672 `_) - Remove ParameterSet.deprecated_arg_dict - its not a public api and the lack of the underscore was a naming error. (`#2675 `_) - Remove the internal multi-typed attribute ``Node._evalskip`` and replace it with the boolean ``Node._skipped_by_mark``. (`#2767 `_) - The ``params`` list passed to ``pytest.fixture`` is now for all effects considered immutable and frozen at the moment of the ``pytest.fixture`` call. Previously the list could be changed before the first invocation of the fixture allowing for a form of dynamic parametrization (for example, updated from command-line options), but this was an unwanted implementation detail which complicated the internals and prevented some internal cleanup. See issue `#2959 `_ for details and a recommended workaround. Features -------- - ``pytest_fixture_post_finalizer`` hook can now receive a ``request`` argument. (`#2124 `_) - Replace the old introspection code in compat.py that determines the available arguments of fixtures with inspect.signature on Python 3 and funcsigs.signature on Python 2. This should respect ``__signature__`` declarations on functions. (`#2267 `_) - Report tests with global ``pytestmark`` variable only once. (`#2549 `_) - Now pytest displays the total progress percentage while running tests. The previous output style can be set by configuring the ``console_output_style`` setting to ``classic``. (`#2657 `_) - Match ``warns`` signature to ``raises`` by adding ``match`` keyword. (`#2708 `_) - pytest now captures and displays output from the standard ``logging`` module. The user can control the logging level to be captured by specifying options in ``pytest.ini``, the command line and also during individual tests using markers. Also, a ``caplog`` fixture is available that enables users to test the captured log during specific tests (similar to ``capsys`` for example). For more information, please see the `logging docs `_. This feature was introduced by merging the popular `pytest-catchlog `_ plugin, thanks to `Thomas Hisch `_. Be advised that during the merging the backward compatibility interface with the defunct ``pytest-capturelog`` has been dropped. (`#2794 `_) - Add ``allow_module_level`` kwarg to ``pytest.skip()``, enabling to skip the whole module. (`#2808 `_) - Allow setting ``file_or_dir``, ``-c``, and ``-o`` in PYTEST_ADDOPTS. (`#2824 `_) - Return stdout/stderr capture results as a ``namedtuple``, so ``out`` and ``err`` can be accessed by attribute. (`#2879 `_) - Add ``capfdbinary``, a version of ``capfd`` which returns bytes from ``readouterr()``. (`#2923 `_) - Add ``capsysbinary`` a version of ``capsys`` which returns bytes from ``readouterr()``. (`#2934 `_) - Implement feature to skip ``setup.py`` files when run with ``--doctest-modules``. (`#502 `_) Bug Fixes --------- - Resume output capturing after ``capsys/capfd.disabled()`` context manager. (`#1993 `_) - ``pytest_fixture_setup`` and ``pytest_fixture_post_finalizer`` hooks are now called for all ``conftest.py`` files. (`#2124 `_) - If an exception happens while loading a plugin, pytest no longer hides the original traceback. In Python 2 it will show the original traceback with a new message that explains in which plugin. In Python 3 it will show 2 canonized exceptions, the original exception while loading the plugin in addition to an exception that pytest throws about loading a plugin. (`#2491 `_) - ``capsys`` and ``capfd`` can now be used by other fixtures. (`#2709 `_) - Internal ``pytester`` plugin properly encodes ``bytes`` arguments to ``utf-8``. (`#2738 `_) - ``testdir`` now uses use the same method used by ``tmpdir`` to create its temporary directory. This changes the final structure of the ``testdir`` directory slightly, but should not affect usage in normal scenarios and avoids a number of potential problems. (`#2751 `_) - pytest no longer complains about warnings with unicode messages being non-ascii compatible even for ascii-compatible messages. As a result of this, warnings with unicode messages are converted first to an ascii representation for safety. (`#2809 `_) - Change return value of pytest command when ``--maxfail`` is reached from ``2`` (interrupted) to ``1`` (failed). (`#2845 `_) - Fix issue in assertion rewriting which could lead it to rewrite modules which should not be rewritten. (`#2939 `_) - Handle marks without description in ``pytest.ini``. (`#2942 `_) Trivial/Internal Changes ------------------------ - pytest now depends on `attrs `__ for internal structures to ease code maintainability. (`#2641 `_) - Refactored internal Python 2/3 compatibility code to use ``six``. (`#2642 `_) - Stop vendoring ``pluggy`` - we're missing out on its latest changes for not much benefit (`#2719 `_) - Internal refactor: simplify ascii string escaping by using the backslashreplace error handler in newer Python 3 versions. (`#2734 `_) - Remove unnecessary mark evaluator in unittest plugin (`#2767 `_) - Calls to ``Metafunc.addcall`` now emit a deprecation warning. This function is scheduled to be removed in ``pytest-4.0``. (`#2876 `_) - Internal move of the parameterset extraction to a more maintainable place. (`#2877 `_) - Internal refactoring to simplify scope node lookup. (`#2910 `_) - Configure ``pytest`` to prevent pip from installing pytest in unsupported Python versions. (`#2922 `_) pytest 3.2.5 (2017-11-15) ========================= Bug Fixes --------- - Remove ``py<1.5`` restriction from ``pytest`` as this can cause version conflicts in some installations. (`#2926 `_) pytest 3.2.4 (2017-11-13) ========================= Bug Fixes --------- - Fix the bug where running with ``--pyargs`` will result in items with empty ``parent.nodeid`` if run from a different root directory. (`#2775 `_) - Fix issue with ``@pytest.parametrize`` if argnames was specified as keyword arguments. (`#2819 `_) - Strip whitespace from marker names when reading them from INI config. (`#2856 `_) - Show full context of doctest source in the pytest output, if the line number of failed example in the docstring is < 9. (`#2882 `_) - Match fixture paths against actual path segments in order to avoid matching folders which share a prefix. (`#2836 `_) Improved Documentation ---------------------- - Introduce a dedicated section about conftest.py. (`#1505 `_) - Explicitly mention ``xpass`` in the documentation of ``xfail``. (`#1997 `_) - Append example for pytest.param in the example/parametrize document. (`#2658 `_) - Clarify language of proposal for fixtures parameters (`#2893 `_) - List python 3.6 in the documented supported versions in the getting started document. (`#2903 `_) - Clarify the documentation of available fixture scopes. (`#538 `_) - Add documentation about the ``python -m pytest`` invocation adding the current directory to sys.path. (`#911 `_) pytest 3.2.3 (2017-10-03) ========================= Bug Fixes --------- - Fix crash in tab completion when no prefix is given. (`#2748 `_) - The equality checking function (``__eq__``) of ``MarkDecorator`` returns ``False`` if one object is not an instance of ``MarkDecorator``. (`#2758 `_) - When running ``pytest --fixtures-per-test``: don't crash if an item has no _fixtureinfo attribute (e.g. doctests) (`#2788 `_) Improved Documentation ---------------------- - In help text of ``-k`` option, add example of using ``not`` to not select certain tests whose names match the provided expression. (`#1442 `_) - Add note in ``parametrize.rst`` about calling ``metafunc.parametrize`` multiple times. (`#1548 `_) Trivial/Internal Changes ------------------------ - Set ``xfail_strict=True`` in pytest's own test suite to catch expected failures as soon as they start to pass. (`#2722 `_) - Fix typo in example of passing a callable to markers (in example/markers.rst) (`#2765 `_) pytest 3.2.2 (2017-09-06) ========================= Bug Fixes --------- - Calling the deprecated ``request.getfuncargvalue()`` now shows the source of the call. (`#2681 `_) - Allow tests declared as ``@staticmethod`` to use fixtures. (`#2699 `_) - Fixed edge-case during collection: attributes which raised ``pytest.fail`` when accessed would abort the entire collection. (`#2707 `_) - Fix ``ReprFuncArgs`` with mixed unicode and UTF-8 args. (`#2731 `_) Improved Documentation ---------------------- - In examples on working with custom markers, add examples demonstrating the usage of ``pytest.mark.MARKER_NAME.with_args`` in comparison with ``pytest.mark.MARKER_NAME.__call__`` (`#2604 `_) - In one of the simple examples, use ``pytest_collection_modifyitems()`` to skip tests based on a command-line option, allowing its sharing while preventing a user error when acessing ``pytest.config`` before the argument parsing. (`#2653 `_) Trivial/Internal Changes ------------------------ - Fixed minor error in 'Good Practices/Manual Integration' code snippet. (`#2691 `_) - Fixed typo in goodpractices.rst. (`#2721 `_) - Improve user guidance regarding ``--resultlog`` deprecation. (`#2739 `_) pytest 3.2.1 (2017-08-08) ========================= Bug Fixes --------- - Fixed small terminal glitch when collecting a single test item. (`#2579 `_) - Correctly consider ``/`` as the file separator to automatically mark plugin files for rewrite on Windows. (`#2591 `_) - Properly escape test names when setting ``PYTEST_CURRENT_TEST`` environment variable. (`#2644 `_) - Fix error on Windows and Python 3.6+ when ``sys.stdout`` has been replaced with a stream-like object which does not implement the full ``io`` module buffer protocol. In particular this affects ``pytest-xdist`` users on the aforementioned platform. (`#2666 `_) Improved Documentation ---------------------- - Explicitly document which pytest features work with ``unittest``. (`#2626 `_) pytest 3.2.0 (2017-07-30) ========================= Deprecations and Removals ------------------------- - ``pytest.approx`` no longer supports ``>``, ``>=``, ``<`` and ``<=`` operators to avoid surprising/inconsistent behavior. See `the approx docs `_ for more information. (`#2003 `_) - All old-style specific behavior in current classes in the pytest's API is considered deprecated at this point and will be removed in a future release. This affects Python 2 users only and in rare situations. (`#2147 `_) - A deprecation warning is now raised when using marks for parameters in ``pytest.mark.parametrize``. Use ``pytest.param`` to apply marks to parameters instead. (`#2427 `_) Features -------- - Add support for numpy arrays (and dicts) to approx. (`#1994 `_) - Now test function objects have a ``pytestmark`` attribute containing a list of marks applied directly to the test function, as opposed to marks inherited from parent classes or modules. (`#2516 `_) - Collection ignores local virtualenvs by default; ``--collect-in-virtualenv`` overrides this behavior. (`#2518 `_) - Allow class methods decorated as ``@staticmethod`` to be candidates for collection as a test function. (Only for Python 2.7 and above. Python 2.6 will still ignore static methods.) (`#2528 `_) - Introduce ``mark.with_args`` in order to allow passing functions/classes as sole argument to marks. (`#2540 `_) - New ``cache_dir`` ini option: sets the directory where the contents of the cache plugin are stored. Directory may be relative or absolute path: if relative path, then directory is created relative to ``rootdir``, otherwise it is used as is. Additionally path may contain environment variables which are expanded during runtime. (`#2543 `_) - Introduce the ``PYTEST_CURRENT_TEST`` environment variable that is set with the ``nodeid`` and stage (``setup``, ``call`` and ``teardown``) of the test being currently executed. See the `documentation `_ for more info. (`#2583 `_) - Introduced ``@pytest.mark.filterwarnings`` mark which allows overwriting the warnings filter on a per test, class or module level. See the `docs `_ for more information. (`#2598 `_) - ``--last-failed`` now remembers forever when a test has failed and only forgets it if it passes again. This makes it easy to fix a test suite by selectively running files and fixing tests incrementally. (`#2621 `_) - New ``pytest_report_collectionfinish`` hook which allows plugins to add messages to the terminal reporting after collection has been finished successfully. (`#2622 `_) - Added support for `PEP-415's `_ ``Exception.__suppress_context__``. Now if a ``raise exception from None`` is caught by pytest, pytest will no longer chain the context in the test report. The behavior now matches Python's traceback behavior. (`#2631 `_) - Exceptions raised by ``pytest.fail``, ``pytest.skip`` and ``pytest.xfail`` now subclass BaseException, making them harder to be caught unintentionally by normal code. (`#580 `_) Bug Fixes --------- - Set ``stdin`` to a closed ``PIPE`` in ``pytester.py.Testdir.popen()`` for avoid unwanted interactive ``pdb`` (`#2023 `_) - Add missing ``encoding`` attribute to ``sys.std*`` streams when using ``capsys`` capture mode. (`#2375 `_) - Fix terminal color changing to black on Windows if ``colorama`` is imported in a ``conftest.py`` file. (`#2510 `_) - Fix line number when reporting summary of skipped tests. (`#2548 `_) - capture: ensure that EncodedFile.name is a string. (`#2555 `_) - The options ``--fixtures`` and ``--fixtures-per-test`` will now keep indentation within docstrings. (`#2574 `_) - doctests line numbers are now reported correctly, fixing `pytest-sugar#122 `_. (`#2610 `_) - Fix non-determinism in order of fixture collection. Adds new dependency (ordereddict) for Python 2.6. (`#920 `_) Improved Documentation ---------------------- - Clarify ``pytest_configure`` hook call order. (`#2539 `_) - Extend documentation for testing plugin code with the ``pytester`` plugin. (`#971 `_) Trivial/Internal Changes ------------------------ - Update help message for ``--strict`` to make it clear it only deals with unregistered markers, not warnings. (`#2444 `_) - Internal code move: move code for pytest.approx/pytest.raises to own files in order to cut down the size of python.py (`#2489 `_) - Renamed the utility function ``_pytest.compat._escape_strings`` to ``_ascii_escaped`` to better communicate the function's purpose. (`#2533 `_) - Improve error message for CollectError with skip/skipif. (`#2546 `_) - Emit warning about ``yield`` tests being deprecated only once per generator. (`#2562 `_) - Ensure final collected line doesn't include artifacts of previous write. (`#2571 `_) - Fixed all flake8 errors and warnings. (`#2581 `_) - Added ``fix-lint`` tox environment to run automatic pep8 fixes on the code. (`#2582 `_) - Turn warnings into errors in pytest's own test suite in order to catch regressions due to deprecations more promptly. (`#2588 `_) - Show multiple issue links in CHANGELOG entries. (`#2620 `_) pytest 3.1.3 (2017-07-03) ========================= Bug Fixes --------- - Fix decode error in Python 2 for doctests in docstrings. (`#2434 `_) - Exceptions raised during teardown by finalizers are now suppressed until all finalizers are called, with the initial exception reraised. (`#2440 `_) - Fix incorrect "collected items" report when specifying tests on the command- line. (`#2464 `_) - ``deprecated_call`` in context-manager form now captures deprecation warnings even if the same warning has already been raised. Also, ``deprecated_call`` will always produce the same error message (previously it would produce different messages in context-manager vs. function-call mode). (`#2469 `_) - Fix issue where paths collected by pytest could have triple leading ``/`` characters. (`#2475 `_) - Fix internal error when trying to detect the start of a recursive traceback. (`#2486 `_) Improved Documentation ---------------------- - Explicitly state for which hooks the calls stop after the first non-None result. (`#2493 `_) Trivial/Internal Changes ------------------------ - Create invoke tasks for updating the vendored packages. (`#2474 `_) - Update copyright dates in LICENSE, README.rst and in the documentation. (`#2499 `_) pytest 3.1.2 (2017-06-08) ========================= Bug Fixes --------- - Required options added via ``pytest_addoption`` will no longer prevent using --help without passing them. (#1999) - Respect ``python_files`` in assertion rewriting. (#2121) - Fix recursion error detection when frames in the traceback contain objects that can't be compared (like ``numpy`` arrays). (#2459) - ``UnicodeWarning`` is issued from the internal pytest warnings plugin only when the message contains non-ascii unicode (Python 2 only). (#2463) - Added a workaround for Python 3.6 ``WindowsConsoleIO`` breaking due to Pytests's ``FDCapture``. Other code using console handles might still be affected by the very same issue and might require further workarounds/fixes, i.e. ``colorama``. (#2467) Improved Documentation ---------------------- - Fix internal API links to ``pluggy`` objects. (#2331) - Make it clear that ``pytest.xfail`` stops test execution at the calling point and improve overall flow of the ``skipping`` docs. (#810) pytest 3.1.1 (2017-05-30) ========================= Bug Fixes --------- - pytest warning capture no longer overrides existing warning filters. The previous behaviour would override all filters and caused regressions in test suites which configure warning filters to match their needs. Note that as a side-effect of this is that ``DeprecationWarning`` and ``PendingDeprecationWarning`` are no longer shown by default. (#2430) - Fix issue with non-ascii contents in doctest text files. (#2434) - Fix encoding errors for unicode warnings in Python 2. (#2436) - ``pytest.deprecated_call`` now captures ``PendingDeprecationWarning`` in context manager form. (#2441) Improved Documentation ---------------------- - Addition of towncrier for changelog management. (#2390) 3.1.0 (2017-05-22) ================== New Features ------------ * The ``pytest-warnings`` plugin has been integrated into the core and now ``pytest`` automatically captures and displays warnings at the end of the test session. .. warning:: This feature may disrupt test suites which apply and treat warnings themselves, and can be disabled in your ``pytest.ini``: .. code-block:: ini [pytest] addopts = -p no:warnings See the `warnings documentation page `_ for more information. Thanks `@nicoddemus`_ for the PR. * Added ``junit_suite_name`` ini option to specify root ```` name for JUnit XML reports (`#533`_). * Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files. Thanks `@wheerd`_ for the PR (`#2101`_). * ``pytest.warns`` now checks for subclass relationship rather than class equality. Thanks `@lesteve`_ for the PR (`#2166`_) * ``pytest.raises`` now asserts that the error message matches a text or regex with the ``match`` keyword argument. Thanks `@Kriechi`_ for the PR. * ``pytest.param`` can be used to declare test parameter sets with marks and test ids. Thanks `@RonnyPfannschmidt`_ for the PR. Changes ------- * remove all internal uses of pytest_namespace hooks, this is to prepare the removal of preloadconfig in pytest 4.0 Thanks to `@RonnyPfannschmidt`_ for the PR. * pytest now warns when a callable ids raises in a parametrized test. Thanks `@fogo`_ for the PR. * It is now possible to skip test classes from being collected by setting a ``__test__`` attribute to ``False`` in the class body (`#2007`_). Thanks to `@syre`_ for the report and `@lwm`_ for the PR. * Change junitxml.py to produce reports that comply with Junitxml schema. If the same test fails with failure in call and then errors in teardown we split testcase element into two, one containing the error and the other the failure. (`#2228`_) Thanks to `@kkoukiou`_ for the PR. * Testcase reports with a ``url`` attribute will now properly write this to junitxml. Thanks `@fushi`_ for the PR (`#1874`_). * Remove common items from dict comparison output when verbosity=1. Also update the truncation message to make it clearer that pytest truncates all assertion messages if verbosity < 2 (`#1512`_). Thanks `@mattduck`_ for the PR * ``--pdbcls`` no longer implies ``--pdb``. This makes it possible to use ``addopts=--pdbcls=module.SomeClass`` on ``pytest.ini``. Thanks `@davidszotten`_ for the PR (`#1952`_). * fix `#2013`_: turn RecordedWarning into ``namedtuple``, to give it a comprehensible repr while preventing unwarranted modification. * fix `#2208`_: ensure an iteration limit for _pytest.compat.get_real_func. Thanks `@RonnyPfannschmidt`_ for the report and PR. * Hooks are now verified after collection is complete, rather than right after loading installed plugins. This makes it easy to write hooks for plugins which will be loaded during collection, for example using the ``pytest_plugins`` special variable (`#1821`_). Thanks `@nicoddemus`_ for the PR. * Modify ``pytest_make_parametrize_id()`` hook to accept ``argname`` as an additional parameter. Thanks `@unsignedint`_ for the PR. * Add ``venv`` to the default ``norecursedirs`` setting. Thanks `@The-Compiler`_ for the PR. * ``PluginManager.import_plugin`` now accepts unicode plugin names in Python 2. Thanks `@reutsharabani`_ for the PR. * fix `#2308`_: When using both ``--lf`` and ``--ff``, only the last failed tests are run. Thanks `@ojii`_ for the PR. * Replace minor/patch level version numbers in the documentation with placeholders. This significantly reduces change-noise as different contributors regnerate the documentation on different platforms. Thanks `@RonnyPfannschmidt`_ for the PR. * fix `#2391`_: consider pytest_plugins on all plugin modules Thanks `@RonnyPfannschmidt`_ for the PR. Bug Fixes --------- * Fix ``AttributeError`` on ``sys.stdout.buffer`` / ``sys.stderr.buffer`` while using ``capsys`` fixture in python 3. (`#1407`_). Thanks to `@asottile`_. * Change capture.py's ``DontReadFromInput`` class to throw ``io.UnsupportedOperation`` errors rather than ValueErrors in the ``fileno`` method (`#2276`_). Thanks `@metasyn`_ and `@vlad-dragos`_ for the PR. * Fix exception formatting while importing modules when the exception message contains non-ascii characters (`#2336`_). Thanks `@fabioz`_ for the report and `@nicoddemus`_ for the PR. * Added documentation related to issue (`#1937`_) Thanks `@skylarjhdownes`_ for the PR. * Allow collecting files with any file extension as Python modules (`#2369`_). Thanks `@Kodiologist`_ for the PR. * Show the correct error message when collect "parametrize" func with wrong args (`#2383`_). Thanks `@The-Compiler`_ for the report and `@robin0371`_ for the PR. .. _@davidszotten: https://github.com/davidszotten .. _@fabioz: https://github.com/fabioz .. _@fogo: https://github.com/fogo .. _@fushi: https://github.com/fushi .. _@Kodiologist: https://github.com/Kodiologist .. _@Kriechi: https://github.com/Kriechi .. _@mandeep: https://github.com/mandeep .. _@mattduck: https://github.com/mattduck .. _@metasyn: https://github.com/metasyn .. _@MichalTHEDUDE: https://github.com/MichalTHEDUDE .. _@ojii: https://github.com/ojii .. _@reutsharabani: https://github.com/reutsharabani .. _@robin0371: https://github.com/robin0371 .. _@skylarjhdownes: https://github.com/skylarjhdownes .. _@unsignedint: https://github.com/unsignedint .. _@wheerd: https://github.com/wheerd .. _#1407: https://github.com/pytest-dev/pytest/issues/1407 .. _#1512: https://github.com/pytest-dev/pytest/issues/1512 .. _#1821: https://github.com/pytest-dev/pytest/issues/1821 .. _#1874: https://github.com/pytest-dev/pytest/pull/1874 .. _#1937: https://github.com/pytest-dev/pytest/issues/1937 .. _#1952: https://github.com/pytest-dev/pytest/pull/1952 .. _#2007: https://github.com/pytest-dev/pytest/issues/2007 .. _#2013: https://github.com/pytest-dev/pytest/issues/2013 .. _#2101: https://github.com/pytest-dev/pytest/pull/2101 .. _#2166: https://github.com/pytest-dev/pytest/pull/2166 .. _#2208: https://github.com/pytest-dev/pytest/issues/2208 .. _#2228: https://github.com/pytest-dev/pytest/issues/2228 .. _#2276: https://github.com/pytest-dev/pytest/issues/2276 .. _#2308: https://github.com/pytest-dev/pytest/issues/2308 .. _#2336: https://github.com/pytest-dev/pytest/issues/2336 .. _#2369: https://github.com/pytest-dev/pytest/issues/2369 .. _#2383: https://github.com/pytest-dev/pytest/issues/2383 .. _#2391: https://github.com/pytest-dev/pytest/issues/2391 .. _#533: https://github.com/pytest-dev/pytest/issues/533 3.0.7 (2017-03-14) ================== * Fix issue in assertion rewriting breaking due to modules silently discarding other modules when importing fails Notably, importing the ``anydbm`` module is fixed. (`#2248`_). Thanks `@pfhayes`_ for the PR. * junitxml: Fix problematic case where system-out tag occurred twice per testcase element in the XML report. Thanks `@kkoukiou`_ for the PR. * Fix regression, pytest now skips unittest correctly if run with ``--pdb`` (`#2137`_). Thanks to `@gst`_ for the report and `@mbyt`_ for the PR. * Ignore exceptions raised from descriptors (e.g. properties) during Python test collection (`#2234`_). Thanks to `@bluetech`_. * ``--override-ini`` now correctly overrides some fundamental options like ``python_files`` (`#2238`_). Thanks `@sirex`_ for the report and `@nicoddemus`_ for the PR. * Replace ``raise StopIteration`` usages in the code by simple ``returns`` to finish generators, in accordance to `PEP-479`_ (`#2160`_). Thanks `@tgoodlet`_ for the report and `@nicoddemus`_ for the PR. * Fix internal errors when an unprintable ``AssertionError`` is raised inside a test. Thanks `@omerhadari`_ for the PR. * Skipping plugin now also works with test items generated by custom collectors (`#2231`_). Thanks to `@vidartf`_. * Fix trailing whitespace in console output if no .ini file presented (`#2281`_). Thanks `@fbjorn`_ for the PR. * Conditionless ``xfail`` markers no longer rely on the underlying test item being an instance of ``PyobjMixin``, and can therefore apply to tests not collected by the built-in python test collector. Thanks `@barneygale`_ for the PR. .. _@pfhayes: https://github.com/pfhayes .. _@bluetech: https://github.com/bluetech .. _@gst: https://github.com/gst .. _@sirex: https://github.com/sirex .. _@vidartf: https://github.com/vidartf .. _@kkoukiou: https://github.com/KKoukiou .. _@omerhadari: https://github.com/omerhadari .. _@fbjorn: https://github.com/fbjorn .. _#2248: https://github.com/pytest-dev/pytest/issues/2248 .. _#2137: https://github.com/pytest-dev/pytest/issues/2137 .. _#2160: https://github.com/pytest-dev/pytest/issues/2160 .. _#2231: https://github.com/pytest-dev/pytest/issues/2231 .. _#2234: https://github.com/pytest-dev/pytest/issues/2234 .. _#2238: https://github.com/pytest-dev/pytest/issues/2238 .. _#2281: https://github.com/pytest-dev/pytest/issues/2281 .. _PEP-479: https://www.python.org/dev/peps/pep-0479/ 3.0.6 (2017-01-22) ================== * pytest no longer generates ``PendingDeprecationWarning`` from its own operations, which was introduced by mistake in version ``3.0.5`` (`#2118`_). Thanks to `@nicoddemus`_ for the report and `@RonnyPfannschmidt`_ for the PR. * pytest no longer recognizes coroutine functions as yield tests (`#2129`_). Thanks to `@malinoff`_ for the PR. * Plugins loaded by the ``PYTEST_PLUGINS`` environment variable are now automatically considered for assertion rewriting (`#2185`_). Thanks `@nicoddemus`_ for the PR. * Improve error message when pytest.warns fails (`#2150`_). The type(s) of the expected warnings and the list of caught warnings is added to the error message. Thanks `@lesteve`_ for the PR. * Fix ``pytester`` internal plugin to work correctly with latest versions of ``zope.interface`` (`#1989`_). Thanks `@nicoddemus`_ for the PR. * Assert statements of the ``pytester`` plugin again benefit from assertion rewriting (`#1920`_). Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR. * Specifying tests with colons like ``test_foo.py::test_bar`` for tests in subdirectories with ini configuration files now uses the correct ini file (`#2148`_). Thanks `@pelme`_. * Fail ``testdir.runpytest().assert_outcomes()`` explicitly if the pytest terminal output it relies on is missing. Thanks to `@eli-b`_ for the PR. .. _@barneygale: https://github.com/barneygale .. _@lesteve: https://github.com/lesteve .. _@malinoff: https://github.com/malinoff .. _@pelme: https://github.com/pelme .. _@eli-b: https://github.com/eli-b .. _#2118: https://github.com/pytest-dev/pytest/issues/2118 .. _#1989: https://github.com/pytest-dev/pytest/issues/1989 .. _#1920: https://github.com/pytest-dev/pytest/issues/1920 .. _#2129: https://github.com/pytest-dev/pytest/issues/2129 .. _#2148: https://github.com/pytest-dev/pytest/issues/2148 .. _#2150: https://github.com/pytest-dev/pytest/issues/2150 .. _#2185: https://github.com/pytest-dev/pytest/issues/2185 3.0.5 (2016-12-05) ================== * Add warning when not passing ``option=value`` correctly to ``-o/--override-ini`` (`#2105`_). Also improved the help documentation. Thanks to `@mbukatov`_ for the report and `@lwm`_ for the PR. * Now ``--confcutdir`` and ``--junit-xml`` are properly validated if they are directories and filenames, respectively (`#2089`_ and `#2078`_). Thanks to `@lwm`_ for the PR. * Add hint to error message hinting possible missing ``__init__.py`` (`#478`_). Thanks `@DuncanBetts`_. * More accurately describe when fixture finalization occurs in documentation (`#687`_). Thanks `@DuncanBetts`_. * Provide ``:ref:`` targets for ``recwarn.rst`` so we can use intersphinx referencing. Thanks to `@dupuy`_ for the report and `@lwm`_ for the PR. * In Python 2, use a simple ``+-`` ASCII string in the string representation of ``pytest.approx`` (for example ``"4 +- 4.0e-06"``) because it is brittle to handle that in different contexts and representations internally in pytest which can result in bugs such as `#2111`_. In Python 3, the representation still uses ``±`` (for example ``4 ± 4.0e-06``). Thanks `@kerrick-lyft`_ for the report and `@nicoddemus`_ for the PR. * Using ``item.Function``, ``item.Module``, etc., is now issuing deprecation warnings, prefer ``pytest.Function``, ``pytest.Module``, etc., instead (`#2034`_). Thanks `@nmundar`_ for the PR. * Fix error message using ``approx`` with complex numbers (`#2082`_). Thanks `@adler-j`_ for the report and `@nicoddemus`_ for the PR. * Fixed false-positives warnings from assertion rewrite hook for modules imported more than once by the ``pytest_plugins`` mechanism. Thanks `@nicoddemus`_ for the PR. * Remove an internal cache which could cause hooks from ``conftest.py`` files in sub-directories to be called in other directories incorrectly (`#2016`_). Thanks `@d-b-w`_ for the report and `@nicoddemus`_ for the PR. * Remove internal code meant to support earlier Python 3 versions that produced the side effect of leaving ``None`` in ``sys.modules`` when expressions were evaluated by pytest (for example passing a condition as a string to ``pytest.mark.skipif``)(`#2103`_). Thanks `@jaraco`_ for the report and `@nicoddemus`_ for the PR. * Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks `@nedbat`_. .. _@syre: https://github.com/syre .. _@adler-j: https://github.com/adler-j .. _@d-b-w: https://bitbucket.org/d-b-w/ .. _@DuncanBetts: https://github.com/DuncanBetts .. _@dupuy: https://bitbucket.org/dupuy/ .. _@kerrick-lyft: https://github.com/kerrick-lyft .. _@lwm: https://github.com/lwm .. _@mbukatov: https://github.com/mbukatov .. _@nedbat: https://github.com/nedbat .. _@nmundar: https://github.com/nmundar .. _#2016: https://github.com/pytest-dev/pytest/issues/2016 .. _#2034: https://github.com/pytest-dev/pytest/issues/2034 .. _#2038: https://github.com/pytest-dev/pytest/issues/2038 .. _#2078: https://github.com/pytest-dev/pytest/issues/2078 .. _#2082: https://github.com/pytest-dev/pytest/issues/2082 .. _#2089: https://github.com/pytest-dev/pytest/issues/2089 .. _#2103: https://github.com/pytest-dev/pytest/issues/2103 .. _#2105: https://github.com/pytest-dev/pytest/issues/2105 .. _#2111: https://github.com/pytest-dev/pytest/issues/2111 .. _#478: https://github.com/pytest-dev/pytest/issues/478 .. _#687: https://github.com/pytest-dev/pytest/issues/687 3.0.4 (2016-11-09) ================== * Import errors when collecting test modules now display the full traceback (`#1976`_). Thanks `@cwitty`_ for the report and `@nicoddemus`_ for the PR. * Fix confusing command-line help message for custom options with two or more ``metavar`` properties (`#2004`_). Thanks `@okulynyak`_ and `@davehunt`_ for the report and `@nicoddemus`_ for the PR. * When loading plugins, import errors which contain non-ascii messages are now properly handled in Python 2 (`#1998`_). Thanks `@nicoddemus`_ for the PR. * Fixed cyclic reference when ``pytest.raises`` is used in context-manager form (`#1965`_). Also as a result of this fix, ``sys.exc_info()`` is left empty in both context-manager and function call usages. Previously, ``sys.exc_info`` would contain the exception caught by the context manager, even when the expected exception occurred. Thanks `@MSeifert04`_ for the report and the PR. * Fixed false-positives warnings from assertion rewrite hook for modules that were rewritten but were later marked explicitly by ``pytest.register_assert_rewrite`` or implicitly as a plugin (`#2005`_). Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR. * Report teardown output on test failure (`#442`_). Thanks `@matclab`_ for the PR. * Fix teardown error message in generated xUnit XML. Thanks `@gdyuldin`_ for the PR. * Properly handle exceptions in ``multiprocessing`` tasks (`#1984`_). Thanks `@adborden`_ for the report and `@nicoddemus`_ for the PR. * Clean up unittest TestCase objects after tests are complete (`#1649`_). Thanks `@d_b_w`_ for the report and PR. .. _@adborden: https://github.com/adborden .. _@cwitty: https://github.com/cwitty .. _@d_b_w: https://github.com/d_b_w .. _@gdyuldin: https://github.com/gdyuldin .. _@matclab: https://github.com/matclab .. _@MSeifert04: https://github.com/MSeifert04 .. _@okulynyak: https://github.com/okulynyak .. _#442: https://github.com/pytest-dev/pytest/issues/442 .. _#1965: https://github.com/pytest-dev/pytest/issues/1965 .. _#1976: https://github.com/pytest-dev/pytest/issues/1976 .. _#1984: https://github.com/pytest-dev/pytest/issues/1984 .. _#1998: https://github.com/pytest-dev/pytest/issues/1998 .. _#2004: https://github.com/pytest-dev/pytest/issues/2004 .. _#2005: https://github.com/pytest-dev/pytest/issues/2005 .. _#1649: https://github.com/pytest-dev/pytest/issues/1649 3.0.3 (2016-09-28) ================== * The ``ids`` argument to ``parametrize`` again accepts ``unicode`` strings in Python 2 (`#1905`_). Thanks `@philpep`_ for the report and `@nicoddemus`_ for the PR. * Assertions are now being rewritten for plugins in development mode (``pip install -e``) (`#1934`_). Thanks `@nicoddemus`_ for the PR. * Fix pkg_resources import error in Jython projects (`#1853`_). Thanks `@raquel-ucl`_ for the PR. * Got rid of ``AttributeError: 'Module' object has no attribute '_obj'`` exception in Python 3 (`#1944`_). Thanks `@axil`_ for the PR. * Explain a bad scope value passed to ``@fixture`` declarations or a ``MetaFunc.parametrize()`` call. Thanks `@tgoodlet`_ for the PR. * This version includes ``pluggy-0.4.0``, which correctly handles ``VersionConflict`` errors in plugins (`#704`_). Thanks `@nicoddemus`_ for the PR. .. _@philpep: https://github.com/philpep .. _@raquel-ucl: https://github.com/raquel-ucl .. _@axil: https://github.com/axil .. _@tgoodlet: https://github.com/tgoodlet .. _@vlad-dragos: https://github.com/vlad-dragos .. _#1853: https://github.com/pytest-dev/pytest/issues/1853 .. _#1905: https://github.com/pytest-dev/pytest/issues/1905 .. _#1934: https://github.com/pytest-dev/pytest/issues/1934 .. _#1944: https://github.com/pytest-dev/pytest/issues/1944 .. _#704: https://github.com/pytest-dev/pytest/issues/704 3.0.2 (2016-09-01) ================== * Improve error message when passing non-string ids to ``pytest.mark.parametrize`` (`#1857`_). Thanks `@okken`_ for the report and `@nicoddemus`_ for the PR. * Add ``buffer`` attribute to stdin stub class ``pytest.capture.DontReadFromInput`` Thanks `@joguSD`_ for the PR. * Fix ``UnicodeEncodeError`` when string comparison with unicode has failed. (`#1864`_) Thanks `@AiOO`_ for the PR. * ``pytest_plugins`` is now handled correctly if defined as a string (as opposed as a sequence of strings) when modules are considered for assertion rewriting. Due to this bug, much more modules were being rewritten than necessary if a test suite uses ``pytest_plugins`` to load internal plugins (`#1888`_). Thanks `@jaraco`_ for the report and `@nicoddemus`_ for the PR (`#1891`_). * Do not call tearDown and cleanups when running tests from ``unittest.TestCase`` subclasses with ``--pdb`` enabled. This allows proper post mortem debugging for all applications which have significant logic in their tearDown machinery (`#1890`_). Thanks `@mbyt`_ for the PR. * Fix use of deprecated ``getfuncargvalue`` method in the internal doctest plugin. Thanks `@ViviCoder`_ for the report (`#1898`_). .. _@joguSD: https://github.com/joguSD .. _@AiOO: https://github.com/AiOO .. _@mbyt: https://github.com/mbyt .. _@ViviCoder: https://github.com/ViviCoder .. _#1857: https://github.com/pytest-dev/pytest/issues/1857 .. _#1864: https://github.com/pytest-dev/pytest/issues/1864 .. _#1888: https://github.com/pytest-dev/pytest/issues/1888 .. _#1891: https://github.com/pytest-dev/pytest/pull/1891 .. _#1890: https://github.com/pytest-dev/pytest/issues/1890 .. _#1898: https://github.com/pytest-dev/pytest/issues/1898 3.0.1 (2016-08-23) ================== * Fix regression when ``importorskip`` is used at module level (`#1822`_). Thanks `@jaraco`_ and `@The-Compiler`_ for the report and `@nicoddemus`_ for the PR. * Fix parametrization scope when session fixtures are used in conjunction with normal parameters in the same call (`#1832`_). Thanks `@The-Compiler`_ for the report, `@Kingdread`_ and `@nicoddemus`_ for the PR. * Fix internal error when parametrizing tests or fixtures using an empty ``ids`` argument (`#1849`_). Thanks `@OPpuolitaival`_ for the report and `@nicoddemus`_ for the PR. * Fix loader error when running ``pytest`` embedded in a zipfile. Thanks `@mbachry`_ for the PR. .. _@Kingdread: https://github.com/Kingdread .. _@mbachry: https://github.com/mbachry .. _@OPpuolitaival: https://github.com/OPpuolitaival .. _#1822: https://github.com/pytest-dev/pytest/issues/1822 .. _#1832: https://github.com/pytest-dev/pytest/issues/1832 .. _#1849: https://github.com/pytest-dev/pytest/issues/1849 3.0.0 (2016-08-18) ================== **Incompatible changes** A number of incompatible changes were made in this release, with the intent of removing features deprecated for a long time or change existing behaviors in order to make them less surprising/more useful. * Reinterpretation mode has now been removed. Only plain and rewrite mode are available, consequently the ``--assert=reinterp`` option is no longer available. This also means files imported from plugins or ``conftest.py`` will not benefit from improved assertions by default, you should use ``pytest.register_assert_rewrite()`` to explicitly turn on assertion rewriting for those files. Thanks `@flub`_ for the PR. * The following deprecated commandline options were removed: * ``--genscript``: no longer supported; * ``--no-assert``: use ``--assert=plain`` instead; * ``--nomagic``: use ``--assert=plain`` instead; * ``--report``: use ``-r`` instead; Thanks to `@RedBeardCode`_ for the PR (`#1664`_). * ImportErrors in plugins now are a fatal error instead of issuing a pytest warning (`#1479`_). Thanks to `@The-Compiler`_ for the PR. * Removed support code for Python 3 versions < 3.3 (`#1627`_). * Removed all ``py.test-X*`` entry points. The versioned, suffixed entry points were never documented and a leftover from a pre-virtualenv era. These entry points also created broken entry points in wheels, so removing them also removes a source of confusion for users (`#1632`_). Thanks `@obestwalter`_ for the PR. * ``pytest.skip()`` now raises an error when used to decorate a test function, as opposed to its original intent (to imperatively skip a test inside a test function). Previously this usage would cause the entire module to be skipped (`#607`_). Thanks `@omarkohl`_ for the complete PR (`#1519`_). * Exit tests if a collection error occurs. A poll indicated most users will hit CTRL-C anyway as soon as they see collection errors, so pytest might as well make that the default behavior (`#1421`_). A ``--continue-on-collection-errors`` option has been added to restore the previous behaviour. Thanks `@olegpidsadnyi`_ and `@omarkohl`_ for the complete PR (`#1628`_). * Renamed the pytest ``pdb`` module (plugin) into ``debugging`` to avoid clashes with the builtin ``pdb`` module. * Raise a helpful failure message when requesting a parametrized fixture at runtime, e.g. with ``request.getfixturevalue``. Previously these parameters were simply never defined, so a fixture decorated like ``@pytest.fixture(params=[0, 1, 2])`` only ran once (`#460`_). Thanks to `@nikratio`_ for the bug report, `@RedBeardCode`_ and `@tomviner`_ for the PR. * ``_pytest.monkeypatch.monkeypatch`` class has been renamed to ``_pytest.monkeypatch.MonkeyPatch`` so it doesn't conflict with the ``monkeypatch`` fixture. * ``--exitfirst / -x`` can now be overridden by a following ``--maxfail=N`` and is just a synonym for ``--maxfail=1``. **New Features** * Support nose-style ``__test__`` attribute on methods of classes, including unittest-style Classes. If set to ``False``, the test will not be collected. * New ``doctest_namespace`` fixture for injecting names into the namespace in which doctests run. Thanks `@milliams`_ for the complete PR (`#1428`_). * New ``--doctest-report`` option available to change the output format of diffs when running (failing) doctests (implements `#1749`_). Thanks `@hartym`_ for the PR. * New ``name`` argument to ``pytest.fixture`` decorator which allows a custom name for a fixture (to solve the funcarg-shadowing-fixture problem). Thanks `@novas0x2a`_ for the complete PR (`#1444`_). * New ``approx()`` function for easily comparing floating-point numbers in tests. Thanks `@kalekundert`_ for the complete PR (`#1441`_). * Ability to add global properties in the final xunit output file by accessing the internal ``junitxml`` plugin (experimental). Thanks `@tareqalayan`_ for the complete PR `#1454`_). * New ``ExceptionInfo.match()`` method to match a regular expression on the string representation of an exception (`#372`_). Thanks `@omarkohl`_ for the complete PR (`#1502`_). * ``__tracebackhide__`` can now also be set to a callable which then can decide whether to filter the traceback based on the ``ExceptionInfo`` object passed to it. Thanks `@The-Compiler`_ for the complete PR (`#1526`_). * New ``pytest_make_parametrize_id(config, val)`` hook which can be used by plugins to provide friendly strings for custom types. Thanks `@palaviv`_ for the PR. * ``capsys`` and ``capfd`` now have a ``disabled()`` context-manager method, which can be used to temporarily disable capture within a test. Thanks `@nicoddemus`_ for the PR. * New cli flag ``--fixtures-per-test``: shows which fixtures are being used for each selected test item. Features doc strings of fixtures by default. Can also show where fixtures are defined if combined with ``-v``. Thanks `@hackebrot`_ for the PR. * Introduce ``pytest`` command as recommended entry point. Note that ``py.test`` still works and is not scheduled for removal. Closes proposal `#1629`_. Thanks `@obestwalter`_ and `@davehunt`_ for the complete PR (`#1633`_). * New cli flags: + ``--setup-plan``: performs normal collection and reports the potential setup and teardown and does not execute any fixtures and tests; + ``--setup-only``: performs normal collection, executes setup and teardown of fixtures and reports them; + ``--setup-show``: performs normal test execution and additionally shows setup and teardown of fixtures; + ``--keep-duplicates``: py.test now ignores duplicated paths given in the command line. To retain the previous behavior where the same test could be run multiple times by specifying it in the command-line multiple times, pass the ``--keep-duplicates`` argument (`#1609`_); Thanks `@d6e`_, `@kvas-it`_, `@sallner`_, `@ioggstream`_ and `@omarkohl`_ for the PRs. * New CLI flag ``--override-ini``/``-o``: overrides values from the ini file. For example: ``"-o xfail_strict=True"``'. Thanks `@blueyed`_ and `@fengxx`_ for the PR. * New hooks: + ``pytest_fixture_setup(fixturedef, request)``: executes fixture setup; + ``pytest_fixture_post_finalizer(fixturedef)``: called after the fixture's finalizer and has access to the fixture's result cache. Thanks `@d6e`_, `@sallner`_. * Issue warnings for asserts whose test is a tuple literal. Such asserts will never fail because tuples are always truthy and are usually a mistake (see `#1562`_). Thanks `@kvas-it`_, for the PR. * Allow passing a custom debugger class (e.g. ``--pdbcls=IPython.core.debugger:Pdb``). Thanks to `@anntzer`_ for the PR. **Changes** * Plugins now benefit from assertion rewriting. Thanks `@sober7`_, `@nicoddemus`_ and `@flub`_ for the PR. * Change ``report.outcome`` for ``xpassed`` tests to ``"passed"`` in non-strict mode and ``"failed"`` in strict mode. Thanks to `@hackebrot`_ for the PR (`#1795`_) and `@gprasad84`_ for report (`#1546`_). * Tests marked with ``xfail(strict=False)`` (the default) now appear in JUnitXML reports as passing tests instead of skipped. Thanks to `@hackebrot`_ for the PR (`#1795`_). * Highlight path of the file location in the error report to make it easier to copy/paste. Thanks `@suzaku`_ for the PR (`#1778`_). * Fixtures marked with ``@pytest.fixture`` can now use ``yield`` statements exactly like those marked with the ``@pytest.yield_fixture`` decorator. This change renders ``@pytest.yield_fixture`` deprecated and makes ``@pytest.fixture`` with ``yield`` statements the preferred way to write teardown code (`#1461`_). Thanks `@csaftoiu`_ for bringing this to attention and `@nicoddemus`_ for the PR. * Explicitly passed parametrize ids do not get escaped to ascii (`#1351`_). Thanks `@ceridwen`_ for the PR. * Fixtures are now sorted in the error message displayed when an unknown fixture is declared in a test function. Thanks `@nicoddemus`_ for the PR. * ``pytest_terminal_summary`` hook now receives the ``exitstatus`` of the test session as argument. Thanks `@blueyed`_ for the PR (`#1809`_). * Parametrize ids can accept ``None`` as specific test id, in which case the automatically generated id for that argument will be used. Thanks `@palaviv`_ for the complete PR (`#1468`_). * The parameter to xunit-style setup/teardown methods (``setup_method``, ``setup_module``, etc.) is now optional and may be omitted. Thanks `@okken`_ for bringing this to attention and `@nicoddemus`_ for the PR. * Improved automatic id generation selection in case of duplicate ids in parametrize. Thanks `@palaviv`_ for the complete PR (`#1474`_). * Now pytest warnings summary is shown up by default. Added a new flag ``--disable-pytest-warnings`` to explicitly disable the warnings summary (`#1668`_). * Make ImportError during collection more explicit by reminding the user to check the name of the test module/package(s) (`#1426`_). Thanks `@omarkohl`_ for the complete PR (`#1520`_). * Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). * ``pytest.raises`` in the context manager form accepts a custom ``message`` to raise when no exception occurred. Thanks `@palaviv`_ for the complete PR (`#1616`_). * ``conftest.py`` files now benefit from assertion rewriting; previously it was only available for test modules. Thanks `@flub`_, `@sober7`_ and `@nicoddemus`_ for the PR (`#1619`_). * Text documents without any doctests no longer appear as "skipped". Thanks `@graingert`_ for reporting and providing a full PR (`#1580`_). * Ensure that a module within a namespace package can be found when it is specified on the command line together with the ``--pyargs`` option. Thanks to `@taschini`_ for the PR (`#1597`_). * Always include full assertion explanation during assertion rewriting. The previous behaviour was hiding sub-expressions that happened to be ``False``, assuming this was redundant information. Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and `@tomviner`_ for the PR. * ``OptionGroup.addoption()`` now checks if option names were already added before, to make it easier to track down issues like `#1618`_. Before, you only got exceptions later from ``argparse`` library, giving no clue about the actual reason for double-added options. * ``yield``-based tests are considered deprecated and will be removed in pytest-4.0. Thanks `@nicoddemus`_ for the PR. * ``[pytest]`` sections in ``setup.cfg`` files should now be named ``[tool:pytest]`` to avoid conflicts with other distutils commands (see `#567`_). ``[pytest]`` sections in ``pytest.ini`` or ``tox.ini`` files are supported and unchanged. Thanks `@nicoddemus`_ for the PR. * Using ``pytest_funcarg__`` prefix to declare fixtures is considered deprecated and will be removed in pytest-4.0 (`#1684`_). Thanks `@nicoddemus`_ for the PR. * Passing a command-line string to ``pytest.main()`` is considered deprecated and scheduled for removal in pytest-4.0. It is recommended to pass a list of arguments instead (`#1723`_). * Rename ``getfuncargvalue`` to ``getfixturevalue``. ``getfuncargvalue`` is still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_ for the PR (`#1626`_). * ``optparse`` type usage now triggers DeprecationWarnings (`#1740`_). * ``optparse`` backward compatibility supports float/complex types (`#457`_). * Refined logic for determining the ``rootdir``, considering only valid paths which fixes a number of issues: `#1594`_, `#1435`_ and `#1471`_. Updated the documentation according to current behavior. Thanks to `@blueyed`_, `@davehunt`_ and `@matthiasha`_ for the PR. * Always include full assertion explanation. The previous behaviour was hiding sub-expressions that happened to be False, assuming this was redundant information. Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and `@tomviner`_ for PR. * Better message in case of not using parametrized variable (see `#1539`_). Thanks to `@tramwaj29`_ for the PR. * Updated docstrings with a more uniform style. * Add stderr write for ``pytest.exit(msg)`` during startup. Previously the message was never shown. Thanks `@BeyondEvil`_ for reporting `#1210`_. Thanks to `@JonathonSonesen`_ and `@tomviner`_ for the PR. * No longer display the incorrect test deselection reason (`#1372`_). Thanks `@ronnypfannschmidt`_ for the PR. * The ``--resultlog`` command line option has been deprecated: it is little used and there are more modern and better alternatives (see `#830`_). Thanks `@nicoddemus`_ for the PR. * Improve error message with fixture lookup errors: add an 'E' to the first line and '>' to the rest. Fixes `#717`_. Thanks `@blueyed`_ for reporting and a PR, `@eolo999`_ for the initial PR and `@tomviner`_ for his guidance during EuroPython2016 sprint. **Bug Fixes** * Parametrize now correctly handles duplicated test ids. * Fix internal error issue when the ``method`` argument is missing for ``teardown_method()`` (`#1605`_). * Fix exception visualization in case the current working directory (CWD) gets deleted during testing (`#1235`_). Thanks `@bukzor`_ for reporting. PR by `@marscher`_. * Improve test output for logical expression with brackets (`#925`_). Thanks `@DRMacIver`_ for reporting and `@RedBeardCode`_ for the PR. * Create correct diff for strings ending with newlines (`#1553`_). Thanks `@Vogtinator`_ for reporting and `@RedBeardCode`_ and `@tomviner`_ for the PR. * ``ConftestImportFailure`` now shows the traceback making it easier to identify bugs in ``conftest.py`` files (`#1516`_). Thanks `@txomon`_ for the PR. * Text documents without any doctests no longer appear as "skipped". Thanks `@graingert`_ for reporting and providing a full PR (`#1580`_). * Fixed collection of classes with custom ``__new__`` method. Fixes `#1579`_. Thanks to `@Stranger6667`_ for the PR. * Fixed scope overriding inside metafunc.parametrize (`#634`_). Thanks to `@Stranger6667`_ for the PR. * Fixed the total tests tally in junit xml output (`#1798`_). Thanks to `@cryporchild`_ for the PR. * Fixed off-by-one error with lines from ``request.node.warn``. Thanks to `@blueyed`_ for the PR. .. _#1210: https://github.com/pytest-dev/pytest/issues/1210 .. _#1235: https://github.com/pytest-dev/pytest/issues/1235 .. _#1351: https://github.com/pytest-dev/pytest/issues/1351 .. _#1372: https://github.com/pytest-dev/pytest/issues/1372 .. _#1421: https://github.com/pytest-dev/pytest/issues/1421 .. _#1426: https://github.com/pytest-dev/pytest/issues/1426 .. _#1428: https://github.com/pytest-dev/pytest/pull/1428 .. _#1435: https://github.com/pytest-dev/pytest/issues/1435 .. _#1441: https://github.com/pytest-dev/pytest/pull/1441 .. _#1444: https://github.com/pytest-dev/pytest/pull/1444 .. _#1454: https://github.com/pytest-dev/pytest/pull/1454 .. _#1461: https://github.com/pytest-dev/pytest/pull/1461 .. _#1468: https://github.com/pytest-dev/pytest/pull/1468 .. _#1471: https://github.com/pytest-dev/pytest/issues/1471 .. _#1474: https://github.com/pytest-dev/pytest/pull/1474 .. _#1479: https://github.com/pytest-dev/pytest/issues/1479 .. _#1502: https://github.com/pytest-dev/pytest/pull/1502 .. _#1503: https://github.com/pytest-dev/pytest/issues/1503 .. _#1516: https://github.com/pytest-dev/pytest/pull/1516 .. _#1519: https://github.com/pytest-dev/pytest/pull/1519 .. _#1520: https://github.com/pytest-dev/pytest/pull/1520 .. _#1526: https://github.com/pytest-dev/pytest/pull/1526 .. _#1539: https://github.com/pytest-dev/pytest/issues/1539 .. _#1544: https://github.com/pytest-dev/pytest/issues/1544 .. _#1546: https://github.com/pytest-dev/pytest/issues/1546 .. _#1553: https://github.com/pytest-dev/pytest/issues/1553 .. _#1562: https://github.com/pytest-dev/pytest/issues/1562 .. _#1579: https://github.com/pytest-dev/pytest/issues/1579 .. _#1580: https://github.com/pytest-dev/pytest/pull/1580 .. _#1594: https://github.com/pytest-dev/pytest/issues/1594 .. _#1597: https://github.com/pytest-dev/pytest/pull/1597 .. _#1605: https://github.com/pytest-dev/pytest/issues/1605 .. _#1616: https://github.com/pytest-dev/pytest/pull/1616 .. _#1618: https://github.com/pytest-dev/pytest/issues/1618 .. _#1619: https://github.com/pytest-dev/pytest/issues/1619 .. _#1626: https://github.com/pytest-dev/pytest/pull/1626 .. _#1627: https://github.com/pytest-dev/pytest/pull/1627 .. _#1628: https://github.com/pytest-dev/pytest/pull/1628 .. _#1629: https://github.com/pytest-dev/pytest/issues/1629 .. _#1632: https://github.com/pytest-dev/pytest/issues/1632 .. _#1633: https://github.com/pytest-dev/pytest/pull/1633 .. _#1664: https://github.com/pytest-dev/pytest/pull/1664 .. _#1668: https://github.com/pytest-dev/pytest/issues/1668 .. _#1684: https://github.com/pytest-dev/pytest/pull/1684 .. _#1723: https://github.com/pytest-dev/pytest/pull/1723 .. _#1740: https://github.com/pytest-dev/pytest/issues/1740 .. _#1749: https://github.com/pytest-dev/pytest/issues/1749 .. _#1778: https://github.com/pytest-dev/pytest/pull/1778 .. _#1795: https://github.com/pytest-dev/pytest/pull/1795 .. _#1798: https://github.com/pytest-dev/pytest/pull/1798 .. _#1809: https://github.com/pytest-dev/pytest/pull/1809 .. _#372: https://github.com/pytest-dev/pytest/issues/372 .. _#457: https://github.com/pytest-dev/pytest/issues/457 .. _#460: https://github.com/pytest-dev/pytest/pull/460 .. _#567: https://github.com/pytest-dev/pytest/pull/567 .. _#607: https://github.com/pytest-dev/pytest/issues/607 .. _#634: https://github.com/pytest-dev/pytest/issues/634 .. _#717: https://github.com/pytest-dev/pytest/issues/717 .. _#830: https://github.com/pytest-dev/pytest/issues/830 .. _#925: https://github.com/pytest-dev/pytest/issues/925 .. _@anntzer: https://github.com/anntzer .. _@bagerard: https://github.com/bagerard .. _@BeyondEvil: https://github.com/BeyondEvil .. _@blueyed: https://github.com/blueyed .. _@ceridwen: https://github.com/ceridwen .. _@cryporchild: https://github.com/cryporchild .. _@csaftoiu: https://github.com/csaftoiu .. _@d6e: https://github.com/d6e .. _@davehunt: https://github.com/davehunt .. _@DRMacIver: https://github.com/DRMacIver .. _@eolo999: https://github.com/eolo999 .. _@fengxx: https://github.com/fengxx .. _@flub: https://github.com/flub .. _@gprasad84: https://github.com/gprasad84 .. _@graingert: https://github.com/graingert .. _@hartym: https://github.com/hartym .. _@JonathonSonesen: https://github.com/JonathonSonesen .. _@kalekundert: https://github.com/kalekundert .. _@kvas-it: https://github.com/kvas-it .. _@marscher: https://github.com/marscher .. _@mikofski: https://github.com/mikofski .. _@milliams: https://github.com/milliams .. _@nikratio: https://github.com/nikratio .. _@novas0x2a: https://github.com/novas0x2a .. _@obestwalter: https://github.com/obestwalter .. _@okken: https://github.com/okken .. _@olegpidsadnyi: https://github.com/olegpidsadnyi .. _@omarkohl: https://github.com/omarkohl .. _@palaviv: https://github.com/palaviv .. _@RedBeardCode: https://github.com/RedBeardCode .. _@sallner: https://github.com/sallner .. _@sober7: https://github.com/sober7 .. _@Stranger6667: https://github.com/Stranger6667 .. _@suzaku: https://github.com/suzaku .. _@tareqalayan: https://github.com/tareqalayan .. _@taschini: https://github.com/taschini .. _@tramwaj29: https://github.com/tramwaj29 .. _@txomon: https://github.com/txomon .. _@Vogtinator: https://github.com/Vogtinator .. _@matthiasha: https://github.com/matthiasha 2.9.2 (2016-05-31) ================== **Bug Fixes** * fix `#510`_: skip tests where one parameterize dimension was empty thanks Alex Stapleton for the Report and `@RonnyPfannschmidt`_ for the PR * Fix Xfail does not work with condition keyword argument. Thanks `@astraw38`_ for reporting the issue (`#1496`_) and `@tomviner`_ for PR the (`#1524`_). * Fix win32 path issue when putting custom config file with absolute path in ``pytest.main("-c your_absolute_path")``. * Fix maximum recursion depth detection when raised error class is not aware of unicode/encoded bytes. Thanks `@prusse-martin`_ for the PR (`#1506`_). * Fix ``pytest.mark.skip`` mark when used in strict mode. Thanks `@pquentin`_ for the PR and `@RonnyPfannschmidt`_ for showing how to fix the bug. * Minor improvements and fixes to the documentation. Thanks `@omarkohl`_ for the PR. * Fix ``--fixtures`` to show all fixture definitions as opposed to just one per fixture name. Thanks to `@hackebrot`_ for the PR. .. _#510: https://github.com/pytest-dev/pytest/issues/510 .. _#1506: https://github.com/pytest-dev/pytest/pull/1506 .. _#1496: https://github.com/pytest-dev/pytest/issues/1496 .. _#1524: https://github.com/pytest-dev/pytest/pull/1524 .. _@prusse-martin: https://github.com/prusse-martin .. _@astraw38: https://github.com/astraw38 2.9.1 (2016-03-17) ================== **Bug Fixes** * Improve error message when a plugin fails to load. Thanks `@nicoddemus`_ for the PR. * Fix (`#1178 `_): ``pytest.fail`` with non-ascii characters raises an internal pytest error. Thanks `@nicoddemus`_ for the PR. * Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_). * Fix (`#578 `_): SyntaxErrors containing non-ascii lines at the point of failure generated an internal py.test error. Thanks `@asottile`_ for the report and `@nicoddemus`_ for the PR. * Fix (`#1437`_): When passing in a bytestring regex pattern to parameterize attempt to decode it as utf-8 ignoring errors. * Fix (`#649`_): parametrized test nodes cannot be specified to run on the command line. * Fix (`#138`_): better reporting for python 3.3+ chained exceptions .. _#1437: https://github.com/pytest-dev/pytest/issues/1437 .. _#469: https://github.com/pytest-dev/pytest/issues/469 .. _#1431: https://github.com/pytest-dev/pytest/pull/1431 .. _#649: https://github.com/pytest-dev/pytest/issues/649 .. _#138: https://github.com/pytest-dev/pytest/issues/138 .. _@asottile: https://github.com/asottile 2.9.0 (2016-02-29) ================== **New Features** * New ``pytest.mark.skip`` mark, which unconditionally skips marked tests. Thanks `@MichaelAquilina`_ for the complete PR (`#1040`_). * ``--doctest-glob`` may now be passed multiple times in the command-line. Thanks `@jab`_ and `@nicoddemus`_ for the PR. * New ``-rp`` and ``-rP`` reporting options give the summary and full output of passing tests, respectively. Thanks to `@codewarrior0`_ for the PR. * ``pytest.mark.xfail`` now has a ``strict`` option, which makes ``XPASS`` tests to fail the test suite (defaulting to ``False``). There's also a ``xfail_strict`` ini option that can be used to configure it project-wise. Thanks `@rabbbit`_ for the request and `@nicoddemus`_ for the PR (`#1355`_). * ``Parser.addini`` now supports options of type ``bool``. Thanks `@nicoddemus`_ for the PR. * New ``ALLOW_BYTES`` doctest option. This strips ``b`` prefixes from byte strings in doctest output (similar to ``ALLOW_UNICODE``). Thanks `@jaraco`_ for the request and `@nicoddemus`_ for the PR (`#1287`_). * Give a hint on ``KeyboardInterrupt`` to use the ``--fulltrace`` option to show the errors. Fixes `#1366`_. Thanks to `@hpk42`_ for the report and `@RonnyPfannschmidt`_ for the PR. * Catch ``IndexError`` exceptions when getting exception source location. Fixes a pytest internal error for dynamically generated code (fixtures and tests) where source lines are fake by intention. **Changes** * **Important**: `py.code `_ has been merged into the ``pytest`` repository as ``pytest._code``. This decision was made because ``py.code`` had very few uses outside ``pytest`` and the fact that it was in a different repository made it difficult to fix bugs on its code in a timely manner. The team hopes with this to be able to better refactor out and improve that code. This change shouldn't affect users, but it is useful to let users aware if they encounter any strange behavior. Keep in mind that the code for ``pytest._code`` is **private** and **experimental**, so you definitely should not import it explicitly! Please note that the original ``py.code`` is still available in `pylib `_. * ``pytest_enter_pdb`` now optionally receives the pytest config object. Thanks `@nicoddemus`_ for the PR. * Removed code and documentation for Python 2.5 or lower versions, including removal of the obsolete ``_pytest.assertion.oldinterpret`` module. Thanks `@nicoddemus`_ for the PR (`#1226`_). * Comparisons now always show up in full when ``CI`` or ``BUILD_NUMBER`` is found in the environment, even when ``-vv`` isn't used. Thanks `@The-Compiler`_ for the PR. * ``--lf`` and ``--ff`` now support long names: ``--last-failed`` and ``--failed-first`` respectively. Thanks `@MichaelAquilina`_ for the PR. * Added expected exceptions to ``pytest.raises`` fail message. * Collection only displays progress ("collecting X items") when in a terminal. This avoids cluttering the output when using ``--color=yes`` to obtain colors in CI integrations systems (`#1397`_). **Bug Fixes** * The ``-s`` and ``-c`` options should now work under ``xdist``; ``Config.fromdictargs`` now represents its input much more faithfully. Thanks to `@bukzor`_ for the complete PR (`#680`_). * Fix (`#1290`_): support Python 3.5's ``@`` operator in assertion rewriting. Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR. * Fix formatting utf-8 explanation messages (`#1379`_). Thanks `@biern`_ for the PR. * Fix `traceback style docs`_ to describe all of the available options (auto/long/short/line/native/no), with ``auto`` being the default since v2.6. Thanks `@hackebrot`_ for the PR. * Fix (`#1422`_): junit record_xml_property doesn't allow multiple records with same name. .. _`traceback style docs`: https://pytest.org/latest/usage.html#modifying-python-traceback-printing .. _#1609: https://github.com/pytest-dev/pytest/issues/1609 .. _#1422: https://github.com/pytest-dev/pytest/issues/1422 .. _#1379: https://github.com/pytest-dev/pytest/issues/1379 .. _#1366: https://github.com/pytest-dev/pytest/issues/1366 .. _#1040: https://github.com/pytest-dev/pytest/pull/1040 .. _#680: https://github.com/pytest-dev/pytest/issues/680 .. _#1287: https://github.com/pytest-dev/pytest/pull/1287 .. _#1226: https://github.com/pytest-dev/pytest/pull/1226 .. _#1290: https://github.com/pytest-dev/pytest/pull/1290 .. _#1355: https://github.com/pytest-dev/pytest/pull/1355 .. _#1397: https://github.com/pytest-dev/pytest/issues/1397 .. _@biern: https://github.com/biern .. _@MichaelAquilina: https://github.com/MichaelAquilina .. _@bukzor: https://github.com/bukzor .. _@hpk42: https://github.com/hpk42 .. _@nicoddemus: https://github.com/nicoddemus .. _@jab: https://github.com/jab .. _@codewarrior0: https://github.com/codewarrior0 .. _@jaraco: https://github.com/jaraco .. _@The-Compiler: https://github.com/The-Compiler .. _@Shinkenjoe: https://github.com/Shinkenjoe .. _@tomviner: https://github.com/tomviner .. _@RonnyPfannschmidt: https://github.com/RonnyPfannschmidt .. _@rabbbit: https://github.com/rabbbit .. _@hackebrot: https://github.com/hackebrot .. _@pquentin: https://github.com/pquentin .. _@ioggstream: https://github.com/ioggstream 2.8.7 (2016-01-24) ================== - fix #1338: use predictable object resolution for monkeypatch 2.8.6 (2016-01-21) ================== - fix #1259: allow for double nodeids in junitxml, this was a regression failing plugins combinations like pytest-pep8 + pytest-flakes - Workaround for exception that occurs in pyreadline when using ``--pdb`` with standard I/O capture enabled. Thanks Erik M. Bray for the PR. - fix #900: Better error message in case the target of a ``monkeypatch`` call raises an ``ImportError``. - fix #1292: monkeypatch calls (setattr, setenv, etc.) are now O(1). Thanks David R. MacIver for the report and Bruno Oliveira for the PR. - fix #1223: captured stdout and stderr are now properly displayed before entering pdb when ``--pdb`` is used instead of being thrown away. Thanks Cal Leeming for the PR. - fix #1305: pytest warnings emitted during ``pytest_terminal_summary`` are now properly displayed. Thanks Ionel Maries Cristian for the report and Bruno Oliveira for the PR. - fix #628: fixed internal UnicodeDecodeError when doctests contain unicode. Thanks Jason R. Coombs for the report and Bruno Oliveira for the PR. - fix #1334: Add captured stdout to jUnit XML report on setup error. Thanks Georgy Dyuldin for the PR. 2.8.5 (2015-12-11) ================== - fix #1243: fixed issue where class attributes injected during collection could break pytest. PR by Alexei Kozlenok, thanks Ronny Pfannschmidt and Bruno Oliveira for the review and help. - fix #1074: precompute junitxml chunks instead of storing the whole tree in objects Thanks Bruno Oliveira for the report and Ronny Pfannschmidt for the PR - fix #1238: fix ``pytest.deprecated_call()`` receiving multiple arguments (Regression introduced in 2.8.4). Thanks Alex Gaynor for the report and Bruno Oliveira for the PR. 2.8.4 (2015-12-06) ================== - fix #1190: ``deprecated_call()`` now works when the deprecated function has been already called by another test in the same module. Thanks Mikhail Chernykh for the report and Bruno Oliveira for the PR. - fix #1198: ``--pastebin`` option now works on Python 3. Thanks Mehdy Khoshnoody for the PR. - fix #1219: ``--pastebin`` now works correctly when captured output contains non-ascii characters. Thanks Bruno Oliveira for the PR. - fix #1204: another error when collecting with a nasty __getattr__(). Thanks Florian Bruhin for the PR. - fix the summary printed when no tests did run. Thanks Florian Bruhin for the PR. - fix #1185 - ensure MANIFEST.in exactly matches what should go to a sdist - a number of documentation modernizations wrt good practices. Thanks Bruno Oliveira for the PR. 2.8.3 (2015-11-18) ================== - fix #1169: add __name__ attribute to testcases in TestCaseFunction to support the @unittest.skip decorator on functions and methods. Thanks Lee Kamentsky for the PR. - fix #1035: collecting tests if test module level obj has __getattr__(). Thanks Suor for the report and Bruno Oliveira / Tom Viner for the PR. - fix #331: don't collect tests if their failure cannot be reported correctly e.g. they are a callable instance of a class. - fix #1133: fixed internal error when filtering tracebacks where one entry belongs to a file which is no longer available. Thanks Bruno Oliveira for the PR. - enhancement made to highlight in red the name of the failing tests so they stand out in the output. Thanks Gabriel Reis for the PR. - add more talks to the documentation - extend documentation on the --ignore cli option - use pytest-runner for setuptools integration - minor fixes for interaction with OS X El Capitan system integrity protection (thanks Florian) 2.8.2 (2015-10-07) ================== - fix #1085: proper handling of encoding errors when passing encoded byte strings to pytest.parametrize in Python 2. Thanks Themanwithoutaplan for the report and Bruno Oliveira for the PR. - fix #1087: handling SystemError when passing empty byte strings to pytest.parametrize in Python 3. Thanks Paul Kehrer for the report and Bruno Oliveira for the PR. - fix #995: fixed internal error when filtering tracebacks where one entry was generated by an exec() statement. Thanks Daniel Hahler, Ashley C Straw, Philippe Gauthier and Pavel Savchenko for contributing and Bruno Oliveira for the PR. - fix #1100 and #1057: errors when using autouse fixtures and doctest modules. Thanks Sergey B Kirpichev and Vital Kudzelka for contributing and Bruno Oliveira for the PR. 2.8.1 (2015-09-29) ================== - fix #1034: Add missing nodeid on pytest_logwarning call in addhook. Thanks Simon Gomizelj for the PR. - 'deprecated_call' is now only satisfied with a DeprecationWarning or PendingDeprecationWarning. Before 2.8.0, it accepted any warning, and 2.8.0 made it accept only DeprecationWarning (but not PendingDeprecationWarning). Thanks Alex Gaynor for the issue and Eric Hunsberger for the PR. - fix issue #1073: avoid calling __getattr__ on potential plugin objects. This fixes an incompatibility with pytest-django. Thanks Andreas Pelme, Bruno Oliveira and Ronny Pfannschmidt for contributing and Holger Krekel for the fix. - Fix issue #704: handle versionconflict during plugin loading more gracefully. Thanks Bruno Oliveira for the PR. - Fix issue #1064: ""--junitxml" regression when used with the "pytest-xdist" plugin, with test reports being assigned to the wrong tests. Thanks Daniel Grunwald for the report and Bruno Oliveira for the PR. - (experimental) adapt more SEMVER style versioning and change meaning of master branch in git repo: "master" branch now keeps the bugfixes, changes aimed for micro releases. "features" branch will only be released with minor or major pytest releases. - Fix issue #766 by removing documentation references to distutils. Thanks Russel Winder. - Fix issue #1030: now byte-strings are escaped to produce item node ids to make them always serializable. Thanks Andy Freeland for the report and Bruno Oliveira for the PR. - Python 2: if unicode parametrized values are convertible to ascii, their ascii representation is used for the node id. - Fix issue #411: Add __eq__ method to assertion comparison example. Thanks Ben Webb. - Fix issue #653: deprecated_call can be used as context manager. - fix issue 877: properly handle assertion explanations with non-ascii repr Thanks Mathieu Agopian for the report and Ronny Pfannschmidt for the PR. - fix issue 1029: transform errors when writing cache values into pytest-warnings 2.8.0 (2015-09-18) ================== - new ``--lf`` and ``-ff`` options to run only the last failing tests or "failing tests first" from the last run. This functionality is provided through porting the formerly external pytest-cache plugin into pytest core. BACKWARD INCOMPAT: if you used pytest-cache's functionality to persist data between test runs be aware that we don't serialize sets anymore. Thanks Ronny Pfannschmidt for most of the merging work. - "-r" option now accepts "a" to include all possible reports, similar to passing "fEsxXw" explicitly (isse960). Thanks Abhijeet Kasurde for the PR. - avoid python3.5 deprecation warnings by introducing version specific inspection helpers, thanks Michael Droettboom. - fix issue562: @nose.tools.istest now fully respected. - fix issue934: when string comparison fails and a diff is too large to display without passing -vv, still show a few lines of the diff. Thanks Florian Bruhin for the report and Bruno Oliveira for the PR. - fix issue736: Fix a bug where fixture params would be discarded when combined with parametrization markers. Thanks to Markus Unterwaditzer for the PR. - fix issue710: introduce ALLOW_UNICODE doctest option: when enabled, the ``u`` prefix is stripped from unicode strings in expected doctest output. This allows doctests which use unicode to run in Python 2 and 3 unchanged. Thanks Jason R. Coombs for the report and Bruno Oliveira for the PR. - parametrize now also generates meaningful test IDs for enum, regex and class objects (as opposed to class instances). Thanks to Florian Bruhin for the PR. - Add 'warns' to assert that warnings are thrown (like 'raises'). Thanks to Eric Hunsberger for the PR. - Fix issue683: Do not apply an already applied mark. Thanks ojake for the PR. - Deal with capturing failures better so fewer exceptions get lost to /dev/null. Thanks David Szotten for the PR. - fix issue730: deprecate and warn about the --genscript option. Thanks Ronny Pfannschmidt for the report and Christian Pommranz for the PR. - fix issue751: multiple parametrize with ids bug if it parametrizes class with two or more test methods. Thanks Sergey Chipiga for reporting and Jan Bednarik for PR. - fix issue82: avoid loading conftest files from setup.cfg/pytest.ini/tox.ini files and upwards by default (--confcutdir can still be set to override this). Thanks Bruno Oliveira for the PR. - fix issue768: docstrings found in python modules were not setting up session fixtures. Thanks Jason R. Coombs for reporting and Bruno Oliveira for the PR. - added ``tmpdir_factory``, a session-scoped fixture that can be used to create directories under the base temporary directory. Previously this object was installed as a ``_tmpdirhandler`` attribute of the ``config`` object, but now it is part of the official API and using ``config._tmpdirhandler`` is deprecated. Thanks Bruno Oliveira for the PR. - fix issue808: pytest's internal assertion rewrite hook now implements the optional PEP302 get_data API so tests can access data files next to them. Thanks xmo-odoo for request and example and Bruno Oliveira for the PR. - rootdir and inifile are now displayed during usage errors to help users diagnose problems such as unexpected ini files which add unknown options being picked up by pytest. Thanks to Pavel Savchenko for bringing the problem to attention in #821 and Bruno Oliveira for the PR. - Summary bar now is colored yellow for warning situations such as: all tests either were skipped or xpass/xfailed, or no tests were run at all (this is a partial fix for issue500). - fix issue812: pytest now exits with status code 5 in situations where no tests were run at all, such as the directory given in the command line does not contain any tests or as result of a command line option filters all out all tests (-k for example). Thanks Eric Siegerman (issue812) and Bruno Oliveira for the PR. - Summary bar now is colored yellow for warning situations such as: all tests either were skipped or xpass/xfailed, or no tests were run at all (related to issue500). Thanks Eric Siegerman. - New ``testpaths`` ini option: list of directories to search for tests when executing pytest from the root directory. This can be used to speed up test collection when a project has well specified directories for tests, being usually more practical than configuring norecursedirs for all directories that do not contain tests. Thanks to Adrian for idea (#694) and Bruno Oliveira for the PR. - fix issue713: JUnit XML reports for doctest failures. Thanks Punyashloka Biswal. - fix issue970: internal pytest warnings now appear as "pytest-warnings" in the terminal instead of "warnings", so it is clear for users that those warnings are from pytest and not from the builtin "warnings" module. Thanks Bruno Oliveira. - Include setup and teardown in junitxml test durations. Thanks Janne Vanhala. - fix issue735: assertion failures on debug versions of Python 3.4+ - new option ``--import-mode`` to allow to change test module importing behaviour to append to sys.path instead of prepending. This better allows to run test modules against installed versions of a package even if the package under test has the same import root. In this example:: testing/__init__.py testing/test_pkg_under_test.py pkg_under_test/ the tests will run against the installed version of pkg_under_test when ``--import-mode=append`` is used whereas by default they would always pick up the local version. Thanks Holger Krekel. - pytester: add method ``TmpTestdir.delete_loaded_modules()``, and call it from ``inline_run()`` to allow temporary modules to be reloaded. Thanks Eduardo Schettino. - internally refactor pluginmanager API and code so that there is a clear distinction between a pytest-agnostic rather simple pluginmanager and the PytestPluginManager which adds a lot of behaviour, among it handling of the local conftest files. In terms of documented methods this is a backward compatible change but it might still break 3rd party plugins which relied on details like especially the pluginmanager.add_shutdown() API. Thanks Holger Krekel. - pluginmanagement: introduce ``pytest.hookimpl`` and ``pytest.hookspec`` decorators for setting impl/spec specific parameters. This substitutes the previous now deprecated use of ``pytest.mark`` which is meant to contain markers for test functions only. - write/refine docs for "writing plugins" which now have their own page and are separate from the "using/installing plugins`` page. - fix issue732: properly unregister plugins from any hook calling sites allowing to have temporary plugins during test execution. - deprecate and warn about ``__multicall__`` argument in hook implementations. Use the ``hookwrapper`` mechanism instead already introduced with pytest-2.7. - speed up pytest's own test suite considerably by using inprocess tests by default (testrun can be modified with --runpytest=subprocess to create subprocesses in many places instead). The main APIs to run pytest in a test is "runpytest()" or "runpytest_subprocess" and "runpytest_inprocess" if you need a particular way of running the test. In all cases you get back a RunResult but the inprocess one will also have a "reprec" attribute with the recorded events/reports. - fix monkeypatch.setattr("x.y", raising=False) to actually not raise if "y" is not a pre-existing attribute. Thanks Florian Bruhin. - fix issue741: make running output from testdir.run copy/pasteable Thanks Bruno Oliveira. - add a new ``--noconftest`` argument which ignores all ``conftest.py`` files. - add ``file`` and ``line`` attributes to JUnit-XML output. - fix issue890: changed extension of all documentation files from ``txt`` to ``rst``. Thanks to Abhijeet for the PR. - fix issue714: add ability to apply indirect=True parameter on particular argnames. Thanks Elizaveta239. - fix issue890: changed extension of all documentation files from ``txt`` to ``rst``. Thanks to Abhijeet for the PR. - fix issue957: "# doctest: SKIP" option will now register doctests as SKIPPED rather than PASSED. Thanks Thomas Grainger for the report and Bruno Oliveira for the PR. - issue951: add new record_xml_property fixture, that supports logging additional information on xml output. Thanks David Diaz for the PR. - issue949: paths after normal options (for example ``-s``, ``-v``, etc) are now properly used to discover ``rootdir`` and ``ini`` files. Thanks Peter Lauri for the report and Bruno Oliveira for the PR. 2.7.3 (2015-09-15) ================== - Allow 'dev', 'rc', or other non-integer version strings in ``importorskip``. Thanks to Eric Hunsberger for the PR. - fix issue856: consider --color parameter in all outputs (for example --fixtures). Thanks Barney Gale for the report and Bruno Oliveira for the PR. - fix issue855: passing str objects as ``plugins`` argument to pytest.main is now interpreted as a module name to be imported and registered as a plugin, instead of silently having no effect. Thanks xmo-odoo for the report and Bruno Oliveira for the PR. - fix issue744: fix for ast.Call changes in Python 3.5+. Thanks Guido van Rossum, Matthias Bussonnier, Stefan Zimmermann and Thomas Kluyver. - fix issue842: applying markers in classes no longer propagate this markers to superclasses which also have markers. Thanks xmo-odoo for the report and Bruno Oliveira for the PR. - preserve warning functions after call to pytest.deprecated_call. Thanks Pieter Mulder for PR. - fix issue854: autouse yield_fixtures defined as class members of unittest.TestCase subclasses now work as expected. Thannks xmo-odoo for the report and Bruno Oliveira for the PR. - fix issue833: --fixtures now shows all fixtures of collected test files, instead of just the fixtures declared on the first one. Thanks Florian Bruhin for reporting and Bruno Oliveira for the PR. - fix issue863: skipped tests now report the correct reason when a skip/xfail condition is met when using multiple markers. Thanks Raphael Pierzina for reporting and Bruno Oliveira for the PR. - optimized tmpdir fixture initialization, which should make test sessions faster (specially when using pytest-xdist). The only visible effect is that now pytest uses a subdirectory in the $TEMP directory for all directories created by this fixture (defaults to $TEMP/pytest-$USER). Thanks Bruno Oliveira for the PR. 2.7.2 (2015-06-23) ================== - fix issue767: pytest.raises value attribute does not contain the exception instance on Python 2.6. Thanks Eric Siegerman for providing the test case and Bruno Oliveira for PR. - Automatically create directory for junitxml and results log. Thanks Aron Curzon. - fix issue713: JUnit XML reports for doctest failures. Thanks Punyashloka Biswal. - fix issue735: assertion failures on debug versions of Python 3.4+ Thanks Benjamin Peterson. - fix issue114: skipif marker reports to internal skipping plugin; Thanks Floris Bruynooghe for reporting and Bruno Oliveira for the PR. - fix issue748: unittest.SkipTest reports to internal pytest unittest plugin. Thanks Thomas De Schampheleire for reporting and Bruno Oliveira for the PR. - fix issue718: failed to create representation of sets containing unsortable elements in python 2. Thanks Edison Gustavo Muenz. - fix issue756, fix issue752 (and similar issues): depend on py-1.4.29 which has a refined algorithm for traceback generation. 2.7.1 (2015-05-19) ================== - fix issue731: do not get confused by the braces which may be present and unbalanced in an object's repr while collapsing False explanations. Thanks Carl Meyer for the report and test case. - fix issue553: properly handling inspect.getsourcelines failures in FixtureLookupError which would lead to an internal error, obfuscating the original problem. Thanks talljosh for initial diagnose/patch and Bruno Oliveira for final patch. - fix issue660: properly report scope-mismatch-access errors independently from ordering of fixture arguments. Also avoid the pytest internal traceback which does not provide information to the user. Thanks Holger Krekel. - streamlined and documented release process. Also all versions (in setup.py and documentation generation) are now read from _pytest/__init__.py. Thanks Holger Krekel. - fixed docs to remove the notion that yield-fixtures are experimental. They are here to stay :) Thanks Bruno Oliveira. - Support building wheels by using environment markers for the requirements. Thanks Ionel Maries Cristian. - fixed regression to 2.6.4 which surfaced e.g. in lost stdout capture printing when tests raised SystemExit. Thanks Holger Krekel. - reintroduced _pytest fixture of the pytester plugin which is used at least by pytest-xdist. 2.7.0 (2015-03-26) ================== - fix issue435: make reload() work when assert rewriting is active. Thanks Daniel Hahler. - fix issue616: conftest.py files and their contained fixutres are now properly considered for visibility, independently from the exact current working directory and test arguments that are used. Many thanks to Eric Siegerman and his PR235 which contains systematic tests for conftest visibility and now passes. This change also introduces the concept of a ``rootdir`` which is printed as a new pytest header and documented in the pytest customize web page. - change reporting of "diverted" tests, i.e. tests that are collected in one file but actually come from another (e.g. when tests in a test class come from a base class in a different file). We now show the nodeid and indicate via a postfix the other file. - add ability to set command line options by environment variable PYTEST_ADDOPTS. - added documentation on the new pytest-dev teams on bitbucket and github. See https://pytest.org/latest/contributing.html . Thanks to Anatoly for pushing and initial work on this. - fix issue650: new option ``--docttest-ignore-import-errors`` which will turn import errors in doctests into skips. Thanks Charles Cloud for the complete PR. - fix issue655: work around different ways that cause python2/3 to leak sys.exc_info into fixtures/tests causing failures in 3rd party code - fix issue615: assertion rewriting did not correctly escape % signs when formatting boolean operations, which tripped over mixing booleans with modulo operators. Thanks to Tom Viner for the report, triaging and fix. - implement issue351: add ability to specify parametrize ids as a callable to generate custom test ids. Thanks Brianna Laugher for the idea and implementation. - introduce and document new hookwrapper mechanism useful for plugins which want to wrap the execution of certain hooks for their purposes. This supersedes the undocumented ``__multicall__`` protocol which pytest itself and some external plugins use. Note that pytest-2.8 is scheduled to drop supporting the old ``__multicall__`` and only support the hookwrapper protocol. - majorly speed up invocation of plugin hooks - use hookwrapper mechanism in builtin pytest plugins. - add a doctest ini option for doctest flags, thanks Holger Peters. - add note to docs that if you want to mark a parameter and the parameter is a callable, you also need to pass in a reason to disambiguate it from the "decorator" case. Thanks Tom Viner. - "python_classes" and "python_functions" options now support glob-patterns for test discovery, as discussed in issue600. Thanks Ldiary Translations. - allow to override parametrized fixtures with non-parametrized ones and vice versa (bubenkoff). - fix issue463: raise specific error for 'parameterize' misspelling (pfctdayelise). - On failure, the ``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback`` are set, so that a user can inspect the error via postmortem debugging (almarklein). 2.6.4 (2014-10-24) ================== - Improve assertion failure reporting on iterables, by using ndiff and pprint. - removed outdated japanese docs from source tree. - docs for "pytest_addhooks" hook. Thanks Bruno Oliveira. - updated plugin index docs. Thanks Bruno Oliveira. - fix issue557: with "-k" we only allow the old style "-" for negation at the beginning of strings and even that is deprecated. Use "not" instead. This should allow to pick parametrized tests where "-" appeared in the parameter. - fix issue604: Escape % character in the assertion message. - fix issue620: add explanation in the --genscript target about what the binary blob means. Thanks Dinu Gherman. - fix issue614: fixed pastebin support. - fix issue620: add explanation in the --genscript target about what the binary blob means. Thanks Dinu Gherman. - fix issue614: fixed pastebin support. 2.6.3 (2014-09-24) ================== - fix issue575: xunit-xml was reporting collection errors as failures instead of errors, thanks Oleg Sinyavskiy. - fix issue582: fix setuptools example, thanks Laszlo Papp and Ronny Pfannschmidt. - Fix infinite recursion bug when pickling capture.EncodedFile, thanks Uwe Schmitt. - fix issue589: fix bad interaction with numpy and others when showing exceptions. Check for precise "maximum recursion depth exceed" exception instead of presuming any RuntimeError is that one (implemented in py dep). Thanks Charles Cloud for analysing the issue. - fix conftest related fixture visibility issue: when running with a CWD outside of a test package pytest would get fixture discovery wrong. Thanks to Wolfgang Schnerring for figuring out a reproducible example. - Introduce pytest_enter_pdb hook (needed e.g. by pytest_timeout to cancel the timeout when interactively entering pdb). Thanks Wolfgang Schnerring. - check xfail/skip also with non-python function test items. Thanks Floris Bruynooghe. 2.6.2 (2014-09-05) ================== - Added function pytest.freeze_includes(), which makes it easy to embed pytest into executables using tools like cx_freeze. See docs for examples and rationale. Thanks Bruno Oliveira. - Improve assertion rewriting cache invalidation precision. - fixed issue561: adapt autouse fixture example for python3. - fixed issue453: assertion rewriting issue with __repr__ containing "\n{", "\n}" and "\n~". - fix issue560: correctly display code if an "else:" or "finally:" is followed by statements on the same line. - Fix example in monkeypatch documentation, thanks t-8ch. - fix issue572: correct tmpdir doc example for python3. - Do not mark as universal wheel because Python 2.6 is different from other builds due to the extra argparse dependency. Fixes issue566. Thanks sontek. - Implement issue549: user-provided assertion messages now no longer replace the py.test introspection message but are shown in addition to them. 2.6.1 (2014-08-07) ================== - No longer show line numbers in the --verbose output, the output is now purely the nodeid. The line number is still shown in failure reports. Thanks Floris Bruynooghe. - fix issue437 where assertion rewriting could cause pytest-xdist slaves to collect different tests. Thanks Bruno Oliveira. - fix issue555: add "errors" attribute to capture-streams to satisfy some distutils and possibly other code accessing sys.stdout.errors. - fix issue547 capsys/capfd also work when output capturing ("-s") is disabled. - address issue170: allow pytest.mark.xfail(...) to specify expected exceptions via an optional "raises=EXC" argument where EXC can be a single exception or a tuple of exception classes. Thanks David Mohr for the complete PR. - fix integration of pytest with unittest.mock.patch decorator when it uses the "new" argument. Thanks Nicolas Delaby for test and PR. - fix issue with detecting conftest files if the arguments contain "::" node id specifications (copy pasted from "-v" output) - fix issue544 by only removing "@NUM" at the end of "::" separated parts and if the part has a ".py" extension - don't use py.std import helper, rather import things directly. Thanks Bruno Oliveira. 2.6 === - Cache exceptions from fixtures according to their scope (issue 467). - fix issue537: Avoid importing old assertion reinterpretation code by default. - fix issue364: shorten and enhance tracebacks representation by default. The new "--tb=auto" option (default) will only display long tracebacks for the first and last entry. You can get the old behaviour of printing all entries as long entries with "--tb=long". Also short entries by default are now printed very similarly to "--tb=native" ones. - fix issue514: teach assertion reinterpretation about private class attributes - change -v output to include full node IDs of tests. Users can copy a node ID from a test run, including line number, and use it as a positional argument in order to run only a single test. - fix issue 475: fail early and comprehensible if calling pytest.raises with wrong exception type. - fix issue516: tell in getting-started about current dependencies. - cleanup setup.py a bit and specify supported versions. Thanks Jurko Gospodnetic for the PR. - change XPASS colour to yellow rather then red when tests are run with -v. - fix issue473: work around mock putting an unbound method into a class dict when double-patching. - fix issue498: if a fixture finalizer fails, make sure that the fixture is still invalidated. - fix issue453: the result of the pytest_assertrepr_compare hook now gets it's newlines escaped so that format_exception does not blow up. - internal new warning system: pytest will now produce warnings when it detects oddities in your test collection or execution. Warnings are ultimately sent to a new pytest_logwarning hook which is currently only implemented by the terminal plugin which displays warnings in the summary line and shows more details when -rw (report on warnings) is specified. - change skips into warnings for test classes with an __init__ and callables in test modules which look like a test but are not functions. - fix issue436: improved finding of initial conftest files from command line arguments by using the result of parse_known_args rather than the previous flaky heuristics. Thanks Marc Abramowitz for tests and initial fixing approaches in this area. - fix issue #479: properly handle nose/unittest(2) SkipTest exceptions during collection/loading of test modules. Thanks to Marc Schlaich for the complete PR. - fix issue490: include pytest_load_initial_conftests in documentation and improve docstring. - fix issue472: clarify that ``pytest.config.getvalue()`` cannot work if it's triggered ahead of command line parsing. - merge PR123: improved integration with mock.patch decorator on tests. - fix issue412: messing with stdout/stderr FD-level streams is now captured without crashes. - fix issue483: trial/py33 works now properly. Thanks Daniel Grana for PR. - improve example for pytest integration with "python setup.py test" which now has a generic "-a" or "--pytest-args" option where you can pass additional options as a quoted string. Thanks Trevor Bekolay. - simplified internal capturing mechanism and made it more robust against tests or setups changing FD1/FD2, also better integrated now with pytest.pdb() in single tests. - improvements to pytest's own test-suite leakage detection, courtesy of PRs from Marc Abramowitz - fix issue492: avoid leak in test_writeorg. Thanks Marc Abramowitz. - fix issue493: don't run tests in doc directory with ``python setup.py test`` (use tox -e doctesting for that) - fix issue486: better reporting and handling of early conftest loading failures - some cleanup and simplification of internal conftest handling. - work a bit harder to break reference cycles when catching exceptions. Thanks Jurko Gospodnetic. - fix issue443: fix skip examples to use proper comparison. Thanks Alex Groenholm. - support nose-style ``__test__`` attribute on modules, classes and functions, including unittest-style Classes. If set to False, the test will not be collected. - fix issue512: show "" for arguments which might not be set in monkeypatch plugin. Improves output in documentation. 2.5.2 (2014-01-29) ================== - fix issue409 -- better interoperate with cx_freeze by not trying to import from collections.abc which causes problems for py27/cx_freeze. Thanks Wolfgang L. for reporting and tracking it down. - fixed docs and code to use "pytest" instead of "py.test" almost everywhere. Thanks Jurko Gospodnetic for the complete PR. - fix issue425: mention at end of "py.test -h" that --markers and --fixtures work according to specified test path (or current dir) - fix issue413: exceptions with unicode attributes are now printed correctly also on python2 and with pytest-xdist runs. (the fix requires py-1.4.20) - copy, cleanup and integrate py.io capture from pylib 1.4.20.dev2 (rev 13d9af95547e) - address issue416: clarify docs as to conftest.py loading semantics - fix issue429: comparing byte strings with non-ascii chars in assert expressions now work better. Thanks Floris Bruynooghe. - make capfd/capsys.capture private, its unused and shouldn't be exposed 2.5.1 (2013-12-17) ================== - merge new documentation styling PR from Tobias Bieniek. - fix issue403: allow parametrize of multiple same-name functions within a collection node. Thanks Andreas Kloeckner and Alex Gaynor for reporting and analysis. - Allow parameterized fixtures to specify the ID of the parameters by adding an ids argument to pytest.fixture() and pytest.yield_fixture(). Thanks Floris Bruynooghe. - fix issue404 by always using the binary xml escape in the junitxml plugin. Thanks Ronny Pfannschmidt. - fix issue407: fix addoption docstring to point to argparse instead of optparse. Thanks Daniel D. Wright. 2.5.0 (2013-12-12) ================== - dropped python2.5 from automated release testing of pytest itself which means it's probably going to break soon (but still works with this release we believe). - simplified and fixed implementation for calling finalizers when parametrized fixtures or function arguments are involved. finalization is now performed lazily at setup time instead of in the "teardown phase". While this might sound odd at first, it helps to ensure that we are correctly handling setup/teardown even in complex code. User-level code should not be affected unless it's implementing the pytest_runtest_teardown hook and expecting certain fixture instances are torn down within (very unlikely and would have been unreliable anyway). - PR90: add --color=yes|no|auto option to force terminal coloring mode ("auto" is default). Thanks Marc Abramowitz. - fix issue319 - correctly show unicode in assertion errors. Many thanks to Floris Bruynooghe for the complete PR. Also means we depend on py>=1.4.19 now. - fix issue396 - correctly sort and finalize class-scoped parametrized tests independently from number of methods on the class. - refix issue323 in a better way -- parametrization should now never cause Runtime Recursion errors because the underlying algorithm for re-ordering tests per-scope/per-fixture is not recursive anymore (it was tail-call recursive before which could lead to problems for more than >966 non-function scoped parameters). - fix issue290 - there is preliminary support now for parametrizing with repeated same values (sometimes useful to test if calling a second time works as with the first time). - close issue240 - document precisely how pytest module importing works, discuss the two common test directory layouts, and how it interacts with PEP420-namespace packages. - fix issue246 fix finalizer order to be LIFO on independent fixtures depending on a parametrized higher-than-function scoped fixture. (was quite some effort so please bear with the complexity of this sentence :) Thanks Ralph Schmitt for the precise failure example. - fix issue244 by implementing special index for parameters to only use indices for paramentrized test ids - fix issue287 by running all finalizers but saving the exception from the first failing finalizer and re-raising it so teardown will still have failed. We reraise the first failing exception because it might be the cause for other finalizers to fail. - fix ordering when mock.patch or other standard decorator-wrappings are used with test methods. This fixues issue346 and should help with random "xdist" collection failures. Thanks to Ronny Pfannschmidt and Donald Stufft for helping to isolate it. - fix issue357 - special case "-k" expressions to allow for filtering with simple strings that are not valid python expressions. Examples: "-k 1.3" matches all tests parametrized with 1.3. "-k None" filters all tests that have "None" in their name and conversely "-k 'not None'". Previously these examples would raise syntax errors. - fix issue384 by removing the trial support code since the unittest compat enhancements allow trial to handle it on its own - don't hide an ImportError when importing a plugin produces one. fixes issue375. - fix issue275 - allow usefixtures and autouse fixtures for running doctest text files. - fix issue380 by making --resultlog only rely on longrepr instead of the "reprcrash" attribute which only exists sometimes. - address issue122: allow @pytest.fixture(params=iterator) by exploding into a list early on. - fix pexpect-3.0 compatibility for pytest's own tests. (fixes issue386) - allow nested parametrize-value markers, thanks James Lan for the PR. - fix unicode handling with new monkeypatch.setattr(import_path, value) API. Thanks Rob Dennis. Fixes issue371. - fix unicode handling with junitxml, fixes issue368. - In assertion rewriting mode on Python 2, fix the detection of coding cookies. See issue #330. - make "--runxfail" turn imperative pytest.xfail calls into no ops (it already did neutralize pytest.mark.xfail markers) - refine pytest / pkg_resources interactions: The AssertionRewritingHook PEP302 compliant loader now registers itself with setuptools/pkg_resources properly so that the pkg_resources.resource_stream method works properly. Fixes issue366. Thanks for the investigations and full PR to Jason R. Coombs. - pytestconfig fixture is now session-scoped as it is the same object during the whole test run. Fixes issue370. - avoid one surprising case of marker malfunction/confusion:: @pytest.mark.some(lambda arg: ...) def test_function(): would not work correctly because pytest assumes @pytest.mark.some gets a function to be decorated already. We now at least detect if this arg is a lambda and thus the example will work. Thanks Alex Gaynor for bringing it up. - xfail a test on pypy that checks wrong encoding/ascii (pypy does not error out). fixes issue385. - internally make varnames() deal with classes's __init__, although it's not needed by pytest itself atm. Also fix caching. Fixes issue376. - fix issue221 - handle importing of namespace-package with no __init__.py properly. - refactor internal FixtureRequest handling to avoid monkeypatching. One of the positive user-facing effects is that the "request" object can now be used in closures. - fixed version comparison in pytest.importskip(modname, minverstring) - fix issue377 by clarifying in the nose-compat docs that pytest does not duplicate the unittest-API into the "plain" namespace. - fix verbose reporting for @mock'd test functions 2.4.2 (2013-10-04) ================== - on Windows require colorama and a newer py lib so that py.io.TerminalWriter() now uses colorama instead of its own ctypes hacks. (fixes issue365) thanks Paul Moore for bringing it up. - fix "-k" matching of tests where "repr" and "attr" and other names would cause wrong matches because of an internal implementation quirk (don't ask) which is now properly implemented. fixes issue345. - avoid tmpdir fixture to create too long filenames especially when parametrization is used (issue354) - fix pytest-pep8 and pytest-flakes / pytest interactions (collection names in mark plugin was assuming an item always has a function which is not true for those plugins etc.) Thanks Andi Zeidler. - introduce node.get_marker/node.add_marker API for plugins like pytest-pep8 and pytest-flakes to avoid the messy details of the node.keywords pseudo-dicts. Adapted docs. - remove attempt to "dup" stdout at startup as it's icky. the normal capturing should catch enough possibilities of tests messing up standard FDs. - add pluginmanager.do_configure(config) as a link to config.do_configure() for plugin-compatibility 2.4.1 (2013-10-02) ================== - When using parser.addoption() unicode arguments to the "type" keyword should also be converted to the respective types. thanks Floris Bruynooghe, @dnozay. (fixes issue360 and issue362) - fix dotted filename completion when using argcomplete thanks Anthon van der Neuth. (fixes issue361) - fix regression when a 1-tuple ("arg",) is used for specifying parametrization (the values of the parametrization were passed nested in a tuple). Thanks Donald Stufft. - merge doc typo fixes, thanks Andy Dirnberger 2.4 === known incompatibilities: - if calling --genscript from python2.7 or above, you only get a standalone script which works on python2.7 or above. Use Python2.6 to also get a python2.5 compatible version. - all xunit-style teardown methods (nose-style, pytest-style, unittest-style) will not be called if the corresponding setup method failed, see issue322 below. - the pytest_plugin_unregister hook wasn't ever properly called and there is no known implementation of the hook - so it got removed. - pytest.fixture-decorated functions cannot be generators (i.e. use yield) anymore. This change might be reversed in 2.4.1 if it causes unforeseen real-life issues. However, you can always write and return an inner function/generator and change the fixture consumer to iterate over the returned generator. This change was done in lieu of the new ``pytest.yield_fixture`` decorator, see below. new features: - experimentally introduce a new ``pytest.yield_fixture`` decorator which accepts exactly the same parameters as pytest.fixture but mandates a ``yield`` statement instead of a ``return statement`` from fixture functions. This allows direct integration with "with-style" context managers in fixture functions and generally avoids registering of finalization callbacks in favour of treating the "after-yield" as teardown code. Thanks Andreas Pelme, Vladimir Keleshev, Floris Bruynooghe, Ronny Pfannschmidt and many others for discussions. - allow boolean expression directly with skipif/xfail if a "reason" is also specified. Rework skipping documentation to recommend "condition as booleans" because it prevents surprises when importing markers between modules. Specifying conditions as strings will remain fully supported. - reporting: color the last line red or green depending if failures/errors occurred or everything passed. thanks Christian Theunert. - make "import pdb ; pdb.set_trace()" work natively wrt capturing (no "-s" needed anymore), making ``pytest.set_trace()`` a mere shortcut. - fix issue181: --pdb now also works on collect errors (and on internal errors) . This was implemented by a slight internal refactoring and the introduction of a new hook ``pytest_exception_interact`` hook (see next item). - fix issue341: introduce new experimental hook for IDEs/terminals to intercept debugging: ``pytest_exception_interact(node, call, report)``. - new monkeypatch.setattr() variant to provide a shorter invocation for patching out classes/functions from modules: monkeypatch.setattr("requests.get", myfunc) will replace the "get" function of the "requests" module with ``myfunc``. - fix issue322: tearDownClass is not run if setUpClass failed. Thanks Mathieu Agopian for the initial fix. Also make all of pytest/nose finalizer mimic the same generic behaviour: if a setupX exists and fails, don't run teardownX. This internally introduces a new method "node.addfinalizer()" helper which can only be called during the setup phase of a node. - simplify pytest.mark.parametrize() signature: allow to pass a CSV-separated string to specify argnames. For example: ``pytest.mark.parametrize("input,expected", [(1,2), (2,3)])`` works as well as the previous: ``pytest.mark.parametrize(("input", "expected"), ...)``. - add support for setUpModule/tearDownModule detection, thanks Brian Okken. - integrate tab-completion on options through use of "argcomplete". Thanks Anthon van der Neut for the PR. - change option names to be hyphen-separated long options but keep the old spelling backward compatible. py.test -h will only show the hyphenated version, for example "--collect-only" but "--collectonly" will remain valid as well (for backward-compat reasons). Many thanks to Anthon van der Neut for the implementation and to Hynek Schlawack for pushing us. - fix issue 308 - allow to mark/xfail/skip individual parameter sets when parametrizing. Thanks Brianna Laugher. - call new experimental pytest_load_initial_conftests hook to allow 3rd party plugins to do something before a conftest is loaded. Bug fixes: - fix issue358 - capturing options are now parsed more properly by using a new parser.parse_known_args method. - pytest now uses argparse instead of optparse (thanks Anthon) which means that "argparse" is added as a dependency if installing into python2.6 environments or below. - fix issue333: fix a case of bad unittest/pytest hook interaction. - PR27: correctly handle nose.SkipTest during collection. Thanks Antonio Cuni, Ronny Pfannschmidt. - fix issue355: junitxml puts name="pytest" attribute to testsuite tag. - fix issue336: autouse fixture in plugins should work again. - fix issue279: improve object comparisons on assertion failure for standard datatypes and recognise collections.abc. Thanks to Brianna Laugher and Mathieu Agopian. - fix issue317: assertion rewriter support for the is_package method - fix issue335: document py.code.ExceptionInfo() object returned from pytest.raises(), thanks Mathieu Agopian. - remove implicit distribute_setup support from setup.py. - fix issue305: ignore any problems when writing pyc files. - SO-17664702: call fixture finalizers even if the fixture function partially failed (finalizers would not always be called before) - fix issue320 - fix class scope for fixtures when mixed with module-level functions. Thanks Anatloy Bubenkoff. - you can specify "-q" or "-qq" to get different levels of "quieter" reporting (thanks Katarzyna Jachim) - fix issue300 - Fix order of conftest loading when starting py.test in a subdirectory. - fix issue323 - sorting of many module-scoped arg parametrizations - make sessionfinish hooks execute with the same cwd-context as at session start (helps fix plugin behaviour which write output files with relative path such as pytest-cov) - fix issue316 - properly reference collection hooks in docs - fix issue 306 - cleanup of -k/-m options to only match markers/test names/keywords respectively. Thanks Wouter van Ackooy. - improved doctest counting for doctests in python modules -- files without any doctest items will not show up anymore and doctest examples are counted as separate test items. thanks Danilo Bellini. - fix issue245 by depending on the released py-1.4.14 which fixes py.io.dupfile to work with files with no mode. Thanks Jason R. Coombs. - fix junitxml generation when test output contains control characters, addressing issue267, thanks Jaap Broekhuizen - fix issue338: honor --tb style for setup/teardown errors as well. Thanks Maho. - fix issue307 - use yaml.safe_load in example, thanks Mark Eichin. - better parametrize error messages, thanks Brianna Laugher - pytest_terminal_summary(terminalreporter) hooks can now use ".section(title)" and ".line(msg)" methods to print extra information at the end of a test run. 2.3.5 (2013-04-30) ================== - fix issue169: respect --tb=style with setup/teardown errors as well. - never consider a fixture function for test function collection - allow re-running of test items / helps to fix pytest-reruntests plugin and also help to keep less fixture/resource references alive - put captured stdout/stderr into junitxml output even for passing tests (thanks Adam Goucher) - Issue 265 - integrate nose setup/teardown with setupstate so it doesn't try to teardown if it did not setup - issue 271 - don't write junitxml on slave nodes - Issue 274 - don't try to show full doctest example when doctest does not know the example location - issue 280 - disable assertion rewriting on buggy CPython 2.6.0 - inject "getfixture()" helper to retrieve fixtures from doctests, thanks Andreas Zeidler - issue 259 - when assertion rewriting, be consistent with the default source encoding of ASCII on Python 2 - issue 251 - report a skip instead of ignoring classes with init - issue250 unicode/str mixes in parametrization names and values now works - issue257, assertion-triggered compilation of source ending in a comment line doesn't blow up in python2.5 (fixed through py>=1.4.13.dev6) - fix --genscript option to generate standalone scripts that also work with python3.3 (importer ordering) - issue171 - in assertion rewriting, show the repr of some global variables - fix option help for "-k" - move long description of distribution into README.rst - improve docstring for metafunc.parametrize() - fix bug where using capsys with pytest.set_trace() in a test function would break when looking at capsys.readouterr() - allow to specify prefixes starting with "_" when customizing python_functions test discovery. (thanks Graham Horler) - improve PYTEST_DEBUG tracing output by putting extra data on a new lines with additional indent - ensure OutcomeExceptions like skip/fail have initialized exception attributes - issue 260 - don't use nose special setup on plain unittest cases - fix issue134 - print the collect errors that prevent running specified test items - fix issue266 - accept unicode in MarkEvaluator expressions 2.3.4 (2012-11-20) ================== - yielded test functions will now have autouse-fixtures active but cannot accept fixtures as funcargs - it's anyway recommended to rather use the post-2.0 parametrize features instead of yield, see: http://pytest.org/latest/example/parametrize.html - fix autouse-issue where autouse-fixtures would not be discovered if defined in an a/conftest.py file and tests in a/tests/test_some.py - fix issue226 - LIFO ordering for fixture teardowns - fix issue224 - invocations with >256 char arguments now work - fix issue91 - add/discuss package/directory level setups in example - allow to dynamically define markers via item.keywords[...]=assignment integrating with "-m" option - make "-k" accept an expressions the same as with "-m" so that one can write: -k "name1 or name2" etc. This is a slight incompatibility if you used special syntax like "TestClass.test_method" which you now need to write as -k "TestClass and test_method" to match a certain method in a certain test class. 2.3.3 (2012-11-06) ================== - fix issue214 - parse modules that contain special objects like e. g. flask's request object which blows up on getattr access if no request is active. thanks Thomas Waldmann. - fix issue213 - allow to parametrize with values like numpy arrays that do not support an __eq__ operator - fix issue215 - split test_python.org into multiple files - fix issue148 - @unittest.skip on classes is now recognized and avoids calling setUpClass/tearDownClass, thanks Pavel Repin - fix issue209 - reintroduce python2.4 support by depending on newer pylib which re-introduced statement-finding for pre-AST interpreters - nose support: only call setup if it's a callable, thanks Andrew Taumoefolau - fix issue219 - add py2.4-3.3 classifiers to TROVE list - in tracebacks *,** arg values are now shown next to normal arguments (thanks Manuel Jacob) - fix issue217 - support mock.patch with pytest's fixtures - note that you need either mock-1.0.1 or the python3.3 builtin unittest.mock. - fix issue127 - improve documentation for pytest_addoption() and add a ``config.getoption(name)`` helper function for consistency. 2.3.2 (2012-10-25) ================== - fix issue208 and fix issue29 use new py version to avoid long pauses when printing tracebacks in long modules - fix issue205 - conftests in subdirs customizing pytest_pycollect_makemodule and pytest_pycollect_makeitem now work properly - fix teardown-ordering for parametrized setups - fix issue127 - better documentation for pytest_addoption and related objects. - fix unittest behaviour: TestCase.runtest only called if there are test methods defined - improve trial support: don't collect its empty unittest.TestCase.runTest() method - "python setup.py test" now works with pytest itself - fix/improve internal/packaging related bits: - exception message check of test_nose.py now passes on python33 as well - issue206 - fix test_assertrewrite.py to work when a global PYTHONDONTWRITEBYTECODE=1 is present - add tox.ini to pytest distribution so that ignore-dirs and others config bits are properly distributed for maintainers who run pytest-own tests 2.3.1 (2012-10-20) ================== - fix issue202 - fix regression: using "self" from fixture functions now works as expected (it's the same "self" instance that a test method which uses the fixture sees) - skip pexpect using tests (test_pdb.py mostly) on freebsd* systems due to pexpect not supporting it properly (hanging) - link to web pages from --markers output which provides help for pytest.mark.* usage. 2.3.0 (2012-10-19) ================== - fix issue202 - better automatic names for parametrized test functions - fix issue139 - introduce @pytest.fixture which allows direct scoping and parametrization of funcarg factories. - fix issue198 - conftest fixtures were not found on windows32 in some circumstances with nested directory structures due to path manipulation issues - fix issue193 skip test functions with were parametrized with empty parameter sets - fix python3.3 compat, mostly reporting bits that previously depended on dict ordering - introduce re-ordering of tests by resource and parametrization setup which takes precedence to the usual file-ordering - fix issue185 monkeypatching time.time does not cause pytest to fail - fix issue172 duplicate call of pytest.fixture decoratored setup_module functions - fix junitxml=path construction so that if tests change the current working directory and the path is a relative path it is constructed correctly from the original current working dir. - fix "python setup.py test" example to cause a proper "errno" return - fix issue165 - fix broken doc links and mention stackoverflow for FAQ - catch unicode-issues when writing failure representations to terminal to prevent the whole session from crashing - fix xfail/skip confusion: a skip-mark or an imperative pytest.skip will now take precedence before xfail-markers because we can't determine xfail/xpass status in case of a skip. see also: http://stackoverflow.com/questions/11105828/in-py-test-when-i-explicitly-skip-a-test-that-is-marked-as-xfail-how-can-i-get - always report installed 3rd party plugins in the header of a test run - fix issue160: a failing setup of an xfail-marked tests should be reported as xfail (not xpass) - fix issue128: show captured output when capsys/capfd are used - fix issue179: properly show the dependency chain of factories - pluginmanager.register(...) now raises ValueError if the plugin has been already registered or the name is taken - fix issue159: improve http://pytest.org/latest/faq.html especially with respect to the "magic" history, also mention pytest-django, trial and unittest integration. - make request.keywords and node.keywords writable. All descendant collection nodes will see keyword values. Keywords are dictionaries containing markers and other info. - fix issue 178: xml binary escapes are now wrapped in py.xml.raw - fix issue 176: correctly catch the builtin AssertionError even when we replaced AssertionError with a subclass on the python level - factory discovery no longer fails with magic global callables that provide no sane __code__ object (mock.call for example) - fix issue 182: testdir.inprocess_run now considers passed plugins - fix issue 188: ensure sys.exc_info is clear on python2 before calling into a test - fix issue 191: add unittest TestCase runTest method support - fix issue 156: monkeypatch correctly handles class level descriptors - reporting refinements: - pytest_report_header now receives a "startdir" so that you can use startdir.bestrelpath(yourpath) to show nice relative path - allow plugins to implement both pytest_report_header and pytest_sessionstart (sessionstart is invoked first). - don't show deselected reason line if there is none - py.test -vv will show all of assert comparisons instead of truncating 2.2.4 (2012-05-22) ================== - fix error message for rewritten assertions involving the % operator - fix issue 126: correctly match all invalid xml characters for junitxml binary escape - fix issue with unittest: now @unittest.expectedFailure markers should be processed correctly (you can also use @pytest.mark markers) - document integration with the extended distribute/setuptools test commands - fix issue 140: properly get the real functions of bound classmethods for setup/teardown_class - fix issue #141: switch from the deceased paste.pocoo.org to bpaste.net - fix issue #143: call unconfigure/sessionfinish always when configure/sessionstart where called - fix issue #144: better mangle test ids to junitxml classnames - upgrade distribute_setup.py to 0.6.27 2.2.3 (2012-02-05) ================== - fix uploaded package to only include necessary files 2.2.2 (2012-02-05) ================== - fix issue101: wrong args to unittest.TestCase test function now produce better output - fix issue102: report more useful errors and hints for when a test directory was renamed and some pyc/__pycache__ remain - fix issue106: allow parametrize to be applied multiple times e.g. from module, class and at function level. - fix issue107: actually perform session scope finalization - don't check in parametrize if indirect parameters are funcarg names - add chdir method to monkeypatch funcarg - fix crash resulting from calling monkeypatch undo a second time - fix issue115: make --collectonly robust against early failure (missing files/directories) - "-qq --collectonly" now shows only files and the number of tests in them - "-q --collectonly" now shows test ids - allow adding of attributes to test reports such that it also works with distributed testing (no upgrade of pytest-xdist needed) 2.2.1 (2011-12-16) ================== - fix issue99 (in pytest and py) internallerrors with resultlog now produce better output - fixed by normalizing pytest_internalerror input arguments. - fix issue97 / traceback issues (in pytest and py) improve traceback output in conjunction with jinja2 and cython which hack tracebacks - fix issue93 (in pytest and pytest-xdist) avoid "delayed teardowns": the final test in a test node will now run its teardown directly instead of waiting for the end of the session. Thanks Dave Hunt for the good reporting and feedback. The pytest_runtest_protocol as well as the pytest_runtest_teardown hooks now have "nextitem" available which will be None indicating the end of the test run. - fix collection crash due to unknown-source collected items, thanks to Ralf Schmitt (fixed by depending on a more recent pylib) 2.2.0 (2011-11-18) ================== - fix issue90: introduce eager tearing down of test items so that teardown function are called earlier. - add an all-powerful metafunc.parametrize function which allows to parametrize test function arguments in multiple steps and therefore from independent plugins and places. - add a @pytest.mark.parametrize helper which allows to easily call a test function with different argument values - Add examples to the "parametrize" example page, including a quick port of Test scenarios and the new parametrize function and decorator. - introduce registration for "pytest.mark.*" helpers via ini-files or through plugin hooks. Also introduce a "--strict" option which will treat unregistered markers as errors allowing to avoid typos and maintain a well described set of markers for your test suite. See exaples at http://pytest.org/latest/mark.html and its links. - issue50: introduce "-m marker" option to select tests based on markers (this is a stricter and more predictable version of '-k' in that "-m" only matches complete markers and has more obvious rules for and/or semantics. - new feature to help optimizing the speed of your tests: --durations=N option for displaying N slowest test calls and setup/teardown methods. - fix issue87: --pastebin now works with python3 - fix issue89: --pdb with unexpected exceptions in doctest work more sensibly - fix and cleanup pytest's own test suite to not leak FDs - fix issue83: link to generated funcarg list - fix issue74: pyarg module names are now checked against imp.find_module false positives - fix compatibility with twisted/trial-11.1.0 use cases - simplify Node.listchain - simplify junitxml output code by relying on py.xml - add support for skip properties on unittest classes and functions 2.1.3 (2011-10-18) ================== - fix issue79: assertion rewriting failed on some comparisons in boolops - correctly handle zero length arguments (a la pytest '') - fix issue67 / junitxml now contains correct test durations, thanks ronny - fix issue75 / skipping test failure on jython - fix issue77 / Allow assertrepr_compare hook to apply to a subset of tests 2.1.2 (2011-09-24) ================== - fix assertion rewriting on files with windows newlines on some Python versions - refine test discovery by package/module name (--pyargs), thanks Florian Mayer - fix issue69 / assertion rewriting fixed on some boolean operations - fix issue68 / packages now work with assertion rewriting - fix issue66: use different assertion rewriting caches when the -O option is passed - don't try assertion rewriting on Jython, use reinterp 2.1.1 ===== - fix issue64 / pytest.set_trace now works within pytest_generate_tests hooks - fix issue60 / fix error conditions involving the creation of __pycache__ - fix issue63 / assertion rewriting on inserts involving strings containing '%' - fix assertion rewriting on calls with a ** arg - don't cache rewritten modules if bytecode generation is disabled - fix assertion rewriting in read-only directories - fix issue59: provide system-out/err tags for junitxml output - fix issue61: assertion rewriting on boolean operations with 3 or more operands - you can now build a man page with "cd doc ; make man" 2.1.0 (2011-07-09) ================== - fix issue53 call nosestyle setup functions with correct ordering - fix issue58 and issue59: new assertion code fixes - merge Benjamin's assertionrewrite branch: now assertions for test modules on python 2.6 and above are done by rewriting the AST and saving the pyc file before the test module is imported. see doc/assert.txt for more info. - fix issue43: improve doctests with better traceback reporting on unexpected exceptions - fix issue47: timing output in junitxml for test cases is now correct - fix issue48: typo in MarkInfo repr leading to exception - fix issue49: avoid confusing error when initizaliation partially fails - fix issue44: env/username expansion for junitxml file path - show releaselevel information in test runs for pypy - reworked doc pages for better navigation and PDF generation - report KeyboardInterrupt even if interrupted during session startup - fix issue 35 - provide PDF doc version and download link from index page 2.0.3 (2011-05-11) ================== - fix issue38: nicer tracebacks on calls to hooks, particularly early configure/sessionstart ones - fix missing skip reason/meta information in junitxml files, reported via http://lists.idyll.org/pipermail/testing-in-python/2011-March/003928.html - fix issue34: avoid collection failure with "test" prefixed classes deriving from object. - don't require zlib (and other libs) for genscript plugin without --genscript actually being used. - speed up skips (by not doing a full traceback representation internally) - fix issue37: avoid invalid characters in junitxml's output 2.0.2 (2011-03-09) ================== - tackle issue32 - speed up test runs of very quick test functions by reducing the relative overhead - fix issue30 - extended xfail/skipif handling and improved reporting. If you have a syntax error in your skip/xfail expressions you now get nice error reports. Also you can now access module globals from xfail/skipif expressions so that this for example works now:: import pytest import mymodule @pytest.mark.skipif("mymodule.__version__[0] == "1") def test_function(): pass This will not run the test function if the module's version string does not start with a "1". Note that specifying a string instead of a boolean expressions allows py.test to report meaningful information when summarizing a test run as to what conditions lead to skipping (or xfail-ing) tests. - fix issue28 - setup_method and pytest_generate_tests work together The setup_method fixture method now gets called also for test function invocations generated from the pytest_generate_tests hook. - fix issue27 - collectonly and keyword-selection (-k) now work together Also, if you do "py.test --collectonly -q" you now get a flat list of test ids that you can use to paste to the py.test commandline in order to execute a particular test. - fix issue25 avoid reported problems with --pdb and python3.2/encodings output - fix issue23 - tmpdir argument now works on Python3.2 and WindowsXP Starting with Python3.2 os.symlink may be supported. By requiring a newer py lib version the py.path.local() implementation acknowledges this. - fixed typos in the docs (thanks Victor Garcia, Brianna Laugher) and particular thanks to Laura Creighton who also reviewed parts of the documentation. - fix slightly wrong output of verbose progress reporting for classes (thanks Amaury) - more precise (avoiding of) deprecation warnings for node.Class|Function accesses - avoid std unittest assertion helper code in tracebacks (thanks Ronny) 2.0.1 (2011-02-07) ================== - refine and unify initial capturing so that it works nicely even if the logging module is used on an early-loaded conftest.py file or plugin. - allow to omit "()" in test ids to allow for uniform test ids as produced by Alfredo's nice pytest.vim plugin. - fix issue12 - show plugin versions with "--version" and "--traceconfig" and also document how to add extra information to reporting test header - fix issue17 (import-* reporting issue on python3) by requiring py>1.4.0 (1.4.1 is going to include it) - fix issue10 (numpy arrays truth checking) by refining assertion interpretation in py lib - fix issue15: make nose compatibility tests compatible with python3 (now that nose-1.0 supports python3) - remove somewhat surprising "same-conftest" detection because it ignores conftest.py when they appear in several subdirs. - improve assertions ("not in"), thanks Floris Bruynooghe - improve behaviour/warnings when running on top of "python -OO" (assertions and docstrings are turned off, leading to potential false positives) - introduce a pytest_cmdline_processargs(args) hook to allow dynamic computation of command line arguments. This fixes a regression because py.test prior to 2.0 allowed to set command line options from conftest.py files which so far pytest-2.0 only allowed from ini-files now. - fix issue7: assert failures in doctest modules. unexpected failures in doctests will not generally show nicer, i.e. within the doctest failing context. - fix issue9: setup/teardown functions for an xfail-marked test will report as xfail if they fail but report as normally passing (not xpassing) if they succeed. This only is true for "direct" setup/teardown invocations because teardown_class/ teardown_module cannot closely relate to a single test. - fix issue14: no logging errors at process exit - refinements to "collecting" output on non-ttys - refine internal plugin registration and --traceconfig output - introduce a mechanism to prevent/unregister plugins from the command line, see http://pytest.org/plugins.html#cmdunregister - activate resultlog plugin by default - fix regression wrt yielded tests which due to the collection-before-running semantics were not setup as with pytest 1.3.4. Note, however, that the recommended and much cleaner way to do test parametraization remains the "pytest_generate_tests" mechanism, see the docs. 2.0.0 (2010-11-25) ================== - pytest-2.0 is now its own package and depends on pylib-2.0 - new ability: python -m pytest / python -m pytest.main ability - new python invocation: pytest.main(args, plugins) to load some custom plugins early. - try harder to run unittest test suites in a more compatible manner by deferring setup/teardown semantics to the unittest package. also work harder to run twisted/trial and Django tests which should now basically work by default. - introduce a new way to set config options via ini-style files, by default setup.cfg and tox.ini files are searched. The old ways (certain environment variables, dynamic conftest.py reading is removed). - add a new "-q" option which decreases verbosity and prints a more nose/unittest-style "dot" output. - fix issue135 - marks now work with unittest test cases as well - fix issue126 - introduce py.test.set_trace() to trace execution via PDB during the running of tests even if capturing is ongoing. - fix issue123 - new "python -m py.test" invocation for py.test (requires Python 2.5 or above) - fix issue124 - make reporting more resilient against tests opening files on filedescriptor 1 (stdout). - fix issue109 - sibling conftest.py files will not be loaded. (and Directory collectors cannot be customized anymore from a Directory's conftest.py - this needs to happen at least one level up). - introduce (customizable) assertion failure representations and enhance output on assertion failures for comparisons and other cases (Floris Bruynooghe) - nose-plugin: pass through type-signature failures in setup/teardown functions instead of not calling them (Ed Singleton) - remove py.test.collect.Directory (follows from a major refactoring and simplification of the collection process) - majorly reduce py.test core code, shift function/python testing to own plugin - fix issue88 (finding custom test nodes from command line arg) - refine 'tmpdir' creation, will now create basenames better associated with test names (thanks Ronny) - "xpass" (unexpected pass) tests don't cause exitcode!=0 - fix issue131 / issue60 - importing doctests in __init__ files used as namespace packages - fix issue93 stdout/stderr is captured while importing conftest.py - fix bug: unittest collected functions now also can have "pytestmark" applied at class/module level - add ability to use "class" level for cached_setup helper - fix strangeness: mark.* objects are now immutable, create new instances 1.3.4 (2010-09-14) ================== - fix issue111: improve install documentation for windows - fix issue119: fix custom collectability of __init__.py as a module - fix issue116: --doctestmodules work with __init__.py files as well - fix issue115: unify internal exception passthrough/catching/GeneratorExit - fix issue118: new --tb=native for presenting cpython-standard exceptions 1.3.3 (2010-07-30) ================== - fix issue113: assertion representation problem with triple-quoted strings (and possibly other cases) - make conftest loading detect that a conftest file with the same content was already loaded, avoids surprises in nested directory structures which can be produced e.g. by Hudson. It probably removes the need to use --confcutdir in most cases. - fix terminal coloring for win32 (thanks Michael Foord for reporting) - fix weirdness: make terminal width detection work on stdout instead of stdin (thanks Armin Ronacher for reporting) - remove trailing whitespace in all py/text distribution files 1.3.2 (2010-07-08) ================== **New features** - fix issue103: introduce py.test.raises as context manager, examples:: with py.test.raises(ZeroDivisionError): x = 0 1 / x with py.test.raises(RuntimeError) as excinfo: call_something() # you may do extra checks on excinfo.value|type|traceback here (thanks Ronny Pfannschmidt) - Funcarg factories can now dynamically apply a marker to a test invocation. This is for example useful if a factory provides parameters to a test which are expected-to-fail:: def pytest_funcarg__arg(request): request.applymarker(py.test.mark.xfail(reason="flaky config")) ... def test_function(arg): ... - improved error reporting on collection and import errors. This makes use of a more general mechanism, namely that for custom test item/collect nodes ``node.repr_failure(excinfo)`` is now uniformly called so that you can override it to return a string error representation of your choice which is going to be reported as a (red) string. - introduce '--junitprefix=STR' option to prepend a prefix to all reports in the junitxml file. **Bug fixes** - make tests and the ``pytest_recwarn`` plugin in particular fully compatible to Python2.7 (if you use the ``recwarn`` funcarg warnings will be enabled so that you can properly check for their existence in a cross-python manner). - refine --pdb: ignore xfailed tests, unify its TB-reporting and don't display failures again at the end. - fix assertion interpretation with the ** operator (thanks Benjamin Peterson) - fix issue105 assignment on the same line as a failing assertion (thanks Benjamin Peterson) - fix issue104 proper escaping for test names in junitxml plugin (thanks anonymous) - fix issue57 -f|--looponfail to work with xpassing tests (thanks Ronny) - fix issue92 collectonly reporter and --pastebin (thanks Benjamin Peterson) - fix py.code.compile(source) to generate unique filenames - fix assertion re-interp problems on PyPy, by defering code compilation to the (overridable) Frame.eval class. (thanks Amaury Forgeot) - fix py.path.local.pyimport() to work with directories - streamline py.path.local.mkdtemp implementation and usage - don't print empty lines when showing junitxml-filename - add optional boolean ignore_errors parameter to py.path.local.remove - fix terminal writing on win32/python2.4 - py.process.cmdexec() now tries harder to return properly encoded unicode objects on all python versions - install plain py.test/py.which scripts also for Jython, this helps to get canonical script paths in virtualenv situations - make path.bestrelpath(path) return ".", note that when calling X.bestrelpath the assumption is that X is a directory. - make initial conftest discovery ignore "--" prefixed arguments - fix resultlog plugin when used in a multicpu/multihost xdist situation (thanks Jakub Gustak) - perform distributed testing related reporting in the xdist-plugin rather than having dist-related code in the generic py.test distribution - fix homedir detection on Windows - ship distribute_setup.py version 0.6.13 1.3.1 (2010-05-25) ================== **New features** - issue91: introduce new py.test.xfail(reason) helper to imperatively mark a test as expected to fail. Can be used from within setup and test functions. This is useful especially for parametrized tests when certain configurations are expected-to-fail. In this case the declarative approach with the @py.test.mark.xfail cannot be used as it would mark all configurations as xfail. - issue102: introduce new --maxfail=NUM option to stop test runs after NUM failures. This is a generalization of the '-x' or '--exitfirst' option which is now equivalent to '--maxfail=1'. Both '-x' and '--maxfail' will now also print a line near the end indicating the Interruption. - issue89: allow py.test.mark decorators to be used on classes (class decorators were introduced with python2.6) and also allow to have multiple markers applied at class/module level by specifying a list. - improve and refine letter reporting in the progress bar: . pass f failed test s skipped tests (reminder: use for dependency/platform mismatch only) x xfailed test (test that was expected to fail) X xpassed test (test that was expected to fail but passed) You can use any combination of 'fsxX' with the '-r' extended reporting option. The xfail/xpass results will show up as skipped tests in the junitxml output - which also fixes issue99. - make py.test.cmdline.main() return the exitstatus instead of raising SystemExit and also allow it to be called multiple times. This of course requires that your application and tests are properly teared down and don't have global state. **Bug Fixes** - improved traceback presentation: - improved and unified reporting for "--tb=short" option - Errors during test module imports are much shorter, (using --tb=short style) - raises shows shorter more relevant tracebacks - --fulltrace now more systematically makes traces longer / inhibits cutting - improve support for raises and other dynamically compiled code by manipulating python's linecache.cache instead of the previous rather hacky way of creating custom code objects. This makes it seemlessly work on Jython and PyPy where it previously didn't. - fix issue96: make capturing more resilient against Control-C interruptions (involved somewhat substantial refactoring to the underlying capturing functionality to avoid race conditions). - fix chaining of conditional skipif/xfail decorators - so it works now as expected to use multiple @py.test.mark.skipif(condition) decorators, including specific reporting which of the conditions lead to skipping. - fix issue95: late-import zlib so that it's not required for general py.test startup. - fix issue94: make reporting more robust against bogus source code (and internally be more careful when presenting unexpected byte sequences) 1.3.0 (2010-05-05) ================== - deprecate --report option in favour of a new shorter and easier to remember -r option: it takes a string argument consisting of any combination of 'xfsX' characters. They relate to the single chars you see during the dotted progress printing and will print an extra line per test at the end of the test run. This extra line indicates the exact position or test ID that you directly paste to the py.test cmdline in order to re-run a particular test. - allow external plugins to register new hooks via the new pytest_addhooks(pluginmanager) hook. The new release of the pytest-xdist plugin for distributed and looponfailing testing requires this feature. - add a new pytest_ignore_collect(path, config) hook to allow projects and plugins to define exclusion behaviour for their directory structure - for example you may define in a conftest.py this method:: def pytest_ignore_collect(path): return path.check(link=1) to prevent even a collection try of any tests in symlinked dirs. - new pytest_pycollect_makemodule(path, parent) hook for allowing customization of the Module collection object for a matching test module. - extend and refine xfail mechanism: ``@py.test.mark.xfail(run=False)`` do not run the decorated test ``@py.test.mark.xfail(reason="...")`` prints the reason string in xfail summaries specifying ``--runxfail`` on command line virtually ignores xfail markers - expose (previously internal) commonly useful methods: py.io.get_terminal_with() -> return terminal width py.io.ansi_print(...) -> print colored/bold text on linux/win32 py.io.saferepr(obj) -> return limited representation string - expose test outcome related exceptions as py.test.skip.Exception, py.test.raises.Exception etc., useful mostly for plugins doing special outcome interpretation/tweaking - (issue85) fix junitxml plugin to handle tests with non-ascii output - fix/refine python3 compatibility (thanks Benjamin Peterson) - fixes for making the jython/win32 combination work, note however: jython2.5.1/win32 does not provide a command line launcher, see http://bugs.jython.org/issue1491 . See pylib install documentation for how to work around. - fixes for handling of unicode exception values and unprintable objects - (issue87) fix unboundlocal error in assertionold code - (issue86) improve documentation for looponfailing - refine IO capturing: stdin-redirect pseudo-file now has a NOP close() method - ship distribute_setup.py version 0.6.10 - added links to the new capturelog and coverage plugins 1.2.0 (2010-01-18) ================== - refined usage and options for "py.cleanup":: py.cleanup # remove "*.pyc" and "*$py.class" (jython) files py.cleanup -e .swp -e .cache # also remove files with these extensions py.cleanup -s # remove "build" and "dist" directory next to setup.py files py.cleanup -d # also remove empty directories py.cleanup -a # synonym for "-s -d -e 'pip-log.txt'" py.cleanup -n # dry run, only show what would be removed - add a new option "py.test --funcargs" which shows available funcargs and their help strings (docstrings on their respective factory function) for a given test path - display a short and concise traceback if a funcarg lookup fails - early-load "conftest.py" files in non-dot first-level sub directories. allows to conveniently keep and access test-related options in a ``test`` subdir and still add command line options. - fix issue67: new super-short traceback-printing option: "--tb=line" will print a single line for each failing (python) test indicating its filename, lineno and the failure value - fix issue78: always call python-level teardown functions even if the according setup failed. This includes refinements for calling setup_module/class functions which will now only be called once instead of the previous behaviour where they'd be called multiple times if they raise an exception (including a Skipped exception). Any exception will be re-corded and associated with all tests in the according module/class scope. - fix issue63: assume <40 columns to be a bogus terminal width, default to 80 - fix pdb debugging to be in the correct frame on raises-related errors - update apipkg.py to fix an issue where recursive imports might unnecessarily break importing - fix plugin links 1.1.1 (2009-11-24) ================== - moved dist/looponfailing from py.test core into a new separately released pytest-xdist plugin. - new junitxml plugin: --junitxml=path will generate a junit style xml file which is processable e.g. by the Hudson CI system. - new option: --genscript=path will generate a standalone py.test script which will not need any libraries installed. thanks to Ralf Schmitt. - new option: --ignore will prevent specified path from collection. Can be specified multiple times. - new option: --confcutdir=dir will make py.test only consider conftest files that are relative to the specified dir. - new funcarg: "pytestconfig" is the pytest config object for access to command line args and can now be easily used in a test. - install ``py.test`` and ``py.which`` with a ``-$VERSION`` suffix to disambiguate between Python3, python2.X, Jython and PyPy installed versions. - new "pytestconfig" funcarg allows access to test config object - new "pytest_report_header" hook can return additional lines to be displayed at the header of a test run. - (experimental) allow "py.test path::name1::name2::..." for pointing to a test within a test collection directly. This might eventually evolve as a full substitute to "-k" specifications. - streamlined plugin loading: order is now as documented in customize.html: setuptools, ENV, commandline, conftest. also setuptools entry point names are turned to canonical namees ("pytest_*") - automatically skip tests that need 'capfd' but have no os.dup - allow pytest_generate_tests to be defined in classes as well - deprecate usage of 'disabled' attribute in favour of pytestmark - deprecate definition of Directory, Module, Class and Function nodes in conftest.py files. Use pytest collect hooks instead. - collection/item node specific runtest/collect hooks are only called exactly on matching conftest.py files, i.e. ones which are exactly below the filesystem path of an item - change: the first pytest_collect_directory hook to return something will now prevent further hooks to be called. - change: figleaf plugin now requires --figleaf to run. Also change its long command line options to be a bit shorter (see py.test -h). - change: pytest doctest plugin is now enabled by default and has a new option --doctest-glob to set a pattern for file matches. - change: remove internal py._* helper vars, only keep py._pydir - robustify capturing to survive if custom pytest_runtest_setup code failed and prevented the capturing setup code from running. - make py.test.* helpers provided by default plugins visible early - works transparently both for pydoc and for interactive sessions which will regularly see e.g. py.test.mark and py.test.importorskip. - simplify internal plugin manager machinery - simplify internal collection tree by introducing a RootCollector node - fix assert reinterpreation that sees a call containing "keyword=..." - fix issue66: invoke pytest_sessionstart and pytest_sessionfinish hooks on slaves during dist-testing, report module/session teardown hooks correctly. - fix issue65: properly handle dist-testing if no execnet/py lib installed remotely. - skip some install-tests if no execnet is available - fix docs, fix internal bin/ script generation 1.1.0 (2009-11-05) ================== - introduce automatic plugin registration via 'pytest11' entrypoints via setuptools' pkg_resources.iter_entry_points - fix py.test dist-testing to work with execnet >= 1.0.0b4 - re-introduce py.test.cmdline.main() for better backward compatibility - svn paths: fix a bug with path.check(versioned=True) for svn paths, allow '%' in svn paths, make svnwc.update() default to interactive mode like in 1.0.x and add svnwc.update(interactive=False) to inhibit interaction. - refine distributed tarball to contain test and no pyc files - try harder to have deprecation warnings for py.compat.* accesses report a correct location 1.0.3 ===== * adjust and improve docs * remove py.rest tool and internal namespace - it was never really advertised and can still be used with the old release if needed. If there is interest it could be revived into its own tool i guess. * fix issue48 and issue59: raise an Error if the module from an imported test file does not seem to come from the filepath - avoids "same-name" confusion that has been reported repeatedly * merged Ronny's nose-compatibility hacks: now nose-style setup_module() and setup() functions are supported * introduce generalized py.test.mark function marking * reshuffle / refine command line grouping * deprecate parser.addgroup in favour of getgroup which creates option group * add --report command line option that allows to control showing of skipped/xfailed sections * generalized skipping: a new way to mark python functions with skipif or xfail at function, class and modules level based on platform or sys-module attributes. * extend py.test.mark decorator to allow for positional args * introduce and test "py.cleanup -d" to remove empty directories * fix issue #59 - robustify unittest test collection * make bpython/help interaction work by adding an __all__ attribute to ApiModule, cleanup initpkg * use MIT license for pylib, add some contributors * remove py.execnet code and substitute all usages with 'execnet' proper * fix issue50 - cached_setup now caches more to expectations for test functions with multiple arguments. * merge Jarko's fixes, issue #45 and #46 * add the ability to specify a path for py.lookup to search in * fix a funcarg cached_setup bug probably only occurring in distributed testing and "module" scope with teardown. * many fixes and changes for making the code base python3 compatible, many thanks to Benjamin Peterson for helping with this. * consolidate builtins implementation to be compatible with >=2.3, add helpers to ease keeping 2 and 3k compatible code * deprecate py.compat.doctest|subprocess|textwrap|optparse * deprecate py.magic.autopath, remove py/magic directory * move pytest assertion handling to py/code and a pytest_assertion plugin, add "--no-assert" option, deprecate py.magic namespaces in favour of (less) py.code ones. * consolidate and cleanup py/code classes and files * cleanup py/misc, move tests to bin-for-dist * introduce delattr/delitem/delenv methods to py.test's monkeypatch funcarg * consolidate py.log implementation, remove old approach. * introduce py.io.TextIO and py.io.BytesIO for distinguishing between text/unicode and byte-streams (uses underlying standard lib io.* if available) * make py.unittest_convert helper script available which converts "unittest.py" style files into the simpler assert/direct-test-classes py.test/nosetests style. The script was written by Laura Creighton. * simplified internal localpath implementation 1.0.2 (2009-08-27) ================== * fixing packaging issues, triggered by fedora redhat packaging, also added doc, examples and contrib dirs to the tarball. * added a documentation link to the new django plugin. 1.0.1 (2009-08-19) ================== * added a 'pytest_nose' plugin which handles nose.SkipTest, nose-style function/method/generator setup/teardown and tries to report functions correctly. * capturing of unicode writes or encoded strings to sys.stdout/err work better, also terminalwriting was adapted and somewhat unified between windows and linux. * improved documentation layout and content a lot * added a "--help-config" option to show conftest.py / ENV-var names for all longopt cmdline options, and some special conftest.py variables. renamed 'conf_capture' conftest setting to 'option_capture' accordingly. * fix issue #27: better reporting on non-collectable items given on commandline (e.g. pyc files) * fix issue #33: added --version flag (thanks Benjamin Peterson) * fix issue #32: adding support for "incomplete" paths to wcpath.status() * "Test" prefixed classes are *not* collected by default anymore if they have an __init__ method * monkeypatch setenv() now accepts a "prepend" parameter * improved reporting of collection error tracebacks * simplified multicall mechanism and plugin architecture, renamed some internal methods and argnames 1.0.0 (2009-08-04) ================== * more terse reporting try to show filesystem path relatively to current dir * improve xfail output a bit 1.0.0b9 (2009-07-31) ==================== * cleanly handle and report final teardown of test setup * fix svn-1.6 compat issue with py.path.svnwc().versioned() (thanks Wouter Vanden Hove) * setup/teardown or collection problems now show as ERRORs or with big "E"'s in the progress lines. they are reported and counted separately. * dist-testing: properly handle test items that get locally collected but cannot be collected on the remote side - often due to platform/dependency reasons * simplified py.test.mark API - see keyword plugin documentation * integrate better with logging: capturing now by default captures test functions and their immediate setup/teardown in a single stream * capsys and capfd funcargs now have a readouterr() and a close() method (underlyingly py.io.StdCapture/FD objects are used which grew a readouterr() method as well to return snapshots of captured out/err) * make assert-reinterpretation work better with comparisons not returning bools (reported with numpy from thanks maciej fijalkowski) * reworked per-test output capturing into the pytest_iocapture.py plugin and thus removed capturing code from config object * item.repr_failure(excinfo) instead of item.repr_failure(excinfo, outerr) 1.0.0b8 (2009-07-22) ==================== * pytest_unittest-plugin is now enabled by default * introduced pytest_keyboardinterrupt hook and refined pytest_sessionfinish hooked, added tests. * workaround a buggy logging module interaction ("closing already closed files"). Thanks to Sridhar Ratnakumar for triggering. * if plugins use "py.test.importorskip" for importing a dependency only a warning will be issued instead of exiting the testing process. * many improvements to docs: - refined funcargs doc , use the term "factory" instead of "provider" - added a new talk/tutorial doc page - better download page - better plugin docstrings - added new plugins page and automatic doc generation script * fixed teardown problem related to partially failing funcarg setups (thanks MrTopf for reporting), "pytest_runtest_teardown" is now always invoked even if the "pytest_runtest_setup" failed. * tweaked doctest output for docstrings in py modules, thanks Radomir. 1.0.0b7 ======= * renamed py.test.xfail back to py.test.mark.xfail to avoid two ways to decorate for xfail * re-added py.test.mark decorator for setting keywords on functions (it was actually documented so removing it was not nice) * remove scope-argument from request.addfinalizer() because request.cached_setup has the scope arg. TOOWTDI. * perform setup finalization before reporting failures * apply modified patches from Andreas Kloeckner to allow test functions to have no func_code (#22) and to make "-k" and function keywords work (#20) * apply patch from Daniel Peolzleithner (issue #23) * resolve issue #18, multiprocessing.Manager() and redirection clash * make __name__ == "__channelexec__" for remote_exec code 1.0.0b3 (2009-06-19) ==================== * plugin classes are removed: one now defines hooks directly in conftest.py or global pytest_*.py files. * added new pytest_namespace(config) hook that allows to inject helpers directly to the py.test.* namespace. * documented and refined many hooks * added new style of generative tests via pytest_generate_tests hook that integrates well with function arguments. 1.0.0b1 ======= * introduced new "funcarg" setup method, see doc/test/funcarg.txt * introduced plugin architecture and many new py.test plugins, see doc/test/plugins.txt * teardown_method is now guaranteed to get called after a test method has run. * new method: py.test.importorskip(mod,minversion) will either import or call py.test.skip() * completely revised internal py.test architecture * new py.process.ForkedFunc object allowing to fork execution of a function to a sub process and getting a result back. XXX lots of things missing here XXX 0.9.2 ===== * refined installation and metadata, created new setup.py, now based on setuptools/ez_setup (thanks to Ralf Schmitt for his support). * improved the way of making py.* scripts available in windows environments, they are now added to the Scripts directory as ".cmd" files. * py.path.svnwc.status() now is more complete and uses xml output from the 'svn' command if available (Guido Wesdorp) * fix for py.path.svn* to work with svn 1.5 (Chris Lamb) * fix path.relto(otherpath) method on windows to use normcase for checking if a path is relative. * py.test's traceback is better parseable from editors (follows the filenames:LINENO: MSG convention) (thanks to Osmo Salomaa) * fix to javascript-generation, "py.test --runbrowser" should work more reliably now * removed previously accidentally added py.test.broken and py.test.notimplemented helpers. * there now is a py.__version__ attribute 0.9.1 ===== This is a fairly complete list of v0.9.1, which can serve as a reference for developers. * allowing + signs in py.path.svn urls [39106] * fixed support for Failed exceptions without excinfo in py.test [39340] * added support for killing processes for Windows (as well as platforms that support os.kill) in py.misc.killproc [39655] * added setup/teardown for generative tests to py.test [40702] * added detection of FAILED TO LOAD MODULE to py.test [40703, 40738, 40739] * fixed problem with calling .remove() on wcpaths of non-versioned files in py.path [44248] * fixed some import and inheritance issues in py.test [41480, 44648, 44655] * fail to run greenlet tests when pypy is available, but without stackless [45294] * small fixes in rsession tests [45295] * fixed issue with 2.5 type representations in py.test [45483, 45484] * made that internal reporting issues displaying is done atomically in py.test [45518] * made that non-existing files are ignored by the py.lookup script [45519] * improved exception name creation in py.test [45535] * made that less threads are used in execnet [merge in 45539] * removed lock required for atomic reporting issue displaying in py.test [45545] * removed globals from execnet [45541, 45547] * refactored cleanup mechanics, made that setDaemon is set to 1 to make atexit get called in 2.5 (py.execnet) [45548] * fixed bug in joining threads in py.execnet's servemain [45549] * refactored py.test.rsession tests to not rely on exact output format anymore [45646] * using repr() on test outcome [45647] * added 'Reason' classes for py.test.skip() [45648, 45649] * killed some unnecessary sanity check in py.test.collect [45655] * avoid using os.tmpfile() in py.io.fdcapture because on Windows it's only usable by Administrators [45901] * added support for locking and non-recursive commits to py.path.svnwc [45994] * locking files in py.execnet to prevent CPython from segfaulting [46010] * added export() method to py.path.svnurl * fixed -d -x in py.test [47277] * fixed argument concatenation problem in py.path.svnwc [49423] * restore py.test behaviour that it exits with code 1 when there are failures [49974] * don't fail on html files that don't have an accompanying .txt file [50606] * fixed 'utestconvert.py < input' [50645] * small fix for code indentation in py.code.source [50755] * fix _docgen.py documentation building [51285] * improved checks for source representation of code blocks in py.test [51292] * added support for passing authentication to py.path.svn* objects [52000, 52001] * removed sorted() call for py.apigen tests in favour of [].sort() to support Python 2.3 [52481] pytest-4.6.9/CONTRIBUTING.rst0000664000372000037200000002721013604173400016365 0ustar travistravis00000000000000============================ Contribution getting started ============================ Contributions are highly welcomed and appreciated. Every little help counts, so do not hesitate! .. contents:: Contribution links :depth: 2 .. _submitfeedback: Feature requests and feedback ----------------------------- Do you like pytest? Share some love on Twitter or in your blog posts! We'd also like to hear about your propositions and suggestions. Feel free to `submit them as issues `_ and: * Explain in detail how they should work. * Keep the scope as narrow as possible. This will make it easier to implement. .. _reportbugs: Report bugs ----------- Report bugs for pytest in the `issue tracker `_. If you are reporting a bug, please include: * Your operating system name and version. * Any details about your local setup that might be helpful in troubleshooting, specifically the Python interpreter version, installed libraries, and pytest version. * Detailed steps to reproduce the bug. If you can write a demonstration test that currently fails but should pass (xfail), that is a very useful commit to make as well, even if you cannot fix the bug itself. .. _fixbugs: Fix bugs -------- Look through the `GitHub issues for bugs `_. :ref:`Talk ` to developers to find out how you can fix specific bugs. Don't forget to check the issue trackers of your favourite plugins, too! .. _writeplugins: Implement features ------------------ Look through the `GitHub issues for enhancements `_. :ref:`Talk ` to developers to find out how you can implement specific features. Write documentation ------------------- Pytest could always use more documentation. What exactly is needed? * More complementary documentation. Have you perhaps found something unclear? * Documentation translations. We currently have only English. * Docstrings. There can never be too many of them. * Blog posts, articles and such -- they're all very appreciated. You can also edit documentation files directly in the GitHub web interface, without using a local copy. This can be convenient for small fixes. .. note:: Build the documentation locally with the following command: .. code:: bash $ tox -e docs The built documentation should be available in the ``doc/en/_build/``. Where 'en' refers to the documentation language. .. _submitplugin: Submitting Plugins to pytest-dev -------------------------------- Pytest development of the core, some plugins and support code happens in repositories living under the ``pytest-dev`` organisations: - `pytest-dev on GitHub `_ - `pytest-dev on Bitbucket `_ All pytest-dev Contributors team members have write access to all contained repositories. Pytest core and plugins are generally developed using `pull requests`_ to respective repositories. The objectives of the ``pytest-dev`` organisation are: * Having a central location for popular pytest plugins * Sharing some of the maintenance responsibility (in case a maintainer no longer wishes to maintain a plugin) You can submit your plugin by subscribing to the `pytest-dev mail list `_ and writing a mail pointing to your existing pytest plugin repository which must have the following: - PyPI presence with a ``setup.py`` that contains a license, ``pytest-`` prefixed name, version number, authors, short and long description. - a ``tox.ini`` for running tests using `tox `_. - a ``README.txt`` describing how to use the plugin and on which platforms it runs. - a ``LICENSE.txt`` file or equivalent containing the licensing information, with matching info in ``setup.py``. - an issue tracker for bug reports and enhancement requests. - a `changelog `_ If no contributor strongly objects and two agree, the repository can then be transferred to the ``pytest-dev`` organisation. Here's a rundown of how a repository transfer usually proceeds (using a repository named ``joedoe/pytest-xyz`` as example): * ``joedoe`` transfers repository ownership to ``pytest-dev`` administrator ``calvin``. * ``calvin`` creates ``pytest-xyz-admin`` and ``pytest-xyz-developers`` teams, inviting ``joedoe`` to both as **maintainer**. * ``calvin`` transfers repository to ``pytest-dev`` and configures team access: - ``pytest-xyz-admin`` **admin** access; - ``pytest-xyz-developers`` **write** access; The ``pytest-dev/Contributors`` team has write access to all projects, and every project administrator is in it. We recommend that each plugin has at least three people who have the right to release to PyPI. Repository owners can rest assured that no ``pytest-dev`` administrator will ever make releases of your repository or take ownership in any way, except in rare cases where someone becomes unresponsive after months of contact attempts. As stated, the objective is to share maintenance and avoid "plugin-abandon". .. _`pull requests`: .. _pull-requests: Preparing Pull Requests ----------------------- Short version ~~~~~~~~~~~~~ #. Fork the repository. #. Enable and install `pre-commit `_ to ensure style-guides and code checks are followed. #. Target ``master`` for bugfixes and doc changes. #. Target ``features`` for new features or functionality changes. #. Follow **PEP-8** for naming and `black `_ for formatting. #. Tests are run using ``tox``:: tox -e linting,py27,py37 The test environments above are usually enough to cover most cases locally. #. Write a ``changelog`` entry: ``changelog/2574.bugfix``, use issue id number and one of ``bugfix``, ``removal``, ``feature``, ``vendor``, ``doc`` or ``trivial`` for the issue type. #. Unless your change is a trivial or a documentation fix (e.g., a typo or reword of a small section) please add yourself to the ``AUTHORS`` file, in alphabetical order. Long version ~~~~~~~~~~~~ What is a "pull request"? It informs the project's core developers about the changes you want to review and merge. Pull requests are stored on `GitHub servers `_. Once you send a pull request, we can discuss its potential modifications and even add more commits to it later on. There's an excellent tutorial on how Pull Requests work in the `GitHub Help Center `_. Here is a simple overview, with pytest-specific bits: #. Fork the `pytest GitHub repository `__. It's fine to use ``pytest`` as your fork repository name because it will live under your user. #. Clone your fork locally using `git `_ and create a branch:: $ git clone git@github.com:YOUR_GITHUB_USERNAME/pytest.git $ cd pytest # now, to fix a bug create your own branch off "master": $ git checkout -b your-bugfix-branch-name master # or to instead add a feature create your own branch off "features": $ git checkout -b your-feature-branch-name features Given we have "major.minor.micro" version numbers, bugfixes will usually be released in micro releases whereas features will be released in minor releases and incompatible changes in major releases. If you need some help with Git, follow this quick start guide: https://git.wiki.kernel.org/index.php/QuickStart #. Install `pre-commit `_ and its hook on the pytest repo:: $ pip install --user pre-commit $ pre-commit install Afterwards ``pre-commit`` will run whenever you commit. https://pre-commit.com/ is a framework for managing and maintaining multi-language pre-commit hooks to ensure code-style and code formatting is consistent. #. Install tox Tox is used to run all the tests and will automatically setup virtualenvs to run the tests in. (will implicitly use http://www.virtualenv.org/en/latest/):: $ pip install tox #. Run all the tests You need to have Python 2.7 and 3.7 available in your system. Now running tests is as simple as issuing this command:: $ tox -e linting,py27,py37 This command will run tests via the "tox" tool against Python 2.7 and 3.7 and also perform "lint" coding-style checks. #. You can now edit your local working copy and run the tests again as necessary. Please follow PEP-8 for naming. You can pass different options to ``tox``. For example, to run tests on Python 2.7 and pass options to pytest (e.g. enter pdb on failure) to pytest you can do:: $ tox -e py27 -- --pdb Or to only run tests in a particular test module on Python 3.7:: $ tox -e py37 -- testing/test_config.py When committing, ``pre-commit`` will re-format the files if necessary. #. Commit and push once your tests pass and you are happy with your change(s):: $ git commit -a -m "" $ git push -u #. Create a new changelog entry in ``changelog``. The file should be named ``.``, where *issueid* is the number of the issue related to the change and *type* is one of ``bugfix``, ``removal``, ``feature``, ``vendor``, ``doc`` or ``trivial``. #. Add yourself to ``AUTHORS`` file if not there yet, in alphabetical order. #. Finally, submit a pull request through the GitHub website using this data:: head-fork: YOUR_GITHUB_USERNAME/pytest compare: your-branch-name base-fork: pytest-dev/pytest base: master # if it's a bugfix base: features # if it's a feature Writing Tests ---------------------------- Writing tests for plugins or for pytest itself is often done using the `testdir fixture `_, as a "black-box" test. For example, to ensure a simple test passes you can write: .. code-block:: python def test_true_assertion(testdir): testdir.makepyfile( """ def test_foo(): assert True """ ) result = testdir.runpytest() result.assert_outcomes(failed=0, passed=1) Alternatively, it is possible to make checks based on the actual output of the termal using *glob-like* expressions: .. code-block:: python def test_true_assertion(testdir): testdir.makepyfile( """ def test_foo(): assert False """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*assert False*", "*1 failed*"]) When choosing a file where to write a new test, take a look at the existing files and see if there's one file which looks like a good fit. For example, a regression test about a bug in the ``--lf`` option should go into ``test_cacheprovider.py``, given that this option is implemented in ``cacheprovider.py``. If in doubt, go ahead and open a PR with your best guess and we can discuss this over the code. Joining the Development Team ---------------------------- Anyone who has successfully seen through a pull request which did not require any extra work from the development team to merge will themselves gain commit access if they so wish (if we forget to ask please send a friendly reminder). This does not mean your workflow to contribute changes, everyone goes through the same pull-request-and-review process and no-one merges their own pull requests unless already approved. It does however mean you can participate in the development process more fully since you can merge pull requests from other contributors yourself after having reviewed them. pytest-4.6.9/PKG-INFO0000664000372000037200000001545413604173421015033 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: pytest Version: 4.6.9 Summary: pytest: simple powerful testing with Python Home-page: https://docs.pytest.org/en/latest/ Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others License: MIT license Project-URL: Source, https://github.com/pytest-dev/pytest Project-URL: Tracker, https://github.com/pytest-dev/pytest/issues Description: .. image:: https://docs.pytest.org/en/latest/_static/pytest1.png :target: https://docs.pytest.org/en/latest/ :align: center :alt: pytest ------ .. image:: https://img.shields.io/pypi/v/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest .. image:: https://img.shields.io/pypi/pyversions/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://codecov.io/gh/pytest-dev/pytest/branch/master/graph/badge.svg :target: https://codecov.io/gh/pytest-dev/pytest :alt: Code coverage Status .. image:: https://travis-ci.org/pytest-dev/pytest.svg?branch=master :target: https://travis-ci.org/pytest-dev/pytest .. image:: https://dev.azure.com/pytest-dev/pytest/_apis/build/status/pytest-CI?branchName=master :target: https://dev.azure.com/pytest-dev/pytest .. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/python/black .. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg :target: https://www.codetriage.com/pytest-dev/pytest The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example of a simple test: .. code-block:: python # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 To execute it:: $ pytest ============================= test session starts ============================= collected 1 items test_sample.py F ================================== FAILURES =================================== _________________________________ test_answer _________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:5: AssertionError ========================== 1 failed in 0.04 seconds =========================== Due to ``pytest``'s detailed assertion introspection, only plain ``assert`` statements are used. See `getting-started `_ for more examples. Features -------- - Detailed info on failing `assert statements `_ (no need to remember ``self.assert*`` names); - `Auto-discovery `_ of test modules and functions; - `Modular fixtures `_ for managing small or parametrized long-lived test resources; - Can run `unittest `_ (or trial), `nose `_ test suites out of the box; - Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; Documentation ------------- For full documentation, including installation, tutorials and PDF documents, please see https://docs.pytest.org/en/latest/. Bugs/Requests ------------- Please use the `GitHub issue tracker `_ to submit bugs or request features. Changelog --------- Consult the `Changelog `__ page for fixes and enhancements of each version. Support pytest -------------- You can support pytest by obtaining a `Tideflift subscription`_. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools. .. _`Tideflift subscription`: https://tidelift.com/subscription/pkg/pypi-pytest?utm_source=pypi-pytest&utm_medium=referral&utm_campaign=readme Security ^^^^^^^^ pytest has never been associated with a security vunerability, but in any case, to report a security vulnerability please use the `Tidelift security contact `_. Tidelift will coordinate the fix and disclosure. License ------- Copyright Holger Krekel and others, 2004-2020. Distributed under the terms of the `MIT`_ license, pytest is free and open source software. .. _`MIT`: https://github.com/pytest-dev/pytest/blob/master/LICENSE Keywords: test,unittest Platform: unix Platform: linux Platform: osx Platform: cygwin Platform: win32 Classifier: Development Status :: 6 - Mature Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: POSIX Classifier: Operating System :: Microsoft :: Windows Classifier: Operating System :: MacOS :: MacOS X Classifier: Topic :: Software Development :: Testing Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: Utilities Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Requires-Python: !=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7 Provides-Extra: testing pytest-4.6.9/codecov.yml0000664000372000037200000000012613604173400016066 0ustar travistravis00000000000000coverage: status: project: true patch: true changes: true comment: off pytest-4.6.9/doc/0000775000372000037200000000000013604173421014472 5ustar travistravis00000000000000pytest-4.6.9/doc/en/0000775000372000037200000000000013604173421015074 5ustar travistravis00000000000000pytest-4.6.9/doc/en/recwarn.rst0000664000372000037200000000010613604173400017261 0ustar travistravis00000000000000:orphan: This page has been moved, please see :ref:`assertwarnings`. pytest-4.6.9/doc/en/contributing.rst0000664000372000037200000000006713604173400020335 0ustar travistravis00000000000000.. _contributing: .. include:: ../../CONTRIBUTING.rst pytest-4.6.9/doc/en/changelog.rst0000664000372000037200000000006213604173400017550 0ustar travistravis00000000000000 .. _changelog: .. include:: ../../CHANGELOG.rst pytest-4.6.9/doc/en/announce/0000775000372000037200000000000013604173421016702 5ustar travistravis00000000000000pytest-4.6.9/doc/en/announce/release-4.1.0.rst0000664000372000037200000000166613604173400021520 0ustar travistravis00000000000000pytest-4.1.0 ======================================= The pytest team is proud to announce the 4.1.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Adam Johnson * Aly Sivji * Andrey Paramonov * Anthony Sottile * Bruno Oliveira * Daniel Hahler * David Vo * Hyunchel Kim * Jeffrey Rackauckas * Kanguros * Nicholas Devenish * Pedro Algarvio * Randy Barlow * Ronny Pfannschmidt * Tomer Keren * feuillemorte * wim glenn Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.0.1.rst0000664000372000037200000000602113604173400021504 0ustar travistravis00000000000000py.test 2.0.1: bug fixes =========================================================================== Welcome to pytest-2.0.1, a maintenance and bug fix release of pytest, a mature testing tool for Python, supporting CPython 2.4-3.2, Jython and latest PyPy interpreters. See extensive docs with tested examples here: http://pytest.org/ If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest Many thanks to all issue reporters and people asking questions or complaining. Particular thanks to Floris Bruynooghe and Ronny Pfannschmidt for their great coding contributions and many others for feedback and help. best, holger krekel Changes between 2.0.0 and 2.0.1 ---------------------------------------------- - refine and unify initial capturing so that it works nicely even if the logging module is used on an early-loaded conftest.py file or plugin. - fix issue12 - show plugin versions with "--version" and "--traceconfig" and also document how to add extra information to reporting test header - fix issue17 (import-* reporting issue on python3) by requiring py>1.4.0 (1.4.1 is going to include it) - fix issue10 (numpy arrays truth checking) by refining assertion interpretation in py lib - fix issue15: make nose compatibility tests compatible with python3 (now that nose-1.0 supports python3) - remove somewhat surprising "same-conftest" detection because it ignores conftest.py when they appear in several subdirs. - improve assertions ("not in"), thanks Floris Bruynooghe - improve behaviour/warnings when running on top of "python -OO" (assertions and docstrings are turned off, leading to potential false positives) - introduce a pytest_cmdline_processargs(args) hook to allow dynamic computation of command line arguments. This fixes a regression because py.test prior to 2.0 allowed to set command line options from conftest.py files which so far pytest-2.0 only allowed from ini-files now. - fix issue7: assert failures in doctest modules. unexpected failures in doctests will not generally show nicer, i.e. within the doctest failing context. - fix issue9: setup/teardown functions for an xfail-marked test will report as xfail if they fail but report as normally passing (not xpassing) if they succeed. This only is true for "direct" setup/teardown invocations because teardown_class/ teardown_module cannot closely relate to a single test. - fix issue14: no logging errors at process exit - refinements to "collecting" output on non-ttys - refine internal plugin registration and --traceconfig output - introduce a mechanism to prevent/unregister plugins from the command line, see http://pytest.org/latest/plugins.html#cmdunregister - activate resultlog plugin by default - fix regression wrt yielded tests which due to the collection-before-running semantics were not setup as with pytest 1.3.4. Note, however, that the recommended and much cleaner way to do test parametrization remains the "pytest_generate_tests" mechanism, see the docs. pytest-4.6.9/doc/en/announce/release-2.5.0.rst0000664000372000037200000001615313604173400021517 0ustar travistravis00000000000000pytest-2.5.0: now down to ZERO reported bugs! =========================================================================== pytest-2.5.0 is a big fixing release, the result of two community bug fixing days plus numerous additional works from many people and reporters. The release should be fully compatible to 2.4.2, existing plugins and test suites. We aim at maintaining this level of ZERO reported bugs because it's no fun if your testing tool has bugs, is it? Under a condition, though: when submitting a bug report please provide clear information about the circumstances and a simple example which reproduces the problem. The issue tracker is of course not empty now. We have many remaining "enhacement" issues which we'll hopefully can tackle in 2014 with your help. For those who use older Python versions, please note that pytest is not automatically tested on python2.5 due to virtualenv, setuptools and tox not supporting it anymore. Manual verification shows that it mostly works fine but it's not going to be part of the automated release process and thus likely to break in the future. As usual, current docs are at http://pytest.org and you can upgrade from pypi via:: pip install -U pytest Particular thanks for helping with this release go to Anatoly Bubenkoff, Floris Bruynooghe, Marc Abramowitz, Ralph Schmitt, Ronny Pfannschmidt, Donald Stufft, James Lan, Rob Dennis, Jason R. Coombs, Mathieu Agopian, Virgil Dupras, Bruno Oliveira, Alex Gaynor and others. have fun, holger krekel 2.5.0 ----------------------------------- - dropped python2.5 from automated release testing of pytest itself which means it's probably going to break soon (but still works with this release we believe). - simplified and fixed implementation for calling finalizers when parametrized fixtures or function arguments are involved. finalization is now performed lazily at setup time instead of in the "teardown phase". While this might sound odd at first, it helps to ensure that we are correctly handling setup/teardown even in complex code. User-level code should not be affected unless it's implementing the pytest_runtest_teardown hook and expecting certain fixture instances are torn down within (very unlikely and would have been unreliable anyway). - PR90: add --color=yes|no|auto option to force terminal coloring mode ("auto" is default). Thanks Marc Abramowitz. - fix issue319 - correctly show unicode in assertion errors. Many thanks to Floris Bruynooghe for the complete PR. Also means we depend on py>=1.4.19 now. - fix issue396 - correctly sort and finalize class-scoped parametrized tests independently from number of methods on the class. - refix issue323 in a better way -- parametrization should now never cause Runtime Recursion errors because the underlying algorithm for re-ordering tests per-scope/per-fixture is not recursive anymore (it was tail-call recursive before which could lead to problems for more than >966 non-function scoped parameters). - fix issue290 - there is preliminary support now for parametrizing with repeated same values (sometimes useful to test if calling a second time works as with the first time). - close issue240 - document precisely how pytest module importing works, discuss the two common test directory layouts, and how it interacts with PEP420-namespace packages. - fix issue246 fix finalizer order to be LIFO on independent fixtures depending on a parametrized higher-than-function scoped fixture. (was quite some effort so please bear with the complexity of this sentence :) Thanks Ralph Schmitt for the precise failure example. - fix issue244 by implementing special index for parameters to only use indices for paramentrized test ids - fix issue287 by running all finalizers but saving the exception from the first failing finalizer and re-raising it so teardown will still have failed. We reraise the first failing exception because it might be the cause for other finalizers to fail. - fix ordering when mock.patch or other standard decorator-wrappings are used with test methods. This fixues issue346 and should help with random "xdist" collection failures. Thanks to Ronny Pfannschmidt and Donald Stufft for helping to isolate it. - fix issue357 - special case "-k" expressions to allow for filtering with simple strings that are not valid python expressions. Examples: "-k 1.3" matches all tests parametrized with 1.3. "-k None" filters all tests that have "None" in their name and conversely "-k 'not None'". Previously these examples would raise syntax errors. - fix issue384 by removing the trial support code since the unittest compat enhancements allow trial to handle it on its own - don't hide an ImportError when importing a plugin produces one. fixes issue375. - fix issue275 - allow usefixtures and autouse fixtures for running doctest text files. - fix issue380 by making --resultlog only rely on longrepr instead of the "reprcrash" attribute which only exists sometimes. - address issue122: allow @pytest.fixture(params=iterator) by exploding into a list early on. - fix pexpect-3.0 compatibility for pytest's own tests. (fixes issue386) - allow nested parametrize-value markers, thanks James Lan for the PR. - fix unicode handling with new monkeypatch.setattr(import_path, value) API. Thanks Rob Dennis. Fixes issue371. - fix unicode handling with junitxml, fixes issue368. - In assertion rewriting mode on Python 2, fix the detection of coding cookies. See issue #330. - make "--runxfail" turn imperative pytest.xfail calls into no ops (it already did neutralize pytest.mark.xfail markers) - refine pytest / pkg_resources interactions: The AssertionRewritingHook PEP302 compliant loader now registers itself with setuptools/pkg_resources properly so that the pkg_resources.resource_stream method works properly. Fixes issue366. Thanks for the investigations and full PR to Jason R. Coombs. - pytestconfig fixture is now session-scoped as it is the same object during the whole test run. Fixes issue370. - avoid one surprising case of marker malfunction/confusion:: @pytest.mark.some(lambda arg: ...) def test_function(): would not work correctly because pytest assumes @pytest.mark.some gets a function to be decorated already. We now at least detect if this arg is a lambda and thus the example will work. Thanks Alex Gaynor for bringing it up. - xfail a test on pypy that checks wrong encoding/ascii (pypy does not error out). fixes issue385. - internally make varnames() deal with classes's __init__, although it's not needed by pytest itself atm. Also fix caching. Fixes issue376. - fix issue221 - handle importing of namespace-package with no __init__.py properly. - refactor internal FixtureRequest handling to avoid monkeypatching. One of the positive user-facing effects is that the "request" object can now be used in closures. - fixed version comparison in pytest.importskip(modname, minverstring) - fix issue377 by clarifying in the nose-compat docs that pytest does not duplicate the unittest-API into the "plain" namespace. - fix verbose reporting for @mock'd test functions pytest-4.6.9/doc/en/announce/release-2.2.0.rst0000664000372000037200000001034713604173400021513 0ustar travistravis00000000000000py.test 2.2.0: test marking++, parametrization++ and duration profiling =========================================================================== pytest-2.2.0 is a test-suite compatible release of the popular py.test testing tool. Plugins might need upgrades. It comes with these improvements: * easier and more powerful parametrization of tests: - new @pytest.mark.parametrize decorator to run tests with different arguments - new metafunc.parametrize() API for parametrizing arguments independently - see examples at http://pytest.org/latest/example/parametrize.html - NOTE that parametrize() related APIs are still a bit experimental and might change in future releases. * improved handling of test markers and refined marking mechanism: - "-m markexpr" option for selecting tests according to their mark - a new "markers" ini-variable for registering test markers for your project - the new "--strict" bails out with an error if using unregistered markers. - see examples at http://pytest.org/latest/example/markers.html * duration profiling: new "--duration=N" option showing the N slowest test execution or setup/teardown calls. This is most useful if you want to find out where your slowest test code is. * also 2.2.0 performs more eager calling of teardown/finalizers functions resulting in better and more accurate reporting when they fail Besides there is the usual set of bug fixes along with a cleanup of pytest's own test suite allowing it to run on a wider range of environments. For general information, see extensive docs with examples here: http://pytest.org/ If you want to install or upgrade pytest you might just type:: pip install -U pytest # or easy_install -U pytest Thanks to Ronny Pfannschmidt, David Burns, Jeff Donner, Daniel Nouri, Alfredo Deza and all who gave feedback or sent bug reports. best, holger krekel notes on incompatibility ------------------------------ While test suites should work unchanged you might need to upgrade plugins: * You need a new version of the pytest-xdist plugin (1.7) for distributing test runs. * Other plugins might need an upgrade if they implement the ``pytest_runtest_logreport`` hook which now is called unconditionally for the setup/teardown fixture phases of a test. You may choose to ignore setup/teardown failures by inserting "if rep.when != 'call': return" or something similar. Note that most code probably "just" works because the hook was already called for failing setup/teardown phases of a test so a plugin should have been ready to grok such reports already. Changes between 2.1.3 and 2.2.0 ---------------------------------------- - fix issue90: introduce eager tearing down of test items so that teardown function are called earlier. - add an all-powerful metafunc.parametrize function which allows to parametrize test function arguments in multiple steps and therefore from independent plugins and places. - add a @pytest.mark.parametrize helper which allows to easily call a test function with different argument values. - Add examples to the "parametrize" example page, including a quick port of Test scenarios and the new parametrize function and decorator. - introduce registration for "pytest.mark.*" helpers via ini-files or through plugin hooks. Also introduce a "--strict" option which will treat unregistered markers as errors allowing to avoid typos and maintain a well described set of markers for your test suite. See examples at http://pytest.org/latest/mark.html and its links. - issue50: introduce "-m marker" option to select tests based on markers (this is a stricter and more predictable version of "-k" in that "-m" only matches complete markers and has more obvious rules for and/or semantics. - new feature to help optimizing the speed of your tests: --durations=N option for displaying N slowest test calls and setup/teardown methods. - fix issue87: --pastebin now works with python3 - fix issue89: --pdb with unexpected exceptions in doctest work more sensibly - fix and cleanup pytest's own test suite to not leak FDs - fix issue83: link to generated funcarg list - fix issue74: pyarg module names are now checked against imp.find_module false positives - fix compatibility with twisted/trial-11.1.0 use cases pytest-4.6.9/doc/en/announce/release-2.5.2.rst0000664000372000037200000000356713604173400021526 0ustar travistravis00000000000000pytest-2.5.2: fixes =========================================================================== pytest is a mature Python testing tool with more than a 1000 tests against itself, passing on many different interpreters and platforms. The 2.5.2 release fixes a few bugs with two maybe-bugs remaining and actively being worked on (and waiting for the bug reporter's input). We also have a new contribution guide thanks to Piotr Banaszkiewicz and others. See docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to the following people who contributed to this release: Anatoly Bubenkov Ronny Pfannschmidt Floris Bruynooghe Bruno Oliveira Andreas Pelme Jurko Gospodnetić Piotr Banaszkiewicz Simon Liedtke lakka Lukasz Balcerzak Philippe Muller Daniel Hahler have fun, holger krekel 2.5.2 ----------------------------------- - fix issue409 -- better interoperate with cx_freeze by not trying to import from collections.abc which causes problems for py27/cx_freeze. Thanks Wolfgang L. for reporting and tracking it down. - fixed docs and code to use "pytest" instead of "py.test" almost everywhere. Thanks Jurko Gospodnetic for the complete PR. - fix issue425: mention at end of "py.test -h" that --markers and --fixtures work according to specified test path (or current dir) - fix issue413: exceptions with unicode attributes are now printed correctly also on python2 and with pytest-xdist runs. (the fix requires py-1.4.20) - copy, cleanup and integrate py.io capture from pylib 1.4.20.dev2 (rev 13d9af95547e) - address issue416: clarify docs as to conftest.py loading semantics - fix issue429: comparing byte strings with non-ascii chars in assert expressions now work better. Thanks Floris Bruynooghe. - make capfd/capsys.capture private, its unused and shouldn't be exposed pytest-4.6.9/doc/en/announce/release-2.1.1.rst0000664000372000037200000000305313604173400021507 0ustar travistravis00000000000000py.test 2.1.1: assertion fixes and improved junitxml output =========================================================================== pytest-2.1.1 is a backward compatible maintenance release of the popular py.test testing tool. See extensive docs with examples here: http://pytest.org/ Most bug fixes address remaining issues with the perfected assertions introduced with 2.1.0 - many thanks to the bug reporters and to Benjamin Peterson for helping to fix them. Also, junitxml output now produces system-out/err tags which lead to better displays of tracebacks with Jenkins. Also a quick note to package maintainers and others interested: there now is a "pytest" man page which can be generated with "make man" in doc/. If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest best, holger krekel / http://merlinux.eu Changes between 2.1.0 and 2.1.1 ---------------------------------------------- - fix issue64 / pytest.set_trace now works within pytest_generate_tests hooks - fix issue60 / fix error conditions involving the creation of __pycache__ - fix issue63 / assertion rewriting on inserts involving strings containing '%' - fix assertion rewriting on calls with a ** arg - don't cache rewritten modules if bytecode generation is disabled - fix assertion rewriting in read-only directories - fix issue59: provide system-out/err tags for junitxml output - fix issue61: assertion rewriting on boolean operations with 3 or more operands - you can now build a man page with "cd doc ; make man" pytest-4.6.9/doc/en/announce/release-3.4.1.rst0000664000372000037200000000104713604173400021514 0ustar travistravis00000000000000pytest-3.4.1 ======================================= pytest 3.4.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Aaron * Alan Velasco * Andy Freeland * Brian Maissy * Bruno Oliveira * Florian Bruhin * Jason R. Coombs * Marcin Bachry * Pedro Algarvio * Ronny Pfannschmidt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.8.6.rst0000664000372000037200000000357013604173400021527 0ustar travistravis00000000000000pytest-2.8.6 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.5. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: AMiT Kumar Bruno Oliveira Erik M. Bray Florian Bruhin Georgy Dyuldin Jeff Widman Kartik Singhal Loïc Estève Manu Phatak Peter Demin Rick van Hattem Ronny Pfannschmidt Ulrich Petri foxx Happy testing, The py.test Development Team 2.8.6 (compared to 2.8.5) ------------------------- - fix #1259: allow for double nodeids in junitxml, this was a regression failing plugins combinations like pytest-pep8 + pytest-flakes - Workaround for exception that occurs in pyreadline when using ``--pdb`` with standard I/O capture enabled. Thanks Erik M. Bray for the PR. - fix #900: Better error message in case the target of a ``monkeypatch`` call raises an ``ImportError``. - fix #1292: monkeypatch calls (setattr, setenv, etc.) are now O(1). Thanks David R. MacIver for the report and Bruno Oliveira for the PR. - fix #1223: captured stdout and stderr are now properly displayed before entering pdb when ``--pdb`` is used instead of being thrown away. Thanks Cal Leeming for the PR. - fix #1305: pytest warnings emitted during ``pytest_terminal_summary`` are now properly displayed. Thanks Ionel Maries Cristian for the report and Bruno Oliveira for the PR. - fix #628: fixed internal UnicodeDecodeError when doctests contain unicode. Thanks Jason R. Coombs for the report and Bruno Oliveira for the PR. - fix #1334: Add captured stdout to jUnit XML report on setup error. Thanks Georgy Dyuldin for the PR. pytest-4.6.9/doc/en/announce/release-3.2.5.rst0000664000372000037200000000063013604173400021513 0ustar travistravis00000000000000pytest-3.2.5 ======================================= pytest 3.2.5 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.0.3.rst0000664000372000037200000000106313604173400021510 0ustar travistravis00000000000000pytest-3.0.3 ============ pytest 3.0.3 has just been released to PyPI. This release fixes some regressions and bugs reported in the last version, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Florian Bruhin * Floris Bruynooghe * Huayi Zhang * Lev Maximov * Raquel Alegre * Ronny Pfannschmidt * Roy Williams * Tyler Goodlet * mbyt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.9.0.rst0000664000372000037200000000166613604173400021527 0ustar travistravis00000000000000pytest-3.9.0 ======================================= The pytest team is proud to announce the 3.9.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Andrea Cimatoribus * Ankit Goel * Anthony Sottile * Ben Eyal * Bruno Oliveira * Daniel Hahler * Jeffrey Rackauckas * Jose Carlos Menezes * Kyle Altendorf * Niklas JQ * Palash Chatterjee * Ronny Pfannschmidt * Thomas Hess * Thomas Hisch * Tomer Keren * Victor Maryama Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.7.0.rst0000664000372000037200000000156313604173400021521 0ustar travistravis00000000000000pytest-3.7.0 ======================================= The pytest team is proud to announce the 3.7.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Alan * Alan Brammer * Ammar Najjar * Anthony Sottile * Bruno Oliveira * Jeffrey Rackauckas * Kale Kundert * Ronny Pfannschmidt * Serhii Mozghovyi * Tadek Teleżyński * Wil Cooley * abrammer * avirlrma * turturica Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-4.4.1.rst0000664000372000037200000000067413604173400021522 0ustar travistravis00000000000000pytest-4.4.1 ======================================= pytest 4.4.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.7.rst0000664000372000037200000000065213604173400021526 0ustar travistravis00000000000000pytest-4.6.7 ======================================= pytest 4.6.7 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Daniel Hahler Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.3.rst0000664000372000037200000000071213604173400021517 0ustar travistravis00000000000000pytest-4.6.3 ======================================= pytest 4.6.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Dirk Thomas Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.9.rst0000664000372000037200000000067713604173400021537 0ustar travistravis00000000000000pytest-4.6.9 ======================================= pytest 4.6.9 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Felix Yan * Hugo Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.2.0.rst0000664000372000037200000000153013604173400021507 0ustar travistravis00000000000000pytest-4.2.0 ======================================= The pytest team is proud to announce the 4.2.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Adam Uhlir * Anthony Sottile * Bruno Oliveira * Christopher Dignam * Daniel Hahler * Joseph Hunkeler * Kristoffer Nordstroem * Ronny Pfannschmidt * Thomas Hisch * wim glenn Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-4.2.1.rst0000664000372000037200000000115013604173400021506 0ustar travistravis00000000000000pytest-4.2.1 ======================================= pytest 4.2.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Arel Cordero * Bruno Oliveira * Daniel Hahler * Holger Kohr * Kevin J. Foley * Nick Murphy * Paweł Stradomski * Raphael Pierzina * Ronny Pfannschmidt * Sam Brightman * Thomas Hisch * Zac Hatfield-Dodds Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.1.1.rst0000664000372000037200000000105213604173400021506 0ustar travistravis00000000000000pytest-4.1.1 ======================================= pytest 4.1.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Anton Lodder * Bruno Oliveira * Daniel Hahler * David Vo * Oscar Benjamin * Ronny Pfannschmidt * Victor Maryama * Yoav Caspi * dmitry.dygalo Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.0.1.rst0000664000372000037200000000075313604173400021514 0ustar travistravis00000000000000pytest-4.0.1 ======================================= pytest 4.0.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Michael D. Hoyle * Ronny Pfannschmidt * Slam Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.3.4.rst0000664000372000037200000000322613604173400021516 0ustar travistravis00000000000000pytest-2.3.4: stabilization, more flexible selection via "-k expr" =========================================================================== pytest-2.3.4 is a small stabilization release of the py.test tool which offers uebersimple assertions, scalable fixture mechanisms and deep customization for testing with Python. This release comes with the following fixes and features: - make "-k" option accept an expressions the same as with "-m" so that one can write: -k "name1 or name2" etc. This is a slight usage incompatibility if you used special syntax like "TestClass.test_method" which you now need to write as -k "TestClass and test_method" to match a certain method in a certain test class. - allow to dynamically define markers via item.keywords[...]=assignment integrating with "-m" option - yielded test functions will now have autouse-fixtures active but cannot accept fixtures as funcargs - it's anyway recommended to rather use the post-2.0 parametrize features instead of yield, see: http://pytest.org/latest/example/parametrize.html - fix autouse-issue where autouse-fixtures would not be discovered if defined in an a/conftest.py file and tests in a/tests/test_some.py - fix issue226 - LIFO ordering for fixture teardowns - fix issue224 - invocations with >256 char arguments now work - fix issue91 - add/discuss package/directory level setups in example - fixes related to autouse discovery and calling Thanks in particular to Thomas Waldmann for spotting and reporting issues. See http://pytest.org/ for general information. To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest best, holger krekel pytest-4.6.9/doc/en/announce/release-3.6.2.rst0000664000372000037200000000114013604173400021511 0ustar travistravis00000000000000pytest-3.6.2 ======================================= pytest 3.6.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Alan Velasco * Alex Barbato * Anthony Sottile * Bartosz Cierocki * Bruno Oliveira * Daniel Hahler * Guoqiang Zhang * Hynek Schlawack * John T. Wodder II * Michael Käufl * Ronny Pfannschmidt * Samuel Dion-Girardeau Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.6.4.rst0000664000372000037200000000076613604173400021530 0ustar travistravis00000000000000pytest-3.6.4 ======================================= pytest 3.6.4 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bernhard M. Wiedemann * Bruno Oliveira * Drew * E Hershey * Hugo Martins * Vlad Shcherbina Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.8.1.rst0000664000372000037200000000102413604173400021513 0ustar travistravis00000000000000pytest-3.8.1 ======================================= pytest 3.8.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Ankit Goel * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Maximilian Albert * Ronny Pfannschmidt * William Jamir Silva * wim glenn Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.0.7.rst0000664000372000037200000000120113604173400021506 0ustar travistravis00000000000000pytest-3.0.7 ============ pytest 3.0.7 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Barney Gale * Bruno Oliveira * Florian Bruhin * Floris Bruynooghe * Ionel Cristian Mărieș * Katerina Koukiou * NODA, Kai * Omer Hadari * Patrick Hayes * Ran Benita * Ronny Pfannschmidt * Victor Uriarte * Vidar Tonaas Fauske * Ville Skyttä * fbjorn * mbyt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.2.1.rst0000664000372000037200000000075113604173400021513 0ustar travistravis00000000000000pytest-3.2.1 ======================================= pytest 3.2.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Alex Gaynor * Bruno Oliveira * Florian Bruhin * Ronny Pfannschmidt * Srinivas Reddy Thatiparthy Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.3.3.rst0000664000372000037200000000405113604173400021512 0ustar travistravis00000000000000pytest-2.3.3: integration fixes, py24 support, ``*/**`` shown in traceback =========================================================================== pytest-2.3.3 is another stabilization release of the py.test tool which offers uebersimple assertions, scalable fixture mechanisms and deep customization for testing with Python. Particularly, this release provides: - integration fixes and improvements related to flask, numpy, nose, unittest, mock - makes pytest work on py24 again (yes, people sometimes still need to use it) - show ``*,**`` args in pytest tracebacks Thanks to Manuel Jacob, Thomas Waldmann, Ronny Pfannschmidt, Pavel Repin and Andreas Taumoefolau for providing patches and all for the issues. See http://pytest.org/ for general information. To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest best, holger krekel Changes between 2.3.2 and 2.3.3 ----------------------------------- - fix issue214 - parse modules that contain special objects like e. g. flask's request object which blows up on getattr access if no request is active. thanks Thomas Waldmann. - fix issue213 - allow to parametrize with values like numpy arrays that do not support an __eq__ operator - fix issue215 - split test_python.org into multiple files - fix issue148 - @unittest.skip on classes is now recognized and avoids calling setUpClass/tearDownClass, thanks Pavel Repin - fix issue209 - reintroduce python2.4 support by depending on newer pylib which re-introduced statement-finding for pre-AST interpreters - nose support: only call setup if it's a callable, thanks Andrew Taumoefolau - fix issue219 - add py2.4-3.3 classifiers to TROVE list - in tracebacks *,** arg values are now shown next to normal arguments (thanks Manuel Jacob) - fix issue217 - support mock.patch with pytest's fixtures - note that you need either mock-1.0.1 or the python3.3 builtin unittest.mock. - fix issue127 - improve documentation for pytest_addoption() and add a ``config.getoption(name)`` helper function for consistency. pytest-4.6.9/doc/en/announce/release-4.0.2.rst0000664000372000037200000000077413604173400021520 0ustar travistravis00000000000000pytest-4.0.2 ======================================= pytest 4.0.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Pedro Algarvio * Ronny Pfannschmidt * Tomer Keren * Yash Todi Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.10.1.rst0000664000372000037200000000100113604173400021557 0ustar travistravis00000000000000pytest-3.10.1 ======================================= pytest 3.10.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Boris Feld * Bruno Oliveira * Daniel Hahler * Fabien ZARIFIAN * Jon Dufresne * Ronny Pfannschmidt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.9.2.rst0000664000372000037200000000075413604173400021526 0ustar travistravis00000000000000pytest-3.9.2 ======================================= pytest 3.9.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Ankit Goel * Anthony Sottile * Bruno Oliveira * Ronny Pfannschmidt * Vincent Barbaresi * ykantor Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.4.2.rst0000664000372000037200000000262413604173400021516 0ustar travistravis00000000000000pytest-2.4.2: colorama on windows, plugin/tmpdir fixes =========================================================================== pytest-2.4.2 is another bug-fixing release: - on Windows require colorama and a newer py lib so that py.io.TerminalWriter() now uses colorama instead of its own ctypes hacks. (fixes issue365) thanks Paul Moore for bringing it up. - fix "-k" matching of tests where "repr" and "attr" and other names would cause wrong matches because of an internal implementation quirk (don't ask) which is now properly implemented. fixes issue345. - avoid tmpdir fixture to create too long filenames especially when parametrization is used (issue354) - fix pytest-pep8 and pytest-flakes / pytest interactions (collection names in mark plugin was assuming an item always has a function which is not true for those plugins etc.) Thanks Andi Zeidler. - introduce node.get_marker/node.add_marker API for plugins like pytest-pep8 and pytest-flakes to avoid the messy details of the node.keywords pseudo-dicts. Adapted docs. - remove attempt to "dup" stdout at startup as it's icky. the normal capturing should catch enough possibilities of tests messing up standard FDs. - add pluginmanager.do_configure(config) as a link to config.do_configure() for plugin-compatibility as usual, docs at http://pytest.org and upgrades via:: pip install -U pytest have fun, holger krekel pytest-4.6.9/doc/en/announce/release-3.3.2.rst0000664000372000037200000000112013604173400021504 0ustar travistravis00000000000000pytest-3.3.2 ======================================= pytest 3.3.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Antony Lee * Austin * Bruno Oliveira * Florian Bruhin * Floris Bruynooghe * Henk-Jaap Wagenaar * Jurko Gospodnetić * Ronny Pfannschmidt * Srinivas Reddy Thatiparthy * Thomas Hisch Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.8.3.rst0000664000372000037200000000340213604173400021516 0ustar travistravis00000000000000pytest-2.8.3: bug fixes ======================= pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.2. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Florian Bruhin Gabe Hollombe Gabriel Reis Hartmut Goebel John Vandenberg Lee Kamentsky Michael Birtwell Raphael Pierzina Ronny Pfannschmidt William Martin Stewart Happy testing, The py.test Development Team 2.8.3 (compared to 2.8.2) ----------------------------- - fix #1169: add __name__ attribute to testcases in TestCaseFunction to support the @unittest.skip decorator on functions and methods. Thanks Lee Kamentsky for the PR. - fix #1035: collecting tests if test module level obj has __getattr__(). Thanks Suor for the report and Bruno Oliveira / Tom Viner for the PR. - fix #331: don't collect tests if their failure cannot be reported correctly e.g. they are a callable instance of a class. - fix #1133: fixed internal error when filtering tracebacks where one entry belongs to a file which is no longer available. Thanks Bruno Oliveira for the PR. - enhancement made to highlight in red the name of the failing tests so they stand out in the output. Thanks Gabriel Reis for the PR. - add more talks to the documentation - extend documentation on the --ignore cli option - use pytest-runner for setuptools integration - minor fixes for interaction with OS X El Capitan system integrity protection (thanks Florian) pytest-4.6.9/doc/en/announce/release-2.6.0.rst0000664000372000037200000001327113604173400021516 0ustar travistravis00000000000000pytest-2.6.0: shorter tracebacks, new warning system, test runner compat =========================================================================== pytest is a mature Python testing tool with more than a 1000 tests against itself, passing on many different interpreters and platforms. The 2.6.0 release should be drop-in backward compatible to 2.5.2 and fixes a number of bugs and brings some new features, mainly: - shorter tracebacks by default: only the first (test function) entry and the last (failure location) entry are shown, the ones between only in "short" format. Use ``--tb=long`` to get back the old behaviour of showing "long" entries everywhere. - a new warning system which reports oddities during collection and execution. For example, ignoring collecting Test* classes with an ``__init__`` now produces a warning. - various improvements to nose/mock/unittest integration Note also that 2.6.0 departs with the "zero reported bugs" policy because it has been too hard to keep up with it, unfortunately. Instead we are for now rather bound to work on "upvoted" issues in the https://bitbucket.org/pytest-dev/pytest/issues?status=new&status=open&sort=-votes issue tracker. See docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed, among them: Benjamin Peterson Jurko Gospodnetić Floris Bruynooghe Marc Abramowitz Marc Schlaich Trevor Bekolay Bruno Oliveira Alex Groenholm have fun, holger krekel 2.6.0 ----------------------------------- - fix issue537: Avoid importing old assertion reinterpretation code by default. Thanks Benjamin Peterson. - fix issue364: shorten and enhance tracebacks representation by default. The new "--tb=auto" option (default) will only display long tracebacks for the first and last entry. You can get the old behaviour of printing all entries as long entries with "--tb=long". Also short entries by default are now printed very similarly to "--tb=native" ones. - fix issue514: teach assertion reinterpretation about private class attributes Thanks Benjamin Peterson. - change -v output to include full node IDs of tests. Users can copy a node ID from a test run, including line number, and use it as a positional argument in order to run only a single test. - fix issue 475: fail early and comprehensible if calling pytest.raises with wrong exception type. - fix issue516: tell in getting-started about current dependencies. - cleanup setup.py a bit and specify supported versions. Thanks Jurko Gospodnetic for the PR. - change XPASS colour to yellow rather then red when tests are run with -v. - fix issue473: work around mock putting an unbound method into a class dict when double-patching. - fix issue498: if a fixture finalizer fails, make sure that the fixture is still invalidated. - fix issue453: the result of the pytest_assertrepr_compare hook now gets it's newlines escaped so that format_exception does not blow up. - internal new warning system: pytest will now produce warnings when it detects oddities in your test collection or execution. Warnings are ultimately sent to a new pytest_logwarning hook which is currently only implemented by the terminal plugin which displays warnings in the summary line and shows more details when -rw (report on warnings) is specified. - change skips into warnings for test classes with an __init__ and callables in test modules which look like a test but are not functions. - fix issue436: improved finding of initial conftest files from command line arguments by using the result of parse_known_args rather than the previous flaky heuristics. Thanks Marc Abramowitz for tests and initial fixing approaches in this area. - fix issue #479: properly handle nose/unittest(2) SkipTest exceptions during collection/loading of test modules. Thanks to Marc Schlaich for the complete PR. - fix issue490: include pytest_load_initial_conftests in documentation and improve docstring. - fix issue472: clarify that ``pytest.config.getvalue()`` cannot work if it's triggered ahead of command line parsing. - merge PR123: improved integration with mock.patch decorator on tests. - fix issue412: messing with stdout/stderr FD-level streams is now captured without crashes. - fix issue483: trial/py33 works now properly. Thanks Daniel Grana for PR. - improve example for pytest integration with "python setup.py test" which now has a generic "-a" or "--pytest-args" option where you can pass additional options as a quoted string. Thanks Trevor Bekolay. - simplified internal capturing mechanism and made it more robust against tests or setups changing FD1/FD2, also better integrated now with pytest.pdb() in single tests. - improvements to pytest's own test-suite leakage detection, courtesy of PRs from Marc Abramowitz - fix issue492: avoid leak in test_writeorg. Thanks Marc Abramowitz. - fix issue493: don't run tests in doc directory with ``python setup.py test`` (use tox -e doctesting for that) - fix issue486: better reporting and handling of early conftest loading failures - some cleanup and simplification of internal conftest handling. - work a bit harder to break reference cycles when catching exceptions. Thanks Jurko Gospodnetic. - fix issue443: fix skip examples to use proper comparison. Thanks Alex Groenholm. - support nose-style ``__test__`` attribute on modules, classes and functions, including unittest-style Classes. If set to False, the test will not be collected. - fix issue512: show "" for arguments which might not be set in monkeypatch plugin. Improves output in documentation. - avoid importing "py.test" (an old alias module for "pytest") pytest-4.6.9/doc/en/announce/release-3.0.0.rst0000664000372000037200000000335513604173400021513 0ustar travistravis00000000000000pytest-3.0.0 ============ The pytest team is proud to announce the 3.0.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a lot of bugs fixes and improvements, and much of the work done on it was possible because of the 2016 Sprint[1], which was funded by an indiegogo campaign which raised over US$12,000 with nearly 100 backers. There's a "What's new in pytest 3.0" [2] blog post highlighting the major features in this release. To see the complete changelog and documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: AbdealiJK Ana Ribeiro Antony Lee Brandon W Maister Brianna Laugher Bruno Oliveira Ceridwen Christian Boelsen Daniel Hahler Danielle Jenkins Dave Hunt Diego Russo Dmitry Dygalo Edoardo Batini Eli Boyarski Florian Bruhin Floris Bruynooghe Greg Price Guyzmo HEAD KANGAROO JJ Javi Romero Javier Domingo Cansino Kale Kundert Kalle Bronsen Marius Gedminas Matt Williams Mike Lundy Oliver Bestwalter Omar Kohl Raphael Pierzina RedBeardCode Roberto Polli Romain Dorgueil Roman Bolshakov Ronny Pfannschmidt Stefan Zimmermann Steffen Allner Tareq Alayan Ted Xiao Thomas Grainger Tom Viner TomV Vasily Kuznetsov aostr marscher palaviv satoru taschini Happy testing, The Pytest Development Team [1] http://blog.pytest.org/2016/pytest-development-sprint/ [2] http://blog.pytest.org/2016/whats-new-in-pytest-30/ pytest-4.6.9/doc/en/announce/release-2.2.4.rst0000664000372000037200000000265613604173400021523 0ustar travistravis00000000000000pytest-2.2.4: bug fixes, better junitxml/unittest/python3 compat =========================================================================== pytest-2.2.4 is a minor backward-compatible release of the versatile py.test testing tool. It contains bug fixes and a few refinements to junitxml reporting, better unittest- and python3 compatibility. For general information see here: http://pytest.org/ To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest Special thanks for helping on this release to Ronny Pfannschmidt and Benjamin Peterson and the contributors of issues. best, holger krekel Changes between 2.2.3 and 2.2.4 ----------------------------------- - fix error message for rewritten assertions involving the % operator - fix issue 126: correctly match all invalid xml characters for junitxml binary escape - fix issue with unittest: now @unittest.expectedFailure markers should be processed correctly (you can also use @pytest.mark markers) - document integration with the extended distribute/setuptools test commands - fix issue 140: properly get the real functions of bound classmethods for setup/teardown_class - fix issue #141: switch from the deceased paste.pocoo.org to bpaste.net - fix issue #143: call unconfigure/sessionfinish always when configure/sessionstart where called - fix issue #144: better mangle test ids to junitxml classnames - upgrade distribute_setup.py to 0.6.27 pytest-4.6.9/doc/en/announce/release-3.1.0.rst0000664000372000037200000000226313604173400021511 0ustar travistravis00000000000000pytest-3.1.0 ======================================= The pytest team is proud to announce the 3.1.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Sottile * Ben Lloyd * Bruno Oliveira * David Giese * David Szotten * Dmitri Pribysh * Florian Bruhin * Florian Schulze * Floris Bruynooghe * John Towler * Jonas Obrist * Katerina Koukiou * Kodi Arfer * Krzysztof Szularz * Lev Maximov * Loïc Estève * Luke Murphy * Manuel Krebber * Matthew Duck * Matthias Bussonnier * Michael Howitz * Michal Wajszczuk * Paweł Adamczak * Rafael Bertoldi * Ravi Chandra * Ronny Pfannschmidt * Skylar Downes * Thomas Kriechbaumer * Vitaly Lashmanov * Vlad Dragos * Wheerd * Xander Johnson * mandeep * reut Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.9.1.rst0000664000372000037200000000356513604173400021527 0ustar travistravis00000000000000pytest-2.9.1 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Daniel Hahler Dmitry Malinovsky Florian Bruhin Floris Bruynooghe Matt Bachmann Ronny Pfannschmidt TomV Vladimir Bolshakov Zearin palaviv Happy testing, The py.test Development Team 2.9.1 (compared to 2.9.0) ------------------------- **Bug Fixes** * Improve error message when a plugin fails to load. Thanks `@nicoddemus`_ for the PR. * Fix (`#1178 `_): ``pytest.fail`` with non-ascii characters raises an internal pytest error. Thanks `@nicoddemus`_ for the PR. * Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_). * Fix (`#578 `_): SyntaxErrors containing non-ascii lines at the point of failure generated an internal py.test error. Thanks `@asottile`_ for the report and `@nicoddemus`_ for the PR. * Fix (`#1437`_): When passing in a bytestring regex pattern to parameterize attempt to decode it as utf-8 ignoring errors. * Fix (`#649`_): parametrized test nodes cannot be specified to run on the command line. .. _#1437: https://github.com/pytest-dev/pytest/issues/1437 .. _#469: https://github.com/pytest-dev/pytest/issues/469 .. _#1431: https://github.com/pytest-dev/pytest/pull/1431 .. _#649: https://github.com/pytest-dev/pytest/issues/649 .. _@asottile: https://github.com/asottile .. _@nicoddemus: https://github.com/nicoddemus .. _@tomviner: https://github.com/tomviner pytest-4.6.9/doc/en/announce/release-2.6.2.rst0000664000372000037200000000277613604173400021530 0ustar travistravis00000000000000pytest-2.6.2: few fixes and cx_freeze support =========================================================================== pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is drop-in compatible to 2.5.2 and 2.6.X. It also brings support for including pytest with cx_freeze or similar freezing tools into your single-file app distribution. For details see the CHANGELOG below. See docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed, among them: Floris Bruynooghe Benjamin Peterson Bruno Oliveira have fun, holger krekel 2.6.2 ----------- - Added function pytest.freeze_includes(), which makes it easy to embed pytest into executables using tools like cx_freeze. See docs for examples and rationale. Thanks Bruno Oliveira. - Improve assertion rewriting cache invalidation precision. - fixed issue561: adapt autouse fixture example for python3. - fixed issue453: assertion rewriting issue with __repr__ containing "\n{", "\n}" and "\n~". - fix issue560: correctly display code if an "else:" or "finally:" is followed by statements on the same line. - Fix example in monkeypatch documentation, thanks t-8ch. - fix issue572: correct tmpdir doc example for python3. - Do not mark as universal wheel because Python 2.6 is different from other builds due to the extra argparse dependency. Fixes issue566. Thanks sontek. pytest-4.6.9/doc/en/announce/release-2.6.1.rst0000664000372000037200000000370113604173400021514 0ustar travistravis00000000000000pytest-2.6.1: fixes and new xfail feature =========================================================================== pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. The 2.6.1 release is drop-in compatible to 2.5.2 and actually fixes some regressions introduced with 2.6.0. It also brings a little feature to the xfail marker which now recognizes expected exceptions, see the CHANGELOG below. See docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed, among them: Floris Bruynooghe Bruno Oliveira Nicolas Delaby have fun, holger krekel Changes 2.6.1 ================= - No longer show line numbers in the --verbose output, the output is now purely the nodeid. The line number is still shown in failure reports. Thanks Floris Bruynooghe. - fix issue437 where assertion rewriting could cause pytest-xdist slaves to collect different tests. Thanks Bruno Oliveira. - fix issue555: add "errors" attribute to capture-streams to satisfy some distutils and possibly other code accessing sys.stdout.errors. - fix issue547 capsys/capfd also work when output capturing ("-s") is disabled. - address issue170: allow pytest.mark.xfail(...) to specify expected exceptions via an optional "raises=EXC" argument where EXC can be a single exception or a tuple of exception classes. Thanks David Mohr for the complete PR. - fix integration of pytest with unittest.mock.patch decorator when it uses the "new" argument. Thanks Nicolas Delaby for test and PR. - fix issue with detecting conftest files if the arguments contain "::" node id specifications (copy pasted from "-v" output) - fix issue544 by only removing "@NUM" at the end of "::" separated parts and if the part has a ".py" extension - don't use py.std import helper, rather import things directly. Thanks Bruno Oliveira. pytest-4.6.9/doc/en/announce/release-4.3.0.rst0000664000372000037200000000150713604173400021514 0ustar travistravis00000000000000pytest-4.3.0 ======================================= The pytest team is proud to announce the 4.3.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Andras Mitzki * Anthony Sottile * Bruno Oliveira * Christian Fetzer * Daniel Hahler * Grygorii Iermolenko * R. Alex Matevish * Ronny Pfannschmidt * cclauss Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.2.4.rst0000664000372000037200000000131413604173400021512 0ustar travistravis00000000000000pytest-3.2.4 ======================================= pytest 3.2.4 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Christian Boelsen * Christoph Buchner * Daw-Ran Liou * Florian Bruhin * Franck Michea * Leonard Lausen * Matty G * Owen Tuz * Pavel Karateev * Pierre GIRAUD * Ronny Pfannschmidt * Stephen Finucane * Sviatoslav Abakumov * Thomas Hisch * Tom Dalton * Xuan Luong * Yorgos Pagles * Семён Марьясин Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.2.3.rst0000664000372000037200000000074213604173400021515 0ustar travistravis00000000000000pytest-3.2.3 ======================================= pytest 3.2.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Evan * Joe Hamman * Oliver Bestwalter * Ronny Pfannschmidt * Xuan Luong Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.0.2.rst0000664000372000037200000000535613604173400021517 0ustar travistravis00000000000000py.test 2.0.2: bug fixes, improved xfail/skip expressions, speed ups =========================================================================== Welcome to pytest-2.0.2, a maintenance and bug fix release of pytest, a mature testing tool for Python, supporting CPython 2.4-3.2, Jython and latest PyPy interpreters. See the extensive docs with tested examples here: http://pytest.org/ If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest Many thanks to all issue reporters and people asking questions or complaining, particularly Jurko for his insistence, Laura, Victor and Brianna for helping with improving and Ronny for his general advise. best, holger krekel Changes between 2.0.1 and 2.0.2 ---------------------------------------------- - tackle issue32 - speed up test runs of very quick test functions by reducing the relative overhead - fix issue30 - extended xfail/skipif handling and improved reporting. If you have a syntax error in your skip/xfail expressions you now get nice error reports. Also you can now access module globals from xfail/skipif expressions so that this for example works now:: import pytest import mymodule @pytest.mark.skipif("mymodule.__version__[0] == "1") def test_function(): pass This will not run the test function if the module's version string does not start with a "1". Note that specifying a string instead of a boolean expressions allows py.test to report meaningful information when summarizing a test run as to what conditions lead to skipping (or xfail-ing) tests. - fix issue28 - setup_method and pytest_generate_tests work together The setup_method fixture method now gets called also for test function invocations generated from the pytest_generate_tests hook. - fix issue27 - collectonly and keyword-selection (-k) now work together Also, if you do "py.test --collectonly -q" you now get a flat list of test ids that you can use to paste to the py.test commandline in order to execute a particular test. - fix issue25 avoid reported problems with --pdb and python3.2/encodings output - fix issue23 - tmpdir argument now works on Python3.2 and WindowsXP Starting with Python3.2 os.symlink may be supported. By requiring a newer py lib version the py.path.local() implementation acknowledges this. - fixed typos in the docs (thanks Victor Garcia, Brianna Laugher) and particular thanks to Laura Creighton who also reviewed parts of the documentation. - fix slightly wrong output of verbose progress reporting for classes (thanks Amaury) - more precise (avoiding of) deprecation warnings for node.Class|Function accesses - avoid std unittest assertion helper code in tracebacks (thanks Ronny) pytest-4.6.9/doc/en/announce/release-3.5.1.rst0000664000372000037200000000116113604173400021512 0ustar travistravis00000000000000pytest-3.5.1 ======================================= pytest 3.5.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Brian Maissy * Bruno Oliveira * Darren Burns * David Chudzicki * Floris Bruynooghe * Holger Kohr * Irmen de Jong * Jeffrey Rackauckas * Rachel Kogan * Ronny Pfannschmidt * Stefan Scherfke * Tim Strazny * Семён Марьясин Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.4.rst0000664000372000037200000000074313604173400021524 0ustar travistravis00000000000000pytest-4.6.4 ======================================= pytest 4.6.4 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Thomas Grainger * Zac Hatfield-Dodds Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.8.0.rst0000664000372000037200000000152713604173400021522 0ustar travistravis00000000000000pytest-3.8.0 ======================================= The pytest team is proud to announce the 3.8.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * CrazyMerlyn * Daniel Hahler * Fabio Zadrozny * Jeffrey Rackauckas * Ronny Pfannschmidt * Virgil Dupras * dhirensr * hoefling * wim glenn Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.3.1.rst0000664000372000037200000000102413604173400021506 0ustar travistravis00000000000000pytest-3.3.1 ======================================= pytest 3.3.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Daniel Hahler * Eugene Prikazchikov * Florian Bruhin * Roland Puntaier * Ronny Pfannschmidt * Sebastian Rahlf * Tom Viner Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.8.4.rst0000664000372000037200000000254713604173400021530 0ustar travistravis00000000000000pytest-2.8.4 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.2. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Florian Bruhin Jeff Widman Mehdy Khoshnoody Nicholas Chammas Ronny Pfannschmidt Tim Chan Happy testing, The py.test Development Team 2.8.4 (compared to 2.8.3) ----------------------------- - fix #1190: ``deprecated_call()`` now works when the deprecated function has been already called by another test in the same module. Thanks Mikhail Chernykh for the report and Bruno Oliveira for the PR. - fix #1198: ``--pastebin`` option now works on Python 3. Thanks Mehdy Khoshnoody for the PR. - fix #1219: ``--pastebin`` now works correctly when captured output contains non-ascii characters. Thanks Bruno Oliveira for the PR. - fix #1204: another error when collecting with a nasty __getattr__(). Thanks Florian Bruhin for the PR. - fix the summary printed when no tests did run. Thanks Florian Bruhin for the PR. - a number of documentation modernizations wrt good practices. Thanks Bruno Oliveira for the PR. pytest-4.6.9/doc/en/announce/release-3.1.3.rst0000664000372000037200000000076113604173400021515 0ustar travistravis00000000000000pytest-3.1.3 ======================================= pytest 3.1.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Antoine Legrand * Bruno Oliveira * Max Moroz * Raphael Pierzina * Ronny Pfannschmidt * Ryan Fitzpatrick Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.1.0.rst0000664000372000037200000000415113604173400021506 0ustar travistravis00000000000000py.test 2.1.0: perfected assertions and bug fixes =========================================================================== Welcome to the release of pytest-2.1, a mature testing tool for Python, supporting CPython 2.4-3.2, Jython and latest PyPy interpreters. See the improved extensive docs (now also as PDF!) with tested examples here: http://pytest.org/ The single biggest news about this release are **perfected assertions** courtesy of Benjamin Peterson. You can now safely use ``assert`` statements in test modules without having to worry about side effects or python optimization ("-OO") options. This is achieved by rewriting assert statements in test modules upon import, using a PEP302 hook. See http://pytest.org/assert.html#advanced-assertion-introspection for detailed information. The work has been partly sponsored by my company, merlinux GmbH. For further details on bug fixes and smaller enhancements see below. If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest best, holger krekel / http://merlinux.eu Changes between 2.0.3 and 2.1.0 ---------------------------------------------- - fix issue53 call nosestyle setup functions with correct ordering - fix issue58 and issue59: new assertion code fixes - merge Benjamin's assertionrewrite branch: now assertions for test modules on python 2.6 and above are done by rewriting the AST and saving the pyc file before the test module is imported. see doc/assert.txt for more info. - fix issue43: improve doctests with better traceback reporting on unexpected exceptions - fix issue47: timing output in junitxml for test cases is now correct - fix issue48: typo in MarkInfo repr leading to exception - fix issue49: avoid confusing error when initialization partially fails - fix issue44: env/username expansion for junitxml file path - show releaselevel information in test runs for pypy - reworked doc pages for better navigation and PDF generation - report KeyboardInterrupt even if interrupted during session startup - fix issue 35 - provide PDF doc version and download link from index page pytest-4.6.9/doc/en/announce/release-4.5.0.rst0000664000372000037200000000146513604173400021521 0ustar travistravis00000000000000pytest-4.5.0 ======================================= The pytest team is proud to announce the 4.5.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Floris Bruynooghe * Pulkit Goyal * Samuel Searles-Bryant * Zac Hatfield-Dodds * Zac-HD Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.0.0.rst0000664000372000037200000001203713604173400021507 0ustar travistravis00000000000000py.test 2.0.0: asserts++, unittest++, reporting++, config++, docs++ =========================================================================== Welcome to pytest-2.0.0, a major new release of "py.test", the rapid easy Python testing tool. There are many new features and enhancements, see below for summary and detailed lists. A lot of long-deprecated code has been removed, resulting in a much smaller and cleaner implementation. See the new docs with examples here: http://pytest.org/2.0.0/index.html A note on packaging: pytest used to part of the "py" distribution up until version py-1.3.4 but this has changed now: pytest-2.0.0 only contains py.test related code and is expected to be backward-compatible to existing test code. If you want to install pytest, just type one of:: pip install -U pytest easy_install -U pytest Many thanks to all issue reporters and people asking questions or complaining. Particular thanks to Floris Bruynooghe and Ronny Pfannschmidt for their great coding contributions and many others for feedback and help. best, holger krekel New Features ----------------------- - new invocations through Python interpreter and from Python:: python -m pytest # on all pythons >= 2.5 or from a python program:: import pytest ; pytest.main(arglist, pluginlist) see http://pytest.org/2.0.0/usage.html for details. - new and better reporting information in assert expressions if comparing lists, sequences or strings. see http://pytest.org/2.0.0/assert.html#newreport - new configuration through ini-files (setup.cfg or tox.ini recognized), for example:: [pytest] norecursedirs = .hg data* # don't ever recurse in such dirs addopts = -x --pyargs # add these command line options by default see http://pytest.org/2.0.0/customize.html - improved standard unittest support. In general py.test should now better be able to run custom unittest.TestCases like twisted trial or Django based TestCases. Also you can now run the tests of an installed 'unittest' package with py.test:: py.test --pyargs unittest - new "-q" option which decreases verbosity and prints a more nose/unittest-style "dot" output. - many many more detailed improvements details Fixes ----------------------- - fix issue126 - introduce py.test.set_trace() to trace execution via PDB during the running of tests even if capturing is ongoing. - fix issue124 - make reporting more resilient against tests opening files on filedescriptor 1 (stdout). - fix issue109 - sibling conftest.py files will not be loaded. (and Directory collectors cannot be customized anymore from a Directory's conftest.py - this needs to happen at least one level up). - fix issue88 (finding custom test nodes from command line arg) - fix issue93 stdout/stderr is captured while importing conftest.py - fix bug: unittest collected functions now also can have "pytestmark" applied at class/module level Important Notes -------------------- * The usual way in pre-2.0 times to use py.test in python code was to import "py" and then e.g. use "py.test.raises" for the helper. This remains valid and is not planned to be deprecated. However, in most examples and internal code you'll find "import pytest" and "pytest.raises" used as the recommended default way. * pytest now first performs collection of the complete test suite before running any test. This changes for example the semantics of when pytest_collectstart/pytest_collectreport are called. Some plugins may need upgrading. * The pytest package consists of a 400 LOC core.py and about 20 builtin plugins, summing up to roughly 5000 LOCs, including docstrings. To be fair, it also uses generic code from the "pylib", and the new "py" package to help with filesystem and introspection/code manipulation. (Incompatible) Removals ----------------------------- - py.test.config is now only available if you are in a test run. - the following (mostly already deprecated) functionality was removed: - removed support for Module/Class/... collection node definitions in conftest.py files. They will cause nothing special. - removed support for calling the pre-1.0 collection API of "run()" and "join" - removed reading option values from conftest.py files or env variables. This can now be done much much better and easier through the ini-file mechanism and the "addopts" entry in particular. - removed the "disabled" attribute in test classes. Use the skipping and pytestmark mechanism to skip or xfail a test class. - py.test.collect.Directory does not exist anymore and it is not possible to provide an own "Directory" object. If you have used this and don't know what to do, get in contact. We'll figure something out. Note that pytest_collect_directory() is still called but any return value will be ignored. This allows to keep old code working that performed for example "py.test.skip()" in collect() to prevent recursion into directory trees if a certain dependency or command line option is missing. see :ref:`changelog` for more detailed changes. pytest-4.6.9/doc/en/announce/release-2.7.2.rst0000664000372000037200000000330213604173400021513 0ustar travistravis00000000000000pytest-2.7.2: bug fixes ======================= pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.7.1. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Floris Bruynooghe Punyashloka Biswal Aron Curzon Benjamin Peterson Thomas De Schampheleire Edison Gustavo Muenz Holger Krekel Happy testing, The py.test Development Team 2.7.2 (compared to 2.7.1) ----------------------------- - fix issue767: pytest.raises value attribute does not contain the exception instance on Python 2.6. Thanks Eric Siegerman for providing the test case and Bruno Oliveira for PR. - Automatically create directory for junitxml and results log. Thanks Aron Curzon. - fix issue713: JUnit XML reports for doctest failures. Thanks Punyashloka Biswal. - fix issue735: assertion failures on debug versions of Python 3.4+ Thanks Benjamin Peterson. - fix issue114: skipif marker reports to internal skipping plugin; Thanks Floris Bruynooghe for reporting and Bruno Oliveira for the PR. - fix issue748: unittest.SkipTest reports to internal pytest unittest plugin. Thanks Thomas De Schampheleire for reporting and Bruno Oliveira for the PR. - fix issue718: failed to create representation of sets containing unsortable elements in python 2. Thanks Edison Gustavo Muenz - fix issue756, fix issue752 (and similar issues): depend on py-1.4.29 which has a refined algorithm for traceback generation. pytest-4.6.9/doc/en/announce/release-4.6.2.rst0000664000372000037200000000063313604173400021520 0ustar travistravis00000000000000pytest-4.6.2 ======================================= pytest 4.6.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.1.rst0000664000372000037200000000065413604173400021522 0ustar travistravis00000000000000pytest-4.6.1 ======================================= pytest 4.6.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.1.2.rst0000664000372000037200000000075013604173400021512 0ustar travistravis00000000000000pytest-3.1.2 ======================================= pytest 3.1.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andreas Pelme * ApaDoctor * Bruno Oliveira * Florian Bruhin * Ronny Pfannschmidt * Segev Finer Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.0.4.rst0000664000372000037200000000114213604173400021507 0ustar travistravis00000000000000pytest-3.0.4 ============ pytest 3.0.4 has just been released to PyPI. This release fixes some regressions and bugs reported in the last version, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Dan Wandschneider * Florian Bruhin * Georgy Dyuldin * Grigorii Eremeev * Jason R. Coombs * Manuel Jacob * Mathieu Clabaut * Michael Seifert * Nikolaus Rath * Ronny Pfannschmidt * Tom V Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.2.1.rst0000664000372000037200000000302313604173400021505 0ustar travistravis00000000000000pytest-2.2.1: bug fixes, perfect teardowns =========================================================================== pytest-2.2.1 is a minor backward-compatible release of the py.test testing tool. It contains bug fixes and little improvements, including documentation fixes. If you are using the distributed testing pluginmake sure to upgrade it to pytest-xdist-1.8. For general information see here: http://pytest.org/ To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest Special thanks for helping on this release to Ronny Pfannschmidt, Jurko Gospodnetic and Ralf Schmitt. best, holger krekel Changes between 2.2.0 and 2.2.1 ---------------------------------------- - fix issue99 (in pytest and py) internallerrors with resultlog now produce better output - fixed by normalizing pytest_internalerror input arguments. - fix issue97 / traceback issues (in pytest and py) improve traceback output in conjunction with jinja2 and cython which hack tracebacks - fix issue93 (in pytest and pytest-xdist) avoid "delayed teardowns": the final test in a test node will now run its teardown directly instead of waiting for the end of the session. Thanks Dave Hunt for the good reporting and feedback. The pytest_runtest_protocol as well as the pytest_runtest_teardown hooks now have "nextitem" available which will be None indicating the end of the test run. - fix collection crash due to unknown-source collected items, thanks to Ralf Schmitt (fixed by depending on a more recent pylib) pytest-4.6.9/doc/en/announce/release-3.7.1.rst0000664000372000037200000000071613604173400021521 0ustar travistravis00000000000000pytest-3.7.1 ======================================= pytest 3.7.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Kale Kundert * Ronny Pfannschmidt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.9.3.rst0000664000372000037200000000077713604173400021534 0ustar travistravis00000000000000pytest-3.9.3 ======================================= pytest 3.9.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andreas Profous * Ankit Goel * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Jon Dufresne * Ronny Pfannschmidt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.4.1.rst0000664000372000037200000000156113604173400021514 0ustar travistravis00000000000000pytest-2.4.1: fixing three regressions compared to 2.3.5 =========================================================================== pytest-2.4.1 is a quick follow up release to fix three regressions compared to 2.3.5 before they hit more people: - When using parser.addoption() unicode arguments to the "type" keyword should also be converted to the respective types. thanks Floris Bruynooghe, @dnozay. (fixes issue360 and issue362) - fix dotted filename completion when using argcomplete thanks Anthon van der Neuth. (fixes issue361) - fix regression when a 1-tuple ("arg",) is used for specifying parametrization (the values of the parametrization were passed nested in a tuple). Thanks Donald Stufft. - also merge doc typo fixes, thanks Andy Dirnberger as usual, docs at http://pytest.org and upgrades via:: pip install -U pytest have fun, holger krekel pytest-4.6.9/doc/en/announce/release-4.3.1.rst0000664000372000037200000000111013604173400021503 0ustar travistravis00000000000000pytest-4.3.1 ======================================= pytest 4.3.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andras Mitzki * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Danilo Horta * Grygorii Iermolenko * Jeff Hale * Kyle Altendorf * Stephan Hoyer * Zac Hatfield-Dodds * Zac-HD * songbowen Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.7.2.rst0000664000372000037200000000101113604173400021507 0ustar travistravis00000000000000pytest-3.7.2 ======================================= pytest 3.7.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Josh Holland * Ronny Pfannschmidt * Sankt Petersbug * Wes Thomas * turturica Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.3.1.rst0000664000372000037200000000213313604173400021507 0ustar travistravis00000000000000pytest-2.3.1: fix regression with factory functions =========================================================================== pytest-2.3.1 is a quick follow-up release: - fix issue202 - regression with fixture functions/funcarg factories: using "self" is now safe again and works as in 2.2.4. Thanks to Eduard Schettino for the quick bug report. - disable pexpect pytest self tests on Freebsd - thanks Koob for the quick reporting - fix/improve interactive docs with --markers See http://pytest.org/ for general information. To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest best, holger krekel Changes between 2.3.0 and 2.3.1 ----------------------------------- - fix issue202 - fix regression: using "self" from fixture functions now works as expected (it's the same "self" instance that a test method which uses the fixture sees) - skip pexpect using tests (test_pdb.py mostly) on freebsd* systems due to pexpect not supporting it properly (hanging) - link to web pages from --markers output which provides help for pytest.mark.* usage. pytest-4.6.9/doc/en/announce/release-2.1.3.rst0000664000372000037200000000226313604173400021513 0ustar travistravis00000000000000py.test 2.1.3: just some more fixes =========================================================================== pytest-2.1.3 is a minor backward compatible maintenance release of the popular py.test testing tool. It is commonly used for unit, functional- and integration testing. See extensive docs with examples here: http://pytest.org/ The release contains another fix to the perfected assertions introduced with the 2.1 series as well as the new possibility to customize reporting for assertion expressions on a per-directory level. If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest Thanks to the bug reporters and to Ronny Pfannschmidt, Benjamin Peterson and Floris Bruynooghe who implemented the fixes. best, holger krekel Changes between 2.1.2 and 2.1.3 ---------------------------------------- - fix issue79: assertion rewriting failed on some comparisons in boolops, - correctly handle zero length arguments (a la pytest '') - fix issue67 / junitxml now contains correct test durations - fix issue75 / skipping test failure on jython - fix issue77 / Allow assertrepr_compare hook to apply to a subset of tests pytest-4.6.9/doc/en/announce/release-2.0.3.rst0000664000372000037200000000243313604173400021511 0ustar travistravis00000000000000py.test 2.0.3: bug fixes and speed ups =========================================================================== Welcome to pytest-2.0.3, a maintenance and bug fix release of pytest, a mature testing tool for Python, supporting CPython 2.4-3.2, Jython and latest PyPy interpreters. See the extensive docs with tested examples here: http://pytest.org/ If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest There also is a bugfix release 1.6 of pytest-xdist, the plugin that enables seamless distributed and "looponfail" testing for Python. best, holger krekel Changes between 2.0.2 and 2.0.3 ---------------------------------------------- - fix issue38: nicer tracebacks on calls to hooks, particularly early configure/sessionstart ones - fix missing skip reason/meta information in junitxml files, reported via http://lists.idyll.org/pipermail/testing-in-python/2011-March/003928.html - fix issue34: avoid collection failure with "test" prefixed classes deriving from object. - don't require zlib (and other libs) for genscript plugin without --genscript actually being used. - speed up skips (by not doing a full traceback representation internally) - fix issue37: avoid invalid characters in junitxml's output pytest-4.6.9/doc/en/announce/release-3.2.2.rst0000664000372000037200000000107113604173400021510 0ustar travistravis00000000000000pytest-3.2.2 ======================================= pytest 3.2.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andreas Pelme * Antonio Hidalgo * Bruno Oliveira * Felipe Dau * Fernando Macedo * Jesús Espino * Joan Massich * Joe Talbott * Kirill Pinchuk * Ronny Pfannschmidt * Xuan Luong Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.6.1.rst0000664000372000037200000000101113604173400021505 0ustar travistravis00000000000000pytest-3.6.1 ======================================= pytest 3.6.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Jeffrey Rackauckas * Miro Hrončok * Niklas Meinzer * Oliver Bestwalter * Ronny Pfannschmidt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.7.3.rst0000664000372000037200000000117413604173400021522 0ustar travistravis00000000000000pytest-3.7.3 ======================================= pytest 3.7.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andrew Champion * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Gandalf Saxe * Jennifer Rinker * Natan Lao * Ondřej Súkup * Ronny Pfannschmidt * Sankt Petersbug * Tyler Richard * Victor Maryama * Vlad Shcherbina * turturica * wim glenn Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.1.1.rst0000664000372000037200000000075613604173400021517 0ustar travistravis00000000000000pytest-3.1.1 ======================================= pytest 3.1.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Florian Bruhin * Floris Bruynooghe * Jason R. Coombs * Ronny Pfannschmidt * wanghui Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.4.0.rst0000664000372000037200000002177613604173400021525 0ustar travistravis00000000000000pytest-2.4.0: new fixture features/hooks and bug fixes =========================================================================== The just released pytest-2.4.0 brings many improvements and numerous bug fixes while remaining plugin- and test-suite compatible apart from a few supposedly very minor incompatibilities. See below for a full list of details. A few feature highlights: - new yield-style fixtures `pytest.yield_fixture `_, allowing to use existing with-style context managers in fixture functions. - improved pdb support: ``import pdb ; pdb.set_trace()`` now works without requiring prior disabling of stdout/stderr capturing. Also the ``--pdb`` options works now on collection and internal errors and we introduced a new experimental hook for IDEs/plugins to intercept debugging: ``pytest_exception_interact(node, call, report)``. - shorter monkeypatch variant to allow specifying an import path as a target, for example: ``monkeypatch.setattr("requests.get", myfunc)`` - better unittest/nose compatibility: all teardown methods are now only called if the corresponding setup method succeeded. - integrate tab-completion on command line options if you have `argcomplete `_ configured. - allow boolean expression directly with skipif/xfail if a "reason" is also specified. - a new hook ``pytest_load_initial_conftests`` allows plugins like `pytest-django `_ to influence the environment before conftest files import ``django``. - reporting: color the last line red or green depending if failures/errors occurred or everything passed. The documentation has been updated to accommodate the changes, see `http://pytest.org `_ To install or upgrade pytest:: pip install -U pytest # or easy_install -U pytest **Many thanks to all who helped, including Floris Bruynooghe, Brianna Laugher, Andreas Pelme, Anthon van der Neut, Anatoly Bubenkoff, Vladimir Keleshev, Mathieu Agopian, Ronny Pfannschmidt, Christian Theunert and many others.** may passing tests be with you, holger krekel Changes between 2.3.5 and 2.4 ----------------------------------- known incompatibilities: - if calling --genscript from python2.7 or above, you only get a standalone script which works on python2.7 or above. Use Python2.6 to also get a python2.5 compatible version. - all xunit-style teardown methods (nose-style, pytest-style, unittest-style) will not be called if the corresponding setup method failed, see issue322 below. - the pytest_plugin_unregister hook wasn't ever properly called and there is no known implementation of the hook - so it got removed. - pytest.fixture-decorated functions cannot be generators (i.e. use yield) anymore. This change might be reversed in 2.4.1 if it causes unforeseen real-life issues. However, you can always write and return an inner function/generator and change the fixture consumer to iterate over the returned generator. This change was done in lieu of the new ``pytest.yield_fixture`` decorator, see below. new features: - experimentally introduce a new ``pytest.yield_fixture`` decorator which accepts exactly the same parameters as pytest.fixture but mandates a ``yield`` statement instead of a ``return statement`` from fixture functions. This allows direct integration with "with-style" context managers in fixture functions and generally avoids registering of finalization callbacks in favour of treating the "after-yield" as teardown code. Thanks Andreas Pelme, Vladimir Keleshev, Floris Bruynooghe, Ronny Pfannschmidt and many others for discussions. - allow boolean expression directly with skipif/xfail if a "reason" is also specified. Rework skipping documentation to recommend "condition as booleans" because it prevents surprises when importing markers between modules. Specifying conditions as strings will remain fully supported. - reporting: color the last line red or green depending if failures/errors occurred or everything passed. thanks Christian Theunert. - make "import pdb ; pdb.set_trace()" work natively wrt capturing (no "-s" needed anymore), making ``pytest.set_trace()`` a mere shortcut. - fix issue181: --pdb now also works on collect errors (and on internal errors) . This was implemented by a slight internal refactoring and the introduction of a new hook ``pytest_exception_interact`` hook (see next item). - fix issue341: introduce new experimental hook for IDEs/terminals to intercept debugging: ``pytest_exception_interact(node, call, report)``. - new monkeypatch.setattr() variant to provide a shorter invocation for patching out classes/functions from modules: monkeypatch.setattr("requests.get", myfunc) will replace the "get" function of the "requests" module with ``myfunc``. - fix issue322: tearDownClass is not run if setUpClass failed. Thanks Mathieu Agopian for the initial fix. Also make all of pytest/nose finalizer mimic the same generic behaviour: if a setupX exists and fails, don't run teardownX. This internally introduces a new method "node.addfinalizer()" helper which can only be called during the setup phase of a node. - simplify pytest.mark.parametrize() signature: allow to pass a CSV-separated string to specify argnames. For example: ``pytest.mark.parametrize("input,expected", [(1,2), (2,3)])`` works as well as the previous: ``pytest.mark.parametrize(("input", "expected"), ...)``. - add support for setUpModule/tearDownModule detection, thanks Brian Okken. - integrate tab-completion on options through use of "argcomplete". Thanks Anthon van der Neut for the PR. - change option names to be hyphen-separated long options but keep the old spelling backward compatible. py.test -h will only show the hyphenated version, for example "--collect-only" but "--collectonly" will remain valid as well (for backward-compat reasons). Many thanks to Anthon van der Neut for the implementation and to Hynek Schlawack for pushing us. - fix issue 308 - allow to mark/xfail/skip individual parameter sets when parametrizing. Thanks Brianna Laugher. - call new experimental pytest_load_initial_conftests hook to allow 3rd party plugins to do something before a conftest is loaded. Bug fixes: - fix issue358 - capturing options are now parsed more properly by using a new parser.parse_known_args method. - pytest now uses argparse instead of optparse (thanks Anthon) which means that "argparse" is added as a dependency if installing into python2.6 environments or below. - fix issue333: fix a case of bad unittest/pytest hook interaction. - PR27: correctly handle nose.SkipTest during collection. Thanks Antonio Cuni, Ronny Pfannschmidt. - fix issue355: junitxml puts name="pytest" attribute to testsuite tag. - fix issue336: autouse fixture in plugins should work again. - fix issue279: improve object comparisons on assertion failure for standard datatypes and recognise collections.abc. Thanks to Brianna Laugher and Mathieu Agopian. - fix issue317: assertion rewriter support for the is_package method - fix issue335: document py.code.ExceptionInfo() object returned from pytest.raises(), thanks Mathieu Agopian. - remove implicit distribute_setup support from setup.py. - fix issue305: ignore any problems when writing pyc files. - SO-17664702: call fixture finalizers even if the fixture function partially failed (finalizers would not always be called before) - fix issue320 - fix class scope for fixtures when mixed with module-level functions. Thanks Anatloy Bubenkoff. - you can specify "-q" or "-qq" to get different levels of "quieter" reporting (thanks Katarzyna Jachim) - fix issue300 - Fix order of conftest loading when starting py.test in a subdirectory. - fix issue323 - sorting of many module-scoped arg parametrizations - make sessionfinish hooks execute with the same cwd-context as at session start (helps fix plugin behaviour which write output files with relative path such as pytest-cov) - fix issue316 - properly reference collection hooks in docs - fix issue 306 - cleanup of -k/-m options to only match markers/test names/keywords respectively. Thanks Wouter van Ackooy. - improved doctest counting for doctests in python modules -- files without any doctest items will not show up anymore and doctest examples are counted as separate test items. thanks Danilo Bellini. - fix issue245 by depending on the released py-1.4.14 which fixes py.io.dupfile to work with files with no mode. Thanks Jason R. Coombs. - fix junitxml generation when test output contains control characters, addressing issue267, thanks Jaap Broekhuizen - fix issue338: honor --tb style for setup/teardown errors as well. Thanks Maho. - fix issue307 - use yaml.safe_load in example, thanks Mark Eichin. - better parametrize error messages, thanks Brianna Laugher - pytest_terminal_summary(terminalreporter) hooks can now use ".section(title)" and ".line(msg)" methods to print extra information at the end of a test run. pytest-4.6.9/doc/en/announce/release-2.8.2.rst0000664000372000037200000000242613604173400021522 0ustar travistravis00000000000000pytest-2.8.2: bug fixes ======================= pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.1. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Demian Brecht Florian Bruhin Ionel Cristian Mărieș Raphael Pierzina Ronny Pfannschmidt holger krekel Happy testing, The py.test Development Team 2.8.2 (compared to 2.7.2) ----------------------------- - fix #1085: proper handling of encoding errors when passing encoded byte strings to pytest.parametrize in Python 2. Thanks Themanwithoutaplan for the report and Bruno Oliveira for the PR. - fix #1087: handling SystemError when passing empty byte strings to pytest.parametrize in Python 3. Thanks Paul Kehrer for the report and Bruno Oliveira for the PR. - fix #995: fixed internal error when filtering tracebacks where one entry was generated by an exec() statement. Thanks Daniel Hahler, Ashley C Straw, Philippe Gauthier and Pavel Savchenko for contributing and Bruno Oliveira for the PR. pytest-4.6.9/doc/en/announce/release-3.2.0.rst0000664000372000037200000000201213604173400021502 0ustar travistravis00000000000000pytest-3.2.0 ======================================= The pytest team is proud to announce the 3.2.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Alex Hartoto * Andras Tim * Bruno Oliveira * Daniel Hahler * Florian Bruhin * Floris Bruynooghe * John Still * Jordan Moldow * Kale Kundert * Lawrence Mitchell * Llandy Riveron Del Risco * Maik Figura * Martin Altmayer * Mihai Capotă * Nathaniel Waisbrot * Nguyễn Hồng Quân * Pauli Virtanen * Raphael Pierzina * Ronny Pfannschmidt * Segev Finer * V.Kuznetsov Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.7.0.rst0000664000372000037200000000737513604173400021527 0ustar travistravis00000000000000pytest-2.7.0: fixes, features, speed improvements =========================================================================== pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.6.X. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed, among them: Anatoly Bubenkoff Floris Bruynooghe Brianna Laugher Eric Siegerman Daniel Hahler Charles Cloud Tom Viner Holger Peters Ldiary Translations almarklein have fun, holger krekel 2.7.0 (compared to 2.6.4) ----------------------------- - fix issue435: make reload() work when assert rewriting is active. Thanks Daniel Hahler. - fix issue616: conftest.py files and their contained fixutres are now properly considered for visibility, independently from the exact current working directory and test arguments that are used. Many thanks to Eric Siegerman and his PR235 which contains systematic tests for conftest visibility and now passes. This change also introduces the concept of a ``rootdir`` which is printed as a new pytest header and documented in the pytest customize web page. - change reporting of "diverted" tests, i.e. tests that are collected in one file but actually come from another (e.g. when tests in a test class come from a base class in a different file). We now show the nodeid and indicate via a postfix the other file. - add ability to set command line options by environment variable PYTEST_ADDOPTS. - added documentation on the new pytest-dev teams on bitbucket and github. See https://pytest.org/latest/contributing.html . Thanks to Anatoly for pushing and initial work on this. - fix issue650: new option ``--docttest-ignore-import-errors`` which will turn import errors in doctests into skips. Thanks Charles Cloud for the complete PR. - fix issue655: work around different ways that cause python2/3 to leak sys.exc_info into fixtures/tests causing failures in 3rd party code - fix issue615: assertion rewriting did not correctly escape % signs when formatting boolean operations, which tripped over mixing booleans with modulo operators. Thanks to Tom Viner for the report, triaging and fix. - implement issue351: add ability to specify parametrize ids as a callable to generate custom test ids. Thanks Brianna Laugher for the idea and implementation. - introduce and document new hookwrapper mechanism useful for plugins which want to wrap the execution of certain hooks for their purposes. This supersedes the undocumented ``__multicall__`` protocol which pytest itself and some external plugins use. Note that pytest-2.8 is scheduled to drop supporting the old ``__multicall__`` and only support the hookwrapper protocol. - majorly speed up invocation of plugin hooks - use hookwrapper mechanism in builtin pytest plugins. - add a doctest ini option for doctest flags, thanks Holger Peters. - add note to docs that if you want to mark a parameter and the parameter is a callable, you also need to pass in a reason to disambiguate it from the "decorator" case. Thanks Tom Viner. - "python_classes" and "python_functions" options now support glob-patterns for test discovery, as discussed in issue600. Thanks Ldiary Translations. - allow to override parametrized fixtures with non-parametrized ones and vice versa (bubenkoff). - fix issue463: raise specific error for 'parameterize' misspelling (pfctdayelise). - On failure, the ``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback`` are set, so that a user can inspect the error via postmortem debugging (almarklein). pytest-4.6.9/doc/en/announce/release-2.2.2.rst0000664000372000037200000000312313604173400021507 0ustar travistravis00000000000000pytest-2.2.2: bug fixes =========================================================================== pytest-2.2.2 (updated to 2.2.3 to fix packaging issues) is a minor backward-compatible release of the versatile py.test testing tool. It contains bug fixes and a few refinements particularly to reporting with "--collectonly", see below for betails. For general information see here: http://pytest.org/ To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest Special thanks for helping on this release to Ronny Pfannschmidt and Ralf Schmitt and the contributors of issues. best, holger krekel Changes between 2.2.1 and 2.2.2 ---------------------------------------- - fix issue101: wrong args to unittest.TestCase test function now produce better output - fix issue102: report more useful errors and hints for when a test directory was renamed and some pyc/__pycache__ remain - fix issue106: allow parametrize to be applied multiple times e.g. from module, class and at function level. - fix issue107: actually perform session scope finalization - don't check in parametrize if indirect parameters are funcarg names - add chdir method to monkeypatch funcarg - fix crash resulting from calling monkeypatch undo a second time - fix issue115: make --collectonly robust against early failure (missing files/directories) - "-qq --collectonly" now shows only files and the number of tests in them - "-q --collectonly" now shows test ids - allow adding of attributes to test reports such that it also works with distributed testing (no upgrade of pytest-xdist needed) pytest-4.6.9/doc/en/announce/release-3.0.5.rst0000664000372000037200000000102113604173400021504 0ustar travistravis00000000000000pytest-3.0.5 ============ pytest 3.0.5 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Ana Vojnovic * Bruno Oliveira * Daniel Hahler * Duncan Betts * Igor Starikov * Ismail * Luke Murphy * Ned Batchelder * Ronny Pfannschmidt * Sebastian Ramacher * nmundar Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.7.4.rst0000664000372000037200000000073113604173400021521 0ustar travistravis00000000000000pytest-3.7.4 ======================================= pytest 3.7.4 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Jiri Kuncar * Steve Piercy Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.7.1.rst0000664000372000037200000000362713604173400021524 0ustar travistravis00000000000000pytest-2.7.1: bug fixes ======================= pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.7.0. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Bruno Oliveira Holger Krekel Ionel Maries Cristian Floris Bruynooghe Happy testing, The py.test Development Team 2.7.1 (compared to 2.7.0) ------------------------- - fix issue731: do not get confused by the braces which may be present and unbalanced in an object's repr while collapsing False explanations. Thanks Carl Meyer for the report and test case. - fix issue553: properly handling inspect.getsourcelines failures in FixtureLookupError which would lead to an internal error, obfuscating the original problem. Thanks talljosh for initial diagnose/patch and Bruno Oliveira for final patch. - fix issue660: properly report scope-mismatch-access errors independently from ordering of fixture arguments. Also avoid the pytest internal traceback which does not provide information to the user. Thanks Holger Krekel. - streamlined and documented release process. Also all versions (in setup.py and documentation generation) are now read from _pytest/__init__.py. Thanks Holger Krekel. - fixed docs to remove the notion that yield-fixtures are experimental. They are here to stay :) Thanks Bruno Oliveira. - Support building wheels by using environment markers for the requirements. Thanks Ionel Maries Cristian. - fixed regression to 2.6.4 which surfaced e.g. in lost stdout capture printing when tests raised SystemExit. Thanks Holger Krekel. - reintroduced _pytest fixture of the pytester plugin which is used at least by pytest-xdist. pytest-4.6.9/doc/en/announce/release-2.9.0.rst0000664000372000037200000001351413604173400021521 0ustar travistravis00000000000000pytest-2.9.0 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Anatoly Bubenkov Bruno Oliveira Buck Golemon David Vierra Florian Bruhin Galaczi Endre Georgy Dyuldin Lukas Bednar Luke Murphy Marcin Biernat Matt Williams Michael Aquilina Raphael Pierzina Ronny Pfannschmidt Ryan Wooden Tiemo Kieft TomV holger krekel jab Happy testing, The py.test Development Team 2.9.0 (compared to 2.8.7) ------------------------- **New Features** * New ``pytest.mark.skip`` mark, which unconditionally skips marked tests. Thanks `@MichaelAquilina`_ for the complete PR (`#1040`_). * ``--doctest-glob`` may now be passed multiple times in the command-line. Thanks `@jab`_ and `@nicoddemus`_ for the PR. * New ``-rp`` and ``-rP`` reporting options give the summary and full output of passing tests, respectively. Thanks to `@codewarrior0`_ for the PR. * ``pytest.mark.xfail`` now has a ``strict`` option which makes ``XPASS`` tests to fail the test suite, defaulting to ``False``. There's also a ``xfail_strict`` ini option that can be used to configure it project-wise. Thanks `@rabbbit`_ for the request and `@nicoddemus`_ for the PR (`#1355`_). * ``Parser.addini`` now supports options of type ``bool``. Thanks `@nicoddemus`_ for the PR. * New ``ALLOW_BYTES`` doctest option strips ``b`` prefixes from byte strings in doctest output (similar to ``ALLOW_UNICODE``). Thanks `@jaraco`_ for the request and `@nicoddemus`_ for the PR (`#1287`_). * give a hint on KeyboardInterrupt to use the --fulltrace option to show the errors, this fixes `#1366`_. Thanks to `@hpk42`_ for the report and `@RonnyPfannschmidt`_ for the PR. * catch IndexError exceptions when getting exception source location. This fixes pytest internal error for dynamically generated code (fixtures and tests) where source lines are fake by intention **Changes** * **Important**: `py.code `_ has been merged into the ``pytest`` repository as ``pytest._code``. This decision was made because ``py.code`` had very few uses outside ``pytest`` and the fact that it was in a different repository made it difficult to fix bugs on its code in a timely manner. The team hopes with this to be able to better refactor out and improve that code. This change shouldn't affect users, but it is useful to let users aware if they encounter any strange behavior. Keep in mind that the code for ``pytest._code`` is **private** and **experimental**, so you definitely should not import it explicitly! Please note that the original ``py.code`` is still available in `pylib `_. * ``pytest_enter_pdb`` now optionally receives the pytest config object. Thanks `@nicoddemus`_ for the PR. * Removed code and documentation for Python 2.5 or lower versions, including removal of the obsolete ``_pytest.assertion.oldinterpret`` module. Thanks `@nicoddemus`_ for the PR (`#1226`_). * Comparisons now always show up in full when ``CI`` or ``BUILD_NUMBER`` is found in the environment, even when -vv isn't used. Thanks `@The-Compiler`_ for the PR. * ``--lf`` and ``--ff`` now support long names: ``--last-failed`` and ``--failed-first`` respectively. Thanks `@MichaelAquilina`_ for the PR. * Added expected exceptions to pytest.raises fail message * Collection only displays progress ("collecting X items") when in a terminal. This avoids cluttering the output when using ``--color=yes`` to obtain colors in CI integrations systems (`#1397`_). **Bug Fixes** * The ``-s`` and ``-c`` options should now work under ``xdist``; ``Config.fromdictargs`` now represents its input much more faithfully. Thanks to `@bukzor`_ for the complete PR (`#680`_). * Fix (`#1290`_): support Python 3.5's ``@`` operator in assertion rewriting. Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR. * Fix formatting utf-8 explanation messages (`#1379`_). Thanks `@biern`_ for the PR. * Fix `traceback style docs`_ to describe all of the available options (auto/long/short/line/native/no), with ``auto`` being the default since v2.6. Thanks `@hackebrot`_ for the PR. * Fix (`#1422`_): junit record_xml_property doesn't allow multiple records with same name. .. _`traceback style docs`: https://pytest.org/latest/usage.html#modifying-python-traceback-printing .. _#1422: https://github.com/pytest-dev/pytest/issues/1422 .. _#1379: https://github.com/pytest-dev/pytest/issues/1379 .. _#1366: https://github.com/pytest-dev/pytest/issues/1366 .. _#1040: https://github.com/pytest-dev/pytest/pull/1040 .. _#680: https://github.com/pytest-dev/pytest/issues/680 .. _#1287: https://github.com/pytest-dev/pytest/pull/1287 .. _#1226: https://github.com/pytest-dev/pytest/pull/1226 .. _#1290: https://github.com/pytest-dev/pytest/pull/1290 .. _#1355: https://github.com/pytest-dev/pytest/pull/1355 .. _#1397: https://github.com/pytest-dev/pytest/issues/1397 .. _@biern: https://github.com/biern .. _@MichaelAquilina: https://github.com/MichaelAquilina .. _@bukzor: https://github.com/bukzor .. _@hpk42: https://github.com/hpk42 .. _@nicoddemus: https://github.com/nicoddemus .. _@jab: https://github.com/jab .. _@codewarrior0: https://github.com/codewarrior0 .. _@jaraco: https://github.com/jaraco .. _@The-Compiler: https://github.com/The-Compiler .. _@Shinkenjoe: https://github.com/Shinkenjoe .. _@tomviner: https://github.com/tomviner .. _@RonnyPfannschmidt: https://github.com/RonnyPfannschmidt .. _@rabbbit: https://github.com/rabbbit .. _@hackebrot: https://github.com/hackebrot pytest-4.6.9/doc/en/announce/release-3.4.0.rst0000664000372000037200000000205413604173400021512 0ustar travistravis00000000000000pytest-3.4.0 ======================================= The pytest team is proud to announce the 3.4.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Aaron * Alan Velasco * Anders Hovmöller * Andrew Toolan * Anthony Sottile * Aron Coyle * Brian Maissy * Bruno Oliveira * Cyrus Maden * Florian Bruhin * Henk-Jaap Wagenaar * Ian Lesperance * Jon Dufresne * Jurko Gospodnetić * Kate * Kimberly * Per A. Brodtkorb * Pierre-Alexandre Fonta * Raphael Castaneda * Ronny Pfannschmidt * ST John * Segev Finer * Thomas Hisch * Tzu-ping Chung * feuillemorte Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.0.2.rst0000664000372000037200000000100213604173400021500 0ustar travistravis00000000000000pytest-3.0.2 ============ pytest 3.0.2 has just been released to PyPI. This release fixes some regressions and bugs reported in version 3.0.1, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Ahn Ki-Wook * Bruno Oliveira * Florian Bruhin * Jordan Guymon * Raphael Pierzina * Ronny Pfannschmidt * mbyt Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.6.rst0000664000372000037200000000067413604173400021531 0ustar travistravis00000000000000pytest-4.6.6 ======================================= pytest 4.6.6 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Michael Goerz Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/index.rst0000664000372000037200000000363713604173400020551 0ustar travistravis00000000000000 Release announcements =========================================== .. toctree:: :maxdepth: 2 release-4.6.9 release-4.6.8 release-4.6.7 release-4.6.6 release-4.6.5 release-4.6.4 release-4.6.3 release-4.6.2 release-4.6.1 release-4.6.0 release-4.5.0 release-4.4.2 release-4.4.1 release-4.4.0 release-4.3.1 release-4.3.0 release-4.2.1 release-4.2.0 release-4.1.1 release-4.1.0 release-4.0.2 release-4.0.1 release-4.0.0 release-3.10.1 release-3.10.0 release-3.9.3 release-3.9.2 release-3.9.1 release-3.9.0 release-3.8.2 release-3.8.1 release-3.8.0 release-3.7.4 release-3.7.3 release-3.7.2 release-3.7.1 release-3.7.0 release-3.6.4 release-3.6.3 release-3.6.2 release-3.6.1 release-3.6.0 release-3.5.1 release-3.5.0 release-3.4.2 release-3.4.1 release-3.4.0 release-3.3.2 release-3.3.1 release-3.3.0 release-3.2.5 release-3.2.4 release-3.2.3 release-3.2.2 release-3.2.1 release-3.2.0 release-3.1.3 release-3.1.2 release-3.1.1 release-3.1.0 release-3.0.7 release-3.0.6 release-3.0.5 release-3.0.4 release-3.0.3 release-3.0.2 release-3.0.1 release-3.0.0 sprint2016 release-2.9.2 release-2.9.1 release-2.9.0 release-2.8.7 release-2.8.6 release-2.8.5 release-2.8.4 release-2.8.3 release-2.8.2 release-2.7.2 release-2.7.1 release-2.7.0 release-2.6.3 release-2.6.2 release-2.6.1 release-2.6.0 release-2.5.2 release-2.5.1 release-2.5.0 release-2.4.2 release-2.4.1 release-2.4.0 release-2.3.5 release-2.3.4 release-2.3.3 release-2.3.2 release-2.3.1 release-2.3.0 release-2.2.4 release-2.2.2 release-2.2.1 release-2.2.0 release-2.1.3 release-2.1.2 release-2.1.1 release-2.1.0 release-2.0.3 release-2.0.2 release-2.0.1 release-2.0.0 pytest-4.6.9/doc/en/announce/release-4.6.8.rst0000664000372000037200000000067013604173400021527 0ustar travistravis00000000000000pytest-4.6.8 ======================================= pytest 4.6.8 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Ryan Mast Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-4.6.0.rst0000664000372000037200000000165513604173400021523 0ustar travistravis00000000000000pytest-4.6.0 ======================================= The pytest team is proud to announce the 4.6.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Akiomi Kamakura * Anthony Sottile * Bruno Oliveira * Daniel Hahler * David Röthlisberger * Evan Kepner * Jeffrey Rackauckas * MyComputer * Nikita Krokosh * Raul Tambre * Thomas Hisch * Tim Hoffmann * Tomer Keren * Victor Maryama * danielx123 * oleg-yegorov Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.3.0.rst0000664000372000037200000001263313604173400021514 0ustar travistravis00000000000000pytest-2.3: improved fixtures / better unittest integration ============================================================================= pytest-2.3 comes with many major improvements for fixture/funcarg management and parametrized testing in Python. It is now easier, more efficient and more predicatable to re-run the same tests with different fixture instances. Also, you can directly declare the caching "scope" of fixtures so that dependent tests throughout your whole test suite can re-use database or other expensive fixture objects with ease. Lastly, it's possible for fixture functions (formerly known as funcarg factories) to use other fixtures, allowing for a completely modular and re-useable fixture design. For detailed info and tutorial-style examples, see: http://pytest.org/latest/fixture.html Moreover, there is now support for using pytest fixtures/funcargs with unittest-style suites, see here for examples: http://pytest.org/latest/unittest.html Besides, more unittest-test suites are now expected to "simply work" with pytest. All changes are backward compatible and you should be able to continue to run your test suites and 3rd party plugins that worked with pytest-2.2.4. If you are interested in the precise reasoning (including examples) of the pytest-2.3 fixture evolution, please consult http://pytest.org/latest/funcarg_compare.html For general info on installation and getting started: http://pytest.org/latest/getting-started.html Docs and PDF access as usual at: http://pytest.org and more details for those already in the knowing of pytest can be found in the CHANGELOG below. Particular thanks for this release go to Floris Bruynooghe, Alex Okrushko Carl Meyer, Ronny Pfannschmidt, Benjamin Peterson and Alex Gaynor for helping to get the new features right and well integrated. Ronny and Floris also helped to fix a number of bugs and yet more people helped by providing bug reports. have fun, holger krekel Changes between 2.2.4 and 2.3.0 ----------------------------------- - fix issue202 - better automatic names for parametrized test functions - fix issue139 - introduce @pytest.fixture which allows direct scoping and parametrization of funcarg factories. Introduce new @pytest.setup marker to allow the writing of setup functions which accept funcargs. - fix issue198 - conftest fixtures were not found on windows32 in some circumstances with nested directory structures due to path manipulation issues - fix issue193 skip test functions with were parametrized with empty parameter sets - fix python3.3 compat, mostly reporting bits that previously depended on dict ordering - introduce re-ordering of tests by resource and parametrization setup which takes precedence to the usual file-ordering - fix issue185 monkeypatching time.time does not cause pytest to fail - fix issue172 duplicate call of pytest.setup-decoratored setup_module functions - fix junitxml=path construction so that if tests change the current working directory and the path is a relative path it is constructed correctly from the original current working dir. - fix "python setup.py test" example to cause a proper "errno" return - fix issue165 - fix broken doc links and mention stackoverflow for FAQ - catch unicode-issues when writing failure representations to terminal to prevent the whole session from crashing - fix xfail/skip confusion: a skip-mark or an imperative pytest.skip will now take precedence before xfail-markers because we can't determine xfail/xpass status in case of a skip. see also: http://stackoverflow.com/questions/11105828/in-py-test-when-i-explicitly-skip-a-test-that-is-marked-as-xfail-how-can-i-get - always report installed 3rd party plugins in the header of a test run - fix issue160: a failing setup of an xfail-marked tests should be reported as xfail (not xpass) - fix issue128: show captured output when capsys/capfd are used - fix issue179: properly show the dependency chain of factories - pluginmanager.register(...) now raises ValueError if the plugin has been already registered or the name is taken - fix issue159: improve http://pytest.org/latest/faq.html especially with respect to the "magic" history, also mention pytest-django, trial and unittest integration. - make request.keywords and node.keywords writable. All descendant collection nodes will see keyword values. Keywords are dictionaries containing markers and other info. - fix issue 178: xml binary escapes are now wrapped in py.xml.raw - fix issue 176: correctly catch the builtin AssertionError even when we replaced AssertionError with a subclass on the python level - factory discovery no longer fails with magic global callables that provide no sane __code__ object (mock.call for example) - fix issue 182: testdir.inprocess_run now considers passed plugins - fix issue 188: ensure sys.exc_info is clear on python2 before calling into a test - fix issue 191: add unittest TestCase runTest method support - fix issue 156: monkeypatch correctly handles class level descriptors - reporting refinements: - pytest_report_header now receives a "startdir" so that you can use startdir.bestrelpath(yourpath) to show nice relative path - allow plugins to implement both pytest_report_header and pytest_sessionstart (sessionstart is invoked first). - don't show deselected reason line if there is none - py.test -vv will show all of assert comparisons instead of truncating pytest-4.6.9/doc/en/announce/release-3.6.0.rst0000664000372000037200000000156513604173400021522 0ustar travistravis00000000000000pytest-3.6.0 ======================================= The pytest team is proud to announce the 3.6.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Shaw * ApaDoctor * Brian Maissy * Bruno Oliveira * Jon Dufresne * Katerina Koukiou * Miro Hrončok * Rachel Kogan * Ronny Pfannschmidt * Tim Hughes * Tyler Goodlet * Ville Skyttä * aviral1701 * feuillemorte Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.6.3.rst0000664000372000037200000000107413604173400021520 0ustar travistravis00000000000000pytest-3.6.3 ======================================= pytest 3.6.3 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * AdamEr8 * Anthony Sottile * Bruno Oliveira * Jean-Paul Calderone * Jon Dufresne * Marcelo Duarte Trevisani * Ondřej Súkup * Ronny Pfannschmidt * T.E.A de Souza * Victor Maryama Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.8.2.rst0000664000372000037200000000107413604173400021521 0ustar travistravis00000000000000pytest-3.8.2 ======================================= pytest 3.8.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Ankit Goel * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Denis Otkidach * Harry Percival * Jeffrey Rackauckas * Jose Carlos Menezes * Ronny Pfannschmidt * Zac-HD * iwanb Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.6.3.rst0000664000372000037200000000322013604173400021512 0ustar travistravis00000000000000pytest-2.6.3: fixes and little improvements =========================================================================== pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is drop-in compatible to 2.5.2 and 2.6.X. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed, among them: Floris Bruynooghe Oleg Sinyavskiy Uwe Schmitt Charles Cloud Wolfgang Schnerring have fun, holger krekel Changes 2.6.3 ====================== - fix issue575: xunit-xml was reporting collection errors as failures instead of errors, thanks Oleg Sinyavskiy. - fix issue582: fix setuptools example, thanks Laszlo Papp and Ronny Pfannschmidt. - Fix infinite recursion bug when pickling capture.EncodedFile, thanks Uwe Schmitt. - fix issue589: fix bad interaction with numpy and others when showing exceptions. Check for precise "maximum recursion depth exceed" exception instead of presuming any RuntimeError is that one (implemented in py dep). Thanks Charles Cloud for analysing the issue. - fix conftest related fixture visibility issue: when running with a CWD outside of a test package pytest would get fixture discovery wrong. Thanks to Wolfgang Schnerring for figuring out a reproducible example. - Introduce pytest_enter_pdb hook (needed e.g. by pytest_timeout to cancel the timeout when interactively entering pdb). Thanks Wolfgang Schnerring. - check xfail/skip also with non-python function test items. Thanks Floris Bruynooghe. pytest-4.6.9/doc/en/announce/release-2.1.2.rst0000664000372000037200000000240413604173400021507 0ustar travistravis00000000000000py.test 2.1.2: bug fixes and fixes for jython =========================================================================== pytest-2.1.2 is a minor backward compatible maintenance release of the popular py.test testing tool. pytest is commonly used for unit, functional- and integration testing. See extensive docs with examples here: http://pytest.org/ Most bug fixes address remaining issues with the perfected assertions introduced in the 2.1 series - many thanks to the bug reporters and to Benjamin Peterson for helping to fix them. pytest should also work better with Jython-2.5.1 (and Jython trunk). If you want to install or upgrade pytest, just type one of:: pip install -U pytest # or easy_install -U pytest best, holger krekel / http://merlinux.eu Changes between 2.1.1 and 2.1.2 ---------------------------------------- - fix assertion rewriting on files with windows newlines on some Python versions - refine test discovery by package/module name (--pyargs), thanks Florian Mayer - fix issue69 / assertion rewriting fixed on some boolean operations - fix issue68 / packages now work with assertion rewriting - fix issue66: use different assertion rewriting caches when the -O option is passed - don't try assertion rewriting on Jython, use reinterp pytest-4.6.9/doc/en/announce/release-3.9.1.rst0000664000372000037200000000067613604173400021530 0ustar travistravis00000000000000pytest-3.9.1 ======================================= pytest 3.9.1 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Bruno Oliveira * Ronny Pfannschmidt * Thomas Hisch Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-3.4.2.rst0000664000372000037200000000106713604173400021517 0ustar travistravis00000000000000pytest-3.4.2 ======================================= pytest 3.4.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Allan Feldman * Bruno Oliveira * Florian Bruhin * Jason R. Coombs * Kyle Altendorf * Maik Figura * Ronny Pfannschmidt * codetriage-readme-bot * feuillemorte * joshm91 * mike Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.3.2.rst0000664000372000037200000000334113604173400021512 0ustar travistravis00000000000000pytest-2.3.2: some fixes and more traceback-printing speed =========================================================================== pytest-2.3.2 is another stabilization release: - issue 205: fixes a regression with conftest detection - issue 208/29: fixes traceback-printing speed in some bad cases - fix teardown-ordering for parametrized setups - fix unittest and trial compat behaviour with respect to runTest() methods - issue 206 and others: some improvements to packaging - fix issue127 and others: improve some docs See http://pytest.org/ for general information. To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest best, holger krekel Changes between 2.3.1 and 2.3.2 ----------------------------------- - fix issue208 and fix issue29 use new py version to avoid long pauses when printing tracebacks in long modules - fix issue205 - conftests in subdirs customizing pytest_pycollect_makemodule and pytest_pycollect_makeitem now work properly - fix teardown-ordering for parametrized setups - fix issue127 - better documentation for pytest_addoption and related objects. - fix unittest behaviour: TestCase.runtest only called if there are test methods defined - improve trial support: don't collect its empty unittest.TestCase.runTest() method - "python setup.py test" now works with pytest itself - fix/improve internal/packaging related bits: - exception message check of test_nose.py now passes on python33 as well - issue206 - fix test_assertrewrite.py to work when a global PYTHONDONTWRITEBYTECODE=1 is present - add tox.ini to pytest distribution so that ignore-dirs and others config bits are properly distributed for maintainers who run pytest-own tests pytest-4.6.9/doc/en/announce/release-3.10.0.rst0000664000372000037200000000171513604173400021572 0ustar travistravis00000000000000pytest-3.10.0 ======================================= The pytest team is proud to announce the 3.10.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anders Hovmöller * Andreu Vallbona Plazas * Ankit Goel * Anthony Sottile * Bernardo Gomes * Brianna Laugher * Bruno Oliveira * Daniel Hahler * David Szotten * Mick Koch * Niclas Olofsson * Palash Chatterjee * Ronny Pfannschmidt * Sven-Hendrik Haase * Ville Skyttä * William Jamir Silva Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.8.5.rst0000664000372000037200000000213413604173400021521 0ustar travistravis00000000000000pytest-2.8.5 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.4. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Alex Gaynor aselus-hub Bruno Oliveira Ronny Pfannschmidt Happy testing, The py.test Development Team 2.8.5 (compared to 2.8.4) ------------------------- - fix #1243: fixed issue where class attributes injected during collection could break pytest. PR by Alexei Kozlenok, thanks Ronny Pfannschmidt and Bruno Oliveira for the review and help. - fix #1074: precompute junitxml chunks instead of storing the whole tree in objects Thanks Bruno Oliveira for the report and Ronny Pfannschmidt for the PR - fix #1238: fix ``pytest.deprecated_call()`` receiving multiple arguments (Regression introduced in 2.8.4). Thanks Alex Gaynor for the report and Bruno Oliveira for the PR. pytest-4.6.9/doc/en/announce/release-4.4.2.rst0000664000372000037200000000120313604173400021510 0ustar travistravis00000000000000pytest-4.4.2 ======================================= pytest 4.4.2 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Allan Lewis * Anthony Sottile * Bruno Oliveira * DamianSkrzypczak * Daniel Hahler * Don Kirkby * Douglas Thor * Hugo * Ilya Konstantinov * Jon Dufresne * Matt Cooper * Nikolay Kondratyev * Ondřej Súkup * Peter Schutt * Romain Chossart * Sitaktif Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.9.2.rst0000664000372000037200000000434113604173400021521 0ustar travistravis00000000000000pytest-2.9.2 ============ pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Adam Chainz Benjamin Dopplinger Bruno Oliveira Florian Bruhin John Towler Martin Prusse Meng Jue MengJueM Omar Kohl Quentin Pradet Ronny Pfannschmidt Thomas Güttler TomV Tyler Goodlet Happy testing, The py.test Development Team 2.9.2 (compared to 2.9.1) --------------------------- **Bug Fixes** * fix `#510`_: skip tests where one parameterize dimension was empty thanks Alex Stapleton for the Report and `@RonnyPfannschmidt`_ for the PR * Fix Xfail does not work with condition keyword argument. Thanks `@astraw38`_ for reporting the issue (`#1496`_) and `@tomviner`_ for PR the (`#1524`_). * Fix win32 path issue when putting custom config file with absolute path in ``pytest.main("-c your_absolute_path")``. * Fix maximum recursion depth detection when raised error class is not aware of unicode/encoded bytes. Thanks `@prusse-martin`_ for the PR (`#1506`_). * Fix ``pytest.mark.skip`` mark when used in strict mode. Thanks `@pquentin`_ for the PR and `@RonnyPfannschmidt`_ for showing how to fix the bug. * Minor improvements and fixes to the documentation. Thanks `@omarkohl`_ for the PR. * Fix ``--fixtures`` to show all fixture definitions as opposed to just one per fixture name. Thanks to `@hackebrot`_ for the PR. .. _#510: https://github.com/pytest-dev/pytest/issues/510 .. _#1506: https://github.com/pytest-dev/pytest/pull/1506 .. _#1496: https://github.com/pytest-dev/pytest/issue/1496 .. _#1524: https://github.com/pytest-dev/pytest/issue/1524 .. _@astraw38: https://github.com/astraw38 .. _@hackebrot: https://github.com/hackebrot .. _@omarkohl: https://github.com/omarkohl .. _@pquentin: https://github.com/pquentin .. _@prusse-martin: https://github.com/prusse-martin .. _@RonnyPfannschmidt: https://github.com/RonnyPfannschmidt .. _@tomviner: https://github.com/tomviner pytest-4.6.9/doc/en/announce/release-4.4.0.rst0000664000372000037200000000155113604173400021514 0ustar travistravis00000000000000pytest-4.4.0 ======================================= The pytest team is proud to announce the 4.4.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Sottile * ApaDoctor * Bernhard M. Wiedemann * Brian Skinn * Bruno Oliveira * Daniel Hahler * Gary Tyler * Jeong YunWon * Miro Hrončok * Takafumi Arakaki * henrykironde * smheidrich Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-2.8.7.rst0000664000372000037200000000132113604173400021520 0ustar travistravis00000000000000pytest-2.8.7 ============ This is a hotfix release to solve a regression in the builtin monkeypatch plugin that got introduced in 2.8.6. pytest is a mature Python testing tool with more than a 1100 tests against itself, passing on many different interpreters and platforms. This release is supposed to be drop-in compatible to 2.8.5. See below for the changes and see docs at: http://pytest.org As usual, you can upgrade from pypi via:: pip install -U pytest Thanks to all who contributed to this release, among them: Ronny Pfannschmidt Happy testing, The py.test Development Team 2.8.7 (compared to 2.8.6) ------------------------- - fix #1338: use predictable object resolution for monkeypatch pytest-4.6.9/doc/en/announce/release-4.6.5.rst0000664000372000037200000000071613604173400021525 0ustar travistravis00000000000000pytest-4.6.5 ======================================= pytest 4.6.5 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Daniel Hahler * Thomas Grainger Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/release-2.5.1.rst0000664000372000037200000000326313604173400021516 0ustar travistravis00000000000000pytest-2.5.1: fixes and new home page styling =========================================================================== pytest is a mature Python testing tool with more than a 1000 tests against itself, passing on many different interpreters and platforms. The 2.5.1 release maintains the "zero-reported-bugs" promise by fixing the three bugs reported since the last release a few days ago. It also features a new home page styling implemented by Tobias Bieniek, based on the flask theme from Armin Ronacher: http://pytest.org If you have anything more to improve styling and docs, we'd be very happy to merge further pull requests. On the coding side, the release also contains a little enhancement to fixture decorators allowing to directly influence generation of test ids, thanks to Floris Bruynooghe. Other thanks for helping with this release go to Anatoly Bubenkoff and Ronny Pfannschmidt. As usual, you can upgrade from pypi via:: pip install -U pytest have fun and a nice remaining "bug-free" time of the year :) holger krekel 2.5.1 ----------------------------------- - merge new documentation styling PR from Tobias Bieniek. - fix issue403: allow parametrize of multiple same-name functions within a collection node. Thanks Andreas Kloeckner and Alex Gaynor for reporting and analysis. - Allow parameterized fixtures to specify the ID of the parameters by adding an ids argument to pytest.fixture() and pytest.yield_fixture(). Thanks Floris Bruynooghe. - fix issue404 by always using the binary xml escape in the junitxml plugin. Thanks Ronny Pfannschmidt. - fix issue407: fix addoption docstring to point to argparse instead of optparse. Thanks Daniel D. Wright. pytest-4.6.9/doc/en/announce/release-3.0.1.rst0000664000372000037200000000110113604173400021477 0ustar travistravis00000000000000pytest-3.0.1 ============ pytest 3.0.1 has just been released to PyPI. This release fixes some regressions reported in version 3.0.0, being a drop-in replacement. To upgrade: pip install --upgrade pytest The changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: Adam Chainz Andrew Svetlov Bruno Oliveira Daniel Hahler Dmitry Dygalo Florian Bruhin Marcin Bachry Ronny Pfannschmidt matthiasha Happy testing, The py.test Development Team pytest-4.6.9/doc/en/announce/release-2.3.5.rst0000664000372000037200000000630513604173400021520 0ustar travistravis00000000000000pytest-2.3.5: bug fixes and little improvements =========================================================================== pytest-2.3.5 is a maintenance release with many bug fixes and little improvements. See the changelog below for details. No backward compatibility issues are foreseen and all plugins which worked with the prior version are expected to work unmodified. Speaking of which, a few interesting new plugins saw the light last month: - pytest-instafail: show failure information while tests are running - pytest-qt: testing of GUI applications written with QT/Pyside - pytest-xprocess: managing external processes across test runs - pytest-random: randomize test ordering And several others like pytest-django saw maintenance releases. For a more complete list, check out https://pypi.org/search/?q=pytest For general information see: http://pytest.org/ To install or upgrade pytest: pip install -U pytest # or easy_install -U pytest Particular thanks to Floris, Ronny, Benjamin and the many bug reporters and fix providers. may the fixtures be with you, holger krekel Changes between 2.3.4 and 2.3.5 ----------------------------------- - never consider a fixture function for test function collection - allow re-running of test items / helps to fix pytest-reruntests plugin and also help to keep less fixture/resource references alive - put captured stdout/stderr into junitxml output even for passing tests (thanks Adam Goucher) - Issue 265 - integrate nose setup/teardown with setupstate so it doesn't try to teardown if it did not setup - issue 271 - don't write junitxml on slave nodes - Issue 274 - don't try to show full doctest example when doctest does not know the example location - issue 280 - disable assertion rewriting on buggy CPython 2.6.0 - inject "getfixture()" helper to retrieve fixtures from doctests, thanks Andreas Zeidler - issue 259 - when assertion rewriting, be consistent with the default source encoding of ASCII on Python 2 - issue 251 - report a skip instead of ignoring classes with init - issue250 unicode/str mixes in parametrization names and values now works - issue257, assertion-triggered compilation of source ending in a comment line doesn't blow up in python2.5 (fixed through py>=1.4.13.dev6) - fix --genscript option to generate standalone scripts that also work with python3.3 (importer ordering) - issue171 - in assertion rewriting, show the repr of some global variables - fix option help for "-k" - move long description of distribution into README.rst - improve docstring for metafunc.parametrize() - fix bug where using capsys with pytest.set_trace() in a test function would break when looking at capsys.readouterr() - allow to specify prefixes starting with "_" when customizing python_functions test discovery. (thanks Graham Horler) - improve PYTEST_DEBUG tracing output by putting extra data on a new lines with additional indent - ensure OutcomeExceptions like skip/fail have initialized exception attributes - issue 260 - don't use nose special setup on plain unittest cases - fix issue134 - print the collect errors that prevent running specified test items - fix issue266 - accept unicode in MarkEvaluator expressions pytest-4.6.9/doc/en/announce/release-4.0.0.rst0000664000372000037200000000133713604173400021512 0ustar travistravis00000000000000pytest-4.0.0 ======================================= The pytest team is proud to announce the 4.0.0 release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Bruno Oliveira * Daniel Hahler * Ronny Pfannschmidt Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.0.6.rst0000664000372000037200000000115213604173400021512 0ustar travistravis00000000000000pytest-3.0.6 ============ pytest 3.0.6 has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at http://doc.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: * Andreas Pelme * Bruno Oliveira * Dmitry Malinovsky * Eli Boyarski * Jakub Wilk * Jeff Widman * Loïc Estève * Luke Murphy * Miro Hrončok * Oscar Hellström * Peter Heatwole * Philippe Ombredanne * Ronny Pfannschmidt * Rutger Prins * Stefan Scherfke Happy testing, The pytest Development Team pytest-4.6.9/doc/en/announce/sprint2016.rst0000664000372000037200000000416213604173400021264 0ustar travistravis00000000000000python testing sprint June 20th-26th 2016 ====================================================== .. image:: ../img/freiburg2.jpg :width: 400 The pytest core group held the biggest sprint in its history in June 2016, taking place in the black forest town Freiburg in Germany. In February 2016 we started a `funding campaign on Indiegogo to cover expenses `_ The page also mentions some preliminary topics: - improving pytest-xdist test scheduling to take into account fixture setups and explicit user hints. - provide info on fixture dependencies during --collect-only - tying pytest-xdist to tox so that you can do "py.test -e py34" to run tests in a particular tox-managed virtualenv. Also look into making pytest-xdist use tox environments on remote ssh-sides so that remote dependency management becomes easier. - refactoring the fixture system so more people understand it :) - integrating PyUnit setup methods as autouse fixtures. possibly adding ways to influence ordering of same-scoped fixtures (so you can make a choice of which fixtures come before others) - fixing bugs and issues from the tracker, really an endless source :) Participants -------------- Over 20 participants took part from 4 continents, including employees from Splunk, Personalkollen, Cobe.io, FanDuel and Dolby. Some newcomers mixed with developers who have worked on pytest since its beginning, and of course everyone in between. Sprint organisation, schedule ------------------------------- People arrived in Freiburg on the 19th, with sprint development taking place on 20th, 21st, 22nd, 24th and 25th. On the 23rd we took a break day for some hot hiking in the Black Forest. Sprint activity was organised heavily around pairing, with plenty of group discusssions to take advantage of the high bandwidth, and lightning talks as well. Money / funding --------------- The Indiegogo campaign aimed for 11000 USD and in the end raised over 12000, to reimburse travel costs, pay for a sprint venue and catering. Excess money is reserved for further sprint/travel funding for pytest/tox contributors. pytest-4.6.9/doc/en/announce/release-3.5.0.rst0000664000372000037200000000204613604173400021514 0ustar travistravis00000000000000pytest-3.5.0 ======================================= The pytest team is proud to announce the 3.5.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Allan Feldman * Brian Maissy * Bruno Oliveira * Carlos Jenkins * Daniel Hahler * Florian Bruhin * Jason R. Coombs * Jeffrey Rackauckas * Jordan Speicher * Julien Palard * Kale Kundert * Kostis Anagnostopoulos * Kyle Altendorf * Maik Figura * Pedro Algarvio * Ronny Pfannschmidt * Tadeu Manoel * Tareq Alayan * Thomas Hisch * William Lee * codetriage-readme-bot * feuillemorte * joshm91 * mike Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/announce/release-3.3.0.rst0000664000372000037200000000200713604173400021507 0ustar travistravis00000000000000pytest-3.3.0 ======================================= The pytest team is proud to announce the 3.3.0 release! pytest is a mature Python testing tool with more than a 1600 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: http://doc.pytest.org/en/latest/changelog.html For complete documentation, please visit: http://docs.pytest.org As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: * Anthony Sottile * Bruno Oliveira * Ceridwen * Daniel Hahler * Dirk Thomas * Dmitry Malinovsky * Florian Bruhin * George Y. Kussumoto * Hugo * Jesús Espino * Joan Massich * Ofir * OfirOshir * Ronny Pfannschmidt * Samuel Dion-Girardeau * Srinivas Reddy Thatiparthy * Sviatoslav Abakumov * Tarcisio Fischer * Thomas Hisch * Tyler Goodlet * hugovk * je * prokaktus Happy testing, The Pytest Development Team pytest-4.6.9/doc/en/talks.rst0000664000372000037200000001134513604173400016745 0ustar travistravis00000000000000 Talks and Tutorials ========================== .. .. sidebar:: Next Open Trainings `Professional Testing with Python `_, 26-28 April 2017, Leipzig, Germany. .. _`funcargs`: funcargs.html Books --------------------------------------------- - `pytest Quick Start Guide, by Bruno Oliveira (2018) `_. - `Python Testing with pytest, by Brian Okken (2017) `_. Talks and blog postings --------------------------------------------- - pytest: recommendations, basic packages for testing in Python and Django, Andreu Vallbona, PyconES 2017 (`slides in english `_, `video in spanish `_) - `pytest advanced, Andrew Svetlov (Russian, PyCon Russia, 2016) `_. - `Pythonic testing, Igor Starikov (Russian, PyNsk, November 2016) `_. - `pytest - Rapid Simple Testing, Florian Bruhin, Swiss Python Summit 2016 `_. - `Improve your testing with Pytest and Mock, Gabe Hollombe, PyCon SG 2015 `_. - `Introduction to pytest, Andreas Pelme, EuroPython 2014 `_. - `Advanced Uses of py.test Fixtures, Floris Bruynooghe, EuroPython 2014 `_. - `Why i use py.test and maybe you should too, Andy Todd, Pycon AU 2013 `_ - `3-part blog series about pytest from @pydanny alias Daniel Greenfeld (January 2014) `_ - `pytest: helps you write better Django apps, Andreas Pelme, DjangoCon Europe 2014 `_. - :ref:`fixtures` - `Testing Django Applications with pytest, Andreas Pelme, EuroPython 2013 `_. - `Testes pythonics com py.test, Vinicius Belchior Assef Neto, Plone Conf 2013, Brazil `_. - `Introduction to py.test fixtures, FOSDEM 2013, Floris Bruynooghe `_. - `pytest feature and release highlights, Holger Krekel (GERMAN, October 2013) `_ - `pytest introduction from Brian Okken (January 2013) `_ - pycon australia 2012 pytest talk from Brianna Laugher (`video `_, `slides `_, `code `_) - `pycon 2012 US talk video from Holger Krekel `_ - `monkey patching done right`_ (blog post, consult `monkeypatch plugin`_ for up-to-date API) Test parametrization: - `generating parametrized tests with fixtures`_. - `test generators and cached setup`_ - `parametrizing tests, generalized`_ (blog post) - `putting test-hooks into local or global plugins`_ (blog post) Assertion introspection: - `(07/2011) Behind the scenes of pytest's new assertion rewriting `_ Distributed testing: - `simultaneously test your code on all platforms`_ (blog entry) Plugin specific examples: - `skipping slow tests by default in pytest`_ (blog entry) - `many examples in the docs for plugins`_ .. _`skipping slow tests by default in pytest`: http://bruynooghe.blogspot.com/2009/12/skipping-slow-test-by-default-in-pytest.html .. _`many examples in the docs for plugins`: plugins.html .. _`monkeypatch plugin`: monkeypatch.html .. _`application setup in test functions with fixtures`: fixture.html#interdependent-fixtures .. _`simultaneously test your code on all platforms`: http://tetamap.wordpress.com/2009/03/23/new-simultanously-test-your-code-on-all-platforms/ .. _`monkey patching done right`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ .. _`putting test-hooks into local or global plugins`: http://tetamap.wordpress.com/2009/05/14/putting-test-hooks-into-local-and-global-plugins/ .. _`parametrizing tests, generalized`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/ .. _`generating parametrized tests with fixtures`: parametrize.html#test-generators .. _`test generators and cached setup`: http://bruynooghe.blogspot.com/2010/06/pytest-test-generators-and-cached-setup.html pytest-4.6.9/doc/en/logging.rst0000664000372000037200000002045713604173400017261 0ustar travistravis00000000000000.. _logging: Logging ------- pytest captures log messages of level ``WARNING`` or above automatically and displays them in their own section for each failed test in the same manner as captured stdout and stderr. Running without options: .. code-block:: bash pytest Shows failed tests like so: .. code-block:: pytest ----------------------- Captured stdlog call ---------------------- test_reporting.py 26 WARNING text going to logger ----------------------- Captured stdout call ---------------------- text going to stdout ----------------------- Captured stderr call ---------------------- text going to stderr ==================== 2 failed in 0.02 seconds ===================== By default each captured log message shows the module, line number, log level and message. If desired the log and date format can be specified to anything that the logging module supports by passing specific formatting options: .. code-block:: bash pytest --log-format="%(asctime)s %(levelname)s %(message)s" \ --log-date-format="%Y-%m-%d %H:%M:%S" Shows failed tests like so: .. code-block:: pytest ----------------------- Captured stdlog call ---------------------- 2010-04-10 14:48:44 WARNING text going to logger ----------------------- Captured stdout call ---------------------- text going to stdout ----------------------- Captured stderr call ---------------------- text going to stderr ==================== 2 failed in 0.02 seconds ===================== These options can also be customized through ``pytest.ini`` file: .. code-block:: ini [pytest] log_format = %(asctime)s %(levelname)s %(message)s log_date_format = %Y-%m-%d %H:%M:%S Further it is possible to disable reporting of captured content (stdout, stderr and logs) on failed tests completely with: .. code-block:: bash pytest --show-capture=no caplog fixture ^^^^^^^^^^^^^^ Inside tests it is possible to change the log level for the captured log messages. This is supported by the ``caplog`` fixture:: def test_foo(caplog): caplog.set_level(logging.INFO) pass By default the level is set on the root logger, however as a convenience it is also possible to set the log level of any logger:: def test_foo(caplog): caplog.set_level(logging.CRITICAL, logger='root.baz') pass The log levels set are restored automatically at the end of the test. It is also possible to use a context manager to temporarily change the log level inside a ``with`` block:: def test_bar(caplog): with caplog.at_level(logging.INFO): pass Again, by default the level of the root logger is affected but the level of any logger can be changed instead with:: def test_bar(caplog): with caplog.at_level(logging.CRITICAL, logger='root.baz'): pass Lastly all the logs sent to the logger during the test run are made available on the fixture in the form of both the ``logging.LogRecord`` instances and the final log text. This is useful for when you want to assert on the contents of a message:: def test_baz(caplog): func_under_test() for record in caplog.records: assert record.levelname != 'CRITICAL' assert 'wally' not in caplog.text For all the available attributes of the log records see the ``logging.LogRecord`` class. You can also resort to ``record_tuples`` if all you want to do is to ensure, that certain messages have been logged under a given logger name with a given severity and message:: def test_foo(caplog): logging.getLogger().info('boo %s', 'arg') assert caplog.record_tuples == [ ('root', logging.INFO, 'boo arg'), ] You can call ``caplog.clear()`` to reset the captured log records in a test:: def test_something_with_clearing_records(caplog): some_method_that_creates_log_records() caplog.clear() your_test_method() assert ['Foo'] == [rec.message for rec in caplog.records] The ``caplog.records`` attribute contains records from the current stage only, so inside the ``setup`` phase it contains only setup logs, same with the ``call`` and ``teardown`` phases. To access logs from other stages, use the ``caplog.get_records(when)`` method. As an example, if you want to make sure that tests which use a certain fixture never log any warnings, you can inspect the records for the ``setup`` and ``call`` stages during teardown like so: .. code-block:: python @pytest.fixture def window(caplog): window = create_window() yield window for when in ("setup", "call"): messages = [ x.message for x in caplog.get_records(when) if x.level == logging.WARNING ] if messages: pytest.fail( "warning messages encountered during testing: {}".format(messages) ) The full API is available at :class:`_pytest.logging.LogCaptureFixture`. .. _live_logs: Live Logs ^^^^^^^^^ By setting the :confval:`log_cli` configuration option to ``true``, pytest will output logging records as they are emitted directly into the console. You can specify the logging level for which log records with equal or higher level are printed to the console by passing ``--log-cli-level``. This setting accepts the logging level names as seen in python's documentation or an integer as the logging level num. Additionally, you can also specify ``--log-cli-format`` and ``--log-cli-date-format`` which mirror and default to ``--log-format`` and ``--log-date-format`` if not provided, but are applied only to the console logging handler. All of the CLI log options can also be set in the configuration INI file. The option names are: * ``log_cli_level`` * ``log_cli_format`` * ``log_cli_date_format`` If you need to record the whole test suite logging calls to a file, you can pass ``--log-file=/path/to/log/file``. This log file is opened in write mode which means that it will be overwritten at each run tests session. You can also specify the logging level for the log file by passing ``--log-file-level``. This setting accepts the logging level names as seen in python's documentation(ie, uppercased level names) or an integer as the logging level num. Additionally, you can also specify ``--log-file-format`` and ``--log-file-date-format`` which are equal to ``--log-format`` and ``--log-date-format`` but are applied to the log file logging handler. All of the log file options can also be set in the configuration INI file. The option names are: * ``log_file`` * ``log_file_level`` * ``log_file_format`` * ``log_file_date_format`` You can call ``set_log_path()`` to customize the log_file path dynamically. This functionality is considered **experimental**. .. _log_release_notes: Release notes ^^^^^^^^^^^^^ This feature was introduced as a drop-in replacement for the `pytest-catchlog `_ plugin and they conflict with each other. The backward compatibility API with ``pytest-capturelog`` has been dropped when this feature was introduced, so if for that reason you still need ``pytest-catchlog`` you can disable the internal feature by adding to your ``pytest.ini``: .. code-block:: ini [pytest] addopts=-p no:logging .. _log_changes_3_4: Incompatible changes in pytest 3.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This feature was introduced in ``3.3`` and some **incompatible changes** have been made in ``3.4`` after community feedback: * Log levels are no longer changed unless explicitly requested by the :confval:`log_level` configuration or ``--log-level`` command-line options. This allows users to configure logger objects themselves. * :ref:`Live Logs ` is now disabled by default and can be enabled setting the :confval:`log_cli` configuration option to ``true``. When enabled, the verbosity is increased so logging for each test is visible. * :ref:`Live Logs ` are now sent to ``sys.stdout`` and no longer require the ``-s`` command-line option to work. If you want to partially restore the logging behavior of version ``3.3``, you can add this options to your ``ini`` file: .. code-block:: ini [pytest] log_cli=true log_level=NOTSET More details about the discussion that lead to this changes can be read in issue `#3013 `_. pytest-4.6.9/doc/en/fixture.rst0000664000372000037200000012767013604173400017326 0ustar travistravis00000000000000.. _fixture: .. _fixtures: .. _`fixture functions`: pytest fixtures: explicit, modular, scalable ======================================================== .. currentmodule:: _pytest.python .. _`xUnit`: http://en.wikipedia.org/wiki/XUnit .. _`purpose of test fixtures`: http://en.wikipedia.org/wiki/Test_fixture#Software .. _`Dependency injection`: http://en.wikipedia.org/wiki/Dependency_injection The `purpose of test fixtures`_ is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions: * fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. * fixtures are implemented in a modular manner, as each fixture name triggers a *fixture function* which can itself use other fixtures. * fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across function, class, module or whole test session scopes. In addition, pytest continues to support :ref:`xunitsetup`. You can mix both styles, moving incrementally from classic to new style, as you prefer. You can also start out from existing :ref:`unittest.TestCase style ` or :ref:`nose based ` projects. .. _`funcargs`: .. _`funcarg mechanism`: .. _`fixture function`: .. _`@pytest.fixture`: .. _`pytest.fixture`: Fixtures as Function arguments ----------------------------------------- Test functions can receive fixture objects by naming them as an input argument. For each argument name, a fixture function with that name provides the fixture object. Fixture functions are registered by marking them with :py:func:`@pytest.fixture <_pytest.python.fixture>`. Let's look at a simple self-contained test module containing a fixture and a test function using it:: # content of ./test_smtpsimple.py import pytest @pytest.fixture def smtp_connection(): import smtplib return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert 0 # for demo purposes Here, the ``test_ehlo`` needs the ``smtp_connection`` fixture value. pytest will discover and call the :py:func:`@pytest.fixture <_pytest.python.fixture>` marked ``smtp_connection`` fixture function. Running the test looks like this: .. code-block:: pytest $ pytest test_smtpsimple.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_smtpsimple.py F [100%] ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ smtp_connection = def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 > assert 0 # for demo purposes E assert 0 test_smtpsimple.py:11: AssertionError ========================= 1 failed in 0.12 seconds ========================= In the failure traceback we see that the test function was called with a ``smtp_connection`` argument, the ``smtplib.SMTP()`` instance created by the fixture function. The test function fails on our deliberate ``assert 0``. Here is the exact protocol used by ``pytest`` to call the test function this way: 1. pytest :ref:`finds ` the ``test_ehlo`` because of the ``test_`` prefix. The test function needs a function argument named ``smtp_connection``. A matching fixture function is discovered by looking for a fixture-marked function named ``smtp_connection``. 2. ``smtp_connection()`` is called to create an instance. 3. ``test_ehlo()`` is called and fails in the last line of the test function. Note that if you misspell a function argument or want to use one that isn't available, you'll see an error with a list of available function arguments. .. note:: You can always issue: .. code-block:: bash pytest --fixtures test_simplefactory.py to see available fixtures (fixtures with leading ``_`` are only shown if you add the ``-v`` option). Fixtures: a prime example of dependency injection --------------------------------------------------- Fixtures allow test functions to easily receive and work against specific pre-initialized application objects without having to care about import/setup/cleanup details. It's a prime example of `dependency injection`_ where fixture functions take the role of the *injector* and test functions are the *consumers* of fixture objects. .. _`conftest.py`: .. _`conftest`: ``conftest.py``: sharing fixture functions ------------------------------------------ If during implementing your tests you realize that you want to use a fixture function from multiple test files you can move it to a ``conftest.py`` file. You don't need to import the fixture you want to use in a test, it automatically gets discovered by pytest. The discovery of fixture functions starts at test classes, then test modules, then ``conftest.py`` files and finally builtin and third party plugins. You can also use the ``conftest.py`` file to implement :ref:`local per-directory plugins `. Sharing test data ----------------- If you want to make test data from files available to your tests, a good way to do this is by loading these data in a fixture for use by your tests. This makes use of the automatic caching mechanisms of pytest. Another good approach is by adding the data files in the ``tests`` folder. There are also community plugins available to help managing this aspect of testing, e.g. `pytest-datadir `__ and `pytest-datafiles `__. .. _smtpshared: Scope: sharing a fixture instance across tests in a class, module or session ---------------------------------------------------------------------------- .. regendoc:wipe Fixtures requiring network access depend on connectivity and are usually time-expensive to create. Extending the previous example, we can add a ``scope="module"`` parameter to the :py:func:`@pytest.fixture <_pytest.python.fixture>` invocation to cause the decorated ``smtp_connection`` fixture function to only be invoked once per test *module* (the default is to invoke once per test *function*). Multiple test functions in a test module will thus each receive the same ``smtp_connection`` fixture instance, thus saving time. Possible values for ``scope`` are: ``function``, ``class``, ``module``, ``package`` or ``session``. The next example puts the fixture function into a separate ``conftest.py`` file so that tests from multiple test modules in the directory can access the fixture function:: # content of conftest.py import pytest import smtplib @pytest.fixture(scope="module") def smtp_connection(): return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) The name of the fixture again is ``smtp_connection`` and you can access its result by listing the name ``smtp_connection`` as an input parameter in any test or fixture function (in or below the directory where ``conftest.py`` is located):: # content of test_module.py def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg assert 0 # for demo purposes def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 assert 0 # for demo purposes We deliberately insert failing ``assert 0`` statements in order to inspect what is going on and can now run the tests: .. code-block:: pytest $ pytest test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py FF [100%] ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ smtp_connection = def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 test_module.py:6: AssertionError ________________________________ test_noop _________________________________ smtp_connection = def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 test_module.py:11: AssertionError ========================= 2 failed in 0.12 seconds ========================= You see the two ``assert 0`` failing and more importantly you can also see that the same (module-scoped) ``smtp_connection`` object was passed into the two test functions because pytest shows the incoming argument values in the traceback. As a result, the two test functions using ``smtp_connection`` run as quick as a single one because they reuse the same instance. If you decide that you rather want to have a session-scoped ``smtp_connection`` instance, you can simply declare it: .. code-block:: python @pytest.fixture(scope="session") def smtp_connection(): # the returned fixture value will be shared for # all tests needing it ... Finally, the ``class`` scope will invoke the fixture once per test *class*. .. note:: Pytest will only cache one instance of a fixture at a time. This means that when using a parametrized fixture, pytest may invoke a fixture more than once in the given scope. ``package`` scope (experimental) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In pytest 3.7 the ``package`` scope has been introduced. Package-scoped fixtures are finalized when the last test of a *package* finishes. .. warning:: This functionality is considered **experimental** and may be removed in future versions if hidden corner-cases or serious problems with this functionality are discovered after it gets more usage in the wild. Use this new feature sparingly and please make sure to report any issues you find. Higher-scoped fixtures are instantiated first --------------------------------------------- Within a function request for features, fixture of higher-scopes (such as ``session``) are instantiated first than lower-scoped fixtures (such as ``function`` or ``class``). The relative order of fixtures of same scope follows the declared order in the test function and honours dependencies between fixtures. Consider the code below: .. code-block:: python @pytest.fixture(scope="session") def s1(): pass @pytest.fixture(scope="module") def m1(): pass @pytest.fixture def f1(tmpdir): pass @pytest.fixture def f2(): pass def test_foo(f1, m1, f2, s1): ... The fixtures requested by ``test_foo`` will be instantiated in the following order: 1. ``s1``: is the highest-scoped fixture (``session``). 2. ``m1``: is the second highest-scoped fixture (``module``). 3. ``tmpdir``: is a ``function``-scoped fixture, required by ``f1``: it needs to be instantiated at this point because it is a dependency of ``f1``. 4. ``f1``: is the first ``function``-scoped fixture in ``test_foo`` parameter list. 5. ``f2``: is the last ``function``-scoped fixture in ``test_foo`` parameter list. .. _`finalization`: Fixture finalization / executing teardown code ------------------------------------------------------------- pytest supports execution of fixture specific finalization code when the fixture goes out of scope. By using a ``yield`` statement instead of ``return``, all the code after the *yield* statement serves as the teardown code: .. code-block:: python # content of conftest.py import smtplib import pytest @pytest.fixture(scope="module") def smtp_connection(): smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) yield smtp_connection # provide the fixture value print("teardown smtp") smtp_connection.close() The ``print`` and ``smtp.close()`` statements will execute when the last test in the module has finished execution, regardless of the exception status of the tests. Let's execute it: .. code-block:: pytest $ pytest -s -q --tb=no FFteardown smtp 2 failed in 0.12 seconds We see that the ``smtp_connection`` instance is finalized after the two tests finished execution. Note that if we decorated our fixture function with ``scope='function'`` then fixture setup and cleanup would occur around each single test. In either case the test module itself does not need to change or know about these details of fixture setup. Note that we can also seamlessly use the ``yield`` syntax with ``with`` statements: .. code-block:: python # content of test_yield2.py import smtplib import pytest @pytest.fixture(scope="module") def smtp_connection(): with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection: yield smtp_connection # provide the fixture value The ``smtp_connection`` connection will be closed after the test finished execution because the ``smtp_connection`` object automatically closes when the ``with`` statement ends. Note that if an exception happens during the *setup* code (before the ``yield`` keyword), the *teardown* code (after the ``yield``) will not be called. An alternative option for executing *teardown* code is to make use of the ``addfinalizer`` method of the `request-context`_ object to register finalization functions. Here's the ``smtp_connection`` fixture changed to use ``addfinalizer`` for cleanup: .. code-block:: python # content of conftest.py import smtplib import pytest @pytest.fixture(scope="module") def smtp_connection(request): smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) def fin(): print("teardown smtp_connection") smtp_connection.close() request.addfinalizer(fin) return smtp_connection # provide the fixture value Both ``yield`` and ``addfinalizer`` methods work similarly by calling their code after the test ends, but ``addfinalizer`` has two key differences over ``yield``: 1. It is possible to register multiple finalizer functions. 2. Finalizers will always be called regardless if the fixture *setup* code raises an exception. This is handy to properly close all resources created by a fixture even if one of them fails to be created/acquired:: @pytest.fixture def equipments(request): r = [] for port in ('C1', 'C3', 'C28'): equip = connect(port) request.addfinalizer(equip.disconnect) r.append(equip) return r In the example above, if ``"C28"`` fails with an exception, ``"C1"`` and ``"C3"`` will still be properly closed. Of course, if an exception happens before the finalize function is registered then it will not be executed. .. _`request-context`: Fixtures can introspect the requesting test context ------------------------------------------------------------- Fixture functions can accept the :py:class:`request ` object to introspect the "requesting" test function, class or module context. Further extending the previous ``smtp_connection`` fixture example, let's read an optional server URL from the test module which uses our fixture:: # content of conftest.py import pytest import smtplib @pytest.fixture(scope="module") def smtp_connection(request): server = getattr(request.module, "smtpserver", "smtp.gmail.com") smtp_connection = smtplib.SMTP(server, 587, timeout=5) yield smtp_connection print("finalizing %s (%s)" % (smtp_connection, server)) smtp_connection.close() We use the ``request.module`` attribute to optionally obtain an ``smtpserver`` attribute from the test module. If we just execute again, nothing much has changed: .. code-block:: pytest $ pytest -s -q --tb=no FFfinalizing (smtp.gmail.com) 2 failed in 0.12 seconds Let's quickly create another test module that actually sets the server URL in its module namespace:: # content of test_anothersmtp.py smtpserver = "mail.python.org" # will be read by smtp fixture def test_showhelo(smtp_connection): assert 0, smtp_connection.helo() Running it: .. code-block:: pytest $ pytest -qq --tb=short test_anothersmtp.py F [100%] ================================= FAILURES ================================= ______________________________ test_showhelo _______________________________ test_anothersmtp.py:5: in test_showhelo assert 0, smtp_connection.helo() E AssertionError: (250, b'mail.python.org') E assert 0 ------------------------- Captured stdout teardown ------------------------- finalizing (mail.python.org) voila! The ``smtp_connection`` fixture function picked up our mail server name from the module namespace. .. _`fixture-factory`: Factories as fixtures ------------------------------------------------------------- The "factory as fixture" pattern can help in situations where the result of a fixture is needed multiple times in a single test. Instead of returning data directly, the fixture instead returns a function which generates the data. This function can then be called multiple times in the test. Factories can have have parameters as needed:: @pytest.fixture def make_customer_record(): def _make_customer_record(name): return { "name": name, "orders": [] } return _make_customer_record def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") customer_2 = make_customer_record("Mike") customer_3 = make_customer_record("Meredith") If the data created by the factory requires managing, the fixture can take care of that:: @pytest.fixture def make_customer_record(): created_records = [] def _make_customer_record(name): record = models.Customer(name=name, orders=[]) created_records.append(record) return record yield _make_customer_record for record in created_records: record.destroy() def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") customer_2 = make_customer_record("Mike") customer_3 = make_customer_record("Meredith") .. _`fixture-parametrize`: Parametrizing fixtures ----------------------------------------------------------------- Fixture functions can be parametrized in which case they will be called multiple times, each time executing the set of dependent tests, i. e. the tests that depend on this fixture. Test functions usually do not need to be aware of their re-running. Fixture parametrization helps to write exhaustive functional tests for components which themselves can be configured in multiple ways. Extending the previous example, we can flag the fixture to create two ``smtp_connection`` fixture instances which will cause all tests using the fixture to run twice. The fixture function gets access to each parameter through the special :py:class:`request ` object:: # content of conftest.py import pytest import smtplib @pytest.fixture(scope="module", params=["smtp.gmail.com", "mail.python.org"]) def smtp_connection(request): smtp_connection = smtplib.SMTP(request.param, 587, timeout=5) yield smtp_connection print("finalizing %s" % smtp_connection) smtp_connection.close() The main change is the declaration of ``params`` with :py:func:`@pytest.fixture <_pytest.python.fixture>`, a list of values for each of which the fixture function will execute and can access a value via ``request.param``. No test function code needs to change. So let's just do another run: .. code-block:: pytest $ pytest -q test_module.py FFFF [100%] ================================= FAILURES ================================= ________________________ test_ehlo[smtp.gmail.com] _________________________ smtp_connection = def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 test_module.py:6: AssertionError ________________________ test_noop[smtp.gmail.com] _________________________ smtp_connection = def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 test_module.py:11: AssertionError ________________________ test_ehlo[mail.python.org] ________________________ smtp_connection = def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 > assert b"smtp.gmail.com" in msg E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8\nCHUNKING' test_module.py:5: AssertionError -------------------------- Captured stdout setup --------------------------- finalizing ________________________ test_noop[mail.python.org] ________________________ smtp_connection = def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 test_module.py:11: AssertionError ------------------------- Captured stdout teardown ------------------------- finalizing 4 failed in 0.12 seconds We see that our two test functions each ran twice, against the different ``smtp_connection`` instances. Note also, that with the ``mail.python.org`` connection the second test fails in ``test_ehlo`` because a different server string is expected than what arrived. pytest will build a string that is the test ID for each fixture value in a parametrized fixture, e.g. ``test_ehlo[smtp.gmail.com]`` and ``test_ehlo[mail.python.org]`` in the above examples. These IDs can be used with ``-k`` to select specific cases to run, and they will also identify the specific case when one is failing. Running pytest with ``--collect-only`` will show the generated IDs. Numbers, strings, booleans and None will have their usual string representation used in the test ID. For other objects, pytest will make a string based on the argument name. It is possible to customise the string used in a test ID for a certain fixture value by using the ``ids`` keyword argument:: # content of test_ids.py import pytest @pytest.fixture(params=[0, 1], ids=["spam", "ham"]) def a(request): return request.param def test_a(a): pass def idfn(fixture_value): if fixture_value == 0: return "eggs" else: return None @pytest.fixture(params=[0, 1], ids=idfn) def b(request): return request.param def test_b(b): pass The above shows how ``ids`` can be either a list of strings to use or a function which will be called with the fixture value and then has to return a string to use. In the latter case if the function return ``None`` then pytest's auto-generated ID will be used. Running the above tests results in the following test IDs being used: .. code-block:: pytest $ pytest --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 10 items ======================= no tests ran in 0.12 seconds ======================= .. _`fixture-parametrize-marks`: Using marks with parametrized fixtures -------------------------------------- :func:`pytest.param` can be used to apply marks in values sets of parametrized fixtures in the same way that they can be used with :ref:`@pytest.mark.parametrize <@pytest.mark.parametrize>`. Example:: # content of test_fixture_marks.py import pytest @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)]) def data_set(request): return request.param def test_data(data_set): pass Running this test will *skip* the invocation of ``data_set`` with value ``2``: .. code-block:: pytest $ pytest test_fixture_marks.py -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 3 items test_fixture_marks.py::test_data[0] PASSED [ 33%] test_fixture_marks.py::test_data[1] PASSED [ 66%] test_fixture_marks.py::test_data[2] SKIPPED [100%] =================== 2 passed, 1 skipped in 0.12 seconds ==================== .. _`interdependent fixtures`: Modularity: using fixtures from a fixture function ---------------------------------------------------------- You can not only use fixtures in test functions but fixture functions can use other fixtures themselves. This contributes to a modular design of your fixtures and allows re-use of framework-specific fixtures across many projects. As a simple example, we can extend the previous example and instantiate an object ``app`` where we stick the already defined ``smtp_connection`` resource into it:: # content of test_appsetup.py import pytest class App(object): def __init__(self, smtp_connection): self.smtp_connection = smtp_connection @pytest.fixture(scope="module") def app(smtp_connection): return App(smtp_connection) def test_smtp_connection_exists(app): assert app.smtp_connection Here we declare an ``app`` fixture which receives the previously defined ``smtp_connection`` fixture and instantiates an ``App`` object with it. Let's run it: .. code-block:: pytest $ pytest -v test_appsetup.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 2 items test_appsetup.py::test_smtp_connection_exists[smtp.gmail.com] PASSED [ 50%] test_appsetup.py::test_smtp_connection_exists[mail.python.org] PASSED [100%] ========================= 2 passed in 0.12 seconds ========================= Due to the parametrization of ``smtp_connection``, the test will run twice with two different ``App`` instances and respective smtp servers. There is no need for the ``app`` fixture to be aware of the ``smtp_connection`` parametrization because pytest will fully analyse the fixture dependency graph. Note that the ``app`` fixture has a scope of ``module`` and uses a module-scoped ``smtp_connection`` fixture. The example would still work if ``smtp_connection`` was cached on a ``session`` scope: it is fine for fixtures to use "broader" scoped fixtures but not the other way round: A session-scoped fixture could not use a module-scoped one in a meaningful way. .. _`automatic per-resource grouping`: Automatic grouping of tests by fixture instances ---------------------------------------------------------- .. regendoc: wipe pytest minimizes the number of active fixtures during test runs. If you have a parametrized fixture, then all the tests using it will first execute with one instance and then finalizers are called before the next fixture instance is created. Among other things, this eases testing of applications which create and use global state. The following example uses two parametrized fixtures, one of which is scoped on a per-module basis, and all the functions perform ``print`` calls to show the setup/teardown flow:: # content of test_module.py import pytest @pytest.fixture(scope="module", params=["mod1", "mod2"]) def modarg(request): param = request.param print(" SETUP modarg %s" % param) yield param print(" TEARDOWN modarg %s" % param) @pytest.fixture(scope="function", params=[1,2]) def otherarg(request): param = request.param print(" SETUP otherarg %s" % param) yield param print(" TEARDOWN otherarg %s" % param) def test_0(otherarg): print(" RUN test0 with otherarg %s" % otherarg) def test_1(modarg): print(" RUN test1 with modarg %s" % modarg) def test_2(otherarg, modarg): print(" RUN test2 with otherarg %s and modarg %s" % (otherarg, modarg)) Let's run the tests in verbose mode and with looking at the print-output: .. code-block:: pytest $ pytest -v -s test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 8 items test_module.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 PASSED TEARDOWN otherarg 1 test_module.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 PASSED TEARDOWN otherarg 2 test_module.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 PASSED test_module.py::test_2[mod1-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 PASSED TEARDOWN otherarg 1 test_module.py::test_2[mod1-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 PASSED TEARDOWN otherarg 2 test_module.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 PASSED test_module.py::test_2[mod2-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 PASSED TEARDOWN otherarg 1 test_module.py::test_2[mod2-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2 ========================= 8 passed in 0.12 seconds ========================= You can see that the parametrized module-scoped ``modarg`` resource caused an ordering of test execution that lead to the fewest possible "active" resources. The finalizer for the ``mod1`` parametrized resource was executed before the ``mod2`` resource was setup. In particular notice that test_0 is completely independent and finishes first. Then test_1 is executed with ``mod1``, then test_2 with ``mod1``, then test_1 with ``mod2`` and finally test_2 with ``mod2``. The ``otherarg`` parametrized resource (having function scope) was set up before and teared down after every test that used it. .. _`usefixtures`: Using fixtures from classes, modules or projects ---------------------------------------------------------------------- .. regendoc:wipe Sometimes test functions do not directly need access to a fixture object. For example, tests may require to operate with an empty directory as the current working directory but otherwise do not care for the concrete directory. Here is how you can use the standard `tempfile `_ and pytest fixtures to achieve it. We separate the creation of the fixture into a conftest.py file:: # content of conftest.py import pytest import tempfile import os @pytest.fixture() def cleandir(): newpath = tempfile.mkdtemp() os.chdir(newpath) and declare its use in a test module via a ``usefixtures`` marker:: # content of test_setenv.py import os import pytest @pytest.mark.usefixtures("cleandir") class TestDirectoryInit(object): def test_cwd_starts_empty(self): assert os.listdir(os.getcwd()) == [] with open("myfile", "w") as f: f.write("hello") def test_cwd_again_starts_empty(self): assert os.listdir(os.getcwd()) == [] Due to the ``usefixtures`` marker, the ``cleandir`` fixture will be required for the execution of each test method, just as if you specified a "cleandir" function argument to each of them. Let's run it to verify our fixture is activated and the tests pass: .. code-block:: pytest $ pytest -q .. [100%] 2 passed in 0.12 seconds You can specify multiple fixtures like this: .. code-block:: python @pytest.mark.usefixtures("cleandir", "anotherfixture") def test(): ... and you may specify fixture usage at the test module level, using a generic feature of the mark mechanism: .. code-block:: python pytestmark = pytest.mark.usefixtures("cleandir") Note that the assigned variable *must* be called ``pytestmark``, assigning e.g. ``foomark`` will not activate the fixtures. It is also possible to put fixtures required by all tests in your project into an ini-file: .. code-block:: ini # content of pytest.ini [pytest] usefixtures = cleandir .. warning:: Note this mark has no effect in **fixture functions**. For example, this **will not work as expected**: .. code-block:: python @pytest.mark.usefixtures("my_other_fixture") @pytest.fixture def my_fixture_that_sadly_wont_use_my_other_fixture(): ... Currently this will not generate any error or warning, but this is intended to be handled by `#3664 `_. .. _`autouse`: .. _`autouse fixtures`: Autouse fixtures (xUnit setup on steroids) ---------------------------------------------------------------------- .. regendoc:wipe Occasionally, you may want to have fixtures get invoked automatically without declaring a function argument explicitly or a `usefixtures`_ decorator. As a practical example, suppose we have a database fixture which has a begin/rollback/commit architecture and we want to automatically surround each test method by a transaction and a rollback. Here is a dummy self-contained implementation of this idea:: # content of test_db_transact.py import pytest class DB(object): def __init__(self): self.intransaction = [] def begin(self, name): self.intransaction.append(name) def rollback(self): self.intransaction.pop() @pytest.fixture(scope="module") def db(): return DB() class TestClass(object): @pytest.fixture(autouse=True) def transact(self, request, db): db.begin(request.function.__name__) yield db.rollback() def test_method1(self, db): assert db.intransaction == ["test_method1"] def test_method2(self, db): assert db.intransaction == ["test_method2"] The class-level ``transact`` fixture is marked with *autouse=true* which implies that all test methods in the class will use this fixture without a need to state it in the test function signature or with a class-level ``usefixtures`` decorator. If we run it, we get two passing tests: .. code-block:: pytest $ pytest -q .. [100%] 2 passed in 0.12 seconds Here is how autouse fixtures work in other scopes: - autouse fixtures obey the ``scope=`` keyword-argument: if an autouse fixture has ``scope='session'`` it will only be run once, no matter where it is defined. ``scope='class'`` means it will be run once per class, etc. - if an autouse fixture is defined in a test module, all its test functions automatically use it. - if an autouse fixture is defined in a conftest.py file then all tests in all test modules below its directory will invoke the fixture. - lastly, and **please use that with care**: if you define an autouse fixture in a plugin, it will be invoked for all tests in all projects where the plugin is installed. This can be useful if a fixture only anyway works in the presence of certain settings e. g. in the ini-file. Such a global fixture should always quickly determine if it should do any work and avoid otherwise expensive imports or computation. Note that the above ``transact`` fixture may very well be a fixture that you want to make available in your project without having it generally active. The canonical way to do that is to put the transact definition into a conftest.py file **without** using ``autouse``:: # content of conftest.py @pytest.fixture def transact(request, db): db.begin() yield db.rollback() and then e.g. have a TestClass using it by declaring the need:: @pytest.mark.usefixtures("transact") class TestClass(object): def test_method1(self): ... All test methods in this TestClass will use the transaction fixture while other test classes or functions in the module will not use it unless they also add a ``transact`` reference. Overriding fixtures on various levels ------------------------------------- In relatively large test suite, you most likely need to ``override`` a ``global`` or ``root`` fixture with a ``locally`` defined one, keeping the test code readable and maintainable. Override a fixture on a folder (conftest) level ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Given the tests file structure is: :: tests/ __init__.py conftest.py # content of tests/conftest.py import pytest @pytest.fixture def username(): return 'username' test_something.py # content of tests/test_something.py def test_username(username): assert username == 'username' subfolder/ __init__.py conftest.py # content of tests/subfolder/conftest.py import pytest @pytest.fixture def username(username): return 'overridden-' + username test_something.py # content of tests/subfolder/test_something.py def test_username(username): assert username == 'overridden-username' As you can see, a fixture with the same name can be overridden for certain test folder level. Note that the ``base`` or ``super`` fixture can be accessed from the ``overriding`` fixture easily - used in the example above. Override a fixture on a test module level ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Given the tests file structure is: :: tests/ __init__.py conftest.py # content of tests/conftest.py import pytest @pytest.fixture def username(): return 'username' test_something.py # content of tests/test_something.py import pytest @pytest.fixture def username(username): return 'overridden-' + username def test_username(username): assert username == 'overridden-username' test_something_else.py # content of tests/test_something_else.py import pytest @pytest.fixture def username(username): return 'overridden-else-' + username def test_username(username): assert username == 'overridden-else-username' In the example above, a fixture with the same name can be overridden for certain test module. Override a fixture with direct test parametrization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Given the tests file structure is: :: tests/ __init__.py conftest.py # content of tests/conftest.py import pytest @pytest.fixture def username(): return 'username' @pytest.fixture def other_username(username): return 'other-' + username test_something.py # content of tests/test_something.py import pytest @pytest.mark.parametrize('username', ['directly-overridden-username']) def test_username(username): assert username == 'directly-overridden-username' @pytest.mark.parametrize('username', ['directly-overridden-username-other']) def test_username_other(other_username): assert other_username == 'other-directly-overridden-username-other' In the example above, a fixture value is overridden by the test parameter value. Note that the value of the fixture can be overridden this way even if the test doesn't use it directly (doesn't mention it in the function prototype). Override a parametrized fixture with non-parametrized one and vice versa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Given the tests file structure is: :: tests/ __init__.py conftest.py # content of tests/conftest.py import pytest @pytest.fixture(params=['one', 'two', 'three']) def parametrized_username(request): return request.param @pytest.fixture def non_parametrized_username(request): return 'username' test_something.py # content of tests/test_something.py import pytest @pytest.fixture def parametrized_username(): return 'overridden-username' @pytest.fixture(params=['one', 'two', 'three']) def non_parametrized_username(request): return request.param def test_username(parametrized_username): assert parametrized_username == 'overridden-username' def test_parametrized_username(non_parametrized_username): assert non_parametrized_username in ['one', 'two', 'three'] test_something_else.py # content of tests/test_something_else.py def test_username(parametrized_username): assert parametrized_username in ['one', 'two', 'three'] def test_username(non_parametrized_username): assert non_parametrized_username == 'username' In the example above, a parametrized fixture is overridden with a non-parametrized version, and a non-parametrized fixture is overridden with a parametrized version for certain test module. The same applies for the test folder level obviously. pytest-4.6.9/doc/en/plugins.rst0000664000372000037200000001022613604173400017305 0ustar travistravis00000000000000.. _`external plugins`: .. _`extplugins`: .. _`using plugins`: Installing and Using plugins ============================ This section talks about installing and using third party plugins. For writing your own plugins, please refer to :ref:`writing-plugins`. Installing a third party plugin can be easily done with ``pip``:: pip install pytest-NAME pip uninstall pytest-NAME If a plugin is installed, ``pytest`` automatically finds and integrates it, there is no need to activate it. Here is a little annotated list for some popular plugins: .. _`django`: https://www.djangoproject.com/ * `pytest-django `_: write tests for `django`_ apps, using pytest integration. * `pytest-twisted `_: write tests for `twisted `_ apps, starting a reactor and processing deferreds from test functions. * `pytest-cov `__: coverage reporting, compatible with distributed testing * `pytest-xdist `_: to distribute tests to CPUs and remote hosts, to run in boxed mode which allows to survive segmentation faults, to run in looponfailing mode, automatically re-running failing tests on file changes. * `pytest-instafail `_: to report failures while the test run is happening. * `pytest-bdd `_ and `pytest-konira `_ to write tests using behaviour-driven testing. * `pytest-timeout `_: to timeout tests based on function marks or global definitions. * `pytest-pep8 `_: a ``--pep8`` option to enable PEP8 compliance checking. * `pytest-flakes `_: check source code with pyflakes. * `oejskit `_: a plugin to run javascript unittests in live browsers. To see a complete list of all plugins with their latest testing status against different pytest and Python versions, please visit `plugincompat `_. You may also discover more plugins through a `pytest- pypi.org search`_. .. _`pytest- pypi.org search`: https://pypi.org/search/?q=pytest- .. _`available installable plugins`: Requiring/Loading plugins in a test module or conftest file ----------------------------------------------------------- You can require plugins in a test module or a conftest file like this: .. code-block:: python pytest_plugins = ("myapp.testsupport.myplugin",) When the test module or conftest plugin is loaded the specified plugins will be loaded as well. .. note:: Requiring plugins using a ``pytest_plugins`` variable in non-root ``conftest.py`` files is deprecated. See :ref:`full explanation ` in the Writing plugins section. .. note:: The name ``pytest_plugins`` is reserved and should not be used as a name for a custom plugin module. .. _`findpluginname`: Finding out which plugins are active ------------------------------------ If you want to find out which plugins are active in your environment you can type:: pytest --trace-config and will get an extended test header which shows activated plugins and their names. It will also print local plugins aka :ref:`conftest.py ` files when they are loaded. .. _`cmdunregister`: Deactivating / unregistering a plugin by name --------------------------------------------- You can prevent plugins from loading or unregister them:: pytest -p no:NAME This means that any subsequent try to activate/load the named plugin will not work. If you want to unconditionally disable a plugin for a project, you can add this option to your ``pytest.ini`` file: .. code-block:: ini [pytest] addopts = -p no:NAME Alternatively to disable it only in certain environments (for example in a CI server), you can set ``PYTEST_ADDOPTS`` environment variable to ``-p no:name``. See :ref:`findpluginname` for how to obtain the name of a plugin. .. _`builtin plugins`: pytest-4.6.9/doc/en/nose.rst0000664000372000037200000000564213604173400016576 0ustar travistravis00000000000000.. _`noseintegration`: Running tests written for nose ======================================= .. include:: links.inc ``pytest`` has basic support for running tests written for nose_. .. _nosestyle: Usage ------------- After :ref:`installation` type: .. code-block:: bash python setup.py develop # make sure tests can import our package pytest # instead of 'nosetests' and you should be able to run your nose style tests and make use of pytest's capabilities. Supported nose Idioms ---------------------- * setup and teardown at module/class/method level * SkipTest exceptions and markers * setup/teardown decorators * ``yield``-based tests and their setup (considered deprecated as of pytest 3.0) * ``__test__`` attribute on modules/classes/functions * general usage of nose utilities Unsupported idioms / known issues ---------------------------------- - unittest-style ``setUp, tearDown, setUpClass, tearDownClass`` are recognized only on ``unittest.TestCase`` classes but not on plain classes. ``nose`` supports these methods also on plain classes but pytest deliberately does not. As nose and pytest already both support ``setup_class, teardown_class, setup_method, teardown_method`` it doesn't seem useful to duplicate the unittest-API like nose does. If you however rather think pytest should support the unittest-spelling on plain classes please post `to this issue `_. - nose imports test modules with the same import path (e.g. ``tests.test_mod``) but different file system paths (e.g. ``tests/test_mode.py`` and ``other/tests/test_mode.py``) by extending sys.path/import semantics. pytest does not do that but there is discussion in `#268 `_ for adding some support. Note that `nose2 choose to avoid this sys.path/import hackery `_. If you place a conftest.py file in the root directory of your project (as determined by pytest) pytest will run tests "nose style" against the code below that directory by adding it to your ``sys.path`` instead of running against your installed code. You may find yourself wanting to do this if you ran ``python setup.py install`` to set up your project, as opposed to ``python setup.py develop`` or any of the package manager equivalents. Installing with develop in a virtual environment like tox is recommended over this pattern. - nose-style doctests are not collected and executed correctly, also doctest fixtures don't work. - no nose-configuration is recognized. - ``yield``-based methods don't support ``setup`` properly because the ``setup`` method is always called in the same class instance. There are no plans to fix this currently because ``yield``-tests are deprecated in pytest 3.0, with ``pytest.mark.parametrize`` being the recommended alternative. pytest-4.6.9/doc/en/warnings.rst0000664000372000037200000003172713604173400017465 0ustar travistravis00000000000000.. _`warnings`: Warnings Capture ================ Starting from version ``3.1``, pytest now automatically catches warnings during test execution and displays them at the end of the session: .. code-block:: python # content of test_show_warnings.py import warnings def api_v1(): warnings.warn(UserWarning("api v1, should use functions from v2")) return 1 def test_one(): assert api_v1() == 1 Running pytest now produces this output: .. code-block:: pytest $ pytest test_show_warnings.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_show_warnings.py . [100%] ============================= warnings summary ============================= test_show_warnings.py::test_one $REGENDOC_TMPDIR/test_show_warnings.py:5: UserWarning: api v1, should use functions from v2 warnings.warn(UserWarning("api v1, should use functions from v2")) -- Docs: https://docs.pytest.org/en/latest/warnings.html =================== 1 passed, 1 warnings in 0.12 seconds =================== The ``-W`` flag can be passed to control which warnings will be displayed or even turn them into errors: .. code-block:: pytest $ pytest -q test_show_warnings.py -W error::UserWarning F [100%] ================================= FAILURES ================================= _________________________________ test_one _________________________________ def test_one(): > assert api_v1() == 1 test_show_warnings.py:10: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def api_v1(): > warnings.warn(UserWarning("api v1, should use functions from v2")) E UserWarning: api v1, should use functions from v2 test_show_warnings.py:5: UserWarning 1 failed in 0.12 seconds The same option can be set in the ``pytest.ini`` file using the ``filterwarnings`` ini option. For example, the configuration below will ignore all user warnings, but will transform all other warnings into errors. .. code-block:: ini [pytest] filterwarnings = error ignore::UserWarning When a warning matches more than one option in the list, the action for the last matching option is performed. Both ``-W`` command-line option and ``filterwarnings`` ini option are based on Python's own `-W option`_ and `warnings.simplefilter`_, so please refer to those sections in the Python documentation for other examples and advanced usage. .. _`filterwarnings`: ``@pytest.mark.filterwarnings`` ------------------------------- You can use the ``@pytest.mark.filterwarnings`` to add warning filters to specific test items, allowing you to have finer control of which warnings should be captured at test, class or even module level: .. code-block:: python import warnings def api_v1(): warnings.warn(UserWarning("api v1, should use functions from v2")) return 1 @pytest.mark.filterwarnings("ignore:api v1") def test_one(): assert api_v1() == 1 Filters applied using a mark take precedence over filters passed on the command line or configured by the ``filterwarnings`` ini option. You may apply a filter to all tests of a class by using the ``filterwarnings`` mark as a class decorator or to all tests in a module by setting the ``pytestmark`` variable: .. code-block:: python # turns all warnings into errors for this module pytestmark = pytest.mark.filterwarnings("error") *Credits go to Florian Schulze for the reference implementation in the* `pytest-warnings`_ *plugin.* .. _`-W option`: https://docs.python.org/3/using/cmdline.html?highlight=#cmdoption-W .. _warnings.simplefilter: https://docs.python.org/3/library/warnings.html#warnings.simplefilter .. _`pytest-warnings`: https://github.com/fschulze/pytest-warnings Disabling warnings summary -------------------------- Although not recommended, you can use the ``--disable-warnings`` command-line option to suppress the warning summary entirely from the test run output. Disabling warning capture entirely ---------------------------------- This plugin is enabled by default but can be disabled entirely in your ``pytest.ini`` file with: .. code-block:: ini [pytest] addopts = -p no:warnings Or passing ``-p no:warnings`` in the command-line. This might be useful if your test suites handles warnings using an external system. .. _`deprecation-warnings`: DeprecationWarning and PendingDeprecationWarning ------------------------------------------------ By default pytest will display ``DeprecationWarning`` and ``PendingDeprecationWarning`` warnings from user code and third-party libraries, as recommended by `PEP-0565 `_. This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed. Sometimes it is useful to hide some specific deprecation warnings that happen in code that you have no control over (such as third-party libraries), in which case you might use the warning filters options (ini or marks) to ignore those warnings. For example: .. code-block:: ini [pytest] filterwarnings = ignore:.*U.*mode is deprecated:DeprecationWarning This will ignore all warnings of type ``DeprecationWarning`` where the start of the message matches the regular expression ``".*U.*mode is deprecated"``. .. note:: If warnings are configured at the interpreter level, using the `PYTHONWARNINGS `_ environment variable or the ``-W`` command-line option, pytest will not configure any filters by default. Also pytest doesn't follow ``PEP-0506`` suggestion of resetting all warning filters because it might break test suites that configure warning filters themselves by calling ``warnings.simplefilter`` (see issue `#2430 `_ for an example of that). .. _`ensuring a function triggers a deprecation warning`: .. _ensuring_function_triggers: Ensuring code triggers a deprecation warning -------------------------------------------- You can also call a global helper for checking that a certain function call triggers a ``DeprecationWarning`` or ``PendingDeprecationWarning``: .. code-block:: python import pytest def test_global(): pytest.deprecated_call(myfunction, 17) By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be caught when using ``pytest.warns`` or ``recwarn`` because default Python warnings filters hide them. If you wish to record them in your own code, use the command ``warnings.simplefilter('always')``: .. code-block:: python import warnings import pytest def test_deprecation(recwarn): warnings.simplefilter("always") warnings.warn("deprecated", DeprecationWarning) assert len(recwarn) == 1 assert recwarn.pop(DeprecationWarning) You can also use it as a contextmanager: .. code-block:: python def test_global(): with pytest.deprecated_call(): myobject.deprecated_method() .. _`asserting warnings`: .. _assertwarnings: .. _`asserting warnings with the warns function`: .. _warns: Asserting warnings with the warns function ------------------------------------------ You can check that code raises a particular warning using ``pytest.warns``, which works in a similar manner to :ref:`raises `: .. code-block:: python import warnings import pytest def test_warning(): with pytest.warns(UserWarning): warnings.warn("my warning", UserWarning) The test will fail if the warning in question is not raised. The keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with warns(UserWarning, match='must be 0 or None'): ... warnings.warn("value must be 0 or None", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("value must be 42", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("this is not here", UserWarning) Traceback (most recent call last): ... Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted... You can also call ``pytest.warns`` on a function or code string:: pytest.warns(expected_warning, func, *args, **kwargs) pytest.warns(expected_warning, "func(*args, **kwargs)") The function also returns a list of all raised warnings (as ``warnings.WarningMessage`` objects), which you can query for additional information: .. code-block:: python with pytest.warns(RuntimeWarning) as record: warnings.warn("another warning", RuntimeWarning) # check that only one warning was raised assert len(record) == 1 # check that the message matches assert record[0].message.args[0] == "another warning" Alternatively, you can examine raised warnings in detail using the :ref:`recwarn ` fixture (see below). .. note:: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated differently; see :ref:`ensuring_function_triggers`. .. _`recording warnings`: .. _recwarn: Recording warnings ------------------ You can record raised warnings either using ``pytest.warns`` or with the ``recwarn`` fixture. To record with ``pytest.warns`` without asserting anything about the warnings, pass ``None`` as the expected warning type: .. code-block:: python with pytest.warns(None) as record: warnings.warn("user", UserWarning) warnings.warn("runtime", RuntimeWarning) assert len(record) == 2 assert str(record[0].message) == "user" assert str(record[1].message) == "runtime" The ``recwarn`` fixture will record warnings for the whole function: .. code-block:: python import warnings def test_hello(recwarn): warnings.warn("hello", UserWarning) assert len(recwarn) == 1 w = recwarn.pop(UserWarning) assert issubclass(w.category, UserWarning) assert str(w.message) == "hello" assert w.filename assert w.lineno Both ``recwarn`` and ``pytest.warns`` return the same interface for recorded warnings: a WarningsRecorder instance. To view the recorded warnings, you can iterate over this instance, call ``len`` on it to get the number of recorded warnings, or index into it to get a particular recorded warning. .. currentmodule:: _pytest.warnings Full API: :class:`WarningsRecorder`. .. _custom_failure_messages: Custom failure messages ----------------------- Recording warnings provides an opportunity to produce custom test failure messages for when no warnings are issued or other conditions are met. .. code-block:: python def test(): with pytest.warns(Warning) as record: f() if not record: pytest.fail("Expected a warning!") If no warnings are issued when calling ``f``, then ``not record`` will evaluate to ``True``. You can then call ``pytest.fail`` with a custom error message. .. _internal-warnings: Internal pytest warnings ------------------------ pytest may generate its own warnings in some situations, such as improper usage or deprecated features. For example, pytest will emit a warning if it encounters a class that matches :confval:`python_classes` but also defines an ``__init__`` constructor, as this prevents the class from being instantiated: .. code-block:: python # content of test_pytest_warnings.py class Test: def __init__(self): pass def test_foo(self): assert 1 == 1 .. code-block:: pytest $ pytest test_pytest_warnings.py -q ============================= warnings summary ============================= test_pytest_warnings.py:1 $REGENDOC_TMPDIR/test_pytest_warnings.py:1: PytestCollectionWarning: cannot collect test class 'Test' because it has a __init__ constructor (from: test_pytest_warnings.py) class Test: -- Docs: https://docs.pytest.org/en/latest/warnings.html 1 warnings in 0.12 seconds These warnings might be filtered using the same builtin mechanisms used to filter other types of warnings. Please read our :ref:`backwards-compatibility` to learn how we proceed about deprecating and eventually removing features. The following warning types ares used by pytest and are part of the public API: .. autoclass:: pytest.PytestWarning .. autoclass:: pytest.PytestAssertRewriteWarning .. autoclass:: pytest.PytestCacheWarning .. autoclass:: pytest.PytestCollectionWarning .. autoclass:: pytest.PytestConfigWarning .. autoclass:: pytest.PytestDeprecationWarning .. autoclass:: pytest.PytestExperimentalApiWarning .. autoclass:: pytest.PytestUnhandledCoroutineWarning .. autoclass:: pytest.PytestUnknownMarkWarning .. autoclass:: pytest.RemovedInPytest4Warning pytest-4.6.9/doc/en/mark.rst0000664000372000037200000000507613604173400016565 0ustar travistravis00000000000000.. _mark: Marking test functions with attributes ====================================== By using the ``pytest.mark`` helper you can easily set metadata on your test functions. There are some builtin markers, for example: * :ref:`skip ` - always skip a test function * :ref:`skipif ` - skip a test function if a certain condition is met * :ref:`xfail ` - produce an "expected failure" outcome if a certain condition is met * :ref:`parametrize ` to perform multiple calls to the same test function. It's easy to create custom markers or to apply markers to whole test classes or modules. Those markers can be used by plugins, and also are commonly used to :ref:`select tests ` on the command-line with the ``-m`` option. See :ref:`mark examples` for examples which also serve as documentation. .. note:: Marks can only be applied to tests, having no effect on :ref:`fixtures `. Registering marks ----------------- You can register custom marks in your ``pytest.ini`` file like this: .. code-block:: ini [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') serial Note that everything after the ``:`` is an optional description. Alternatively, you can register new markers programatically in a :ref:`pytest_configure ` hook: .. code-block:: python def pytest_configure(config): config.addinivalue_line( "markers", "env(name): mark test to run only on named environment" ) Registered marks appear in pytest's help text and do not emit warnings (see the next section). It is recommended that third-party plugins always :ref:`register their markers `. .. _unknown-marks: Raising errors on unknown marks ------------------------------- Unregistered marks applied with the ``@pytest.mark.name_of_the_mark`` decorator will always emit a warning in order to avoid silently doing something surprising due to mis-typed names. As described in the previous section, you can disable the warning for custom marks by registering them in your ``pytest.ini`` file or using a custom ``pytest_configure`` hook. When the ``--strict-markers`` command-line flag is passed, any unknown marks applied with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error. You can enforce this validation in your project by adding ``--strict-markers`` to ``addopts``: .. code-block:: ini [pytest] addopts = --strict-markers markers = slow: marks tests as slow (deselect with '-m "not slow"') serial pytest-4.6.9/doc/en/_templates/0000775000372000037200000000000013604173421017231 5ustar travistravis00000000000000pytest-4.6.9/doc/en/_templates/sidebarintro.html0000664000372000037200000000017613604173400022605 0ustar travistravis00000000000000

About pytest

pytest is a mature full-featured Python testing tool that helps you write better programs.

pytest-4.6.9/doc/en/_templates/globaltoc.html0000664000372000037200000000151613604173400022065 0ustar travistravis00000000000000

{{ _('Table Of Contents') }}

{%- if display_toc %}
{{ toc }} {%- endif %} pytest-4.6.9/doc/en/_templates/layout.html0000664000372000037200000000113013604173400021424 0ustar travistravis00000000000000{% extends "!layout.html" %} {% block header %} {{super()}} {% endblock %} {% block footer %} {{ super() }} {% endblock %} pytest-4.6.9/doc/en/_templates/links.html0000664000372000037200000000067213604173400021241 0ustar travistravis00000000000000

Useful Links

pytest-4.6.9/doc/en/projects.rst0000664000372000037200000001142113604173400017453 0ustar travistravis00000000000000.. _projects: .. image:: img/gaynor3.png :width: 400px :align: right .. image:: img/theuni.png :width: 400px :align: right .. image:: img/cramer2.png :width: 400px :align: right .. image:: img/keleshev.png :width: 400px :align: right Project examples ========================== Here are some examples of projects using ``pytest`` (please send notes via :ref:`contact`): * `PyPy `_, Python with a JIT compiler, running over `21000 tests `_ * the `MoinMoin `_ Wiki Engine * `sentry `_, realtime app-maintenance and exception tracking * `Astropy `_ and `affiliated packages `_ * `tox `_, virtualenv/Hudson integration tool * `PIDA `_ framework for integrated development * `PyPM `_ ActiveState's package manager * `Fom `_ a fluid object mapper for FluidDB * `applib `_ cross-platform utilities * `six `_ Python 2 and 3 compatibility utilities * `pediapress `_ MediaWiki articles * `mwlib `_ mediawiki parser and utility library * `The Translate Toolkit `_ for localization and conversion * `execnet `_ rapid multi-Python deployment * `pylib `_ cross-platform path, IO, dynamic code library * `Pacha `_ configuration management in five minutes * `bbfreeze `_ create standalone executables from Python scripts * `pdb++ `_ a fancier version of PDB * `py-s3fuse `_ Amazon S3 FUSE based filesystem * `waskr `_ WSGI Stats Middleware * `guachi `_ global persistent configs for Python modules * `Circuits `_ lightweight Event Driven Framework * `pygtk-helpers `_ easy interaction with PyGTK * `QuantumCore `_ statusmessage and repoze openid plugin * `pydataportability `_ libraries for managing the open web * `XIST `_ extensible HTML/XML generator * `tiddlyweb `_ optionally headless, extensible RESTful datastore * `fancycompleter `_ for colorful tab-completion * `Paludis `_ tools for Gentoo Paludis package manager * `Gerald `_ schema comparison tool * `abjad `_ Python API for Formalized Score control * `bu `_ a microscopic build system * `katcp `_ Telescope communication protocol over Twisted * `kss plugin timer `_ * `pyudev `_ a pure Python binding to the Linux library libudev * `pytest-localserver `_ a plugin for pytest that provides an httpserver and smtpserver * `pytest-monkeyplus `_ a plugin that extends monkeypatch These projects help integrate ``pytest`` into other Python frameworks: * `pytest-django `_ for Django * `zope.pytest `_ for Zope and Grok * `pytest_gae `_ for Google App Engine * There is `some work `_ underway for Kotti, a CMS built in Pyramid/Pylons Some organisations using pytest ----------------------------------- * `Square Kilometre Array, Cape Town `_ * `Some Mozilla QA people `_ use pytest to distribute their Selenium tests * `Tandberg `_ * `Shootq `_ * `Stups department of Heinrich Heine University Duesseldorf `_ * `cellzome `_ * `Open End, Gothenborg `_ * `Laboratory of Bioinformatics, Warsaw `_ * `merlinux, Germany `_ * `ESSS, Brazil `_ * many more ... (please be so kind to send a note via :ref:`contact`) pytest-4.6.9/doc/en/requirements.txt0000664000372000037200000000012613604173400020354 0ustar travistravis00000000000000pygments-pytest>=1.1.0 sphinx>=1.8.2,<2.1 sphinxcontrib-trio sphinx-removed-in>=0.2.0 pytest-4.6.9/doc/en/existingtestsuite.rst0000664000372000037200000000237513604173400021436 0ustar travistravis00000000000000.. _existingtestsuite: Using pytest with an existing test suite =========================================== Pytest can be used with most existing test suites, but its behavior differs from other test runners such as :ref:`nose ` or Python's default unittest framework. Before using this section you will want to :ref:`install pytest `. Running an existing test suite with pytest --------------------------------------------- Say you want to contribute to an existing repository somewhere. After pulling the code into your development space using some flavor of version control and (optionally) setting up a virtualenv you will want to run:: cd pip install -e . # Environment dependent alternatives include # 'python setup.py develop' and 'conda develop' in your project root. This will set up a symlink to your code in site-packages, allowing you to edit your code while your tests run against it as if it were installed. Setting up your project in development mode lets you avoid having to reinstall every time you want to run your tests, and is less brittle than mucking about with sys.path to point your tests at local code. Also consider using :ref:`tox `. .. include:: links.inc pytest-4.6.9/doc/en/capture.rst0000664000372000037200000001243713604173400017275 0ustar travistravis00000000000000 .. _`captures`: Capturing of the stdout/stderr output ========================================================= Default stdout/stderr/stdin capturing behaviour --------------------------------------------------------- During test execution any output sent to ``stdout`` and ``stderr`` is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback. (this behavior can be configured by the ``--show-capture`` command-line option). In addition, ``stdin`` is set to a "null" object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests. By default capturing is done by intercepting writes to low level file descriptors. This allows to capture output from simple print statements as well as output from a subprocess started by a test. Setting capturing methods or disabling capturing ------------------------------------------------- There are two ways in which ``pytest`` can perform capturing: * file descriptor (FD) level capturing (default): All writes going to the operating system file descriptors 1 and 2 will be captured. * ``sys`` level capturing: Only writes to Python files ``sys.stdout`` and ``sys.stderr`` will be captured. No capturing of writes to filedescriptors is performed. .. _`disable capturing`: You can influence output capturing mechanisms from the command line: .. code-block:: bash pytest -s # disable all capturing pytest --capture=sys # replace sys.stdout/stderr with in-mem files pytest --capture=fd # also point filedescriptors 1 and 2 to temp file .. _printdebugging: Using print statements for debugging --------------------------------------------------- One primary benefit of the default capturing of stdout/stderr output is that you can use print statements for debugging:: # content of test_module.py def setup_function(function): print("setting up %s" % function) def test_func1(): assert True def test_func2(): assert False and running this module will show you precisely the output of the failing function and hide the other one: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py .F [100%] ================================= FAILURES ================================= ________________________________ test_func2 ________________________________ def test_func2(): > assert False E assert False test_module.py:9: AssertionError -------------------------- Captured stdout setup --------------------------- setting up ==================== 1 failed, 1 passed in 0.12 seconds ==================== Accessing captured output from a test function --------------------------------------------------- The ``capsys``, ``capsysbinary``, ``capfd``, and ``capfdbinary`` fixtures allow access to stdout/stderr output created during test execution. Here is an example test function that performs some output related checks: .. code-block:: python def test_myoutput(capsys): # or use "capfd" for fd-level print("hello") sys.stderr.write("world\n") captured = capsys.readouterr() assert captured.out == "hello\n" assert captured.err == "world\n" print("next") captured = capsys.readouterr() assert captured.out == "next\n" The ``readouterr()`` call snapshots the output so far - and capturing will be continued. After the test function finishes the original streams will be restored. Using ``capsys`` this way frees your test from having to care about setting/resetting output streams and also interacts well with pytest's own per-test capturing. If you want to capture on filedescriptor level you can use the ``capfd`` fixture which offers the exact same interface but allows to also capture output from libraries or subprocesses that directly write to operating system level output streams (FD1 and FD2). The return value from ``readouterr`` changed to a ``namedtuple`` with two attributes, ``out`` and ``err``. If the code under test writes non-textual data, you can capture this using the ``capsysbinary`` fixture which instead returns ``bytes`` from the ``readouterr`` method. The ``capfsysbinary`` fixture is currently only available in python 3. If the code under test writes non-textual data, you can capture this using the ``capfdbinary`` fixture which instead returns ``bytes`` from the ``readouterr`` method. The ``capfdbinary`` fixture operates on the filedescriptor level. To temporarily disable capture within a test, both ``capsys`` and ``capfd`` have a ``disabled()`` method that can be used as a context manager, disabling capture inside the ``with`` block: .. code-block:: python def test_disabling_capturing(capsys): print("this output is captured") with capsys.disabled(): print("output not captured, going directly to sys.stdout") print("this output is also captured") .. include:: links.inc pytest-4.6.9/doc/en/goodpractices.rst0000664000372000037200000002473613604173400020465 0ustar travistravis00000000000000.. highlightlang:: python .. _`goodpractices`: Good Integration Practices ================================================= Install package with pip ------------------------------------------------- For development, we recommend you use venv_ for virtual environments (or virtualenv_ for Python 2.7) and pip_ for installing your application and any dependencies, as well as the ``pytest`` package itself. This ensures your code and dependencies are isolated from your system Python installation. Next, place a ``setup.py`` file in the root of your package with the following minimum content:: from setuptools import setup, find_packages setup(name="PACKAGENAME", packages=find_packages()) Where ``PACKAGENAME`` is the name of your package. You can then install your package in "editable" mode by running from the same directory:: pip install -e . which lets you change your source code (both tests and application) and rerun tests at will. This is similar to running ``python setup.py develop`` or ``conda develop`` in that it installs your package using a symlink to your development code. .. _`test discovery`: .. _`Python test discovery`: Conventions for Python test discovery ------------------------------------------------- ``pytest`` implements the following standard test discovery: * If no arguments are specified then collection starts from :confval:`testpaths` (if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids. * Recurse into directories, unless they match :confval:`norecursedirs`. * In those directories, search for ``test_*.py`` or ``*_test.py`` files, imported by their `test package name`_. * From those files, collect test items: * ``test`` prefixed test functions or methods outside of class * ``test`` prefixed test functions or methods inside ``Test`` prefixed test classes (without an ``__init__`` method) For examples of how to customize your test discovery :doc:`example/pythoncollection`. Within Python modules, ``pytest`` also discovers tests using the standard :ref:`unittest.TestCase ` subclassing technique. Choosing a test layout / import rules ------------------------------------- ``pytest`` supports two common test layouts: Tests outside application code ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Putting tests into an extra directory outside your actual application code might be useful if you have many functional tests or for other reasons want to keep tests separate from actual application code (often a good idea):: setup.py mypkg/ __init__.py app.py view.py tests/ test_app.py test_view.py ... This has the following benefits: * Your tests can run against an installed version after executing ``pip install .``. * Your tests can run against the local copy with an editable install after executing ``pip install --editable .``. * If you don't have a ``setup.py`` file and are relying on the fact that Python by default puts the current directory in ``sys.path`` to import your package, you can execute ``python -m pytest`` to execute the tests against the local copy directly, without using ``pip``. .. note:: See :ref:`pythonpath` for more information about the difference between calling ``pytest`` and ``python -m pytest``. Note that using this scheme your test files must have **unique names**, because ``pytest`` will import them as *top-level* modules since there are no packages to derive a full package name from. In other words, the test files in the example above will be imported as ``test_app`` and ``test_view`` top-level modules by adding ``tests/`` to ``sys.path``. If you need to have test modules with the same name, you might add ``__init__.py`` files to your ``tests`` folder and subfolders, changing them to packages:: setup.py mypkg/ ... tests/ __init__.py foo/ __init__.py test_view.py bar/ __init__.py test_view.py Now pytest will load the modules as ``tests.foo.test_view`` and ``tests.bar.test_view``, allowing you to have modules with the same name. But now this introduces a subtle problem: in order to load the test modules from the ``tests`` directory, pytest prepends the root of the repository to ``sys.path``, which adds the side-effect that now ``mypkg`` is also importable. This is problematic if you are using a tool like `tox`_ to test your package in a virtual environment, because you want to test the *installed* version of your package, not the local code from the repository. In this situation, it is **strongly** suggested to use a ``src`` layout where application root package resides in a sub-directory of your root:: setup.py src/ mypkg/ __init__.py app.py view.py tests/ __init__.py foo/ __init__.py test_view.py bar/ __init__.py test_view.py This layout prevents a lot of common pitfalls and has many benefits, which are better explained in this excellent `blog post by Ionel Cristian Mărieș `_. Tests as part of application code ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Inlining test directories into your application package is useful if you have direct relation between tests and application modules and want to distribute them along with your application:: setup.py mypkg/ __init__.py app.py view.py test/ __init__.py test_app.py test_view.py ... In this scheme, it is easy to run your tests using the ``--pyargs`` option:: pytest --pyargs mypkg ``pytest`` will discover where ``mypkg`` is installed and collect tests from there. Note that this layout also works in conjunction with the ``src`` layout mentioned in the previous section. .. note:: You can use Python3 namespace packages (PEP420) for your application but pytest will still perform `test package name`_ discovery based on the presence of ``__init__.py`` files. If you use one of the two recommended file system layouts above but leave away the ``__init__.py`` files from your directories it should just work on Python3.3 and above. From "inlined tests", however, you will need to use absolute imports for getting at your application code. .. _`test package name`: .. note:: If ``pytest`` finds an "a/b/test_module.py" test file while recursing into the filesystem it determines the import name as follows: * determine ``basedir``: this is the first "upward" (towards the root) directory not containing an ``__init__.py``. If e.g. both ``a`` and ``b`` contain an ``__init__.py`` file then the parent directory of ``a`` will become the ``basedir``. * perform ``sys.path.insert(0, basedir)`` to make the test module importable under the fully qualified import name. * ``import a.b.test_module`` where the path is determined by converting path separators ``/`` into "." characters. This means you must follow the convention of having directory and file names map directly to the import names. The reason for this somewhat evolved importing technique is that in larger projects multiple test modules might import from each other and thus deriving a canonical import name helps to avoid surprises such as a test module getting imported twice. .. _`virtualenv`: https://pypi.org/project/virtualenv/ .. _`buildout`: http://www.buildout.org/ .. _pip: https://pypi.org/project/pip/ .. _`use tox`: tox ------ Once you are done with your work and want to make sure that your actual package passes all tests you may want to look into `tox`_, the virtualenv test automation tool and its `pytest support `_. tox helps you to setup virtualenv environments with pre-defined dependencies and then executing a pre-configured test command with options. It will run tests against the installed package and not against your source code checkout, helping to detect packaging glitches. Integrating with setuptools / ``python setup.py test`` / ``pytest-runner`` -------------------------------------------------------------------------- You can integrate test runs into your setuptools based project with the `pytest-runner `_ plugin. Add this to ``setup.py`` file: .. code-block:: python from setuptools import setup setup( # ..., setup_requires=["pytest-runner", ...], tests_require=["pytest", ...], # ..., ) And create an alias into ``setup.cfg`` file: .. code-block:: ini [aliases] test=pytest If you now type:: python setup.py test this will execute your tests using ``pytest-runner``. As this is a standalone version of ``pytest`` no prior installation whatsoever is required for calling the test command. You can also pass additional arguments to pytest such as your test directory or other options using ``--addopts``. You can also specify other pytest-ini options in your ``setup.cfg`` file by putting them into a ``[tool:pytest]`` section: .. code-block:: ini [tool:pytest] addopts = --verbose python_files = testing/*/*.py Manual Integration ^^^^^^^^^^^^^^^^^^ If for some reason you don't want/can't use ``pytest-runner``, you can write your own setuptools Test command for invoking pytest. .. code-block:: python import sys from setuptools.command.test import test as TestCommand class PyTest(TestCommand): user_options = [("pytest-args=", "a", "Arguments to pass to pytest")] def initialize_options(self): TestCommand.initialize_options(self) self.pytest_args = "" def run_tests(self): import shlex # import here, cause outside the eggs aren't loaded import pytest errno = pytest.main(shlex.split(self.pytest_args)) sys.exit(errno) setup( # ..., tests_require=["pytest"], cmdclass={"pytest": PyTest}, ) Now if you run:: python setup.py test this will download ``pytest`` if needed and then run your tests as you would expect it to. You can pass a single string of arguments using the ``--pytest-args`` or ``-a`` command-line option. For example:: python setup.py test -a "--durations=5" is equivalent to running ``pytest --durations=5``. .. include:: links.inc pytest-4.6.9/doc/en/cache.rst0000664000372000037200000002373213604173400016675 0ustar travistravis00000000000000.. _`cache_provider`: .. _cache: Cache: working with cross-testrun state ======================================= Usage --------- The plugin provides two command line options to rerun failures from the last ``pytest`` invocation: * ``--lf``, ``--last-failed`` - to only re-run the failures. * ``--ff``, ``--failed-first`` - to run the failures first and then the rest of the tests. For cleanup (usually not needed), a ``--cache-clear`` option allows to remove all cross-session cache contents ahead of a test run. Other plugins may access the `config.cache`_ object to set/get **json encodable** values between ``pytest`` invocations. .. note:: This plugin is enabled by default, but can be disabled if needed: see :ref:`cmdunregister` (the internal name for this plugin is ``cacheprovider``). Rerunning only failures or failures first ----------------------------------------------- First, let's create 50 test invocation of which only 2 fail:: # content of test_50.py import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): pytest.fail("bad luck") If you run this for the first time you will see two failures: .. code-block:: pytest $ pytest -q .................F.......F........................ [100%] ================================= FAILURES ================================= _______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed 2 failed, 48 passed in 0.12 seconds If you then run it with ``--lf``: .. code-block:: pytest $ pytest --lf =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 50 items / 48 deselected / 2 selected run-last-failure: rerun previous 2 failures test_50.py FF [100%] ================================= FAILURES ================================= _______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed ================= 2 failed, 48 deselected in 0.12 seconds ================== You have run only the two failing test from the last run, while 48 tests have not been run ("deselected"). Now, if you run with the ``--ff`` option, all tests will be run but the first previous failures will be executed first (as can be seen from the series of ``FF`` and dots): .. code-block:: pytest $ pytest --ff =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 50 items run-last-failure: rerun previous 2 failures first test_50.py FF................................................ [100%] ================================= FAILURES ================================= _______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17, 25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed =================== 2 failed, 48 passed in 0.12 seconds ==================== .. _`config.cache`: New ``--nf``, ``--new-first`` options: run new tests first followed by the rest of the tests, in both cases tests are also sorted by the file modified time, with more recent files coming first. Behavior when no tests failed in the last run --------------------------------------------- When no tests failed in the last run, or when no cached ``lastfailed`` data was found, ``pytest`` can be configured either to run all of the tests or no tests, using the ``--last-failed-no-failures`` option, which takes one of the following values: .. code-block:: bash pytest --last-failed --last-failed-no-failures all # run all tests (default behavior) pytest --last-failed --last-failed-no-failures none # run no tests and exit The new config.cache object -------------------------------- .. regendoc:wipe Plugins or conftest.py support code can get a cached value using the pytest ``config`` object. Here is a basic example plugin which implements a :ref:`fixture` which re-uses previously created state across pytest invocations:: # content of test_caching.py import pytest import time def expensive_computation(): print("running expensive computation...") @pytest.fixture def mydata(request): val = request.config.cache.get("example/value", None) if val is None: expensive_computation() val = 42 request.config.cache.set("example/value", val) return val def test_function(mydata): assert mydata == 23 If you run this command for the first time, you can see the print statement: .. code-block:: pytest $ pytest -q F [100%] ================================= FAILURES ================================= ______________________________ test_function _______________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_caching.py:17: AssertionError -------------------------- Captured stdout setup --------------------------- running expensive computation... 1 failed in 0.12 seconds If you run it a second time the value will be retrieved from the cache and nothing will be printed: .. code-block:: pytest $ pytest -q F [100%] ================================= FAILURES ================================= ______________________________ test_function _______________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_caching.py:17: AssertionError 1 failed in 0.12 seconds See the :ref:`cache-api` for more details. Inspecting Cache content ------------------------ You can always peek at the content of the cache using the ``--cache-show`` command line option: .. code-block:: pytest $ pytest --cache-show =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR cachedir: $PYTHON_PREFIX/.pytest_cache --------------------------- cache values for '*' --------------------------- cache/lastfailed contains: {'test_50.py::test_num[17]': True, 'test_50.py::test_num[25]': True, 'test_assert1.py::test_function': True, 'test_assert2.py::test_set_comparison': True, 'test_caching.py::test_function': True, 'test_foocompare.py::test_compare': True} cache/nodeids contains: ['test_caching.py::test_function'] cache/stepwise contains: [] example/value contains: 42 ======================= no tests ran in 0.12 seconds ======================= ``--cache-show`` takes an optional argument to specify a glob pattern for filtering: .. code-block:: pytest $ pytest --cache-show example/* =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR cachedir: $PYTHON_PREFIX/.pytest_cache ----------------------- cache values for 'example/*' ----------------------- example/value contains: 42 ======================= no tests ran in 0.12 seconds ======================= Clearing Cache content ---------------------- You can instruct pytest to clear all cache files and values by adding the ``--cache-clear`` option like this: .. code-block:: bash pytest --cache-clear This is recommended for invocations from Continuous Integration servers where isolation and correctness is more important than speed. Stepwise -------- As an alternative to ``--lf -x``, especially for cases where you expect a large part of the test suite will fail, ``--sw``, ``--stepwise`` allows you to fix them one at a time. The test suite will run until the first failure and then stop. At the next invocation, tests will continue from the last failing test and then run until the next failing test. You may use the ``--stepwise-skip`` option to ignore one failing test and stop the test execution on the second failing test instead. This is useful if you get stuck on a failing test and just want to ignore it until later. pytest-4.6.9/doc/en/doctest.rst0000664000372000037200000001531513604173400017275 0ustar travistravis00000000000000 Doctest integration for modules and test files ========================================================= By default all files matching the ``test*.txt`` pattern will be run through the python standard ``doctest`` module. You can change the pattern by issuing: .. code-block:: bash pytest --doctest-glob='*.rst' on the command line. ``--doctest-glob`` can be given multiple times in the command-line. If you then have a text file like this: .. code-block:: text # content of test_example.txt hello this is a doctest >>> x = 3 >>> x 3 then you can just invoke ``pytest`` directly: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_example.txt . [100%] ========================= 1 passed in 0.12 seconds ========================= By default, pytest will collect ``test*.txt`` files looking for doctest directives, but you can pass additional globs using the ``--doctest-glob`` option (multi-allowed). In addition to text files, you can also execute doctests directly from docstrings of your classes and functions, including from test modules: .. code-block:: python # content of mymodule.py def something(): """ a doctest in a docstring >>> something() 42 """ return 42 .. code-block:: bash $ pytest --doctest-modules =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items mymodule.py . [ 50%] test_example.txt . [100%] ========================= 2 passed in 0.12 seconds ========================= You can make these changes permanent in your project by putting them into a pytest.ini file like this: .. code-block:: ini # content of pytest.ini [pytest] addopts = --doctest-modules .. note:: The builtin pytest doctest supports only ``doctest`` blocks, but if you are looking for more advanced checking over *all* your documentation, including doctests, ``.. codeblock:: python`` Sphinx directive support, and any other examples your documentation may include, you may wish to consider `Sybil `__. It provides pytest integration out of the box. Encoding -------- The default encoding is **UTF-8**, but you can specify the encoding that will be used for those doctest files using the ``doctest_encoding`` ini option: .. code-block:: ini # content of pytest.ini [pytest] doctest_encoding = latin1 Using 'doctest' options ----------------------- The standard ``doctest`` module provides some `options `__ to configure the strictness of doctest tests. In pytest, you can enable those flags using the configuration file. For example, to make pytest ignore trailing whitespaces and ignore lengthy exception stack traces you can just write: .. code-block:: ini [pytest] doctest_optionflags= NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL pytest also introduces new options to allow doctests to run in Python 2 and Python 3 unchanged: * ``ALLOW_UNICODE``: when enabled, the ``u`` prefix is stripped from unicode strings in expected doctest output. * ``ALLOW_BYTES``: when enabled, the ``b`` prefix is stripped from byte strings in expected doctest output. Alternatively, options can be enabled by an inline comment in the doc test itself: .. code-block:: rst # content of example.rst >>> get_unicode_greeting() # doctest: +ALLOW_UNICODE 'Hello' By default, pytest would report only the first failure for a given doctest. If you want to continue the test even when you have failures, do: .. code-block:: bash pytest --doctest-modules --doctest-continue-on-failure Output format ------------- You can change the diff output format on failure for your doctests by using one of standard doctest modules format in options (see :data:`python:doctest.REPORT_UDIFF`, :data:`python:doctest.REPORT_CDIFF`, :data:`python:doctest.REPORT_NDIFF`, :data:`python:doctest.REPORT_ONLY_FIRST_FAILURE`): .. code-block:: bash pytest --doctest-modules --doctest-report none pytest --doctest-modules --doctest-report udiff pytest --doctest-modules --doctest-report cdiff pytest --doctest-modules --doctest-report ndiff pytest --doctest-modules --doctest-report only_first_failure pytest-specific features ------------------------ Some features are provided to make writing doctests easier or with better integration with your existing test suite. Keep in mind however that by using those features you will make your doctests incompatible with the standard ``doctests`` module. Using fixtures ^^^^^^^^^^^^^^ It is possible to use fixtures using the ``getfixture`` helper: .. code-block:: text # content of example.rst >>> tmp = getfixture('tmpdir') >>> ... >>> Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported when executing text doctest files. .. _`doctest_namespace`: 'doctest_namespace' fixture ^^^^^^^^^^^^^^^^^^^^^^^^^^^ The ``doctest_namespace`` fixture can be used to inject items into the namespace in which your doctests run. It is intended to be used within your own fixtures to provide the tests that use them with context. ``doctest_namespace`` is a standard ``dict`` object into which you place the objects you want to appear in the doctest namespace:: # content of conftest.py import numpy @pytest.fixture(autouse=True) def add_np(doctest_namespace): doctest_namespace['np'] = numpy which can then be used in your doctests directly:: # content of numpy.py def arange(): """ >>> a = np.arange(10) >>> len(a) 10 """ pass Note that like the normal ``conftest.py``, the fixtures are discovered in the directory tree conftest is in. Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree. Fixtures will not be discovered in a sibling directory tree! Skipping tests dynamically ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. versionadded:: 4.4 You can use ``pytest.skip`` to dynamically skip doctests. For example:: >>> import sys, pytest >>> if sys.platform.startswith('win'): ... pytest.skip('this doctest does not work on Windows') ... pytest-4.6.9/doc/en/img/0000775000372000037200000000000013604173421015650 5ustar travistravis00000000000000pytest-4.6.9/doc/en/img/gaynor3.png0000664000372000037200000005477013604173400017752 0ustar travistravis00000000000000PNG  IHDR,&lҚsRGBbKGD pHYs  tIME ;T IDATxwEǿUݓ6'KAdh bΞ wÙNNT0"H%]yg'twUe , ^}yxޙ·ߪ Bq]6mZի(BP(~+2BP() BBP(J BDHP( %B BP"P( !BP(R( Xю;\y/UiY. KKܺ<=5KMfĬYf 1G;ԱsQaq72 xm R,;JHTPZOKɥ )@NR[ B#|1=w nrU* S*#w,)/[u D$$x]naa0 )eY\pFәp_vut;F lGP(Uvn+D{W`xŊ ?-_~9-U( Bvya02 R2b|1Iax< BRu4MR9n1%֨1&  SnRRQl⽦jl@@hE~1DLU B{ɯbQJ !nۄPMӨ@Jr] ) qQiTiiuYP{x_Jی2"m,ZxEai/ tdh wu*]B]кJ)!J朂1J\2 vCHnۄR5D+e(k[ʉRfv qnoLu0c?<lyDJ!6CBPD,(@) B B)eJB ]4M͝HqcLMOKHglߗ[ ٔ_msրCtwӶVڶʪv@P@ԓZ+By y.U B{e:"DPI(!$:XF`۶RiiUzA:j,554s6啘[lݾ2M;lj\q? &PVP(~o"d2F %RBHB)HH) @# sF(ӘiIIIi۶5&.`:4#!3p! j2*9D/Ik") %BR뺮SB#Y)!PPJ (SB)RJӲ4FOѣOf8p>'#-cpMh_Qk &6)5dR=$(J@Q) %BPJ u JTFGۆam޼UZ  mی**c]؈%2ڦDjVQ[H JDO/:HB(F) %B.]' HM q!cBP>/Ņ;2I!-Zn4@M ӄiB@A9SAFFBpDH ) N.r?uj!RR75)BYa_$H!EǓT Ro/޲Jk ۔2CJXŨ.$RY:_ 5-^A;&~K[ABP"D))P&9 v#B$ԹK{'8"!FI$hs!Y$Du0Mr„$"HՎ A I_l\KnO> !2v_|Y;,0ZugcT-V(#" 9Wdt68:4 uJ&'r 'tBTHH"3F k#@ۍ&kꜲ|d##3ٙnZ]~HP4c $q&Q:YjnŁpXs%aPPPBiV8]\Z --.Rh0 ]/F.NAOt뾘Wq>ț͎o[8h7Sg{W^>ܾbj{y[wi;BcE{j QVGJIuٛY'nUNJJJtּez>̬)k !j!.%f+Bɋ{Θ<}?mZTlNjKQ0R:9<4MQ2ᱏ?+SK$xuӃyU*|?q瀴Ax[6)YWwL4ˢzˌޙ4k 6v7yÐ 7w,h\Y[|]#3=Q7}Q\ bozrakz%[i/߉Yjpv5n'-F??/א}gsNN#LR('ԨO;Bu/ZP@DRJ!M<^oN޾E˖߼VM8( f(4\zF.Θ!Brι#92pΜ(RJ=, \rv F9S7v7g?YY ;"h}8 #d0g@ҨDwKK'Ω}{z @O܋4kWWڧ&\tBYyLk$bf.+ul~J )>kd Xx?M]I\.S9  \}ɔxooq:r)#/ɉvP(IRF/RCvN à+w=n*Q %d|\l߾}ڵo䋉1 ]41C] wӺwptɔt=( 2r?3sЎ{K{N0׾\M`׻|nj<H[7:*=aaɽ޻]?6yx?%i^[kjlx"`JkgB;  4ho{ߛSÓcm0qsDh+@ڌzONڽC^(!anܶ'GZ˥P(;ǜaDH)%($T\MB5eڭ];tnٶ2w.H$4Hh܈MCגBnyc*ʪ"`6̖[RJH !4Y("!d7Qpj}V86o_7(_};.÷oR#cyJۜ1A^*79]g'"/7ꆵ7&h/aiu@!&6|P{RzСA|paX jڻ@Uyfߎ#Ҿrwk+.<7fui'瞺>]a7Z!w8"%yS/n~B>PG9(:gr >n߬IQ+@Kˆ@ɫ+oW?2!/^l!4=O/KW?}px;},H<*Yyg7KZk`L 6 -i.sN_B*BnI 9%oh)-ݞȭsYS])c!DEEH1Fu I` D pn6B׽1) @V$brSA:aqBJB&PJN3 I.:Jᅧ>T>W,3,.qqZ z6ӴpfXHHOJkSbYn{dk6U|4ZHAG>iwܩ]0靻f&yȾ˼9ca[gmcz:H zqt5P(]j]RH P!%Dg \H)`1\5OKI1-3!.qk9>8[mے  {9B8J DHH)Ǝ#}@b^8'Z$9hWf98tB.`\`[> |~Hw&`.܁We\Y7@1^°$ OuO5jegeӨB#(Wk!,y:YmlP(D#R{fO^ 2 BtC#ò.JAKz].7%477F1vR]UUSSCx.KnYbg… H(e8#oNod;7r,`^1)&jפF8'HF65(5;vZ􇯿ٺyKMu5-P}Ivn޼5+uk *+>QK)!UHB@DF㎞?G5hx#GUhWe -.8ٛGsE7<5=zgG9z7wǽ)64iиw&ZH5Wjgv2Vx:_ 􍣀4DV<.-=xbǟ~ˊ1.AH={T'O+OҠG_@l<^_P }P( P/gdcҠykW\leK!¡mFiQqqeeE$͝3{ߓ-$Z1Mz͈iEL)2IC4.N$??h"(!G/Yv5 Άc^chdD2z]tkS߽M #c4&&yC=w6HgԭW+wRfAo{&>>:qmϼ)\[ե>f^}L']M{KtLvE݆]?/4TR(?(~G+kSF(ejR{)!c@W_zJBUU>[r[p[#Rh>gWR\r@ PVV6{ܪEU h^oL PmELHPJ$-4% n."]N cD ³U;.}}77~2NJ2 ůOrB_ur"#5ՅLOOI T1C sZjaaa4۶kd}G,KHBl 2NmyC(uF9qH R/xOѠXƎjHP"S wю 9o f(ijnۖx`AAAM Ѫe ).)!ڶmZ1M`0dR.`e#Fj44 DEα⚦iQkPъL-߰;.j)(@=R(:Rsi[&疰Ψm9+{@(ms‘iF8TF)c6AJ"DR!lgE,v,@B afO띓z>pYj'BqRP0ڒ;MUJʘ#ӕרP(N*rjծ%$cT5JBD=۶m!DIiiEEE4+`0bY葬2בRr!5@ 0J%#u`H*j󝓖ݩP(NƉPa0 H)$ceYܦh:eI!**ʂ᐀4-+lF8R%0Z;lH'Z1iLәkp w.B+RJ5@ RRJ(a2WVUIiYR4]ӝo 1UJ)BP0h5F;%B B{DBe2TN\xP_K!#β,1PA !mn; Ji  TFc{B0/`ᐮ딐e )J3#(*..((G"HĴ,)$!QBr.8e9;ZiۦmN U BqrqLqf$sBB¡H8l0KX`ȭ{F8tN,)ٶ"g_^=kj JP(~SA/)?;)!$s! VV*@7 nFlnSH0F)#.ð G*`JJ]ܶ-1 Rj$w:3rkJBsDORFth|t gL?u@T~b%hk5={ ՚LHB5 ů,BVe5͹i+bF, [M"N,-hZ-.OuUeyYs2](&3(%4!ugS̜mf8$7c<uDc0‰mܒ\ PiuT8c.jnݕWĕٹ7{Θw,]3'1 Ke) Xu'eo`V^zh/[qOq Iiwڀ5iN~yW k& ̛!)I(-XvOI):WΙըkw kgKޗ]֫vW]2Ԟ9}WCb.]<"/KR"f9%ANl;$h6mw+mn>Q W>$K% IDATB [p\XKɹFtX1] 2ܺR@) Myݔ;)-,(-=kNf<9)55E _\lN97iڶf]ʦo*۹;ڭg.kr֮N4)ؾioܞKBc}Twgih5s}?s]$SEx-O=5P(~u,($PݣQ蒀0asa^n )-%1-B0B[<#IJ},FqvZ,)ʫmӖ먁]u1-r(%frZ(HP&"䥚sXB:^s!q0! aDHB %&5f$dYĶ 8,KmiB$90xM pž [6<p*%maے &ăeՈRi+Gbf$0G'NWV Rk\U%M;HnLDD0Ӕ~ò:wf[E`͚F]?i!9U?s?f=:شa_m2{sqnkAO-|4+4%#[& ռIJ o%Bz1R:l;T1F%L$ Ҷer 9Br &ˬ Fꪤ2tMH۴욪 2tMӸ)8Kn BP1>ލJBpCACimWK`ͺH^g6b*k7I׎Xy>|뮪(ũbzjKeBKۯrۣ ?s?ZRnIWlhZn7p6"^Ra~@: ;b nc o&B<Mڠˆ%6# qnۦer+1%ن&$BH!k"M#+ 5 kWٽ;ns[у$M85H.ĩ|ϮJna x]˶m"- siͣ5j`zB]1$J޸`]KG,tn呰GױG6Lo)4$n =:%tĆє(_-L(cn۶1%kTcH6Вס[Ww|2 %@0*/+\PJ݆Kg,۷z/x|۶Aq{ܚI ˃OpG:QWaU9kIz`oq +PcHp ;[xIS.2 Z0Q]EckRjx+JR8W4gM}`D5RwwmݣQ- ]NIg"ҜC|oI-աC !.$ tNde: aݥK*9Јfjfg :UV\h4PBtBf2-g aV߹m}Ee52m\PB5QPPB2:E.%DӴ P_ZVSZB*6.quy$n[' lLګSk񭒩wւ`u3H-{uOp㾲@Uin/?]V]~ʲCk :Y՚9 kNcZwNEErU%{V<*'?{?=:zvdSOsM<1o),زM.LZP(~zS0䠵RJ) $  i `hZDpgG8[JĄf*U%IT#,]r]9!TF41,C76ä;$.ے߫Mb۞gge@6Q~9KLt 6wVKgn~j_z_tk銵OY-Y~ J,n 2l=v@h\s tWOjރi,sUg2%ulݸ5G[Qoʞl˲rrrY\IJdtwjOL:'v= y={h9" P#&!%-gf ۴yȮ?_Pڢ\VU+YG ftyVCz)R('eneVBjOD mBRZ@!Ж6uY!2ޒPyz@("[s ''@؂C@ H)8nG>3u%fq ,[گ7U𓯷 {+9?xy֜i8Ťa#m?_Vŵ{1*MP"4|-[;ޏ99NrDR"T#Z8\#mp)%M-6\nR"f$\w WشpG-%B)6$C]5A Com?s融I\P5\P4#ڵiH"%LrI$\!$TP!%LBta[nF3P ӲӢĝPmٶ{\q&gRsՄkF!PJ({[SEP(7ugwhێH J@lAљ&p1 nte:#.\ J)uM nŧ-(pUuzv4IT* ʼnO;^{eƌ[fIA)# !)FBXft]m'sJuaEL!Ch1ʈDHB Pcc}=vÅJ dq! BP4!*hJP(J BDHP( %B BP"P( !BP(R( BBP() BDHP(J BP"P( %B BP(R( !BP() BBP() BDHP(J BP"P( %B BP(R( !BP()7U^u ݻw#^9wnupn'c|ʐj&Bi]Nyuhz|B6<3z"x i#3K C'a ]PͺsNdOLs^3[ҴuWT&냧6a*27tz 7g[lSapv>c5w:H3o!#6y)n{MGH-~.J?3x᷏9a>Rm}>%g18boOzg,(<=W<"w|L>seHv+!w 9VV%yʊp{_?~gr}XsǓe6o\r >^q*Zv %vj)^+o N[Gݽڮ:rT!^4[:/qaFMa%S^wio~R_/GF9;HRZIOk%Y V@ҨL}u | <j=їuV&QX)?JǞI.^  ډD~HhԠ2Fo]]wUaQ"z'q} e䒜\p` $e̿<1b}W_9*Q1i2AɞRINZͭ@$h2:ʾeKˆ"~'s e/3h@>zqp<9Y-.M{nWdp\2-~'.=ӦL0V:\77Iq23h F,a[5Wt9EkJyѨ{퓮7=].Sk}@gƔ]> 6㗡ā=qgV=|PӰBN0'K+hM&B2Zo~W.igᲳ9mx JnѲYKj蝯lҙ'/w#mguck{/"'}ӗT'D fC>~tEv#zuaCアI쯷zv]ͼK -rza[f??gك^x Qg&@Vg !GS%)ͩ`lW#qSMaC7 Gң~ӓ: MO2dW[Mg&HV:\qB)Shk 7 ExO9UdDhOz3^놝ѷo߳ƌ[{o#H1iCoڱv@k4cpj47!} TV4cdW@%8i* Yhסvld͟L*gӖǥ,P1L{gvnUU{ʮ)oJǔcHK =a@oDf<:զ/[-v~V?iK̟s.[>n=ф' `˻Ͼ4C=.ʣ겎#NcbO.'t=nap8 lJsnz`\ @η+W}esx׎ª)#* u~98ecEEƯ6^⁢jͻ ${yěJpn _GlCbS4;.k\@Y^ _uʢՠh+GaH*kq^[(Š`0///"țޮWP)'n䕧4]QjJcC|yp@xg D=5sߨdcݺs3DUɯmnc}0\0{ځA/ّ+ ޱݻaOB9nɵ<1j} ~z˗٧]aʊe_ԟ֡yiW W q`͎:GLitH_s>`IDAT|$=KNJ`Et{u7dĻϹ{Kig8Nwx[~MY]l˦ p݋st7Q ?;3%}it]O>y]'4yDulqm OCEb$FDcCX5+Iib XTD@ĀHՂ6@ad]²l},p٢0=s̙;w۩,+9hX;X.,e޻uitOS1‚ܜ,WPŭ͵ũNFF:_``5/$g{)iL~(9qW]Hq$nH۳*<W]&pt{KӤ.&쌽Ce6:^I:_+;B6{ė4\InH| jJLsɪPKCu⊹#c+MVȺ|gܺy >|œ@T]u-%Iw%zυ @C՜j]{ja~L@^x4)ѧ"Ϝp1?\y#sZ1.4 _ot;˛B氻&.~ͺDf'f䳌=j3<-5B.Ͼr򚷒a &Mӭ F~~JGV>d\ԯ8?xzq)=ӰcbZՌޯ;=L?wȯg%6/۱3Wb]Cc{ıHZ2y/K 2x1@}D}EUt;1n8{#/ Jd~& f(lA,^zc f9omW캐hFT |[BAEۢW"K,YR{[dZ۪AOcJ%;OGd)r*B B\{~[̋?>5!#g>i'-=I8GOVfhCC |$_ҔwxѦQ+ .ɁxM'/xlKځt~AJtѽ`e@TDYs!j6c9ǢGHCoLAk1Ls^zs%hpˁxGoENȸ$u#^Nm**B@qH@E@ !@ PB !@"@ *B@E]G/Ovc0 Ƃz ;V/`0){:Q%߭O XЀ'h4@EP~^2uңwy 8́l #{3V8\d0 yE@q,Vh4@EŒ6I?H Pd(weV[R= lG3wrE&J!Ť'AP#|q\ԭfӅQu&.@ 䇪o@4s Dߵ@zDIY,$U )ۧ3)X2}o`DTAѝWygeT =eSm1ISZh@X&`ؚsut!!%#8N_u k5L^}穴V(p}~*v\SuYx NRJ&4ziձO랔& O_>yXqXq8~RTbVdOq8nuuu5+JR1"֊^^ q7}>'Ov#b8t%~7Oqu{/}6~}V.`_8ǽW.{Z^|.s"RT*2Oq_|'x7q|WSpSW=C[Yy&ldm>n/qAݸu8㓷q%r;p[ ARnT>cVHX<q Iτ/t慳9u\»8)у?p׋_\bW'6dUV*-uHZN+2u?y"yGPק'85 'v0oG+` 7ĨZ)Lҭ u܏Oc*NId`BxL>>8UdwS=.LQW#^T~LwCIӖb0XiԴfd-"_covf]0߮[|>SBIEFskA`9S^ ǹ *.1a3>pf02 [Ni8{itdۛCfMgž# F- }uk$*a$xQ4 wlȬD, ፉTuQWHKNN/H9{)MI ĭ̻kyh˶rh8Ι=1|RŞI3Mf`3)ۯK$e&1ģ &4 kP2&Gv:>b.Lj_2$]9cU7~<~ub?!$ Ss `6+%)p'׵izjx\Y}3OQjNڦY ?:mU]4_,ZPw#"xnTk}S_S]=*,P Gѡ[&[`\Ѱy lUЬls8tT A]đyϲh:Ցũô$fVUV"i?gf*Sj3'Ű-goA{5 s4bmi6 w}5{Ea[3bm)_o}Y=eq2IoBXfם[>bqzŒvy+2*f?K;%%[Uywd D<d{N֧G$F@͹MOӾ; Gn: -*.w۶m`YzN4sYn]/,)ky{XhU氅k)L0qkE$2;63%e1Tf`vygb?vou(9qW5:c6RSNzdbH۲HN)@cNbRz]]☤eL=K8uj8`d8ȨS Llܵۘ$+W=&C]/ Ɋ+v_t!r̝"0Ptt:7,q!Ze׸>$,ؾ qCڞU/y!-ǐ{.TtHT]=2;{1-Fw{~_#ϜK(lw_cgnf"ugׅDWuob<#G EI" zz%r%z8 9EkkГR F>kٛ!P?G\{~{̋?>5A˸Jsr8|M j|s+{d%`6? )MV^9[?E }JNCGOEmI;[X>ܔN/HXµ!gIpO!9tL̈́f s4RӐQGľy:!fԍdxM ;u@! @"@ T@E@ !@ PB !@"@ *BKc%IENDB`pytest-4.6.9/doc/en/img/freiburg2.jpg0000664000372000037200000031317113604173400020244 0ustar travistravis00000000000000JFIFHHExifII*  (2id SONYCYBERSHOTHH2003:05:16 06:51:43"'d0210& .6  >0100F1( 2003:05:16 06:51:432003:05:16 06:51:43  R980100 (2 SONYCYBERSHOTHH2003:05:16 06:51:43   (1#%(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc//cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzx! ?}7n+s ?҉qBOMEZ43I4LҸZع4u0(OaY79Y q Q#BKKPN!X_4oLW nu&if!2jJ (&+)3Nⰹu;f n@7Pf5% K LInhL@&i7Z` S}.E`(04n!~`{m)9sB3Ajil HNzԌi4f:g3ޓqИL HX7uMƓ4)Ph'4Jb ^( &h3/Si,ʄ6%B JZ:P 稤1 hBc @ @ @&}02i 0J% xr>h9ai\i!dp@&@ۂ(rz()ӞOK@tT܎t+01꧊BErj68ѠYTނҋCdt)#f)̅ߍ&6O㚖);!\AN32zw~XWcȦ_ϩR(<  џaG銚I\F?Z7B2ܴFc`UrD`2MlԲ,HXg PyYM2"y@.&{/^`eu^XRrhvDldpը%GQ|!s5ԔIa. dԼnXsTh67AhaȄoyޯƷRD+ܨC0RbWT2(8`Γmd4+lX՘ yPbǹhIr9U%d )|tsyb:𨊳Թ; mid,nawqiq+Xȥ$ӑ'kd\@oZ]$D y5oW*N}i%Զ,ζ$rԖs)WS)ᴎNHpL|"#1!sj4KvFߕp>lqEdXb8-G#pOa5.7EeQq]\G"$・4XW} u6V/|>qi *:MNT+TJdE"x#f'cBhmخTi1+֤1 J;H>l-G]VTgWROr6& pKpASM̀#{h]i@](U RpMBKi7 JѰ"yj\n䎙js q¥)X˒ z$(@t;@A'RB: kR$es"M4eY\ʐgru4I?\MLdKFLNG=0 20=MU yn?ziIw@ RlMn>38w@l}M6%FOJq<Һ 2qn0vbpi9[#ޅ}qMݩ{QMHZQ`j"h@s:նMSIsړ,IQ@\J7Tjp~.}].{v<ѕ^`v2Hc*jL&l ҭZy2)&Eݑ iV-u%k}Zc98 &% Rj Q"#lT^,9gӥ4xM~Q~^)7֙OZhlj~B'IB9;2>Y JJ媖зGls[rX©}"`M-[z c= ݊NpƐW@́r=qM21pʷS늖(ղYߊR*T2vɩ(вt$2$/v 6؊RzL'Qq2UZ{qW =̞1(^R}B(y'NMnrO8=3~6:DZL6Mer%KGkMMH?BzVwJ#IZS^*C8'c!jM~nU .J1I98LWsϽ'.z]¢z2qih;a9A2}ir=lp0z(gг眷M޹$ '7vGQi!sdOQif랼6' WMWI;jd0s1WTmԑݜC⡕ GԦL*qU$z}*`! Fswz+}̵df=;RIi FaܞjW԰'.N=잹ہP"7$g@$JM}}hHb} Tbz1qҘwoRh7 9jz߼o$Gtl[+O\S Þ ˀF>+n];u?5ʩqSsr93'5|L ҼN*"ݞ&{yo _(~ l=-'iCPb4^fF5FCKD52 +J͛Ok0)7nx=M7qIK ʑwYesWE+f9vCEe9@UY~$Xx"' CI)rCB>#bns\r80EԞcm2*XQ^G.|\`=xxҶDnlT?2œNN`} .>kH_ޱA8? C3sY)3NXq 1R9'2i7ݟZ.M.8ZS+p*hN?Zb-rڳ>]#ݲFFcW?V}D1֚eN[qyF;1߮k*j}YKEbS^=XrE5R9-ӓ)jWu5gg/N 2Ql8$RČDq늲qڥ#%m irGn?JDu XP:`yE$p=s**:׌܃uzR.J3c޵ќOslXǜRضrAY+xN7{.:$'Ўaڒkn=qTQKRfpOz1{t2@=;v )6Jz+cړ?7dv'v4RKdj:Ǚ#65 \} gBW5fIM0da.1KJ-κH(('=~ok*$޳͗xLЊJ-&[;x|Vm(-Y5<tbv[LdpG4GfS7W}dph}j/ 9|^^I$c4,N1}SnȕԵ) %e$mS5 ݬӓbb }N:դu,폩'qYIiC1H>_HpvT+uoPɬ/R#0'Xr:|(ј Ur*b`%n`eϥVkK19L0ֹiMtJ Tdp;OvK|5XJ<EXxGs)TobdjS'|9ՀsDL OS+r ;Ry8_OYvp#-;@UY<=erx99@FI'֢.1օ|d i69;y=遉<:I㺜[`HW{ԋ;BQe2M8= ;- n VےsS _8sv @-ۊ+Tr%nڪIFgv&GO\Ƣ+'yLJRjWwgB9Vg4`9%\nt0OiQU˶I=Zaqj2?<7ڈv8 JΉ, 8@@4N?5jH$ Wqץ'zdUruXWu  8TW9ӓT8\@OTecOZn8UY={;-"d)/{Jd9TrcVmĂ2z9O':΁4>bs!?zBޜbn(2L8 çZv:Q(Z˹>~o>Y$)|贜n+0F=ųGc֫FźqZJJEAr})pLqQ%?+ @H@w&ْwJcmqJ$ʀ;L8K6$=iߨX4)#* 4Jw[!77Bi8׵UVh$_񦐶,?q'c֪I= 89#Kro0c1y9A֓~F݋C?Ozi;hHǺgy`HM F3P20NE!lWI(cސm鸑֙|Zi#ߚX/g ҀNNj_o>:)BX}*y>84٧3k0y~q{Hmï?ʣ=˥ճ# 0"zR;~"+Y8Z^x;M!7b9 Ov҉H8&( NO!O0)š&O^ TnJoTHi[=z{S8GNjq8` &QU^XPdVj|I,NzP>bp ɧgj8\qޞE=>TgZQ1urв#x稪a?A8U+UC8ޔ"fw4":栒@Z 5ٝ@±RugdW/)#.XG$񥌰h#v3p;ɍ'(B69[:Mh͚(r,͜!xIcXlTU}~5eM$M>剈㯽'|M%Mwo%bFzzUY8p *Z@N韠VʃfJZIsjgsnP7/T@$8 2s#dJ*׿4lLuEYIQ( 3% HhY9g?IDw1T^¬"GtvBXq D(dO<31\`w>^7p̰+tfINYUUnN4o`+*iv[PNwc$vçJ| e*6\0?TJڄ@PI=@ q#Gb9u#[,I;ˁUZBkhs=hW*bFÌB`{UKB}S zqO[2m?jq|m,08`*%Cx N[J]ǯjB[8Qqq v+A<.㎙45zcM^dt#vハZfA \zlHW<.*0ǁCi2G K:Llv~tONH>OzS'n4kAI=)Җ^23vlFYrpG@b1=^mi71Zi38=Iv%)d+vN-~qhJX*<`n4X9לGW O3"wtϡftHG~ CdUօ֐+' 2<`L/p RN܎ixBAO>E; M&bzP[,F9*yy1*ˎ1w8*ӏj] %bN3}EI(m4olI9|^*p⫘_Hbu0qZw dsT&`ۋ&CI=ּKvCND0N*IZ"X2}j sNӗ̝q&ӟ41(,Nxf:h8{RpF);xy@hem=SNyz pEIS[^+jM`d֬ȒŋeA {TX3Q~b1+EJ G_W=YucjqM1]-@ q(O%--xrI<-G5r_A5 ynKqҝxL0ϥS?x1~WH%ߥOS^9$߳(;eg<@XFO54ϕP_Wi9p| W}>#ORė "#s"P|8wL2j$fv$G3BϜ)ەQ,w7|Qwv.m,:I1BwJF ?*<cy4X+wuVWXvhSWB,[+sUtf6?Ȣ)+A%X*P0gSv5yeA@>E"+p;R@RU`I3@i9 ¶*NJU`rXuǽXBb{t1w ˻#>۽&4fvT%xr0}Ja 8*ܪb`yڪg0&F8V(HWp!i.m2X.~m`Q͕@S֏RbΈ8}J{u:χo@6k.u˴stJtcQѻ*ϟBFOzkp dlVea ByzQ,O+LJ@sOJгU,N)?$l[Dž*\t\u,A ک@7.{U+߭l[%45e8ߞpI⋒Ɂ84?ZMqǽ SALg+֜=sH&2@4yITۭ4$xL _?+h&>F@ 01i+~q*%Wq?01dv@5z$@ '4#Cޛ)!{ޚ_'©&ÎF m\I[ VL8'4ByL^y{fv:JwNFw*p2jbLye\{NȸÙ\dPʤqדCaE+yO-? |Pe~*S!?*l!eQ;}D3қ=6gƉ[QK*Hi{0]dqP: r8sRkbħrg'hA* sɐX>3lpsZYjCw-y:u<3{Mh/ʋud+ˑTg S]Zh=ALGi(ڟ K) y KoSx# Ï2N[ WJmeP>Rzz`8^f@=O]cX19'x|c!b]}}(gs!2{M[ofl|ѲVl$(xela֛ 'AqpzqL3`N֭A\w!W b ϙV\U"M5f46o稪ʡ99Tᱞ4vl<{Z|: rԞ,n;{z`#Y3&6Q4W$ S` v7 $5 N*!xڹf118n0n1$Ny*]H҅TA,Z?'Y5oy}`^\ ~g=iC $e6IZܥrƳe (sG'X5"weRPjIR8E ~LCY;8he0u$MD-LsQ՘ iFQ3|Ğ>N;uR,oީ ;{U͊n$sXI}<,Z#(*(d G.$T9;b#U'6.Xs}1T?̒4Jwc&I%9jCͫ$}hC6ؿj↝CDɪK}G2egC96)}HAqh2#FQqf0Nܷ$n"kkb\cg#<$+s§p2J^֫L[?$*؉5p36rTK0eTHsUH%f5%ӟvZr";RMfҞnqS݈U$8jEj9{I4A$AסZrXoƘ$FY.B/4*^3ׯJt*QMob࿑ $HdlmeF3\HiVyd w`WZE9Q o@0zR Esdgx~$wǽ5U6 '1qڱUVSE8VۡxCLYhxpH~|v);vOB9'U7fAQ=i6 &]9mn= zOet wrn 9x` fSp۩ߥ֑6; 7z28QKb~PHJP!v6$t⭭F0O,ǁYάV+b.vΐu⦂8RA1qT7#XSQ%2;sYβHJF9<jȟrOBqҚ_b+ԜsXrO]e]ư]J ֚nU09n g;TLY$2kx$Jv-$[C|wOF]u1Lu19{Z(FKmH89JBX|jTU\NP9-ށn=Bu0zsHl̎۸yie8j/۸yj{ԋ6UBF* ^Da^51҆K[= [hhؓ(F tb}8"Fy$zdL6́Ojz"Qܐe|=1Uifw.zPyd1%G:юfg]rBJzyjLo5$ڑpĒIZIr䍻F3Ut=Fj J0~&Ɂ=3ґNcv7dϯ;X1sԜTFM ?F[1I:wܴ@SB*@yc'<+ BQ$Ífk*(II7c \v>";Q54M`>YJB_h ^jWx/@* eB77soJBRJBsKBO̲ ⡖IQr})҂P!œfr$*GJ[tw܎%Eo!Hʟ,=jT5e΁-r͎N+Fk2NtBwx).J=ƭ7۹'Ҝ}qNp^qov sOتĕ!Ih*N;p5]ٖa3U|ҙ uBIVߗ#y$+a j)POC֋]i/{ǂ7rzTF K {U]:+%K2Rn3RIܹfsvA*99fW) .ks(ϴ|ֳvdL#nw5_o6WۻUlib*,HR+Zf H>~_j$(UcE yӤG`T*XX0wn.0Nx fبwU*:Fc yc?FGj S>jI6@ @ SA["#2c i@*GW"CzTQB092`8}*ѵ{YH 1+9t'k4bY>fOQ 1\iA(Iߚp~SV8y l/r:yN)d m;jJLyUFع[5'LW<\;SV#`pjG#OC`JhL}"AOm+^Y5*έY nQJs&c@LC 42ojYr{dw*[CHvZyؒ{zSS=Q~j͌Wdʫ(q=9'nj*Gheu8j=A1׽< `q{ąWp FqYH$&Aݻh=jcXzxt"+n;1To*˸x'8*);Q]_c:G{=G[8Ē?.;T(${FN8OΫWY`%݅oDBZݏiWnXs'W23Z1zOʟ4䃴N V5;Ҫ;l97颇-$9ۑ: "FچD@ZF^rQ[9RM-X3BƧBo<@ +D @)%F7ZF^ H[yqL Hր~ZcqeQR21˞GPwsڜG=jZ^rHG> B eu(yu/3GLj&k8n:vX9vLxrPK"'GO)>Ј"13+{VNdiw؛U11ֈZYq̮̅rB穪jNcLmo_Q$ۻe\Ua$ژ3`FG^-.=7)>R9z.Xo&KZ$^QD}wΠૄnϵP,dZ~Ոj`rdUS|Wv3pAd(z*Gr"*mąT!òtzZCuaӞ0Dn21ۚ{WddE90K!9Dɑ P cc Nr&m+߹v1W A#.o0h \ ^T*7Ք[DjLni6 qg4es Kd~j2E,JL`s TrO~:U7mCv)1=R\ƣ~y9>mLXbE$0 )Z &屝w[ِf@ =Eg*H0otEv奌ƙ$g syLPczw< I t c:O.c$anAs7rEFXg>A֒Aony#oQ0aUQ9)ٿYFV2?Dqv&Mb ?Zu< $9•5J[Xlxl} K nN{%aJvw-]\DF3J52>DYXc{GR'U IՈ&8O؏m 9rj'Il?:ԫN~`rc$~k۳9W%})+Z;w.M(&i`Uޚ/ٌ.>Ma%Y]܁U,o_j+ۊ2oc :2ǐO9GZYHA!$I$| \L#OF7hDr^vTzt^>lF1KDAz07nr),8Ɯ 1 ~}8UzNqfIVvݷJR8H>\HH yޏ9,p{Yv O5) UC:3 dd}YY %n@U۹N3O 7PޤϗwӃڠRA!q[3i" $ہ9fz_sCwI@ж3o[@`ҋ{sM6IwMǂX`d rJQ 89NAI?^+#bʧ"G&L( ň)sMYv\3l/\ h-Cj%t+/sDrmqqg9 o t4!p{{;V?ϧCړpU'gi g 7+OAw!p0zUw:g}x#vbVp{S[pc|Ğʂ{#L9-rX)Ĩa)*l=4%.YYHgOʣrpqqTXX q9'cidm$ٓ뎕"\nvTz#; 8#ژ:▏K7Bp+đ+(KgsQ w֢_G@}F.eNK˨ ~n7*~B ԰BeD=H6̒wW{H8$*pgq˪#75DEc,9ǠeqW?Z-M;X&2lߑ?$F̯'z樉KC$lN=W/6psVHK|F谟{ 9 >U[=)[#3loVОRf3R¥]]4L6ː cv9Ot#+ rQc!x8< R?ֳd\nzSḐE"3ǂ.f>tdLª(6$A;FORX۲},y ; ,<FZdKZs4=->ra&V@F2{V|Mx^` kW ki:!3ٍIӧKϙGF uc ӥr2lM>-Ll$B u.$`CgSPYēHeq}>Vo2YX\hI݊SKحKhn@Rs׭2)$wwwr5 u`9xemFW|2FU@Ҟ7>pN*7rӧ.1`vxtGA:И=2=k9*{ڜH\098+' _'8}iɤbJt4!`pr3چNOhKz1~f'@@O\ p˜g;ܭ)#g4`r!` !c)4,M{T,sOP027LU`z;rhعKGm 1Yȯ9_zHxUn{Ödwcerɰrr۠$GU *y76Gj~S[Q OS[&%~i1D }$}*DWNj44;w`N:1 ѳ5D{xY;#2#4cpV9f2*A2(=>TuI܇iǷ'AZ)cQLKu,Xs3 PCyp+E+uױ40C&`> ֲzҴ,2˰ɴ2g( i2$ F*owNǑjħ-˥1,Np*,5`$*EICsoS|ՉppE`)&jҰσ ۚ5Jz}CI!$ b1F͒I.T^EeQfEJ1O#?>X?Ql{E&;NI=kNjBth#M6H\]8%I=9TeߍpV.,J lTU Yl9 \m6 B]LOP;9-Z`k|ùKk<GB:DjœuR3UȤޯݐ~9ł3֮3rZљ ?z֍`.a#FR4lu+h汷 mT4%_0GYcG~sW"7qڦׯ-Wâֆ:%(rpkJv ިɝjWU;C? tmSUHNLzOCx~ N/k-ؗO[=qFR@`1#0 [sj4lfxu~OlE?5'{(f[{ +(6'9YoK$$(}hf2y@wXJ3m#(\{hSyu J-`f17@u&LPHڛ<"aOBrv]KmBJbpzgTYݕRT~i3D8@'t2fq[!dM˻n8皫{,WnN^hlp|U;ϚxAP/RJDDsk$89 fh,OsU.Lf'SiںcMۚN4QЙ~g*IJAC[iNI"L v\3|8>s~C +ɪ8Ęr~l֌IfBq<ECI<ێNk1h0Y"3`d󊸒쨑ƥz Tj#%h?%=9NG 3 >Jt*pxt,Kc PdYn$>ݹ["#H C0zMRm9}Tcvy[A3Vt(rwV@q`E d.p'څ;ibB0;@waN}Rи9,vc0Z'} 0vuJUl[ħ 5q4֕Hٰ ׭,CNU,O#隋)_7Z!ŒHp\ Ay394\z-.NOCQQM[YZ39sefڄ}A*ocV_Wti۴IDԋrijRAmI1Ɲ Qцd.L77qy]婟jCEL ?jPFLQcPJ;9P#BXcJڻ?pP`OJek^J [0d}9V!nN1Y/ S+XS%1-vRO{F@V@y겔Tg<Qzvї;ГiR΄)'$.ḑz$C=W銉sGRMCVDG`i(zR1TBvQH)A*#(}Ih ATIG4QEmm,FOcZ %J..#@84ֆk\ۤh9}[tV?ңcum$Tz((IJ5Z%K-Bf'Jʶ+ֲ;mL穦JW{jJ28u69LhXp:>Z=*/9LIO}WóYGnq\܊o|I=g;3f,y) %WGSIp_zշ5nbz q$0]sޤp`~9n\Fy("!{+[o#dٜOyumVb [ۭ@uo0?qp5¯o̬$qOHgtkb\Wd6'؛@c`Ɖ5f(^E`DbG=8@cyariEq v2=Ȱ UVzI= { Nk?PTp1j1{!vo$TLnz{$pp@zU koQ;T [Tb]IJKA3ʡAI~EN5Կu!>+8=Q42 @df=EvVWZn'|.Ӂ*Jvi7N3M=Ī@$GL h;ڸqѼۑWrYA¶l,eVMHw4ϐrGnu1QOvAM  Ɵd 3Iw}4K6F.NGfXbHn61cl/隯(1F0;[m%ǥ#Gc'qU\Ԟii ޝ;Ri6H(ʻqIq ګxX`c5t%Kp\ G>š,|Q$, >nTbyLv;wصvUosv_hы@mŵN-}>i7B"ވIЏCVwg{9>7t֮[ qh7)V#{2**{6K4{. VɎn;{xx Vr,VVLBģ eP(e01k:SIEsV I&8_zܣ$F,~]dv3(=3U$rD.GQiJLm18t#`󎵫z2II>eRIJU䌁ۥ$8\qKlD~_Sڸ O@p|br8JGz=M 5|n)s71M˕;#'=j^{ۉBHڜg ~[NsґE rA5.膒f.2 Q Q *9/Bv@dcO\;o-= @NuBѨW,I\z zRo-Af+5vY(JʢRo>Y߰䞾˝Ni3O:gY/5I.m 7DƯ2z ;^p 1^g2 ,̥',VnejS#MK[\-;O$$ޟ>qd!H_= @I뻅UTuC @ s ժMғ%Ԋ#LiQہ֙DCF~co̠hT9b@:IMrT2 !ATQ=Fj@` !ru }Yv׺vd+o'jђ!c4JQ+%pSJt$ [$gW4VH%xTܚSW4uRِHL0p$ kb28%NET7r;@ hJ_۲XҷRaoob(Jٸ 5UKPTwZ5j9*q5$1!IV 8?..}ѓpg+nee'# C/RX94:ˡb Ir>gTRwo.D7dS֮b;PHw1c97ry7yhW[ RDU#:ɚ9Z&xUD[:>_5sin4<9;?ZS/R#5$L(Vf?U!֗yE/QGH_QOַQՌPRҥi ox 1U0*z96ղ, w=(9# b! E.fL:bW ]WfyUIK|$lI'(jJVFZLIpźWG$].9-ax]ܒC |'=y$Csc8JILVn^Tp{hz}՜ݻc9a8sԱke06TG^TbT8IrY"sX]-~gOdǬ}I`C֤N$w(d8$U+XRe),^0Xsڶ<^j1 ڪ>P=8JTbNm)A}ߩ:7a֮"C4vr]͵Kwm}֛n\qt$1& OkF$9=+5K#Hԯu+YM@Bۑz/=4MYC 3\pSQS.6ol47DnI{SI1@gc"ܘ&}|={VZVӲRB2\MnҒ\qRIgguʰ,FG~zUE]X|B;G8'P[TacSMJk ZLŴKHovz<x^]C)] 5ݞ3`r9gW]2+]*fhvIQwɓ<9s&$m#cs{g9CK c *1 ODI0'AÒX9^]V jvi"5Plc_ g8RuINJ;D))W*\jxoWTdLs^6^Т,ɕ]*,FӭpMIy[;L@'XUﯖŖ,Z²L¶#Caw8A $%*3ry=)l--uxY Xx*rI5~0pnNn2@܍ȧ\]Nmm㔐YA *w46W&Y!0TZus<u[D'vGp2ՠ!=Ό n{B<77Ki*eIT .sFo%dpVF$p3hT^Ʋ) qk/cYj2L H8Ith qD|ך#~u&|6o]4V9]yX_}ˎ"d/ ?mǣA 4v/˒}NV/{?J8%wㄯU*緇uw7(a5ֿ-mXN+=|2l#hbViTBv-,wV T[49X dE'8ӵNR m"!`q7#9 !ҠmMhembigcbǯ*22]1q2ӎ.@}i7C[^b@sCqI1'a)/}R/7#16,)p[1je. bq^И>g&PW}dw!{\U{GC-qH313]Emfka4Jz3[Z e)6~û8SZoʟt˫\WHēL v+@ |nx^I=U@IĜ=;ZݓDc(s *r[DDR 8XhjMZ#\f@wwdWYŴ$( lBJ6XpOj_Mg 7{ [6[(L2goQ*No0+3,Tg=>є w.1ȬUlmXoӎrƜ[@/lj7.xMlG'--dɾ@F3<$znݷ΁-oB;] Cs&>YKSBcЧ^MOliZgu{kjǣ1zrMjJdѣr^bX. zkR񝴷Oss-ަʐ 0r:q٩MwU Ey^;fb`'E$oqi;K~V:.,7zVIp0 dqҏ&MGY(gӦ,f<&@U/ '3T:u._4#,oTAi54߻??=T8i%U13WV l.,meY EW@2Ay=UglZ崖AdC'z89#=+t 噤vipOSZFFI]: ay\|S5Z|ܷWKy^jn鍴 ?6<;#Bdpڙ̣>j _5$fFhIvgpGLI;&'Z"{)ԲatMH{ .cmX8qP2Ax;2…@=3Χq.}3`<#o_@jfOC =[ϮGimR-N/=qk4w?yAQZxWf~J=]?&J^)^s*TnNG=ymKXȼؗUex*8GiP rGa4\m֯7$X4}9TECHoYֱ($s\H,TN=EgY V4,lm 0ۀG˖sճP]\֞[c"9PG\Z*=jTmx{PL FI,~a;8ǯqU|Ak6EU.7OL?2\s"yp£d2Xr^j妡`e`2֣{9b9\lL=Jq<Xv);S#kKeեRxznQ#%@Z7ZLJ5]KW\Hlk1`׎yf}# wO `6@GԨE{мF%ӷ mZI [kGcJdu4_0Ј H ʪ 1>IѴĢ4r4gR2cǥX$-@ iM]lc*,䑫} 0e/P9+nZfu]:L g,eF>װ&Y>usܼb X*'sҩ$xV4zT8)M47aM:Cg_lj 7jTȯ4ItF',B}V'MmJZL沌?PZnb?1HDX*܁ں-JސuAZ {ˆ\~=?*MŽQsJ#I:o ǁ\v81z3Z^NNu KK=Ũ1ѼD fd<VLJm[U& .8<zz `V:Z7q\rTTeO;=WmB%AtK,U #=3R5tw]tJ)OC4j˺[饹v\v~b YgLB;{Jm=oq>#&hR(Fu'to/tF15_%$qg  rNvV0N|1Oc1E[k[hC&XU#+ \ܤ^brFc߇5Vt]>g3ƠVWN0?ZRav*#fGlsgpQ&]L$wXQ|shjVz~&C ij`n^-ܑ,[6sl񑸒@znFԡZ YӇsBGBzut"E!Xӯq_J烽G=HJ cH"K A!rZ.qi&p49'+>5+Ko1eb}ۿJƋzLw5UPmv:+(<7 W 3Vt;GAy 2E6wdT"ZomeO ]*N85ϫ5cP7*=8qwʭhAʙIo>2G10mˌ Sji_@ dlVg:k2I2YH#,u$zY}Ca>YFwPr9!ʭܰՅ*єT- 3Xk3Lv&>tq<[VV0G8!eRig:n+79K%$H$ҵK6ĉ=IPs]cSy!Uu1"q*ܬ{b  ҙ)%J]OOZ;ه+$f">3Vݠ*=HY-]Y8tҶ4?J/. 'w1H##\a'K?JqZ гLz6 A}*&v99*H#9'V&݃}*@Yj?jid'V}Aઞ9-Vx(ڳU:SX =1Cjw$MWn F9:RR-8i0)eampI,ަ[)VHiQ©Jd}6b:[z rJbBB fK+ue"@G4Nw1 nI C-`ɬ#rY `m=~^',Pt$–VF15j+cKe[cdC Ů1Pșݻƹ17Sv7),Hzm RFXЎk[ZɦVh Ⅳ<0UH ׎;yjm[Zwk6 s?yuRu`Gu,dy=="ծe м; {)b8q]Fۅ =x⺖"e mQRժcQ{๊y!UMaI-waX[^Ɔ[h.$?30E:N8c ǧjZU3jXH˹K FI @nÓjOˡţewèZphQsӊQ9(JGjq֤ZIt_76Iի2hivc֯l53ol쀼*b$n#w閶n.'yMu& MaNOSQ"G,D6ij!1>q򎣿ncSJxNa#mDy"fpHzfe%umLyn*Mw_|aP'{sYZ֙5@jv"0yR4Tqڻÿۺ^Y؍:JWTO<$dzc׊/l' ̬HA OT)ӊM߾;9JS#i$u`isK'0`/ #/ӎk!kM*3ܘOzx4]g?PIH$Gp,h UqߡXU2}o}rĚ\>>mrU5 S:%$Nv{ 67ڕwŬdR܏|o|.k cC(@1=@U.ZZu]Ѝ J֯ge3j^w̟}w\J$MJN=9'M|YKpXM-̑KaW$M] vDsZTLݿ)xjFq*ӲiW+RRo}վ$ܼ.>$=©`+j^i MFr7gW&okotE}g1uk}<׃N5# z 4⋨$̼[r?r,7N&87C(T{OcYڜWKfYwE1̘dt[n$Ӛ졅fWkLĸ$ݗg8Ri$+Wgqgd۾-;f8Q9Wwq!~mi-1م6=k~ƩYJy!2K:I0 yXoMJ0\XJZ FܠFq7:j8(wˤx5;6̲Grҍ{56V5mN}B.Wt6Vw ?,^r' X7 Gl.b4Q|l8]уZ(Լǽ-u 60@8r{ QvZY~b٢&XU2x¶}jm͜^-vv}#+ -J&4g<CuqoYYi<\C0 uvy*TzzZ.ZZ[l΀a-5MUEz4s ~U`>ap+˼=jmo\yA@[ v둌Waqqp:p& $rn 5--XU?}>: kKF&fpm`nAEjZ4 D ]anInɏԝjuӮlFRqu CyW]jJDq53r6'wO֪X^j54(U\񏯗׾H,"T FC~?CWXcq(hY$+HM>º]&i.Riy-Q\kB-Y`N5ש _xTR1]`\HX(]򜞜5 rk[9!V!=; Cc<8u~xIǦ2­ 6=0ys0[O1`~c\cW&֧X f3mԜ5j >|vIl˲Uu`3Cs XHWO5 ['dlnS:[U+ӑxBtmBLomVK˻uyd1cA9 o2Kah7,o ~a>=[utq*ګ/Jc)G4DM*1wXA[rg^]ė^Z{%tY6y[MfE1^I^"伒KXlXJH0dr99^3E- oLC̯0ӡKoH:Klszԩzn.`1` V'\waOo$ib>X~``}0?j;Ţ3J筥8)*uڱ:۞}<[|s=ǽ`3ۼ5Jg@p"D aʅ?^Vg$l2Hi ZʞTJއgXW C3 >?O| <xʙ1=;qTm-K;"$,y9-cmpn"n%#4M;ք>fޑ4:u>{Oœ ")lO{z+i֢]*[ 9Pct\Y;[{X.OP DW$iڔt9s;JT;x5wޡO/N\qA= d-UnTg֯m-: % 4 OD&dP>UdLt:qírvZT$2UX~OH#k8B\gj牬d,9Y(7 w=PJ\hkEOx<5c*\i-4r@0$qjI>Gr̲1 圜 y#+Eƕ]Km\JR;Tֻ|KںF $Cs,*| cӒ3\\ӡWoǥB=KerjQw:ĹQv ޮ}͠{mEK$95Fpsҹ#Z\kiwZ|W'O:ic Sg'åkZSN6f0Np~;W1uKFSȢZAq66p:6:dӴ˫I'۬\^MaLc ciVؙl%,>I>9ts|NԒXʛAq 4pUx9yVoTiWo-_mSxKe%F8].ضy?5K :~̰iHU)'=v漊%ω4CLHe }!3̅z. N*uxu-.(.[h@ Å# ڊ֩_u6= 1auEk{m&G–ڌ .#єw=U񇉼!"iSXe yjQ[Yʰc8~WkޘHxJV/elӾj׈Ȭl̸Ei xk{;7N/SA0#o"8d$Gז;Mk[& o XDM&O$|>ӈiS\r9QoMz[ˇQJ䏑@I=Hivě3y*7r9^Bv'<Zff%:R{ɀ^qQyV<9i=?0FphIu t*G F,>ANG< qJM+&;ж b IS8ٺg;vL^r3yFCV>˳}ppkO޿a8NbeNJ( }?'4\b95,R#ZJi_zA)\XMVGc?1'$qSwl` )`NO=iCںfKO0 9 5O=Z`$sS傽yM6̩n:PӪnoc# P>֥ϊ52Fņy㧵n=yѩ[L\qОMN"  >+;)VQeViMsh݋IdŊWjkn~ =+ֿ$m%RŠ^gA<֥ۚTȬ{S=gp,\qSֽ.%_W.$cIQ"zuQ$q$AH*3БҾ6t? x".n@fH?UB1$zj2-ţ*:60\a1HRY?yHJE{oo=ez#}3Mӆt䇞C!F;y :ӊ7 /Sgw\ˉGe*[smmsy!Y̅$0^jQՕto dʼ8r05ˬPE#cp*.S) i…N-9 əb-asp+b|x]-wh;;I08`d`N ik> Ӵm$r$N^ev@d>6`wc9tXiQ#Qk!Jv=:S說qwnz|uIgV׵@bE0B@gN0ztDӼ={(F.7RʩI2pP#^^EwI7{~,ZZnZ[]Hq!V#(޼RmYT.݇Uxw /f]oUԬ"HC2C1#8QT -Z'W[#e͝:߇JCm69TEgP-q^^ -t Ŗ)6YQYIFI Ҵy1j?1[[Go o;hFG\Z]ha])ԥmse&A<{HOz]8ITn7)A%--xBżIw]- ۻ rNFpx`bM<16'W̷ėd~XSh@wƯ+*`#Rx wR_4ƚ~߁B6GPzTpV̹haHY[?xUXoK1~Qqh$j2;&8dSi&Kwgs_q\ۑn^Gp<}jXuIgVGᔎgV{JNA3rOD۪fI8L&v $> ;M]1jڵm-SlJ\=Jz.7y{W,d1F*8dG+EYVTCY:)-E(O=}ҨiJ$ҹ&2Ǯ+u <^H皞Wg!@#9$5({X#$)$~1&CU'F0hV |SkBXH-7%r>[&~OLfFrvr+ ա}ҡ*_|@F-jj*_ O Xu缍Nٓ,jt*N}=k4_n_6q|tPM0?&X$Ao5K;i@2Bّ .2($rQPw/[Z?K=jQxڰE.x7`I޸;lhsj$Y-{s^'7$3bHnIcq[*1[ZyqDlq>= *ӟ,VO|NoG_ mAx܇pi9PpE^|K j XbdA$~"r UIT;RqЊjm؊~ozۊuc)Gqv-_jZD8Z|w \~9AhZIlR@NU@3Fڌ-.nURir=El}ZY>.& 3>w|jԚnIyuظA#ɖf'rNkCO-PR])skĒՓqr8'źkMnTCRSe,${ֱ Fq)ev?oi61떈y`G# .]NMlءپmz"m.ؖdϝ8 7˴ I~!@K$s5.Qt;iSQM3R{)CpTWl,",cǽW㸌IːHq9DY#[`3MQJd*r5:FaǑ8,~ E֤~)5wrN}|9ojb}PȺ,N V7G';WqZ3xFvv50ͅ.yQ3@8o;[9㛘/i.b'962l.Q&}X9}ϯjei&F,w끎8HP^q2[1GpŕW:{u8 6u{ΣٔגClC'&m}whYVIpq)8뚤5{V}{b| t WE[il4(-6'mܭ 9]>E"cA/oh[j:/DÖ9Q&m V,-%GYo2Kcj: f$nb%--.v &V?:< q*)֧Z<*aaSFmiWVLQgϿCP~}k$Ec`g+AiCHQ^88s)sN+/ڥr(n# Kߛ5FQi]uKxu|2wv :gA~aFwVnq$+ eĩ,ʣ'޳֤`kx.|J92o5mWWУӭ jَ[˂?v>r\#܁֓ JQZ3⪧IRK}}bˉe9oFռ)#uHf84%UNsG89w&Dn!bUH$cӚ|=,K=3{E O'y5QZ/f[$|L<+2Zlu uW 1$Ez}x~T6z~`7ddV-τ:+["]lR/or&iڴJoh>AEӨ *iB5~|>1xA_s_X$vNctP)-2=J5o^P0cקx':h|8Q|D X#D_(C ֹ+OH2x|X-N1LVũJToGC|ftڧ5wh1Omnr$\8?-*ki|3e4*i^1ʱuQZW\ kmhFyY.0vUq%r2`ݬHR6fPzuNb^KoZNNoZ_+t)5g]|IteY`6&U`by ֗>7Nj-5x59m-bԣиL 8@5gIm yBǸ^lf8Đ>rY3׭uԝg.C<]OAlsߍSWt5n^ RlEq^૗zOGcxP x>#OpvW[nU}&O. jy0Z0e "ZjK_ZFZNԮ~Pv2#ܞzW."%5e%$劉ݟׂzRyHT(Vш{Wiyֶv1-lUQ{M8'+̬Ԥv4ZI:+JUַydVG";.A#=q[8|^]xJzBVӤ8.P0P]Pp ֫jh}7ėsܨ>}wcd$wyҼ*Ehdr2ܼIDCWN~I}tf~pV,_Cj3Sab!F>Py~^ZŁ&r5cn%,hZ,ٕr p}ka knvpIh)(߹\],KyU?5ݾgs%՘aw$2;qg9'KWfYDDlSrrb^}kʮ&O?( ;̀ϵgjYm5KR)(8 EsC ){}:'z+3=2m/S2 ~B#{W7%t4+V ]%pB;t6öڔ7Qk<{c2 *MjxVbqϷn9^ x_NtbOn6ݹ#'/<<RC,Jww:|E(ҴDnܰ09Mdjio%6h. &p09;Y3+mtz]R++6odd$$se?A8[C 5 UMZ;Zc+FTϭ6oKt+G9;-2;hcJr'iP܏®j:\XyՊ-P\l~5R74׹3N:zy5ėfU+o`|:~-k꺖w0M8,9 xQc qk6喀Nꍥڰ<# f:ҫCvM㬓s{inx֥BS,8;=EakV^(OZ\)e5Ǫ茊di,^i+m)(3wD/Aڝm"*\wW-9U`E KvÖvj`RL?xsQu:RbpbF TE&'x2'j,&O^2F 99שK/m-%F!8z+2׼9%1}q`9҄#kr IEbZGZuFjmpW;:5弶FQAj~zd$/SjD?1Vwn$c cKO 0.&rĘgڹy ت GZpK} L=j3䝾G_&PEt2*>8S=.>g)Y{$+bHgڽΉo2Z9gxs3cn`jsijf}4!Tq02p98'':in\}i1y,E?6 ڊrPW].*dsE;+VnT6{`Δ6 (`;YWF+R֧h>yO9.kFr:dgV=:]և#n8*{W3slN7&={ZF`5oZfYyha <\i3Aj (YHXsWӺ/XkhoA$l%673ؚ̱RN[t5ʲU\Mm{5Pw[YZi/h>]bY32NOB3e"-w ]Hd1|g ҵue-#r? ^ltX]aȳM΀+2+1K1# ҰNrm45)sϴ[Լ=mXiV"M= LV PjWB2$c$&etLFy^k?~V^$!+wPFqs7?x4CGjfxn79s#=Uy _S {w]7S4]F->YzqU㘬yRCgP[#V./98#&|_jS٥ޫw)w,`a?*wvrL~uOJRJv<6mn[zYV+f-t8ϊDbD$d~zzP7"ܽڶkJKG"~pz σV:f= .eiRosf5 `6[u˖`ʧg|xo*\Xds|sZ:jounba2&kz֫r$O8V^AV8{ֳ/WS:7ZkmA 7tkug69g^^iw0i62韗`*G5WzmKE$KW/gRXi'K6h|B Frcr1ku enLC]$0ma`ϥKsm8%i- 9. 5іbgL0s5oֹխ} g7+ ͼǍKuz+%~t-7+nV+$ԟ`kKK،IS(p:[Oٻ[$L#aFJQnoꯇ[,4zM$ڒX^Bmx܂sۜ[^#.o,2v9!#Rc>VI=v;W1}-emMm5'ܦp1,Oc\=#PwI$f`28+8_jiMgԽ{v;M=}C¸b%7qډc} 5i=H%2dG'r3\~<#{Đq43Q,lGG ^x[ĺjZ&$ g $dzRQW&M\e 7Wo|`zO.t8$%++ zF;נxo\Fl4elIe;eRw9N\W5ˉ?4m:}ImRCI`vl磰 t#8==O1Y4gYedb#HW;rF3۩5xDu j$ȩnܔҸR6eMիRݒտ]gwemd0@IHLqΏg6Zʤ$a-KSz08q5Z6*2ݗnK /8HI+yuUF|;W3Pm2iCg3ikfK9$5$~<烌"Rv_yyPͣL8Ӝs[v:a]0x!qkmj- MNCF$QWv(*>OGNjg[to,w 4֠yyWܫ`*e_Kߢ3TA' GToo2[a40#eOޕ^J͊zdti9w@Û]oam0F N=y9Z]@P6q[VmTǭM n:kGU(Փ]k1f:Dy8ȿ#qWxUҤhxBz1^^ZZZlgrR/['޹g~]ᰂW%-i&e+?aiC][{Ϗ:R[ZߑVS&R˝sX"α|׎wTԦb[2r)c<==+ѥ6>}vwgqkдgWJ 0qon!ǂ޽BV) XJǸ_s=2q[Mo-zš"M \mPHAEEyzwzJ*IY=xZiw)y@4E@K.S$v {szM]@<!RLl1|BL\[RX zz?Wi.[yb "Fx*OV88F.8*w5Bx~g~=cʏ`?ߍv֬:UƁko{J#O^|33aj0\,g';+~^(IM{Pn SgE9+ɞi Fv=U4>ڏT`+#>ԼZKFxr_B|/5ͫYkea%FaFy|̖ X6>bqUIr;zܣMS|S?jB Z+Yߵ$g:!1:G9b?J? !/ Ҽ7:ȗJ}pq֗.[fC_U*̼N2>J8NzPG:zhUmd8֖ŕƵ}!nRCW#eƗ᛭J >8"hIᙣ >xuM}%ݙw翭tQEͶLBT$ tfhMb OKα6 4+sl ،ܧpQGS]=ZTR}/h++KlvP 7Gz}mX.师e+ؼ=o{5zM(b.psG[O-G#qc9b!&4q*5n/T|E~4pV1q]>G&1^GHI]fRFd`r+C'⹝Cͤ 6!y`fq>h8V8c(;W\ խM 8Qڟm4Z;粍% xw\$N\n 8NjsyfQ/.xzc*a'xI4ɥVN֫j'ѯ`ɤu0Fy#Qu&=},h< *)$Nr;{A.a# gO֭]LP 5)%vycR'9 Ҫkt[c+qaXnnmXr cz!uz?-Ԓ!UJkn9k}B[bݛ'<`>HW-+X{J+PFҭ2p?]OO>Үnm+mf%Mm6r@$V5Bkv\?S]Ɠi:t{[<IX101ּ=AcU9Rݭ2m%)VWw:~-?Ss?WHEOK('ƙ'pRbͭa?LGyk۲J@9澂kZ~2-J*(@-J7ψ66 I _igJNӹpѫjk]Oߏ%{k$ |Amb7??zZඛmu3 $;S#blvy]-#`Okɋ1Xp@A'K6hg"`.:Ҹ9VE,DaV-4T+iojYm$1.@8z:ˣi4mmp&[ ,N0yn ٦_0ڏnd@ns `:0ywLC팫 ps\ƫY6NHΠ RG MF^,)Jo;g's'KV(;C}^o Ţ>/bZ"z8 0x^$v:Em\Wxww:%ŽMLb6x+y} ʚc KG$ߡx C6ڵa!%E\ n95m{Ě"gMR\\<9D ##g?K-OFS{ӫ#}1Oyq/4!WoR  om}6R:?w빟n|esY ,( & IxCQ<* pG]֕'D[-Y]H\^Lݔr@'Zѽviŵ91\08$^*ؤ#ΩK (')߆+z64Yzmwk,6z-2Hfsdcg23Ժa1&ylb s 玔+z I$67b8lsz>ן恵t$ARzlkx{zu[V!A*z|3&[ ^z~\zylF|'OLVhikn$i *N 5EۘR)LqQ ]%7wjUmk AmjQ\=w[Qԃqk)[/th3Lj-`[oi`G:>3'pX b)?y Խ;tWu j,,Epɽd.2ĕ`lvCcqb9.#fFC8]8W˿kB4cݘ] GgݏHKmi}+GwLd(Fhԩ~Uzxwg9K84rur/9#'R-"g{6}GC=: [i#MP'ncPo YnNXE@O1tASOK~gu9 Ϡo )nV&o#d4LK ?.qzm<1LBWmp?!K⮛eIt4.I|>+x^mVXϳY o3w9E%o?T6I & OI#A#|dn JMwAѭ.Oɸbo,32:pK |K'"@4ٓ)"%vָe&iSvJw+ .pϮixjEII{= xt))܀T9|5Yj;mo㍜le*až<~^.𥝴ڋjWvZ|!70$u^_<Լ5\IZKeslH8FA@l1jVދfJ' өc<_e9񢍍9sjox[P.gszYwA0eP$Js,J% E{-ƗYK"-ϘxrQu⦝^xvB`>~8xѻlGxMSZKuF^f<`^_X($'Fcmtd}כxZ& ®_ᆱ7|G]ka:n'D޼ GJӾ0&|Gp6Z\N_w"׫9?xNcb+qW@oSWU{WJԥof! kmE# O#99F#ӽ*[LS`\*JI8k]ڷ%;W4nѶ2!)I/xoxl$usQ^E&=OXck,NNtyM("<@*ѣZ:>$T{5Ͳqr$ G+׼EdΨn$vX{9s\8ʊ諷]Uq:˟megYΑy#J O}q} Hb $z´NZRka-|3wu ^jz .:]TmH m m jۗkI'^%'$y^6@>VK[$i.OӚ>hwpð e` quyYP52#Ks\ǥDD `,̈bi&x~_:7+Lb%b$o?Üc_ 9=Z٭ IbFpxϘ볪JÞ$ Imq7*vF(Yڛ(ޒ_׫>`oz_e.緅wVJhޘ% qcbTnnu NN^[,I<`wtPҍhLF %NI}k>)vxbc'o^'U(?U%7e,thOn$j 88Wxc~ |!m|8\#B^lL%ZRVk)r?#o iZ ףe&8~6w 0t rkˣ|Sk,wϤܒxcz22xǑw8$$q BJ }х$(O1>{KiBD刮Fn:~uOZ>eVW=xbio4u7_i ̚}t,REzOvA$5o^GA,"N{ gPO ֚B7I# ǂx u'd'+:_? 4WM {hV^^H%)'vGj%|+<⽯:66Fypkr``|pz՘t p'k¸prkۣNmEťv-]ӑEr/I-.zOf{VK]$]q@ c<^Ӯ&t`͂;1Y@$g<[G8Uu"yMj? /Fh-n zU-F&{0VY%=c8:U_ ϶8GSlLn~$r1^+|+'k[Omavk9Aye]iڬ$C$1JŶ]Tkn_JhZ]Ygح5 5aѕ $x\ ߃#Z>Oܕg=Wh5 &E-/F!Oߕ67U˯n,qj Rh?m2qnK*mt y?h[Y: ኰ֗Q]ܿsܒzs^6}M5Ȥ#O-o#EVef[IlךN՚c\)X W-gZU֝Ѫaڷ5,ύl%jVyrƸm't渭S_-uf;U'HI\#]nw K8ls# c[:Kx3W״=;VM3l$P AdpF)>QYrBI򮇎Li8"W,&F2OMm_-d?]R]jPKM,78#n2qsoiMDKiɯXܼŴP[3X]u`ٸ j^i?S`J>8ϦjH Ld~ׯ 6.mƓfˍdݷ;ÅA~_xh EHs H 3S"S=~J]2?|'X6\%C͢'9Oξtķ[,DGcyHX*)c"vo>Ƴ,[J;[Ǟ˚d#)g% 200^y^e :p+KֱϨ$11bQaucJ篔.k//"Rן?tj3o{r[yxp1msBl;{%Ժx[2+\g <6 yOjs:w:Z7Wa׏ʽ x4캛Oo a=rvqد?cU96# Rm~oS wFm㴺ymBJ|_{]ky6j|NvI+yχNls\T3jѭhZ#^i0]NƀĎ~rNT1F5*+MqT}|~t/V_Au mbeWLϭze(ǫRoa]0]ʏjCۯICvra\rpy\cǭp:~ˮ68\tsCKWׯIԃC}U.5k<:#>8cn|2&^ir]ݴnHB ?2+ԣe]60S8#8WͺԖ4ծmⳙ4Ld̨NCmڽ Ѹ`Dx|Q{?3 }=亜ՙcwH$byېA$ɾKHf$I![+?ixj*,rCBiʎOkȼ ogsCsKusgĒV_uyɿ~M jq鋝 M%,$VO8ҹ2ɖ?A+G\5x^/oo4+V0351cֹoXIԊŠ[@ϴ*cu3<zQo}>^U/:v[\\b!%8'p$q|5[9.ϩ$s9n-xmr1s𖩦6j:}V׶6ycewbsI+Z0$`F߭ϝu?>>CAf4珘 1V&VCD1=Ǜu"]G@OGOenI挂l#sʾ:>owiټA9fy d WFڝEoCѭF.q>?5]pPW]6o,>#.؉%?7?qJ{;{#7S-s^G}{fRLKFHՊS gSpܧνHlL.ROݦ$\n[G~Lck_ED h>T c VQgv@sl׸~̟CkKtگě1iPr]Yk)y7ʚGIr$=IVu{4.o\{ၯ`PeOS%1\~>(\E4F%J*yV15rV0 IHН6^={_ xsZL^ K3d  }A 9N'A*'pW_QʥAt # ժQV|tN.ћ4Khgh W/s֯*W.5{;K쁎ss\凎PmGHӯ#xˆ|ۜ`Xu~UCYҮFtv9P1\({6擳ZpGCyqķ}hb w I =:b?EN4ll6@=ְ`eUhA7 ++~|#5E1ې˃ϭtQIs񵷊t}&p)Z`ÀpveE C{+.t__u-7m\HDe,pHuG9^Njpi%uC[¤iv & &O!}*kbB·Nsz.Uv }:˵LSr^#ak#&1yw,qʎuFu[KfYDL*#=;^5[ pҬr:zCp-N]VG$i$7FNOb{cJk++̿hpd cnJO\`{߳,7?/#_,cҾ-$szj-^aGތp{חI:V aVOw nkU5+7$2:݌{M 74c;(t&\1r+3} AuL)\e %_ZYi(%w_Y>a#_r7Em3m#&]T)~N}kkXx-?OcC$+} sfrİ#P-Ē$JJIf7JWj >kFezta6Ҥ8z WF]pF&UxR@Y/oly#}61&8;@I8X_W7pάBdʠ<u砷4#=Gmu?OhA(ϐ8R#qӚЍ:IKM57&76M[UqsgZ餴/mluF=1ҳ`RӮao,bK"Lrh#۸fK6gD们++=}ӵW(yeEH69gwa?N9*-:~/1¥Gm[hWL U2&ܮ;̣>ҒDģ?2O?/=A>!v֒i/%H9V,9LO[z>ib˫@OD ^sRW$U7l#wuo|6ls3ʑکsk$u{%^er8t^{o~$h$ +Vh,sFbɶCw3u4kK{{ҀsE8sʢ[D^#S~?b>i,La$I,^A<ҵoWQCpKHʲA8u;W>t-"?Mn$E UՎ!z7"ZOmmhtw u$en1\u0Ti׍Y۹R F6]Z_Vdi.0] 6+kxV[xTD&9&|$ g9[G§2mR G,~(&/KЌ HME]WGn3z]x5hMnbIlEVM@?62sY9_iOcu@И FF߻F|CRINWWm-n >[Y7n}{GPߤ,ѩS0}F;V~.ZKhfCNE,Onq^B] zuhR"ɪi^ZOqLw uڏƽZu$I<*,)H{T*1m~zRh[~z<~I,|[\D\.I $<(,2"T2 Pc~f^( HceZO(B6ߗc mGIGY'u<9Њ+൜m Mv\wEN}F{?8HWʌq2|3yuWVRwBTApC {uG@:^ u w_lyo$(-VȮ[4?Bt;-nM[e!\G*8⼪X|ӏ+t+b:{GGq-&O.`]`V9@7zQK>qv(}dtЅmFttm4 .$ǦF zV?[]GymY݇,RDž1=k.[3I{ǢR#5?jjn&HvOZm,;[GcqKj?39zX^.-׆umb8Z&$QlѨ'j iB\Ҧ7 %PQ|Zx|LpNKBn~gSH{ Di3XA%Lf/P[]Ve5뢻S`2+gtbJҬ졚LqqPI9g9?LWK{$V:w[i0qPI?=+/̡:ze',&<Yӭ5O.9R)#[)q=H{c{/O !t)1o*؝F1QYny뺖>-ӉU 7nkMӤocx/Ǹ8U8Wf7_\i5̚z#Z| wBо$xsTyM̷\1pqu4!]ҖXle Uy$SҷʨjJ"O[ݝ V#n1E/gqF>fP܂FOW]][l_\F=.:y,#}\są֬4-leJ% D݈r}& !ӭL)Ṑn>,UI%G*>hl7=Ǘb.#H) cG$`ӼOpIwiqI^3$eM14YK{lֶqHN;r'vSkv&WVY%PHWmQӕ%YES{=4iѭxJ؄mqʏyZ֕Y~ZhLQ^KhNUGFUH}+𽦠WgwW:%]E^2l nވ;]Tq4s[q($=@5BUY'W#WQ1v.|Uk(-mka 8$${Hw=k{5R S6ErzW3e[_emnP e <@#8~״]7VO,+Vt% ,a 2X&?'J1KEqouž~TP)20q WCh>/mТm5*[h䟗ۨn|Ik}fD!{UyYPp\⻽mW:ޙm>%]I9F"z tfQ)J _ևZ2kmO93nϪj,4|0{V?{KƄ@q'j鯼c"u%{S6{8%sY77NjrL#{(n?#ӄcʞJ5Jgq| t]FMNSy4F{KϽCz]/@Ggs"j-ǐ2: ^QuR$UI${>{k t׶VU+)=w% `\<{VUN"ƭ:^/4{K.Y8bP[UX9+H9>c;7`E%rbBV4`稭{mJV% 9PN_MmHo䲎3[ҥ8B4y|eV)^~g?zj')jE98ʮ~TC/5x[=QYVvĜf@}1_MAv/3]\Z^G?I{# _>#yo0s$> rG My8,5%aˈ'0Ex6 ɡXe|aZV#q=9?mbt5O8<k%T{:Xͦ㷘__o J EPe|]/0KdfsQ uA_2VoVՇhm>'{O}k/[^K5RZ >]ܐ xUIY؊z(W? |CԯKi٢o=$HV5ZJ(S~~M|V˿59}b)b佄?/,,}23m#s{r}㎆K KdVx<cK<9&-dEI"@7Um>aqҽmNrN7>4kQ g$i^rO^;Vky;ɲ0U(Ay;uԎ H):TzH>y'rq\8S枲}18b3?fu, {m 2yew sJ/fKO j 5Q" _27\`sT;HB.8-Lg ĥ }6!0Ox߈4kK5b"f2n_1_1 p@M._ ms\TQ|k#Ft #ZvGX1Bc'5_|~wtSzn%L&I͹H`ts~vcQa9q<R[[fY_xlH?LMSeMG:k6:PfMJ% !sӞxu'Cx朳<6PW$x5ދstd8W 9Ɵ2ڻ۴'i* s޴RbtcM)9sax+? ^x w=RɅhʆ>hV cڽGeWϕqk4l9RB>7|Ciu{â*yp1RDA# p84ˣ4jc;I.@뎽OyZ7o1Yz-V> #LC$61I 5UdR /;*P%vRM`.$>asN}R,ׇQG6 Ep'4GY\4i$FE&Xc ͐ݘ (tC\׵y 0 I"$?=x5A79U=c5S,c{O3YiEʭ#G^gC;znx+#:]͕FY[Xcw_S_hC+7F,jX['V5U(Jv:*n0/A]B{m;Jе!"Am?03^|[N _ϽXa{1h)/ FT5m8DF]! aa :SG<~˧>U:֙om Q[FX_fjԢNI716-αDH7GARʣ=tnY/[kwcDj;.2ǯWܗ?agm/[I]C|Rm8f"wrÝִ*BvTJۯʭ_/'K[yw0Uzu~|@swe!Մj\+y⣼.Mk75ڰa ĹTٷO~+hby`,q zOcYj{:3&b0xgKyk/]h~ՄZƘC Y ݃cr <IJ'-٣VEhmD ^ 峊R`>6ID5yp]A9Qh5}+[ϘAxNsݑr KT> }zt9tCIp?@G1c9ͼ桍t\2t^3\~"3iF$MPdbH +uf=Xc':& K#'=< et}wmٷ^GvQ!LVφxAŒ"ŵ l]_^+*byRI'(QS\iXuH@ӕmy R$ 2͖@sG#z? kxIYD1L$Hlr36ּjR,3I1TDV8 ֻpU)Sݿ7gRrMFn|+e$pi1;!RA~jYL")wp H=N*tkYAHd`?y*3Ԟ=Mf`]&{Ac31|P@=n+pS2ġWv`g& 7ĺD556ȢfH#0NI5zN]]K8H[[[D9~{=<ɟ60ʈp=:xUSr_͡#;v;?|Ovrkַ[mij 5>m܅ڣ'?uڮgecjMCͼ[]À[SXْIc},D@ s_NPJR9=fm>)e<0sՍ?[\c dOM.D$QlHjƙ:]8I W1J׳[zxjѥ>+拧|9ֵ=DvDp̈t7)vH9oammkZ"TONšM\j:n"ri=9Gym, +H-/D0<9;W,57QE˩ׁ}{SE]N;̵$):yh,z=+ <#2eDP:t} SK{|0/];ztf'tGcaIaPO1tNGe?UhEwMHХ-'Y=sk]ZA1\9\ֹfީ )r=͒P1ۏlUm7\-o/o c ӥz bœe-;XŬw:uן$uΠ2r Ť5V;:ޭ\ZGcY?c~ T/zU{֟/txcQE* ٜ=G^?cjPݤ2Oi`y A]gN[bԬ^c={m]}Sq%ژ > 8pڤu_5j:]HmC wb8}]8L50۟,d)Ɍ5= j异=.GXesqWQ9t_֧m)wuX> ZIs \] P!  2J$W7]"M>Q%^K+Us H0W7]rQa&du86yCA_w[>&ԭ.fUDgvVNPdpI95Û꺩KS˱IEQ Αo.7I!k891lnx~)>Wn=6+;i-$FH;8enX}u{ӛK|?M{eDO.=' 5߂k &":ijwvbke7) z_|g/l.&1wq’ 3a:R`4n1'VƬrA]j!~a2Nm#ϊ}sRHֱ^nȒP92I9#!xƺޑNMGFx<3'cp WLFSM-<ᦎא酒3ZPx+c1tJ6ʲpj!>=prkY ZFj֋#fo"v$xZU,K4bP0TyѼ#\Pqb,z zitor8o0Q޹I ]kº٘Sӝw<#gr G}K< ;{}7јc>Xo,sO*o^ޤ֜o?Cχ\s2/-ՠx%2*s/_~hzͪ0*ja(9wT}G4]K\y`FI{zjRpI{G"qqy0#$۲)֯%$w9W2?NO sV]i5ocI@e1yM@V1XV%ү4&ӼIYXjVo"~*ĨGk};FGtm$n#H2"8 lͫZxG(eFäsd=4ך1X 6r4wZO~,A]F_< ^&,+sj> 2x~]:`B 16F}z׶bk^;X6(-Nc2;Y7vWwu}cn =i]eo%xR-ŰCp#TXo$6@0sAj4/xMЮs⏅1.$2!8S>pr+n@6i=Q#\ZevO5W 3J")u>k~1Ե]NR%;qS[{(A$Elt ~"ܬv6sOI&;[D!=2~x[T4vO"7AF#*Ê:K NiX|2Ikȍ ]YUu3XPC x#Ɨ]tm^H \hQnjzu t4> /iun;;+|pi[w+oj-k l?ykOZxM9ʓF~W7Rn6_uXnXeVfbtJa1< 8$WU5=CVOX ,*:4K(Gu9O#Z^:O|;q|/bqY'Ml7Os^|TDouCZC,ڄw b *+^1SUU:xZu.켧!,pJ8;O + лFІBT9N1!o* xSRǷλ9в8nGҹk[b+HB[<{ֵs ԥV*ʎj8A[ϛ9'> ,+|-1nu-*E+RV[_3'Epڶ66\-lȟ6z|zVTn1OI'(ZE ]+RBTq9ZwH,4i}/D'~$#n6zE(?2kC\Mo=/\(s=uRF/4mRF-A$ S-"Oo%mePsrzzԴ[ȡcVCOj{jf!'W-^}aRH;= #RA96a %qiqF uד~ gGG"ţfY^e"'IXvzΏ=ťpڝ4qJO#9k8v/OTcڒ^"-&@?ʺD|iouma$rms^(/|;7 ]Tc' x$}+l%uFx3NϮ+Zԩ-F=㞕e5)~Ek?/ɯ=rlQ!Mns#1|M-BY+Imǘ])R3ݥde nF˟z1ּUF0QY{:>\iYn .<|r x.-*( ZKFnH$> nx*wWOoq~ͨۥw#)աMQ[oGB1*/^v0,gꦥ߹V\ǣ|꺄Z׈klkhe2Ic ĺ7SZKy5?K˄fqRc*K?͎w~Q1&L2.A8-"cm4e܅H8=9Ӛiϱ%mߧs]5?ZwcAW&Je;c$2'{m GV?'=ɭDžM}nHmaH"Xrʣ#ׯn+6k`V<GcRSٿ:񳒜`VW:4E7BUđڪ}坣;Nw mՖǤF֙wf^ ;Oy,r[euCqWߜ+zPy/M*$ 0dn;TQcK03O]?ʰxZ1yw+qӌXI4 4LwTF>=? f.%{fG_ Eirl\|mZp}ʓckB İ!ss^>,,nͤ3FW"1Ak¤u?#zkw-p^(k:~UT^OOs>W5WMR,oaY?rǪL9|Cu"$!o)V:5xa%ل-:8W5'Wzlz.m^YC@CcnqİuV9]{6OokjR$WhwyOZKu20^ 9nE+8]1g^9⸋{}`ޢ:(ZEH0B|XT*sRbx+kמ}ܷ}G*F1{WG_w̶eh{<\ŕT5ʬGw۟O AAD A |y?DgWͨ;yl2 ~bAޱ_Yn̆j`G'{҆e 0z~%K.U+MC/,R4?/$/.Xg Kݽ/$EH}2p{*Y%|SXc%`V Sc(N2AzV5,te_xS ṭVew ǒ\zU-I!'Vnڥv׎[Eo\Ck 8@QMtIi*-<*&D#wB%uUOth qaWkgI@SI 2:Լ?xڈ/,,Yݮb +H8 8[ǃ]Q(̲0UbrGڸkQQGv=<=+P+tzJ҆qfoYOVCwY77Gӯ4w>ZBAp;@Ojl7 <Bp 3T|Us=BANC'GC^hv^Oe"9 6˜1SҴ-OįԽceۅ,[ 77OF@5wJwGR(W$r2Lӿl}+L(tm 2F_20F7%$NaNU"Qh>TU#L$䬬Gq}k'FκHTQvs팏ƽE|Ag!kKFZC_ *|Vt0i m\ e<S7g-uN2Oٺi53P <9(4ocY@E"5w<бL|״XXXG׭4{81~@*Ԧ+KY}|;M{i|#6/vg8}ai+x־UզUא?R=Bz81(8j~ܞ.Qlt-:h~gNj{ZWF*nt7 vuh,Eʹ2O6;33{_~OjrS]s[KnR%B/k4sM ]O1,U_ks<LnzfI${@=3֢ʒSw ⭄qJv<'cuotg zw{ʾ%0Aon>)zd-ZDC7 s޺/-֡% ifN{+wqۺGZkNcmM<*[Av.nR8F͸9zG uoY8\%֯e=ϙ!s!Pg Yeu28R7V;#BRn7j:,<c+mNiq,At yrI?J.D7,XՔoCWF4,i62j7?¨4{B'Dh<^yIjm .k&0F]Ȼsz߉5;{ޑ GB>EjFnΈ8(5%ۺiˤii*HH]jO,<+"8*֮RJOԧi4zn=23k i1FҮm[iXI쎎ܓANssyuqI"GzKpW2z waKϯ]ʧEUj~i.ť*+y'}(ᆺ~VmncY4^\xBwu{ջm[ƆV6\w HpOPHy="_\DQ`y.R?:[}w*ynF+h0W-JqUݥ/o-./PI'9鎕-S]}KaoD+|ڭ85 Ѣy.X Io (#I/O jV95,0 w}qZ& M#Zjk(AYuW6zmƔR9+(Rb'rៈ>3Z]*Д*Rg5xygřixi> l$Ce #Rj?M=?pZ45XnEA!I9g8`pxRg5o`^D #U v:}힡a*G6 lI^馓:rY˻t/4a[qg`u'ڵT#ekrСAfK8E ͝_o6HֽJI`o*_ ɃqcGgӭd&E+޵ω^!-[<-$%02reL:##85bWiK<' XS_?.V8JN1Wu/ Z64gח*xPJJ㚐xMkj+>RiJm#<Uռ;ZR~QkWدϚʅc{TTNZrmo}O}0$¶v0q*:8lv_m}bUXqpcV?x%^TIZpO?PUjҭyWq4 *-|HQh̹ߴ/g%m6wFyc:H'K}a_G?Pӕo`b Ao#׆[~*=JZI1=wGTo|^me{G'-cgRתnƺ߄4mB{;ȾvrwF00ǟ?HS{F{yHbpw8l)[.U5-N7[X8Ӣv󞣭q?gVY/kZ^]ԏ.cTBd‚\n?Z5_-oC)ћ)2|Ke~nAncgXw'qq&F+K( {ץ^9a)c^"passYj,{\"!w#)zWT+S#r۹b0\߱%ǂ/RdԠ'#svk/u.oo*y,Ut]ψ Τ —^A=G3 )k}>$-,b˔۞q֣ AJSOs%+(g+Ե=/R,t NV66L* zWB.{kInBBK{^cO3I\JX-v׎[w%]8hF|Ն%7r t [V>Xe"I'V@@p#+}OpZG_o^<'rxun{qx#ڹ\MÅ޽DcRQ䔠icў9 ciZVI3*1遵x  16mf8Qֽ3M7:Nihc,˔>#ꑢQ^zXjR?ݽ}KxQ[kQ q7npzIl|jYҥE *LpRw^q""/:D8RppӻǸ֡_+#3#n;# Up1T,?1J||@v{ux;NѮ'i@lc`s i+&m,Q2G,p:ݫU{LZC3 QpI$sZmCLbp;o@1wi[ SٵJ˪:i\o?á WLmg7 6W  uZׅ5**mP6z:ݴ1.7W%їIx5jVHnI' 9 j7v$mFI?(*SNWU[n&.1íu9(tYu)PGzsہZv팸B$X+>qiD8wBi(-/mʻ6!sޫ~rYv8(F/.ڦeS4.K{*A7k cb[ wMbPqfx]OT iUΤ0vGʋHҹ]C:lg<7ϩ*S喯n9I&=JGir*ưɿ!J9Է&.n+xb^80cݛB{Ui/sm>ܛ`bVہޭ\k^-ً߻y5 mH5e/*XmncckB,#pXn9=+n3UΫo``k>[bk/-Ge,`0 S$x/eXf!qQCʬW5TlGjYKl,O sN_Ïi_hV["J  b~^5ܖnag(+9;[ֽP4e'(+1l:p˩\ET12M>NmE̋ Ig2JHPXzھ?k:]ƨV)ɓ,TXArK>,LO1hwFޘH=+EsEt:f+(E]Dд?6/5`fNr<ZuMZZI*W7RZͨ] "yjH6*k#I]Bhu[cum9 n(ieic΄,4 ٞ3߃\fV)f P/'q)/yz <$=ղ'YCg;j^Ԯ乒Cnܺ?0'crkѾxX_ٙ,|Α~88/U,\ՙBzs׭10;;\w5 &ox@Ҥo2ib8I#64QQ;(ԤiR_=?J҆)/L6?'mm&u'c1;toNFY$BbBCs^6=>ܫ=jj>ҤuOJ0is܉/f H=q֌WjLxbgAqMY沞Reh؃SYthDy%. 2Ih;D'N4Ť{N AX>.X&;28k]_L6μNg]גDdw98̗. >C+ 4* }r{. -a(mgED5[wNtuO$H<\ o?6ҽOfraZooik\N/~FSy^5ƨn&/" _{a]k4/#%Geˏ5T(\tW݋ޝ=Lo,.eA A!U|̕pɯ^VkrNMB"0:8|O,1z4>-[;v.m*@¹1'/_RTJ7~Y캟]5&|.%em& -UÝYfsA=[qACmqpd7xJtQ|v2]}ƶu?-f猢(,8 9ϯOƽ =ybe|uGgF76sFs}3j^jt m0DPՊg'|Z6mʷ :b TcpyyShx܀r]uƫpmNU`]vi7ḹ{Z-fn@F^<3,K"a35K}cH\>T*<̖.ɗ+LUHR$%vߙ ޽]PIkk6pvRQА@ֹC5I:5w?x>/Kaf Vdr1<ǩbcBe>_CT<; +XFBc, : k;-f uuP&1_t#tjmill^"1[HJGNr=T▬劳zL6Ţ*,"igErΛHʿliWMtC4@E-#9C5>fmmU'<䓒_t%{tOkJUpʍӭyp GM*TE=m۷[֤̳:mS6U܃H܌rzr<=|eG.4*bEdm(?;rmp1x;R|)qCpj6%q 3ڟ/E鯣,VKBe2:7 OZ:pSn  {*?k^-,{yb_6:xKW&4#֖B뎔uoxu,ÆI/]ëC%+rT#- ȭ+?bKW^4P=d(@$erB5QPV{pW[I}ޝT5]GIZ,e]:`lJSxf pnA(Â95o0IK\j0^Y<2O9hX+F8f_W[Umk ȪҎѿLj,-H-cXXm;qQxZms0P$ؐ\qETќw:4:k?LKˁ6w-P #;1{&8-ܖ{#(YxPXn`=+TwxbSw$6>йq*?BA4!ې|8WLkD.[6m T,//cĪNY\)^#T[:M91*ĥ9`AҊ+{H4{X* II+]-SNTKcI?w4QQ;S6稹Aյ3DcO6f-ϠǨ^\-;ފ+ͯ^q;=:H%UTL85kډDҀK$28QEJ$GEcNIyS0yke1篯#5xbHbeP@}sx侹!rS{n$2L5c;-G@9b{EzF+K!pogH䓓8hS%AkY@巒WHp֭ś ۿdqƊ+ўT]B[Qœ֔3j`h9'Q^V"jK4qI3D&ڶOBdSE?ach0QVFܲDi?ٷm^>,I≥乕p R'W2q YB񠶎]Uj dVf^(𑴕%FRTʿn>2(6cV*ιox" -GԤd&EjQEF*TMjT*vj*t9h؟NxWXM^,Z6fV?'\ (TKv4iT|mI%URpJ`p}^mj1;ZeȐ< '("ϢIѣFW涭rfQMr̸e p}?LkKhpbr@N88"L E9 f/AO<|gKQEZM4xkǎֿ,P7qqZdĊvJDžVԮ5cXd1r?x`p=8h1XЩ P %:k ax{\j71i=΢NT988U|eXcm!xpFYOS$|_Ɗ**Tt^E}e9m'ц,U\#ߌUik̚|;a!9 +שkS1Rљ6BeZ" ܃=L?ζ>gn;vPf'"σKπy"0GE61]0` +G U*f!V8vmNvctT1ʮݻi!ǝQ+$g'{ICC#sY(@E)Xj%*+*([^ nAnCC شMd4H ݲq! Ҍ@Oy }lQ "Ir^9`d"Z_b^{&H mti5H3ҋsW+ |{CM~uHBx2U89̨ƟS+J M0  BEPaQ(TX  BEPaQ(TX - *DoMAT'`y=A` Re KEMJA1_J]!BE¢PaQ(TX*, BE¢{"{ (B 2\4LYLDRFEQ&c&47ц1 BEPaQ(TX  BEPaQ(TX=z⑋(!-*%N)T'jB;28.A@]! BEBE¢PaQ(TX*, ENQŢPaQ(*,  B¢PaQ(*,  B¢PaQ(*,  B¢PaQfjTϒ,8 GŎsPdRSaEC »]nt8"(܃t] }}nxxJ;;ZXnq5%:^G4xWqlKUE~*,Eaic t1JΨmmp\j4:5ePTr3$ N u4 9fTߘ:7.DH8[YI+ hY}q!а(31?SN!.JB*eψ<%KFEEy' سw?N֝zSG0mTpo[Xjj*~/OQv;~||:+˲bV'U;pcVQlbP x\zܱGg*3hYu]l~|wOcܫ"$c~TTC8t=GY]~YzɱD}{Ʉ%.w\j2Q5o.:-zt %m_Gmc{a0,3((wG9;.v^owSv&5 hsr/CFDQf-ػgLQzl+ӧ!3S Ak[{7xQ82 *"Ty|֪HTyގu{za*ϥ KT4A/q.Q]\Qc(v܅%TPTh4|iCZغulܴz>Xa?HԂj?|>M,z%WL( hq0>0Eà̬/} KNVơ^츆,c ;( iՇ建qbƙa٨VE-K1 A ǻe"Ms4 dj\\(~R`vJ˄Ԍk*-+]sd7yZ2x`!<ȚSQQ>&Ыt`t.]lb - x c;c{{1`MK+˳?9c0Q$tgLX+\owRrK]o+G{&M&yl(5bSIǖ{qbЋvGzHnVIuC>@rm+kA}VD,ZY͝8ywƬU%nZry{ϣ#hkkGVVf\eNJKV#>A`M6v^2Ԭ|bZ Cx~D--hĂdAGGl豾n[yv]r,337\;NɱR' q^,λ;ݰ#&.!$)ˆF!ݳxFz:Á޾(,**9C3(zK-Kst0%gd\SW3&rsL$15 %ѽ1 BY,G m\xI: hP^^sC6*[bт#Ixfe۝nRq*dff$d.KbQ180h3!-=Mڰ//(J෦fA"QvwJ]6ːMKLtEɻ9J-bǺ=8kELpq a+( yO`/9d;(' XptTa1 M׳*T~<prxȵjph@iSa0&<<3hq'pG. KdNj Jj6;0?Cgx 5nO+dY&(CX6!_x` Q_98+*hpU ‚KPf]N ~ ;;\ &qG`HEQQXsS <O4Cɱ)>}j-(o&@\@@x؈,C4ǐOڽxlo(X\(1-L4QXMhAfΜ!oZpH0 I%E㚩Wd Mz 0YhFZߜe׫x(6_rމ`Ԕ KU­ KǁAii19*ɼ>LX$̽kNR]I\Y3n ǚ!n0Pf—Ja{p 6 a\,%0az`"?`SUTm ;[: +qݸ)M())Nի^k qT(!Ң~tl]iAU:¨Sďก18N? 4S+ ԠXgy^INr- pѢIx%&N6ʲ zokX+LuC^4$'!aA [675݆ɤ8iYS@6;yj] `wjRԲ~T CY-g}(KHX aS1,"7ǢXXfY6:i|VTt<ݤ a;'Myⴰ4S't)8{|^,pˍӧ(8qh277g;S$s@v;r]ZFUeE܁cdQ".ܬƣaqxޞwԼGJh p($zhnjQt'NJX`r'@.ɱ,d[SԲ hs!XfDZ]ŷ]<P;KɁ9~y^ZiT30O<'l{7*Ë;ddzdS5JJ~L^-mn*hوg>xNJV|OڱN\:+=;pus6ܳ ;ԏךFGV׋3a TL8X-/*uĮpjj*xoaϞX| @(him{fGlTfbFFFNXc``P+..J9zgjdtz=хk2ag02Xjta>5lpk1KQa }8eck^m#j5fWB{{ǘ/ܜ_JPjKI/}[Y oMzGnpE<<Ǘ`}bTX"ssVGkQt?nn!n窪 Y=< KBWW7&#,!fC{e~W))z,]$){:Qnaaĉ,DEq˗/ g '! k`}˓[XX@ fnZ55 DFšb,['J+ G`0s @D~e_6N!?#OqIENDB`pytest-4.6.9/doc/en/img/theuni.png0000664000372000037200000007536413604173400017666 0ustar travistravis00000000000000PNG  IHDR,sRGBbKGD pHYs  tIME : ax) IDATxwU?nݖ-P&6,>(EA#XQR$B-IHM6z)$$a%+$ٻwf3|9sfc,˲-eYeCȲ,˲!dYeY6,˲,BeYз|rS/^šL} Z|4TB!Q$cАbT*K1LHEZ0 6el.J2L&ሪXT*B740)__ 30H>VH)Ig &6kaP-˲v*r=ˍR!wd``@ Th}o8*JZDŽhc("I8R*%dZu8f2g@Ƙx^ XJyZ Hoo… a9>/iYogZ+帜q0pUJ $cTR)"83J1dXrJ\6L:r#IVrYD8edid0X.A*͘8h5Y۵H˲]'3s ChF)"( 8rZ )քP1!!HV֪0岙:)aVk(ju8PR$e 3Np]ω*ZsFeY(!! =YҲ,k ^r8#EQJIb:I(9 D"B(8IPqXRJ|?Nqv\'r81A>R)XD!&!HDzܕN8I|&+0<Z)!yϷ!dYPa(1(Vb  GaTB$d1Z()j1BbQQ"0LVCr-`L!L4QIZ Zs4I$`D G"ѵZR/_cweY֮BQTSR 0hUw1v1w88C1T)j-RHXS}C:!4ža4  [X֪rQHIRF F0nho0Z"¸\. jݑeYZi)!(M0%@H!``4njf !@T6RXW400Vʽ FԡXĜHJ%Jk3488Rr% 0#ݑeYZa Ō` J PR 8\JEBq] B,8cmal& 'dh` KSB8cZU(%TJ!0`L8~}רfĜ@ʤR- H%1ƜK,˲vrP1FaX&5$JJRR)I0E!19Bp]."q*d"Ҿ9.!B! qi0l<GQUQJK rdvGZej!e!0RRXk5` F L'a&˜c-QTz XFZ*ʉa4 Q%BJ(%a 3#5Z$NSd1c0!:bJichY˅L+GqZ& JRD($c28J)$JH0g"<!DZa`F(# j5 u, b9I-A0JIAI@b8G͍BIFsBɲkO=n?eۮeY;&!ZX,JZ-I `B6JjF8!RQ%Q 08p8A02Z 0F0~:g298s0`#36qQ8T)72FG~Vw9vtŧv[e;Jb֢Z a0( ZcCZk!D)B]m(sq<ϋx1scRmwOX$%I$1%Q8JP #Z.!QQFpld4 蓾C8hoS/,zׅP\NH&aQ@ZiB1B*eat'HkdLw]?6Zkm(`w\?H9K0J+MTJ*'SJ0a5`%4R*dJq<)߶m,kWDH1p!c!R6e3s{hZn ~jB`!8N:!M2BF% .I"M2T)0 L`ʸN|.KP! "ZýO;ӟ<F ^׻{pGzcsq_߸_7@/]<̟ԳOO⮵0 _]{| [7ėoeٞB*1`1cbPcC,I+! B Ä3jg(Fl.i(RFiu]i*Vq-U0* CA6W7X~ZƎ7mmJ4\.Ňt.ͯPqeэ9+Nj).PԪozQ93F9}=t ocu~c /xKŋe{B0c2L,b@q!\Fry; ql6 u -m\>7b87DQm@I%PJ" q?pSF(ܥUGB60}I }z:vH-;q cV ti3k=F3'Χ 鷱̣{E8=iKٛYe{B:;w\F) F@-)-1x Jkl|L3*()df0J1BFiN2ѲbX,J-Ҁ\?@oklhUԔof㹱'Mh|*`oZUZ_yվW4nG5y`7lD˲w{suQP8VDIB1ŒBaJ:빜q;L ҈PDHJUkaP(j(F]I(Kau87_kQH)KN,asJqשklD$AAcztӹ@u?rPezsk IHQ":|!qRIxq8 \ l6Qו AH(&I5)kqb4V01& ]k šSyrXaBRaʙr'$Ӛ|UiƁEt?eYP*QR8R@JMN@(XERH9N*+ܒǎc1#u$0$Nuù뺩 s~'B jah!g2*$FZT! p]%k%O=lu ʹçy&]guu)HP̚DGs{Yemw'c ZK1Fi0!hc"~PsÙޖfeRR9I\Yvݺu*Z"donjԘҩtJ5vYz e,˧RPPsNr|sLHƒw=?4iDw~@Cyo{x;~Wnh~)3cP$˵JwφJdYrb0BҠ `8˲].VUT&=|}SCC}}è斁00sP0h!FVJbooϪU) qweR_߻Ai9õj(u8FF1`Av,˲v/'Vm QLf\}"L*B12qT3JJ@ $0P8A*0~w- €TR#.Wr)22hc'&Xer!sCRQQBTXkcZZh堵È8Ij)c@k51ZۻvmWR>!RJ )W*QJqkM!DSKb0S3J qeY^+B:F2R A}C4H3{ߛ4a>f)2`2Ri"@ah@eF5)qJA3)0D6ci?F-c>؂Ǟhi9z=er=)~9=hN=nzn0%P[>=f|G~ooi=zM˲!]&rSĉ =F)JIhL"D&HM 挘xX\}!SI*D,JRFdYwV|׭&p*^Y4wmo;up4勓XچeY[!O2V_FaQ%FqABĘrui%E,8Igcwv^ c J5 IDAT!B:{aJu.vu.^q9 |3&E%6;i.U i׾{# V/]g]TRJF] Wi}4@ pzƚX)ČŦSCeYoBF&J8Vn*?гnekV* ZǍG0 #Puu$P!$BaH)\a #D1=ʋ<]x%PZ jm0=@(qP9# 1c%A@(XkscZk%kiqQGIʼnc$bhp4T 3DEHZc%PwφO8v¸n-Ndњ~EZJtS4eZk=1Dǚ:;yٗ)@S&|{07#>: sc>²,_BPO'[K\)ͤJRZ+dA(sG90ccRzE*IPعsm׺rG HSL(#5B&` hOYnW-Y(mSp0FxxJ5J m`V -mʄٛ}Zez!@9y± }sN|S.:aF(hF!01 Qh7]Ã1R !DUbB(3( 1`0Fxxn1\-^)Na9,)ږeYSa[˲,ˆeYeCȲ,˲lYeY6,˲,ˆeYeCȲ,˲lYeY6,˲,ˆeYeCȲ,˲lYeY6,˲,ˆeYeCȲ,˲lYeY6,˲,BeYeCȲ,˲!dYeY6,˲,BeYeCȲ,˲!dYeYݥƔҟu+k O\Zꚁ`7r6 ڊ~u-C|EE"ߵ薮WCoO?d wOw V&~ߝ1zG6#o7}<fSWzGGm/>#5e:[nol8tIV?~iW}gPXzOy;f/H쥧zzct梼64˯Nn:8λq鋿ϸJ{'z>w;\S9F->}ֳ>u |싧 ?kCĝࡍk!nc[:Ȟg3/gN0i}9Ky%SZ g҉ghδ'lQ}s8O/.{ ݒ2~c[wX4oZPR-)J]]}qSK󪷃v+m !f߽wocrwO}էOrnꮛ^v5C#nsߵ,_[3OmX 렷wl{_yi8ӑ6dTo훶A !PJ"+}|NCWwR@RA>O퀏G\꿠;HavTNYsNZjcb>_t4.:~kk/2s6M]~{l.E䆋{W;M:Ļ_[2}[>܂nwnji]]m7 Wf-c>o|xڳu_k-g|Q㮇nـu{u/o@~wg;5ah:_YUoyWӵ 5Y/܇OA&iS YŅ}> (ϟtבG 33s7,xQ{>{X z5Ap/C^x⅏?߷$4tG/'Eۛ}}EUJ`> owW5x1LތI GPpEiz҄Xiw?GZnSw/J\}aKg޿+m?u_wXp]wl]C}y~[;u;?{?<宒i!'| g"ЃOko]m7wf/ 'pkvEW]0+w^sCe}߮H}yıo" KVviseW~|/~ߞ:\m=?ͅvvoXr_S/v͗rZG޿^qEv;=v6^|e?xk!ƎgZݿg;~)=n^u7`hmў_/?9ixXքKo:S׾gs>W+Jaٟ?m^Bo_ţ&y Kwqmg[-˛={Q=Z)zT 7{tu'dx1W!T_:9 dZnjjhmk |Mw|yOF|IhhR M'L:'HjTknﳯ'w; >|ha/.T/wCvoh. ^R- 刮co#vRx۔9Occ'|_?E'|Vˌ׫yC:b>S*:{VuuӏtSg\RP>;A1j>0yR. آU{̉mc0(WoO)zmaUIzW%ZF؞{w?:~ P\0~p>\V[bS֪g>bn~ҔoIxvYϗQ>1qo gK{[Orm{sٹ\A4Sd7?]0G39DS] ܎ro<noc$]ଫs~Mtz>|WTy2;=ǝ|کGw~CẃOٟl홻gmNx]>lٟ~G7+G|֬H^2@^S1Mq0=[Q'M]9&G~ >}ʏ^}ג ::|^7o^a>|Ξ@_ڹ_cP#k_J`~vէ'mNlVvk ˇ0‹ϚՂ`Tͽpn=?A~oO?< i8{oS%MmO?v6Vwʛ3N|W|lㄙ>u͟5{)[F=tO/>)vns >_mWf>v;=|+)r3w/| @}һt@5' 㥷0OSl=6i:Zy8tFmh<^tuɼU TrG&)#XXFC%Nm{mJfYPz׿vL;OzMPX[|smmf|-Դ6 y֊D?$mz$ zoir+/u-X.vNion&9CEP2UD{Lq&?uܑ͝^?qŵn`.G~w'4fHzb<:N:y] K=@ru{w TdO@A{_|s~7bݘTĆyJ0afZ {;@n21|{]w $f㺧893mxoܔJh/ɶuN͝W?FM=UO..뷹D-TM:ýw%F 61 ۞XQrU{Sve >tttڔ,ƍSlZbe1n#+T:-M+t(W5q&P叔6舰F&;`*f5wϿw텇lyvxsÆxe~ʑg]rY_Z:S_q͇:uЦoc(3'yD]O]E߹q{< L⡞ad߫;t$ W{́$n1SGP6sWV7@PZ\{ H+7^0"wBOWu;JZM++Dr;YZP)XkI]n[6Vo7vm0amP۞@U +\bOۻV}h%$}.d /ce[H6b PTMr]GIR[ysY|>zQCtcT,#etF3>s;שhsP)|FqqzlbRꅜwѺXbD3Nm a-v2굄ß]K޹.eL6CZ$%ik H[TaB"/}V&gn>5uH`!onn%>{\@ b3r0-2Pe35Uʋ³uX\&j͸\ ,#. ю;"Gh5V)?S+ʣS*e f4&ւtz^B LCc7o썑qREsċ`pxt5,vYe%濝yjS8р* |-^]Q̿vxUi'֞tlJyqAØ;JciQU4 >ZNC%{(5Жp'i.޾l;ؽN~C;C|R/ȍϩ)_?Y^u>Z]<;5 Ⱦy+^n>21ŒJS`[zlme &- tU&)*td`~-,~k-+E%;~Sp>@bq6U>CCV .'oӎtf:k,225,>QS[vdEoeԏ1$ZXWl-Ӿg82'+՗Hq*u$=&UM}y%w~|_g5@ƹMb1si v`'d@f³taW/gQY…7Ko3hLk%z;@~Y:cu -!h5f |?D0\sا5,V }]n392abFYbj'(Z3%KS&\.*L v>)X:7UO[ IDATg7c(:$SփU?tdqH)nbX(>-bd̨zn!ܘ ՄM9VQ9Hyfk#D!ǯ>:;~IK-n>ior =r@v̒/?e>_},B-4c5%|^܇r!ꬃ8FNHڱtcAoKFfx OwvH 8/5!+&ګeh~h~@zVQw6*r3Suƭ0-^e{gf<>$;1((wU%1ь/I ߶/Ӈ4T۬I- N!A%+t& 1" DAn+Rrm'nBVݬX<ݒ_FlyE:$)D]" -^wr8B&2[Ǟ\x1dhIKerVHRYnzxY"Z"&SJjk=L7|NG6fϋE0o|_|lX(p=ڸ5*,)yYU"T0jI֎tdƱxBrvx~N [vN,SVsUSϪš69CKn߸:r\sm)*ԄŸ"x=7ŷD0mVں(Ul*PKCn]wM.褣nCcŷDhb|vs)`ZJkD7/>u)_K[jR{*;KP2b?` 4_)ػE#}yȹ::jLZ7+ "-)i[soLxlV}(s2?}07R}#ܚrnmpOHG IءŞys6)!8YV L^f(u<;[j=:Rc#?|MfˁӼorBNٮ>5|b+Einc}/fvtef+o\SfUC"eq.(CkkF褌\50p-tN08}<_>sp߁#'<2p6cͺԠR0R Ю;oĦ=U)laJnD$l IW">:N~ìQNǶ|k=HIz,4:'-Qh ma41h-}]9FS=ڞ0zzf[4eJs2ki^ $ߓcgio]ujDO%% H2mƎ׆$0޳qw}*2?̔n[Uc;5CeJ^$܌Ϻ J!mͲa &g Q"nNgz6TuUt*y}LdE9O빍oKb2Y*͝@pzGK( /40DԢg' ey @}?qB7Z}hРA ,Jq ݃ݿVkANI2yaݜ膓syrU/!Lu ѠAƗfA ͦLXx8=ݧ 4kI<MYz4  4#)!ҒBD&54aNЌA׶"\SJdW> z)T4h4CyN.̍h7SRJ kw!# fr^R(},9f̠adiu#-94i'D 4 4hР 4hР 4hРA;!4hРA;!4hРAvB4hРAvB4hРAhРAh=5g.....6GJi|A(qzL$k;ީK/W34YhNj0.?8vrS/:Kr7SF*,v8w|Q`d f_y7rßGN0͈I/QyTBK`&Eq\9s^]k'Zn_ i7_W~a3w|D'LptQ Bv Olcͥeַ\X[_߸lϦ豋綱37ȋSc# ||ȍKS^ }yQũ}fMqL] @L^6PݛB՗ĸ*T?7a~K'7ӡes c#c掮F\s.Jh=WB_p9|uyo?_Yczk۔b>UCIgbw]]Y$GJˡ8٤j1/Eދ MHu\- L|{uLfSncGu0dzT+!I WM(7c=ܹd,)?p2gnCW=@K;] |,Rge5dIqO\{!xF9I{UZ%e~&Y̱k΅L) !;UCpT5eՕ3&I-86qlY繥ۘ_6Lr=">Gq>w.~#: /tl;zPU wΑ3[-4{_OQby_|l}̥sAbslC3W.d|4މ?]BsKYl \5L=yL`< ̬bh,.y# 0ut5y@G!* so;yU? _5HGV;v W;g.NÖm]E_< ~bU_U ͉u&.%O ݤMwYsF90媚4R/T0FdiAGvQW̲Ei+gkY+k3V:&=XJO<鯑A>r?;AׅPEQ͑QADK6#{7J'*B=g2%=M;?HʾAD _.[Ӌ~ VfSU O_6Aߓ zy&= l(Wy3/Mq(Te$Yߟ ĤBEQš?~CK^>3/]:AW?zmeΕo ƜNSIy}V' y|`xv  hbO .퐡oE/!3tzz֬-8]z6P)e!a /%vTaT` oo\Deh;Zx" }u9m|4*cMz9 ~qL"ɻ{`h9i">R5tVWܬA%L }].QzoJ`]u_"_2V/j ux iqvqi%F-?G_ya#@B2'/D m +f:I.imB7SeI]0^hcmӴqoWVZa(_CUdu(dxr* f4t̊@Imvl!ùSo@y=s M"ُWz Mu";&+[B(FvBuڏkdmKr3Pȉ+q҆ۏ Xg4qtŝ'|a#cڿR%X= $1A=R:62p&F兄иk9ٽ WO=.'\k:b=MR4ޮ܈г!i.&XhZnH|"GX*z(kGr`9NHyIoʤc8 l`TNl~An~.*/8zUP߶meDq޳d_]Y}ڶIbgZvjTiLH-!'kǁ*#_Ml\<s07֮$9ooCJޜp;STDͻP&)7/؎e&fæwm9-RdIҍۂ>ۖHN\M.(dڔ\"&Rkdq3PޠbhYhO}I +#2o^yMe4q5b8,PH겄$QL"ql2H3˄^\t߱Z5 V#=|8 iTFrR5t.įSst^+KY ,djYhH ~Q(sf qjH)tjR,,`dʭٚ^!so-2|^ ӌm;h@lӜmw3:U:8V>jN!A%+t& 1" DAzQT&%ݺVz]Iz"f4ޣ`mUf&[=(jx#^xԔYm3ᶇv@-#VB$^X%Pa3tYsCmpj=z7~x6dC!KMj튉uȁ+}tY2o~}U[aNY&ݧ]6@7j(p{oAW{).5/F[vnaW[7Wx%[%P4̝c#4օ?U=wwK}e^DGb_Y jkNdUH^3KoT Uڅ+BFǥ&zZnCtűBӣwh˹ p!CI'@]sR~ј:dq#Zp,zՓ O$+h"HplD߸M{p0s_Wӈ ;p<!q9<3Yճ.ohj_Ur>S&8:~>{|)M>=ÛN^2i7z 0K PIDATOļŬff훙pYl,,&RҶǤ[ҷl|bYn9(}Y}_w2eQK{^ 7벓Ov!0|oGq|f"؄uV4n(jhACThe`SU:ZYy.(OӔa<@dũ}f}RqbEKۙm"kp<+aW&\ZG.mFYbԽ, m9AcvMLfx >wF7 gghMCuҠpN| _}-@mu5>c_4!pi>YHQrQѷ> r9y+&K:e{߈$TFxl+Ƚ~qA?f9Ac`[p 5b(3onٝsncQb+ÏM YӸܾ͇HS_m#ɖlIFz`A%wh(.N{_1uH`fm[2*4 @"RY&v=~7SOȷ9ҁ_Ҩ9'D҂:t1(2 eV_=/~˭TaW=3gQvY57z\nD$[N5NLfIS$Cu$ _&y JW2z) *5"Rt7z608g[ LmU)\R,4uӖ[k]:m[#S$YzG&Uz&ս|r?Lwߗ]^R{DeC~fNo+`ʌ+gL[pl;iٲZ{K/m/&rɂ?n#iز|$ d-P¬kr}]/}5qюwCQv箚ӧF 5Wňv/oï j(BjRcP[ "s]5at6^rޕ?"Yߺ5o^YyAN?ji7gSoS#pv˙ڢ5}^bQ+ c6Ut%æݭWwU@;'C(]՘ [@̙1|2 ,|{'z#֞xݕ4n)ɀ{z d=kȀi^~-gya`]0{։\L>G?vj¬BYfCC 49{n)X !ˊuxo~>5mJy&k*FfԨ5I4l%HG`~\:EEU&C-{mTfW<= 1eG&kژsV;g`޹U=ԬJ_^ 3nc H`]v~jAvF/;5ؤfi7m*`h7ԪAϕ6fWtvi#`2@.:"uL9(0~:o.dF=.~{0ݱ [D\FnQy߶wU[᣼㞰}kԝP)9)-F H]? }&VDXx X>ls׀…J{s!#w1 h58df Qۗ.]mNL @onQ:vq '*tv'4bߢWe*QQ5:WSmi4JH"_|2V/*%ys|fѮSg5g. ?}3UQ 1d<Zn.Ï%Lb0kwx_<htRXx"`ަ釉js M" zn,. /GO}x)bp(1x%sYleL-(oe;;pdgKp~We:\gb¨˼~FᄫVmjQs`ަv\uyBo$INS8fpR&OBж%lEfRiΐ\#=yZ~+~4xPT0\j*FAT+괯hH'n9C ?{^7+j,]VIŹ\CK]dvJBHS;&moX$dݒupBo20 tU-V1e]ɟu,t ;!GQoVsՠ(01qi~Np B>8')0R$[KW4@Ic+%htT]/(k_7.|u5ݼՍ~Fni;qj2mB/Ӳ zK9FTҰ-zrf%~JKoo_54ئl@UEY jEPTT[28ж'&Hn>ݼג? ^hG|nC|:9`9 Hfj}GZ{\2jע=mmUV)yQ[9klb9>krj~1ZmzQCIogzqEqaag!ԇ2XҊB%UqGrd 8y=tw)DiӮf}Nի ^'^ixd3$poB߹cڿzv=c2 C2;8BcuNcuvF< >Lu1rFFJPUM jEP"ԖFC0xMom ;Hq{#H;dlwSe1@c- ďbxDWҨʉJ.W˨QP"ULa9r*a˩`i>FzD)R`|^A&kXj es|DxgFn /n]wEp .Όv42ҦQ͝r**ۼWN>zu\1m+$j Eekzn]2~6)Oy)vD/4mZT=}uՖF;2pz/ywvk}"S~ew G{`X9Izl; 373{!*61ŒJ?S`[smԵ܇ք`Iy`E̕f$]JR:88גbq6Շ@.m-[ @ᚫI1 ٦eїm\ibWW2@᭽{v]ʶf[aR@n|SU~tx9W7F5F?VZD"TTT[ d LfИJ%,8.k$K@uZTH(cw{M:%ELJo?aRWL1X ņ ND0s7B@Zx҇ǒӟ,`(dObD}1qYXH4%Ǭ|9Eâ ,mʭ'˨|+L3Ӷc`ioc(R_Ί(ڣ;ܳKm2ֽ˖{Rm[P1QߜtuSX cG]z,m?Wpl2~}uŰQ?_UnE VOi_M/;C.NE^:x5E̜LC[LZ:o~/#sC_}<5w̽l{m|\ KjNk v-Bԋej{I:N|@c5VcFZߠ >* ='`ׅ 2?V>W͐t@V,@\(@)3v'H&KY>x0VW!FJhwjkF_JgRts=!˲>/[BJy#Z'VvN#{H:/V ʷ: 7 nzU_UP 5Q}uն C65Y561o7~ QS(Ul*PKCn]wM..a0C2AK)ʲi}馑gMGśڝQ)mt * ^1离6)HOPH$ `m.K?3sd!]Y%ىED 0č}[깛Ǻ[ ~?(d8FNHڱtcAoKFfx5?W68-C\lX\[M<y;ڗR05=ۚ%+ }&[1y2?4((c,fD6ͣ0I;])E[In޷YLZ'LR8&&t#>:Eia[Gڀ(LMLzEfm팁vQ |.bAT(5Q}uնVvF}{|oEŤ0̝1+K`Tݵӗĕ{v~t\fYF[vfI_˓zq,1 khۗU`H^3Ko޴: -s?p>KhfߧJp _Mz,ٷӔ2_6Jkdޭ[~ҟ`cɍ doCN9tb\{~ۆ.Xtq汃mdX:Vm=;YȬsNP, qnY ڙ׷c{WhaJATQW_`bW^W͒.6i&Nq F=^A431~EkaI$s!Sk1УWBdžt 6j;cOHo>p W*DmUO:!* ^>jcsG_>|Gb]V.Fxv1Y6/~-Gw˲A,sc#ԧeɺpkzY4h04z'D 4htN 4hN 4hN 4hР 4hР 4hРo w6tIENDB`pytest-4.6.9/doc/en/img/pylib.png0000664000372000037200000002012413604173400017471 0ustar travistravis00000000000000PNG  IHDRr1\gAMA7tEXtSoftwareAdobe ImageReadyqe<`PLTE lbek<Ԑ~ ]cR ƕ!"L==*GRkki "D;H>EqzIDATxb`cfa6I@ rrBrrbr $$YA[KV\Ogb11begb` ~fYYY9&!>!9> 91)+/+ ce@ A9aYFr ,''$''., $"'@ i&19AiFaARR &㔇@ rBF AVټpAZZ$& +  v PA0-++@ lP\p 7 T"()''%a 9Fy&Y9 YN A"L B@cNbr@C@ ZGI d@VcY$@ >9 iY.yy.Iyyb`bxEi^!^D(愄#@1-IKk PX1CńA(E @c((`g`YyY]]EJ fA&&&r&^ `*㕓c 6>vPpCee90 @I]A 11HHCQVKfVVV+Jف @ +'t8帙X>:p Lx)Bq+J03h/0 :AHb ^^ ^79% :V*~v>v^ )y Xc`B` !YyNb9H l1( IΨ6 /4(\X$P$L< xxxy$_WTa<ZT fZ  T,Fs31H*c`b&}PЀ 8 $';@ 9)BbĀu0WKDx8ehf9~&&`f0v9NV9^)IY.bfCnN~q9^fiiYiaipV`fȃU,*5+8QrKq^ i6@1̫b(فTzJJ@@1\IHH E,K@:Ԡ EZ  @\*bdv&AP. i  XN ZI0q`j*&)@pӀƁLfdpRr0kX9 P1>Np  U00H0KKJJ 12\-f AvNE |AH`g `TxJBfKJ7)PǏ+P,%7+( 072CLebe3A"+2PvX = )t@MUWh)K,$ jq2jm9vqq-B  &>,2MO9vi`\-H[8Y@ŋ(A]oYd ħR‚,J$ >N&VIȉWH`g H td".(ɬQ [ؤb?&!`H;UG9XNe'` ,x9UBEJ @QPbD2bL(. ,,RK$89M 4 pge @øXLp(|b@ 8jX%,< %++̣b"X#R p E02W <*T<20M 8Yq&,V!êETj 1MF `\zɰR='4FD-)' IB+$CK1 h,<>8ƈH@T!LPIoa&aJ@<@M,NglF| C< 2y @q33! i,^6`?Q (tOYҒdd`fd4ԩaf 1D iInnfX,@1kiHBLGX !4k/n>^ij㖀6ٱ5 XV N.,!03<Uވ+j>Nt,Jq4`Dnp)~N99,9\Rrr4v>p3 D,.,,%#XRq$sr4@Xq lqW l0$AP'X2 ڃ%p5 @ ,`AM41>a`KI|vąX@LfPPA &H `*% nrpKIBr`0)0 I(R&D5(z  `21A0ƈ,l+sH[0Àe ,@O؜$.b ̐z޶ L "7"IpۛL:Zd'ȑ\n²Md``6d /P7$ FZ @ږ,L 2PAUhRbDwB؅A!',2 ib|`DA) = li!L}Yؖ> GJM iP@)Sjī9&kD3._LLІ%y* 8l/$NA$5C Y#iK H ,| Hn i &< ns1_X,, yi1I0B?'@&I)1pK\XO!\|  qr4!9i\3#H1 h@ PS($CM6!(hC]Ҥ@B)"z`B2 a!qBKl`dgG*YY\lʖ惕p>8@ E-xIXm84pn8K eBkWF @1di v p4HT(2 U8xf` Yn;h %/ 'RK A?$@@# l$gB\ !.V`g58xh 4>P*9A`1ʡ [ wvvpWXLq4)`Nfafv^P ^EA>Ĥ Ř+G* lVdN.b01j9=pޖ61-v`׉ K9$hO49BzoLLY xHصvh$xS&`K 9!c"¬ & J[RF#$j@A9v`%, L4M4" y!20ܽZ.fI Y,(jZw4M g1\P7%S Uh+(gId~1P$=,y%Pۡ߀N _AJ @# ,X 䔐bd$ (@H\ދoи C Ob7 !/ 5*XaQ Lw+%QΌ[2J"@>R4=J\^N9! R1#zm`+ey,-zJYܦ2w^ pMMWw? h 1x##!7 N99 Y/ 7 nw4iRcD@ $@IT:!,Oi,(pR@KK`A\:<ȩD9$T``ƗyS8$ p 8X[j 0=q)s PM' ,9PWj8;.)@@ˠ~_()L4FhRe & ̪1Pi%v , ,<yY@/4h<8Jh?;Ri"0b`&$yxAS%4 V[2B0'^@F.Y@lȈH@1p6>P \V̐he 0K3t@1)|cR\@jENX D8@IX AE D547 22XT{\ E@IUX @@@! Ġg|@a֭=B|*4 H.BqTq2 X@v\@e@]9mفN tAА\Ƌ.&A.Lo:Bf 9 0Xmh XL3Hb #!,.`q~fan`dZoAN d 0XvcM4 qAv$,LEd$/ XYYđ}!Ό)9 i Dd5ASRRR`sŐKXsp1#iPIl +8V}(p{|B j9 g̴ۤ-`K KZڲ@ 䥁^0y&dcG䆔 }9 `#2 ,iZ kPsj03a}pc,0l77feG- @nqOYC|LHNH\.߸nf&Pi XŚA֠ (B9pA5y hd P jVd3CWHn pH# JʄU|n:;D P:^ / F,@ ,P\ iH)y1 <1/ N1kQp` d 8) N X!Ŋ WJA=_$G$Q*s<6#d.(A9@%;dt 4: A$NpBD!83K"B@:uHN K=vp而Xo8ai@@r84;CMe98y@%8q)HvLD j`g i+D`:d&b҂RP6٠Ȁ8H1 . 2 AU؅ Hu/@itL2ȅ08Y$ˠ.'%.'j>P6d\lna! @YW ,prʡ8 XBqiHR4jy K p!drnoe@/r@ 25BQ) Z(9 @ 2h %1IM qO2KC lRҔ%5QX $$M YQxʐ8ilN&,N%i4q!.bQ! NH]@ C@CFgWX(#p]ԑk2<;>\PgE`"@1H H\8吀Z QK׃.1dN +Eߛv%ߠ0'wjIENDB`pytest-4.6.9/doc/en/img/cramer2.png0000664000372000037200000006131313604173400017712 0ustar travistravis00000000000000PNG  IHDR4sRGBbKGD pHYs  tIME :hM IDATxw|Eǿ3O۾N IP E@gyӳ\lbCE)** $ޓf>e&!( _;>3gg; J)0 q*  ĉ`0 &N ``0 '`{sB+%<ŞH@#tN"I)40O)RD(" !! P#JB] R' ** 74cVn?P@b`0:q U* B(*NS]T./B>$Mш/EEQ5] !TDIЉsGѯP]p̿vm=&[qEi O@8V Nb+9L)*5J Zk2HȆi:P҄0Vo UtFqsQª*bhT(ִ8A|"pfN8)3y4 AD`J_[;Y=28%;Qs适<~#4MP"Tx[ Rf]Isڊ8&G<@9T$d0_8-)PtQb#*IWi(b# 5@Tp(6ou=BH4%$b;/9MΆy]%0`"8N^xdBL!k3՗gL5RJú>qH!@#8[^F0%(9NTRuxKuWDEK6ٌ`FNt&) cklIT}Q(Ѩcd23"~pYsSas? % xKf#Hd4[1شTjNv{ lڪ%"PBg) ""(sz'3yb0_8 5!7 UURjw X)!@AUTBnĥQ cJ Bs~0ԉ !FĘc|o4*0Zɗdjօl B{|o?!6Ĥe ?-oyI~&@;V|Wvl Qm٣'qSخ/;.3rX@Hש37V7yb=.MtB4] +BiGE]O.`u=P A`2%)5p@ )=5k 7~?ozѫOvΧ{y! ؜h{zQj +@1_8MNbG A\jmiqVFxpufB7Dt8| Oٌ1SJC]ȍ/()<.J7inmBXf6D cB(%D'c GB@%^)Bh[0R[]X􇔄xi9vk¡0a @((:'a6wbt#<[gSqwp~f dJ6izWyGxܸ55;4rokN{Ͽal?Ἣo"2KP&Ø^z7t)vKy)4Bxs M{iMOH+^{*@xs}bʿ-KO.&~s8h?qJ:%ST/"-߼՝)hExWeg߬>Sraĥ~ppctx,^L]q|ƽv_%188"i1c$ML < fEM'6G?*_x0%kLyiÇ䦦&:v+LN3XDeeՍཅŻ쮭wa,XP`0`LKy.BhSɠ "i!$'W)k5c^h7TA9c۔o|w/kkϹ晓bૃi(bW?9<ٮL|s)%^x JϹ o()er餸O?vq7}qY_̉V))s3/̨~{?o3>A גr_d @oX?s| 0>ֻ_fRGxtیI=q7`HG-8fYC==(-UBC+f[qN )"2&Zvz#r0CA&斶Psn2`0Ԅ:$H6 ن}_}m/7!:9 f6cb#@ MMℤÝ.jHjڈ!9]'&P8R:UU&cRRRjyn3c4)щYpTS|jTO5]Zti->ҍ%L;Kiڛsʵ[ļ 'yH %[;oA$3"u+Dчq:whmڗhm7>M!J(Z`g/l)&`yȀG۲њhb bkKuSS->7#]wz<%啮V Jf/=]簚cV(2S?rWV> v:L\4LP4x1롶V` y L?)ܻ9gҐdOa19ghf]N{ T޼(=w G؜1E^U$P늘,?Z^ܻuy ,zƉFQc0ӎjwY]*qDIV}C%5ռ c@ltH-1ZMDMxJ  D=fiI 1iVG<'kA]kJ$R쨩T,}H @N)h`߻)mצ' o+}[f<ɓ3b1^| 4޳4iiݭcs`K.s6 o6MSGԚ,x"s nnFV- Rc=?Zyš8Aq:,;sJ\Bjn*+`xSM 9%DƔq RvpF& VCװJ9ܔR2>SklIF2JuS)P9jXGFz'8zH-$I,{>\%pc+z=}°h6i-X_9թ5ޥCW{,S6 3G%îz([uaj"Ҳy]9;n''}9wT_|Tn^/V{][sgR"4u*ͥl`0NX'<4 (vn>T][cK$UnO8@7F̢&yyfnwdfdڬvd~r8:"Oss&y\8!y* )jbJy pw6:L] !?y=_f(J}=HN}Csx@# 7V;/ͳ͇.)qi#S횶m/;cVo軟y5dxs]2}8l6D?q'L~4?~(~j΄opɵ1mj&njyL=Y={ŝ+[;_Wy>]#ŎI$?>tңnHv SD':z7]F/.?()1TZջ5]H|j| IOHN藖kJMuu%nn5Vgkހ!=1ǘqX ! =(b0Q(&@;ғtJ @ڦzf24B|&# xGOw6cI"ў+OZ9){s/l~~{WaU1s_>+ruNLeSz 8g7;aQŃV ;}rsu;J*=mmtCv֠r_[lLMJLyhT%:Q]o! FE E[Zk}PVvv, +眫m̅`(%׎d`0j8f% ߴjYUumա2/0_fcsS]C}bbB8(ogv BbhNimcSC(BP(PBR4ܭa/T'&#NB11٥J+}@U[X4@] S@tFʏŇK|1vYҀId ak R%ڵrpX-D?r)j?QBb66$7'S_ WQ] 332vٽu xڌQ(`D ' y?Bĸ1c|VRZj0H6%F@Gѵ/S>_)???)9%ŃQ}0P@s -.illluW)+!ƚ:ꔏ/`r8΁fa!l/Z9(`Z8n:#D)E]G tXf<5G~ah4(E98u{<^O ɒ:dty@ZldQO0eb0_8B9g ɡ( "vɡG^qƎ1縮&Ee&:XN"2wd1Xw$cvZ(`6KJAꮭ#~7toBx;\Tס?hg:mb2DQ#GFO`^-xQ>D1b\/,R`0q0qIɛ7lnry\m8q騫 dNM!D!}c@2n);[]#Fy#!Iщ2'`L)*R4hpNVv.!Ziq^{t9WGA.5B\$6حVJ `0|l 1XP^yq-C|"0Ô`0~DJITt  @8l25E"AL D#&[;Qf[*FD t]oߨ!~P ~45"FEBc`y +Θ:P#Ho4-i?\3-o X )B P$.ܿ nKMJMEAP@Ibk֒ʒ`L?C(`0_89tA RN0"Bq'owz"DžH!7f/u]԰Dc$ )&1=!>6V$q4~F Z,dm j[KK?ݲw{_:HЏGQ}W&n>Vefd7LHX]nИ<(8%ũFEǠ}m>Ij!pXLAJ`ם7crnf#|DSP% IDATxDQ=rdJJq!q/V&k~iޖP~`V(.N]24pC G4EّRg"cd`uB Y8ub}ٕsFeS}u^, C NQ%ے߳}pySNAhl?l28E0uaLDQy_+["? <5cvA, `ބ $PRR^ Hrd 6U(8bp떲j`+V׾L=: ۘHnE- 1~ਉG$GU%nػ~cA[AAĜ+Ϲlj2ܑ`0z%N/-x`f6Z R @u$AQGab2aRW+]:kc %9N( qFBaፕ_ kȱ\V#dF0(ԷN"0lt@sFOJ4@₺3cp34j'^xԩa@e3/*w~yŗtb/u_lyƼ!Ўo7yv`^Y@`G9`qߔpBmH$MrP$FMMa`9P]K֯_?yj1 <!iH8 ;[-M5%ՠ_^c#^Z~JU@S%NhmIcb?xcnB fdgo*< jjP`hG_HCUm)M>ol]Ĭsfɐ:i-u+4Tkuaf=u8>vWd`0}` c0Z]ABF-ݥ%𫪆" SAW|Hu%`c+F:FRUT}i2S&4Р( KȑeL};z{ S Iu%J )6FB3Kq939[QqСu 5KJAS`0 N*0 b^<0P] ᆊ=Jh#!q zhqqqC(ᠪ@)x,`:$Eъu#L@Zs\|gE$t'@Ր&XD 4 j4U-Al2b}&uorsS\E%ETkzHވåĈe7y``0}'kPT*5kVlʋRV8a㊳KIF4!6 F)8 K[ݲaF,JqEoEIh+ۨaڀ)G|߬.T1gTloO39Y6’EwM@W##a!`Q{0 ܁EBQcBCueB!BuAJ0}o0qDKJ=a G8Yu~j_ul1%&$`0hZĵ*ZLQARu9`*m X%#7TTFu-ZLM>6j}崹4yoK ]^lh7"sKH7!ws`-l͉`0[xq,Ӧr$iJYQ0QrSyx"H P" v#UN@eכϾ  `0 |榦iiqBAIl1N2ꚬibWyI_[0#{_UBb1[ 2ԥq b!Jӵ#{Ubn(zP/oqhBjwW)߿)f@~$ 7aDWlfKq_GƄjXݜ;ω`0:fz'aG z5 HuhZPp<@@ :Uohu5:]FPXyI((%s xen/,{ vlm?_J# jq4׹btOHy3K˘:jQTn/!CYH`9 [q2XI0bGPL4 #PUڝ@`m6,蚪Ii(qqquuup$\^^ ::2E@uM(!"*_g0'G؉qu{+T@hOdc׷6v`wnoҦfAI kqkhm! :!Q(C4yrLLL$QB֬Y8hР ֵ'v(J  bP4OзrcɑF'(DՂ29o4s=o9#b0F) ݵ&H"Ǔ#FHLLT`PU0aٿ?ѣb Ʋd0_8IZuq@(EG( hj[RRxZEz@ ''gy(PqWk@ %P S8-ָ"fk6 zi&a_>^:"ڀ1nޛN)8b655 "v~PBB :!puT!kEĘ.r\˔`0~a;ۛ`0%`0 &N `0qb0 '`081 `0L ĉ`0 &N `0qb0 '`081 `0L ĉ`0 &N ``0 '`081 `0L ĉ`0 &N ``0 s_\ZV<2[1_8U+vzO=SlbJzwּ3rސ^ͯWo/v "f?q*nxx-z?1Q;Y'y_Zۤu⛞\[cSGʼnK?}WUjҠ_vY++ڹ~UnYOa_}~{>ʡC%3?m{>NX;f@$<ĬE|uv{xbUp܌G>ZeU噘Nqz5̡WnU$,_9nȀĄĴCθG^]&mug޲#)kɶWȔgwՅGEWO5S Yq 粳;Y=C8_Gq=/ c9мTN#?QRŲxWC/XeNOz-!z%k,oJ,80a39EE17=|a+_8 R|fR3BYQ9pן~\9NfrŶ7/pUG;}\ 4́=-'I\>{5/}?}USм3\syCm{]t/9WoQ]4p~w-Q䌙'ɖowm#`|֭,. y_~) %?;'˄U㫫0߾߷>Y9GҖ=C^vǴ~B;˽^ }pRn㻯rӾ* R3CN}ͫw09crߡZw5}KnmHI nAȲn{3Ee93^˲,/:+?K;JKߪTE nk'~φ6ҽszs,˲|5?֛|ֹy,˓nXR鹲 v0JidYr߶@g{'TkZ|Y^Š]e5nw)Og[jc;޺3dy-olUdYλfivꥃKz'Z&Ȳ<]EUo]<0j;˲|YJ>a[U=1E֜LiT{LzK81"x}I=#ԥAG4v=o#. uKL+m>sps_! Zpy1z`HɒwF-Zc: <጑RMAm-kJhyvAZ~L{mޓV0ILמnl7کWn=cO]~tlIOe(Oxl{T?{ְD#;fjN$OܷUsI*3=gIo"Ad ?(hO\Y#ߗ Wm=z>t྄R-=95؛w,WuL37ߵ_O]Smj"N5: ##VDךw( M$|ϛ^vnM"RӾOjƭލ^: Uy N!=4UJX=sG$O c%MDzymօg2moz2녟`}4ҧoV/yKS'oy~O/}O҂iޓRkz'2}w)w?8O>}O?brNO;^y(Íz/z<;$~P<V(q`l%'N&uk|?i]vU=U0ϯ6R7ucg5@~S܁m3to+Vh`Z ϯGZ= t~O%'KBlG:p~YI/3?I :yOB 8˜yݔL'ӰI 'ǟ@/\N9R8XoDH{Z]]֧.E\]Ja'ܗ1O҂3oO5{{{oLzUM3,;8-P>{82g۞[|L}m ܚ]zQ?74q+ī<Ћd@[qKFm͌7ZZ{#(4O &m?|ȍq6y|T__ S)if\yuazm'Mĵ坯ZlMMSwq wͥahkt)#ž8oA=$u\0+,xr^x9f;fNPfivv+&gIln 2?;7 % /=9w/=mlnzU|ڊ~A\Vzu÷?{LCz'/>L8Lw#_}|`- |Csvyg/Yy9Ɖ\Wh[GgLH1ޒ~d۬Lͽbj o(}њ}o~sL^ն3,ز!BAx*S"=? IDATc;_SRVǛEѳCƂG??z'+w5Oo.swr>pie宪6 ԑ}36D9Xڼd|_.VGkvh 9lԳ/d${oC:8~K 4Q," E[vhn-vEQQB) (X`l} .Xq>s8qy͝y;sg澿Ÿ24>ƃ\yŕܸ?HT|/>;=i7rsΡ=6ψ8H.Gnmm5d]}6:JV3~yTI4wץMG,X9 m.V}?3Jh:&{EoIY jF,Xuf'ʹ =ݳqtVxwNV}w8, iTkdVr1jظu6wo]ܨj[v\!̐ c,` fhf5j9i0+V:5{"V,Z6wdγvQv˽K5{f Q[N֋vRqUN=i{ k]H-{';4-s׽6Xx}:tZRGuԦ&?XLةgwK`pi2f˔ V,P 3.O-_#>f9/mJc=>ȆNH' opx𹴹יTK_5󽴻n5@q}:j574e4LkA5'&5 )SV7 o|huD >H>6GdadS;]:@L!_49Ѡkn/0yW5AD 8}{Ҩ2`줳`6͈MM̼rcFmSKꐾ\+@/a~o5!#|.Hc:X>DW"j.~vfԽ~v{X _,?=T+3:ʔ=h#MF+D >3]Z*ꌠ~!soviZͭ'p 9T+;W:KA5R+ޮ4xtO EVe9-맘оWD 8!9!@ H @ $N@@ '@ qB /]17xS0۱/ifa=@uAWE+c{MzW\#-w}.bnz<@m>=>^ ңa=PXķ)N%>n<9o>?rŹB_,1Ԑ0|؝46w*n͟*]յ5eō\ "Y^SAFߡzf򒟙S:"c֑OPw=Mξ>.*jWBՄ$DFdUW@PT4=W4iA7Uz; 癛 >6ݿ5ƳV&Q `CIJT*440Jb|rc{^Be.>g4Yw;dAY m.g 5񍋓є9/͝GBS D|i4ڄB.rnۯ ԍűyլPq4D"_(4SK L'-Rvq;&ȡ;Zdѻ1!h$+W+ͨŤImxnW,.$_{̸/M""ʥ2X(S Y}Z5z+Yo,5ZZ8~&X_@/<Ėo}SI{5v-b) Vg6ECC*AQs"}y^E|D_~ks9? qi}cy=u^I=umr9cge=4yP/mz` Ah{ Խ ZHT7 ~|d4mf3߈ե`DfAZS8RU1vMyZRlsy.uw1 eh+wj;R^ W-?+ )Z#, ܾ`L@NrҖC#T3G*@׫'G Oe{x4Шg%8Ek𴵞kƶ*͋:Jx .sb\Lx\p& P5tFsՈ:V;vzB&cڦko{VHdBZ[Lְa Hp~+/&fՊJ=y&lR֫ZČS=n6aثoD>|VP# *jXn1 6XEa~b#ɓd$X\~YSY%@Vm`}YjރuJ*J, /o4Na^sª:4"`6y0o,ec h2a^GQ:vy&hڜ2>rwW҈!:Ԕމq@@Ak"%GvI@sfe_ݼ|W$'e̖3Cb-GyU+ ֧b}7r/kQjOHTu<mKkQd:~QT,h_L^_^6}݉( 1!~6cA}ږE1c۬MbV36q'4uSzW={T%M.)B'/HXАuG$o. lf/dӂ N&M{~ȅNxD|stywG8)t}YfAѕyVt:~{N?H^"h0ǝЅt:>zeOC7|hZsFG gb  .u Ǫt b;Ij7ؓz\ḾN;zs:>N6&ַx}C8ޔuh"Nw٭Ste/.dͤ?`6n-][$R;9t:>Vko9a,t:>t:}eF't:ag C'Ҁmt/fE3lLHm"KC(ojy'OϬN1qQIι'W p %>84**mMy5}uiȯr"yM[čýH^?ԴTuۉ5q;?\DrL.}sI1'mʼnICbj6 <qqSRBr"%#Cݬܔwz79Aw]+Aq̽*cڝdLd\@mL@fcK/B}knErg_,gN7]+Jo^WcC?-B^b΀@30UB~" 5w}~n9f Ufu8"ΫkD2I{ҁCzd~?;fl5%oˍ74 "ǘZ@}VF;BXr/o)҅@Tr:cl& 'MGcl9'̠D{Z\PQSlLd".d†2M8 N&4'T'=pa[^[. hcHmZ]!.8ɵZHjAAjL+ujzSM W ,bE =T7hLH{P\;GcTS/Airz=ثudmi&7`Қ7pos6RvL( Ɓ@Sao ӳ@HCb3?YIWD,Fj/Uxu\J[i4RasUm1Ne-r*DI+eYL KbBzV% ,5,䧃IiQ[ (&ͺUF#n%\\2:zsBA@t6E c 01@գU ś8lp6kâTYryAWbsgWC[|фB:q}p9 u'LزCBGTv'% Yy%)) ͺnQY\P}ZQUnbYYOZ$Y!ԚU̦ϿWXèhmՌ۱_-{N^~zDGc]'wYVIW$gj5<%1z-,^*.(Ʀ,m ;IR^HU !5x(:uGoL69*Pئa9G7iIF?xC+7LmpBH-N@PE"dFq+ƒR0kkki;ykV[^YBaau[il:R79] -J%XZEC;&Sᦟ(a8 Ⱥiz2#7nЖ7_.w pBMv5%~.TXzLgLj tDPe6Pqk>M@?E ϟa[oLR{U:Kٛ+5|YѨo] qe?Jy[uxR=ĕ68p2T% K)Vj[^s$Z9vá >0#+mGVֶ3Mˮy+/~^T^2]itjt {_bOڴJ=j6jVǩ)O^J&|3Ʀh/]in@!AFGASά%.; W*~jIc'꨷$fx|GД2.hԇ^q>c6":ϕ]N/KW>ۼLh_u;KϣM-'rT0=bE$jդdhL'IDATj =W;qo(9d\ A0SPvKݭ#F=$N?%3 fN4zAtO#0C#Q}Ye'h3peQVUW)/d>_S?.f\}cՈ `>Ua`n_JĠЪa줳`6͈-@1fvv_z# -= iD\2/Y9)&>C@ q`5<[3~!PFtvO5U_D_`8u96lKԂ9cMUV+ um~#_GWD`sdz:Vkwt֣:A|ꌠ~!soNiZͭ'p 1qS' 2MY) ;&ŧ݆%Ckiۗl ]h @ H@@ $N@ qB '@ 8!@ H@@ v,7;IENDB`pytest-4.6.9/doc/en/img/pytest1favi.ico0000664000372000037200000000723613604173400020630 0ustar travistravis00000000000000 H & 0n (< OOOI*O*L,*ɧ*ɫ*ɫ*ɯ**K+%******K+$*****ɼ*K+$*****ɼ*K+$*****ɼ*K+$*****ɼ*)|K+$*****ɼ*)v,)|E)|D)|D)|C)|K+$*****ɺ*)z֝)|)|)|)|)|:K+$*****ɺ*){֫)|)|)|)|)|?K+$*****ɺ*)z֩)|)|)|)|)|>K+$*****ɺ*)z֩)|)|)|)|)|>K+$*****ɺ*)z֩)|)|)|)|)|>, , , , ,,K+$*****ɺ*)z֩)|)|)|)|){?%H,,,,,~,K+$*****ɺ*)z֩)|)|)|)|){?&L,,,,,,K+$*****ɺ*)z֩)|)|)|)|){?&K,,,,,,K+$*****ɺ*)z֩)|)|)|)|){?&K,,,,,,K+$*****ɺ*)z֩)|)|)|)|){?&K,,,,,,K+$*****ɺ*)z֩)|)|)|)|){?&K,,,,,,K+$*****ɺ*)z֩)|)|)|)|){?&K,,,,,,F*!*****ɰ(){נ)|)|)|)|({;%F,,,,,z)'2ÐKIIĐL3#X;H;I;I:J?:\|?u8:wJ:wI:wI:wHPpFJ#/;L0#=#=#=$B]{~ Ct$4>#4>#3>$@HVYI+$*****ɶ))z֦)|)|)|)|){>%J,,,,,+`      ` `( @pytest-4.6.9/doc/en/img/keleshev.png0000664000372000037200000005531613604173400020173 0ustar travistravis00000000000000PNG  IHDR+[[XsRGBbKGD pHYs  tIME :iu IDATxw\USnd% )!$!" ⏠"@B  d&fN e ݙ{Os{RBP(ujBP(R( R BP() B)BP(J BHP(zJ`lyb47xgMuF 54/kW@f\uӣL[K݂ꝭ i{{WF3Bh 3"'}?mG}L9܋λH4՝ BT)/Ƅj㭫i\a85pߒWB(P@<3݌xvk6 $OĻI&at !&<!# ~rSťG $8qB2uD+˯S$tv"A=@ SIDwwk[kKkKwww8)-[1B5#Z~+p~(\XhW]œ19rP68p@S)Hի~% ]U* @s_:֕oRגH1ʒH5g: J#խݰvsͦƦD" )(uĀc(VN4-ݍHP3J0 ˜f9R0΅A4 Cݜf{O<ĕ_:UP(r3#Za}M60'a|G$p!0!;[[x%+469gI֕M M=hpYUIεMT 3Hva%pRd=Q} s S\>zJ Rtw1-+ W\'71!)1!)AH9XڝC|{LŽ7[4o]1CBwĹvPiMW <ۺ!= d"$5"77w&vvfR6kDmuT* a@20!e:%:s1F!!]VwuwyF $r OY?y|qnn *j:7 oZP>--hayи2JiY>e{g XV(LOs̗+-+&L3Q)TJ2#?;IBSaP@HBP^ %% .F z0H]УID(d #×0cGccHN.D rBe#BA3/,8h< `J;[v+vm?:I _cUM$N9Ac]d*eL3;Ҿ'4=vOBPV kD"@@ X B:K &&@BJ S?cilqcJYg]__޷$c:[Z$$Є L e+*V\#M158)ܶ]M<? yoOvks`]&Yn_*YvvJ 4u= gh:t=_p0v0 HH)aL47474R"*6[:'7ԵE %H"at@Vӵ5"5yE}s=YauBsNx]F4" n#nD޺[#tYx`s}{^Jrۜ[|;_jO#w`6BTzSB5B06 4LpYIё}!x\d,G lY&귙dɎp߂T2{RiEDL$|oO'3HPXp8NL&={sk۵7a5oG:&>/~%PEeIj/^o+Q(_RJ)# `<_ !AJ)R )d$dk6kh ɀҒ~Elt-_bY7H6;zLx44c4A B빶D8Q @جB e{h[@;f֤B `Q/&%@W"BB*G) A@4m9Skf"#Qd̓sh?aCvKXF0'Z- (g)_s{M<xݵm;v7z'?ᬽk7`7oӊFpk#[~7^'5wM]ƉsM~t Л>1BIJ?\}óX;瑿ΊMm^8t+vÂgu`3y%W^8'E/|Xh䩗ïɎ7?w{6>7:`]2>ﺳ~^}2B*PB]FuJLjknv[:U5vljFP=NR ?jڭ %rB5֙Ҿ=ޞJ>ک{$ie2.3`H/tn[|$A   Bz99/t4;CLns<3wj{=]Bsݼ< -+Qͫi#YVT<̔CY۱/ 路}7_Ƿg/uw~Ϯ!e>2Zkwz|^^Tܿ?=ZڱGnZl{qWĵs_WBpVFzՉt'>⤓N:)ߺn5Lhj9֮hu閯U[o%xz_^{ҠN {Yn8mH'|{'e˗~p_=tU3L~ЁY!zՏkǫ{U O5Z+oQbs7sٛxrG;$ 攍>~{o]?]ݣo}oO;s7~~0G׮ʎLvDWjeȑ3&*R( $0$B" !Ak{olpk흉ƖKƍu?xcCS~V+~oo~БLXpsΪƎPSWӑZ4v@(55-@H 4"<]\&AGigNuo~LV_Ϝ64˟xo{YEpK`ׯ?qt^Oܱg Z~޸*ud+y8~,z,?-UG{[ֳܴv'& %Q^EA\=S>qoċ+֋=A:3wIF#{ζuhGl sWS6\D AI$IIiyE$ﯨ^b%Mmز0/qdJYDK".$4v$lܣ۹^#y9n3 RRJ}q1k$@]~5{{#^̚yeSZH_?Sygƹ2 KB/RYZς#=`ϛWH"2dg=t$ aO}s׫G_{Ä%]Hn|?KaKBB$1(8m4ZZB6@ֿa'>qyJ/I#I$BR $IP=n j&E8ޝX~%[6o3߸}nP}`1ąX +rE9#D" !M{QdB0p@i:3N(0uaC8komΨ7~?o|ƋZCGT۴||O!DiJ ѱ?~ kA_&֗nuM]ڐO$MtItHHr py]2enӷ/Z\ȣ׏ۉ^m]vicj }yQ ŗD1$HP" C ab ."aҼe/z: Aql"kHRQ;u] -G- .˳X0BMCg6Q,}]?`"9%rloΤp򬣵e>lzkUSUgc>~{r>O_2{ D0$p֘&=UJ 9nX{8؋/#=324w;>!o'Wwܑ|s <ݼeث2uO=OE/Åf+lyww'<2H镧w)!7$h}'֎k:T֓΁(D;ˏHA V-гAz^IzC;nwײ?{E߼U]+_lQ?|On'Jd-7?[cνx4xw9߹~ou]tzn!罸K>q7/l[ڱWAziB3b؊?e @jfP(4 A 250 D H$kGT$cҗ#$=+HBH)$)H @RJ@2Κ;}!'SϐܒD#N;ݙ. ¦IR -H|t`!V?=Ў5!SfM7qʵo_~7YE.9`xƋϺkף^y8z ^;xO+nh+vڹf#CppI?v]jiO>ӧ_=}BcŲ@GĠP|y(k 022n3f1rV"0Ƙ 9 dh t$9@$FX",!ʍȏ2p#F dʲCAbzpeQO֧,o>x9R!f#vWBPV 0_@5jBƄdY3aW2Bp ! ޷l!{0) L @R2 1VkU ֯oLY-h8u3tգ BqX)P*㚺XpZ8 !@"dIEn hBp% Ļ${'@3$̗-oRGVF =gذ. AKskD%D" $d5; -BcIN'DݴZץA08Jv}H HLGӎD`=SP\VQYskٗsi}{oܲ7k/=*{ W}!#D0RH!0Cj0 2ԊD6dZB 02<GOyqcRJRb@ 0BHR"H"D=Ht˴'=e`PA8>aȤ%qhN5#L tw4lY4rZ[R(/N\r@c)9Cv\J S3L qs3{d׶`51P@9~v+l!Fec ,1=?㞝HRf3DrmlE/P$ Iٹn&􈉃=HBu3W\֧vUFJ R 1)B4Bbs=NQrc;:{N#c+"Խb)%L1XJ$,#4.+0g{ڱvShܼȫ:8}G \!]<8?us/ͫ;책jZd (0fQ&tL:;,Z=Hr1VdAumע[:O0 Zgc.ydnOez+O`9cUB *P:놦k˻ c F :Eʏ컳;B33#$m٣:RJ@#,> ݝipDa##|3$H$ޞ ~׶:hMf8CN*LK͆&TZa 鵬x)g)X9U;Gr_ޢoʙX~}ž/cikӏi…om^8u(o_5+*6ޚsրOj/^aM]rLLæ6=fpBwgܳ]'XIJf^rәx2ٞ6mW8Lz8 DU .J--Lp9!DzLӄ#?iܸRwrDw:7`c7to~㍝2_n˦#Gt=W1Bg,P@4|;\O_~>O9 -;km˄ghȐhnaϝ>ere` U:1UEyyż9U /EdvALd7CሁQBj*!]x]T/c{Wknu 0ʒ݇fJ)$yʞ^M$2 ^[Y~⹓XŻzAUrZw8e(R/w%k5'ABEW+  VwIHo"J W =)A,|;@ q=!$ K0 [5'.I B0QQ60g+R6dƎ=i&Z :1w#4nH>@U%<@j(|D_NM]ȓTN\vreHR(e6)(Xr: 5"\&9$4̙t* Vř^ӚV1HH$3@""sP pfиQNxl|N0Nf`3BKyRH.v+S[@:m`i^p'1חq 7ur l)t@ECƝ2d0z呋&7
!Z6US<_.Z~ZyP BzX~zFGJH.rǤdsmm-hj}ikjA25[wĵe +ljTHp\s}) =21K@nNPxs= iS[ܙN6&3/O0Xqdz@2ˆaQ]/"Fƺۛ<=jHwc[K$P)^n>CŅ.{5*w>72z5.ڪ5mLxe lڜǍRj߼B#Jx60i.KW]nfI} 5 WYav`"ؠ򥐜1t}) F7>9S2'0iH"1IB11u1sQ-َۜ unj[|aAVdM,I0B\ Ka1` IFCL"\m$aҙ$\7!SpٚOu(xGgs\5~|۪~~U}rTK:alP |ǘ!XHąI9OgDȵ]/mK!zH/_5RNv6BDb$pB2JI4# .҄$AmnN$Ѱs8.sA1}x>0%!SN1eGu$ʺfKHM'XH%cR.'X̰F7<抴\xJHnu扌ń+4$hC ;c#bX^0@D(VP(_:}57a(` HJ3!8gR_"A&$`p}ϖݕ՞\j46sRqmKBt0 3SF=7IA1ifZSP$p$fش̀f}k Jϝ8upP0܌c0փfj0\ )`sԦ BP P( @ BP(R( R BP() B)BP(J BHP( @ BP P( R BP(R( R BP() B)BP(J BHP( @ BP P( R BP(R( B)BP() lgc3L+ @ª[3/dMMs"Ee1«/MZu~UÕ# ?xheܵOmuUSC@_\rUIWB٭Κ;Ͻ}}U0W~rLW~;w5ʟ^7_Γ-[V곯۰s̙ysMZެtmL&FOIQLgUP| ;͉=O ;y9#1ϲPƠiLX탾>uF)KwlG<$Ni`s/=Lj,GzQ;h*sOVDn’Ci|)[sV"hP`1 ݔn=A}2pSņ. r?'kwCW&X|w_u̻V嗱|OР+Wz+$\??p|l|{dSo,jECǟy5>kPoonp`_}|g{=w7W7&%Î'K\'ve(|Ewu ]t/'kM]xuPFw|glif A裱jv o\f.P9CklAؒs[֖hpt'O3%m[}^Q⿲~ >0@#0m[˵[nOul(mTmsSV{ $+߿juݾD~zhŭy暚Ƹ 8R>椯] F`;__/*C}շ,^]8k,]ۘZ3NqCMhM R:m]rͭ?]~bEhny1A ^no?/Εw_ޖ/Q3ΥS"Oߺm95g %V>}/ߊF^]k޵ǜ1@W9 zS]J6dOtո?;R vWP0WnZϽz=yY2( 7R LP^mFzpjS#o\A>eVWi+̦eWup~9; 璽)؜o^Z9ɤ:֜ֆ&('&~aJoWO\UPyߎx,X]x)cGQuG&*=9~(5t6QTKv?SzЎ':W7~vũeO^h coC3{,q~8=--+k@v4ʲ ߼zCncUR 6"e%e}Klj?#{S5?F^_rl7; f>ͣx`*Zikn7 d䗕IZF[ pi'_c8+̣?Ysy7$n;қ`?-"b{q?/.ݑ៺^w9XUgv}m{xz,z,qXl7gPKywT[RJ;_F,]\3I3bؤhۓHX,6;d/b|K)'b\mg/zX,vͻb;q7[^$žݗ8=+NG*[_o]o_v⚺][_\rOxfٳa Y=0/'{J}0{vῚ{e'Kܞw.0{6<w5EgÓ~εWΆ=uKI)y/̆x0= s&T''*[$.o;9߈bKJOm{d=8#Ŏ=Ļ޿X,3`5:g\05!+b_{aw">cI.'֭ypj,;>]9bמnb{ʲˎbxqOקNuѴӎbqǝ~7W-:6n?oF/{ E^oqD=y5N:Ǐ~~YE+;|ok>tL( er9}ld{ _\(h1pϽ>Pz7 oOS  <{== KWo|ӫruoGǺпHZ%,;ܻڟm?=S 7 k#~懥`6o\_\BQtuwl\wX:8t^AFRS~ e1FASGk6FҨ`&hTsVoh(EO`-O^o,QTKwNƢ+@N>Νي @?18 owįg569t6Xǧ9|h\\\?06IDAT\Ғtc t zݭYu ܅}uԓa;+XZf#՝̝ā+ͷsz2pV}lwVu;}nw , 0jW:msXbHP& lF29,0iMEpvmݱL<ඌ|ƾe6'ƾi:xq$7ԏe7:UY{NQ,)B%jҙl[BN}95zy1]c|kiiasJA# tq:K@ Ia^|%# saE޲eNzAr/.@X6wuzO<9q!M+KW,>-u5+ )uؓ1,0hK_7{xiXFw,BZWZPoJ1nqV}ke A ^'\]ۖӡq\}|;!ѣ 0-դ7[?q,zm]6o7o]CSʓ?Uۛ,;|qt_ Eыy#l?.Mj1lkDNaT2R*TD0t ح?ps~% t@8o,hUNlߚnhKOEy{;7Z=kp.gQ;ducczpmTw ˷[h,mpӭkԝRf\ ,$K8oD3?{0Nʡ4Y5F{*#9EK\A+*Hb%>F~5^E4 .Mn =8oj:;Ԅ$aRެ逶d#cv{R\9pc[4:Ç 颱x }cQO S`0!~ -T'Nio(Rb/q%K3ꮸлQڲ [,k l;\,>ڌrGI'~32]nf!IbswK$c94nɏD)=fXQ'zGcr՜Zm`E.6QQ+h F@w`X"C|_]3jR1:]/aWJo"5D(_|1:f73)O;v*=3ލ=fV}lwde=rLhUxVU5I|GqMXvX#gէV}׳p̣uE %nkZ fCd_n:$[4Oܭ'۲ TncC Z4j1^=i~927m"KY:qX\ ,=^D֏jP\RJWX p h0|b=%Q~,66$ 7K/5jo م_rQi_z o_YUE5Ujc}fVu{Ͼ[KsSeBժkn*5ϻ2"'R EPk$p46vclN)`zrfwϮ.nj?>v}GfÑW\N;{R^"HgڵSںЄCr#9[ɘ>qh+۬iW;{IO!Ol|3{wz4,;xp&-:w.]'(\כ ekXKI9~Rq SCف)3Bvm/(qe۾5Юgլ鞢jz b,nh* krǹL& '"$\ݹ-A~ɳ **Aٍ z 8&\u=] D׋Bo֯M=)U.;+O^s'gO^׋{>0a;ϻw[3ʚ(LI@'W\يwTTߠ/|tK{c.l_pNe3b0W(;Q_IA 73-WC9s;66 dI 7d9 rϑӹU|ȸI3L|w1Z/-4U{Ȉ]?۟E@@  d@ D@@ @@ D Q @ "(@ t:Ge/NᾉLjg4~9uͲf1&\9n6i/B 57tA0岭f+`@ ):snk1o)j Uy _}@Vc, [{[jM3K꣞zb$@Ȣ+әdRx}k):H!;žog\oʔV v݉]\hT_|rIȊX-LJ]?_сy'>#%-ZGi'>tﯙy5Z6|M w)YQ4eA*wxO6蝁!L̬Snɏ1 kCe 9VG=M?]ig]GM IS\@zDPMGmp5Fyxi ;x s/ʾ x{Or0o d> &FG0@stOs꼏<1Xڷ7lY#2X &ٝ# DVmvNM/_5!5ㄨqbZU+5f:7 $"'?(XPjd1X/pf;+O Z\i >[1g;O?; s>sS>~ M rO.wsRNhQ\$I^oD^]VvUsi"& t|LPY{n-f>eO:'v gdT1st c|X f28Q[x'2Q?p y 2&+l;6.h_7F^9"_djR7*eڷG),UMUӸtzM8n'|/ P_+)GĔ<4:77^1[?lS _tJ"8\,~nb yCzKc:1PF(0RMܝRg-ٴTն75ɚ Cʰ6g~X f KYL*Qi!њFѨ4e*=<ɨ4Ufw;jQK' 0'-FS.;Nr\@# @q6 {8wL_qp[m>c_߲j> F^jSS*/Kqn7TeyZ8ikG벤 }wdEݦS4h_NM^wLZZZXqV,m+XHC 9o­ck,:wݨ:q87|6`iwE_l4|!g_D`M#̊Cl)bS NL`:1ªz#% MlK@ Dh,m0\\SX$V"nǨn[z QN+qd]onv[(t |P4;L5 1@v.YvNV,(qO~$N 8+7:j/rZAq~sAe  c PuϨI|% xo.jEs^@0ӻSZT*-<"1^nLS]AJu_dŢUb9}T߫}J/C"jC5tVݎ@Un)|:J 5|rO~tGǣ4: uQkhLdzgyJ9a`HPoOԊ3xlʬd@ ];$:w.]'(\כ[6F 4a)q.`FUъK丸Dڛ}'n?Rڢ=iYJVmvrӭNoܗ^>$K;*˜qjLEmW-Nug)_! mRh0*ݥn4ִ9荖phʜq MelwaU朌i Spdh"'O7G!@ w tӪ@=#kSj6`Ҝm @ UǛ);eZ'K?J@ 9=CBC(PGey̐ZX W7"$@ } ;FiNzIi@ m*,\-%G^>,Gq-tY:U^ p> 7 (@ <@ ×,JIENDB`pytest-4.6.9/doc/en/img/pullrequest.png0000664000372000037200000004121313604173400020741 0ustar travistravis00000000000000PNG  IHDR(-qsRGBgAMA a pHYsodtEXtSoftwarepaint.net 4.0.5e2eAIDATx^pǝj*w[[^y+wUtFNvqR v;lPS CF,A%Be!_x%,xf{gޓ$Th׿}=Fi M@x /4& @H^i M (e2wl-[<'Xt\:$\ ErQ$E+NO.iF?(];uR9kάOaHT9P8]2QIBZ /žE'iQ{= ĽƢ1YD>'ZII":h$8HCS>X¡59~*a'iQq]̬OiHTZpֈ$Q}&:ގJ˭|8RVe|k06S(Dg C *ŖջE睦\[P1Ψ vP[f%&t!Vj (ٯeg! /n5t\g2: ke}>غ (![`z}> vp\2/-NXZ(¥.-ɷy_Ϊ: Tt' }3Z܂Aa>8S{44 ٲ~[GN~h`n:Mq`6k=?MB${fSB5&g}9`y.u -CKtEJe)]@U,[sD#ݕ3%>>MJIso>ηlơGՏJ|8K%#r}d!G'4qc…E|&QE` µR~*j:]Y-?VS\ZsP[0%8~+Vg şą2b>R KPWX5>dޓ#^/=!(%/m.0F$v ź*c_(Xb)lأ9C=YOQ9 e%(~c' ST&^jo9youOg.AX—ܯҭl0τ_֩J}5\h$]8okYsT+Zs;e )LyD(EVk ?{4\`Zݭ 5eĚ8U w|;GCeCMB6a:Uՠ4k@Q\a{^av*;SjI]j]/" Žqx R[0ZպלJ;%]!SS䞡.\EhՆًC[vUo}Dhz훟v#zǧs4[\V:Vooƿ.[}ll* #vHSQWuD@u^v֩i4u1<`wYC{on:@xʧɷ0ve)z:븃~n릨*>=Wٶ6v^ {g)uxY}<v(5y˰j;^qKD ȑ9G5GEkot'!mB@e$/S+Y' ePx~T`%h(ز|zIpiKu75~Po5Je]JR|-tVXkNbF?i$E?{4\pjWbU ^[֟8ѱz9-.+* 2Յ֪ykҬ1Wf6kIiSĜ`aZQJ)d?t/֮;QTQPkay4x+SWbQG߯\{ hǡڢR6agij;4xEAf)5׿#zϪ9-Z}\E-\c$wDi*Ab?+5C,!E‹|?r*7:ȇjʘo8%wR' ](I@,NҜ? ZY{6jѬ}ˉwjW v۬OpܽtbJJLQq.0xN\TWt]tUkvԆS9G1R㤄>Mz` SkԱguq_{=.o8=Wɢl^yO q@ms' R8;tVj h}NeF0ysEE5.3U;\y˒cX βQ[*MBgAYG s֗_&_9h"N~b&_Q< V^tk{,~u">'|ͯ$~0(ǁvf(B~GFs 1ml9hEOw+)lakVTjrTV?Oh(t0ٿ \7܃467-f׺~MV_j.n؋FX"Ss|ƕI }+SWOMU|/E[Et8;q;(1T) ٯ|XE H 5>, Gkys,><1[?X>v9&RwSk,SKk̕0eߔ?^Wj񷒹YAʄir{=SzQ iB]Ʋg! 勧ZL\I~-*Y ji댟`?9Y^P:hB}SK$~)lkVT* ʿ`-|zǷsTT[:߅ JXpYWRS=?ev<>m3?zXKci_x1I=N t;/r t"D_><1[c,\v)7p)R$|\xT]'mk.?>&Hۆ*zJ7Tȃ)3 iBM =rl94Zv殫fgՊKjA«%՝S壻ZOgtO_xp؟*= &ďYdP «ȉڬ]BxM^!U_ܘ%Vl`u. /Z솠Ax)K\Q[v'>#^2qf2[;KOЊd<+^EX)^O\9BYig$T{-",dx*MWxzK񏻥3^ZcIfݬ=U/^a扑h4r ᵤm}_5Y-Qt^Xjd>K*fO߱-4ֳ_5N)iUW]c=.CY_5K\ה@ێ##nZcJ,tuyWvcc{?t^$2ii/bKPLxi,^g>qJ+LڽE/+kWRxn9.㥽 L֖ >_x[Y!ڎ?+E%L$1&8vJx՗[Nj^n&.XjltskYh{[Ƈc-y͵J/ ^By|p4:g.4cx‹ūub~߉0iUuUdl'o + 3{u9/W;K*H^ @H^i M@x /4& @H^i M@x /4CxMNN\vĂ')<^78DH8ݼyS{!HQ2>>~5y!{ @2H^i M@x /4& K9og g. 7n޾}رcG-Ṡ~(3 i6-0266vIO12 8 u СL,B9yø@$r9JێBMMBhb@Lnݺu 0"v")2H0[«8!hI#n-zFKm*|st#ϼr]{?b9>S.wg<ll\‹X;}g\ ,{3ĴJ.hmm:i/eFJ܂10Kw^}}+Dcƣ]L{&uq¨){2WW]uA\UUex3lNsĹ;̿JĔA>MZvbM>q e(9\Ebd:m^>3كHtwwGiEV#Xڒ]x..Sus:5gX~ġ=͉Cap/K<2.dw->o*1ek{dT4ޞbscQ|%2/ mreh|L5Wtp H$Z3YxUz6j)r3 ueyYTl]I1&wT^I+DJ)tDbK}zq]3{0|MJf kܦh+^뺋?)cZ'%5۰(X\">rxAB񬏕13tR1BI+X#T?[rh/)`nQGgY> S80lRhp&ؑ6~s4bF|6ѴZ=}Z$'R#l<4ɓqo,Bx/ϟъzW}uWf{lDntӜ٫\} j0V+r󊪯v1 A^tAyxڀT]7Z}b=o261cN6R^#$zΝ;#bi/)`nQܳ}R5f 6a#vd,ǎ;2V| .m%Lneeَqf'm,osYxa.HjkЛ[ sGxpEC5ָ? guK6%eͥbUWW싫s!tnJۨ=h鑑Z?K2OeI&wE/Rcbԩ4]lY+Oi0`ؤ4 [[iّ858 W8]<̩f  TCchh͛C$@ ‹~U_m7fKs2l2gYu޽{U$UYk2(>?z_'}n0nwX_.jͩb׾N>1ܸs<L(]ah-/xYO(W0hr}`cƜc4Ɂe.#$KGu0L9sŦu2R<0lRX%iuرji)EyfD|*3lZ _/|gEbS* 30lR[ ‹N|xYf;NN)L(5w3Viu̾6g~Bpg\0! PHQ-)ZHy eE$q&a`OT!\`H]٫_U.0[ Ҳ5H^i M@x /4& @H^i M@x!ᅀᅀᅀᅀᅀz,tU"1_vM )1׍7JN}jWXPww͛7CxMNN"bARb/, @H^i M@x /4& D uuy.Ϥ.PTbrr߿x0c 7'#ZCxmZΗV{_>xnҢsΝ j}{]ׯf!)ccc?jvޕ5mMvo}$KViAf}E=BR-0+Yݺ([HViA~D"lgԖE0 IV^/_/9 հ^``V |;t~yST⿹\Ձ}[f^zJwռpGF$Dur7FKFBkV{ּ=ƥZ|䬈fʛѱ/{wz232Z~=AWۆ%.\_i=r7׺Y `2Ӆ׊o췌7n<{7:<oQAy㹮Qxܬ=54>t_r+cjoޕ) $~5w]ER0 I!!^MB%]On~W+pK#q†#Sf,fʚ#<3| y/V}"ů47õc.Ѩ$iA`~IvFFFvWm^?>o8=_"۪H{ wYPsO0 /2ݟkGrYGHfLRl b*mYΏcZ6nUS<~e.vzjFi\T8V.^dpK]r"痗چS-_| 1FۄWnrk-4RխQx4ץnǜʄW;\K:|ɊlغvHٸ3d)=gD@ÞUs:mSJؾy}9|xP|EѤyy6Xer]E^ 2Uړ{.JeF{^K<+m«1,#r]jm߼j3>y .!2b=~dZS_ҭ诘+Ev`.}/ʢyZ\⚧gҌF5 ;w~ێbsa|SWgmV )Uem$V]t5|h yV'Ygʗ9y-^gl[Ë232׷]dkWLNQ;2ȳ;}T,` ŝ>ڋT7ջMT;. /`}CN2FښNylˍ1KxKVd ^0K ȋO MHx'~X֒V,ؗ_@\u'+k"瑜R?Ic [H89oKY yv< *&cBs6ջg}Nœ%QꜥgTԪ$wT ~˟R(=b2$ ][X# $$cy;+큞sc Alg6q%_-|<& 0^ o}!["L#qKUh%q{ A`$${ lg6f2i o{}R*+ɒUZѽd}?3dsECpׇ;^$z*hfc2('姳\*ʡҨL*A"֭[kCղ?C%m_}xnҢ6m[#ׯT@ʸp"G>*YʧZ..)oV^w z=@*UZLNNkH323I/$GU]?}wˆEf?W>=Ux+('姳53*,!{*mj|!*!W='\FLcRT E5RT$kf^@zioUl]hّ@=dkI,'`@xi̦u w[?hQ`!e? y[@xqYZ}bTB~ oK 𚘘vڙ3gbѮ"mqCe, g?%򕌝Ӑp"$«ʕ+CCCL!U6}C7?%yLFQFFFH8BC EbhهS]VdlL#cc9fkhh̙3RHy-d19 ֺ&s'c@,WrݫVFQH>I! "r L^p@xIT_8eo{}ME 0q([G"eųqI3䚕L[9 .c]N( ˽[~ȓ ȝZ=\1$ɗ_dK5S~|Fŋτ wXSOj>UQQwuj// *q2^CEՙ-uU-7Hu=zdCCS5@ȇM^$?Qvk8Nn:WɺS夻l*o\]cLg[\sb U6Eaf;*Hͅ\{<Ť]Dp H LxM*W9>~"iSk_'/ޑgfd{zmULp}(;#ceA-$$]}%l?,^SEE~<,4`J'gp2.a Wi_0-OR wH Hj_Wk;R4S!]fedTg},$i)E7&xy 'uiu7n<1J}dHA[x}m[ik']H]EӨ\xmVȒ zȵ7.]Q>R,Ŗ^ku, mQFlⱍuk!'2ڴ;v+yrsѰ{+^HVۓ&?*?6`9*Q n6QJKP.OYPURa9a@LV#aX}iEH[x)G#W-*-eo#Wh^&Ek3}KjٻZFDfnV#o=wp \.uy!ӬӴW^6Vo,ƅp$hM[x${Vozf/o,3FFF{.W*ܴ QuD!&ar*,7=]YGs dc2ρҙLMlܸRqdB-N1z L ٣h0ixI)۱^)݁jwJ1j)ɅY$g*B.%P,esk߼n42 ?GLNŘ;#R^d?-)/:E ֻ1\$ŷ}>_h $t3vŏ]%5xA]YcH~]]XYX.mD%4rŐ'^.C6h]!iܬR&%m{O*#GlݥhlN~^)6]x tyI)[XNf@ ;@8@rÏ^Lix%)xx:~MI&R}P\Fw˛~~#oq a΢mNommvAxpQ0MMxNGtFfBEQ0 =iwF/[l6qƓx]erUN'^90w쒝ܬ!4xJ<͹Avyvwyڕ ^q1~\ikʻ[NȦNe^&oMExE"#g>~j8RGgg9[@CPX9ңlyjW$"Iÿ3G[*r=xRDq>F}UkryIղl}ʺItjvzcus)&x闎r%NfȇIKV7I8wJI4핗Ul y2A1d(zI+]:\4zLq?e?>xʪ76~j<9͞Ec녟(I[\/xʽؕT ĩmؿyq1f%S]Dq_#x, H2J=!/Iup@x^쉥`. H Ǐ}WeV y!_ع2^S!Vt&W- τgg "H!// X)2?M9-)FA^@a?HmP ~pPś{0ɭ?rW4ס:"?9FzoM?L?ܼ~챿ZGmwէ3Aܜ?BL}+?T_'sj-"jh KlCxՆo ! L3 >#7nkJrgo,'9.jqV6'5R n^^l"B0яo9=ӯd!I֒Ͷj˕ a[j_ZV0>Cwշuw )dYBUd9Op`/n^(1+aUP?"3rY j)FS-THR.{BV< aFo{yvQ/>Gh=ovouuO% ~EI,:Jr4*ӵ%@IdYED+ړ$kIENDB`pytest-4.6.9/doc/en/Makefile0000664000372000037200000001257513604173400016543 0ustar travistravis00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . REGENDOC_ARGS := \ --normalize "/[ \t]+\n/\n/" \ --normalize "~\$$REGENDOC_TMPDIR~/home/sweet/project~" \ --normalize "~/path/to/example~/home/sweet/project~" \ --normalize "/in \d+.\d+ seconds/in 0.12 seconds/" \ --normalize "@/tmp/pytest-of-.*/pytest-\d+@PYTEST_TMPDIR@" \ --normalize "@pytest-(\d+)\\.[^ ,]+@pytest-\1.x.y@" \ --normalize "@(This is pytest version )(\d+)\\.[^ ,]+@\1\2.x.y@" \ --normalize "@py-(\d+)\\.[^ ,]+@py-\1.x.y@" \ --normalize "@pluggy-(\d+)\\.[.\d,]+@pluggy-\1.x.y@" \ --normalize "@hypothesis-(\d+)\\.[.\d,]+@hypothesis-\1.x.y@" \ --normalize "@Python (\d+)\\.[^ ,]+@Python \1.x.y@" .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " showtarget to show the pytest.org target directory" @echo " install to install docs to pytest.org/SITETARGET" @echo " install-ldf to install the doc pdf to pytest.org/SITETARGET" @echo " regen to regenerate pytest examples using the installed pytest" @echo " linkcheck to check all external links for integrity" clean: -rm -rf $(BUILDDIR)/* regen: REGENDOC_FILES:=*.rst */*.rst regen: PYTHONDONTWRITEBYTECODE=1 PYTEST_ADDOPTS="-pno:hypothesis -Wignore::pytest.PytestUnknownMarkWarning" COLUMNS=76 regendoc --update ${REGENDOC_FILES} ${REGENDOC_ARGS} html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/pytest.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/pytest.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/pytest" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/pytest" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." make -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." texinfo: mkdir -p $(BUILDDIR)/texinfo $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: mkdir -p $(BUILDDIR)/texinfo $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." pytest-4.6.9/doc/en/funcarg_compare.rst0000664000372000037200000002111313604173400020754 0ustar travistravis00000000000000:orphan: .. _`funcargcompare`: pytest-2.3: reasoning for fixture/funcarg evolution ============================================================= **Target audience**: Reading this document requires basic knowledge of python testing, xUnit setup methods and the (previous) basic pytest funcarg mechanism, see https://docs.pytest.org/en/latest/historical-notes.html#funcargs-and-pytest-funcarg. If you are new to pytest, then you can simply ignore this section and read the other sections. .. currentmodule:: _pytest Shortcomings of the previous ``pytest_funcarg__`` mechanism -------------------------------------------------------------- The pre pytest-2.3 funcarg mechanism calls a factory each time a funcarg for a test function is required. If a factory wants to re-use a resource across different scopes, it often used the ``request.cached_setup()`` helper to manage caching of resources. Here is a basic example how we could implement a per-session Database object:: # content of conftest.py class Database(object): def __init__(self): print("database instance created") def destroy(self): print("database instance destroyed") def pytest_funcarg__db(request): return request.cached_setup(setup=DataBase, teardown=lambda db: db.destroy, scope="session") There are several limitations and difficulties with this approach: 1. Scoping funcarg resource creation is not straight forward, instead one must understand the intricate cached_setup() method mechanics. 2. parametrizing the "db" resource is not straight forward: you need to apply a "parametrize" decorator or implement a :py:func:`~hookspec.pytest_generate_tests` hook calling :py:func:`~python.Metafunc.parametrize` which performs parametrization at the places where the resource is used. Moreover, you need to modify the factory to use an ``extrakey`` parameter containing ``request.param`` to the :py:func:`~python.Request.cached_setup` call. 3. Multiple parametrized session-scoped resources will be active at the same time, making it hard for them to affect global state of the application under test. 4. there is no way how you can make use of funcarg factories in xUnit setup methods. 5. A non-parametrized fixture function cannot use a parametrized funcarg resource if it isn't stated in the test function signature. All of these limitations are addressed with pytest-2.3 and its improved :ref:`fixture mechanism `. Direct scoping of fixture/funcarg factories -------------------------------------------------------- Instead of calling cached_setup() with a cache scope, you can use the :ref:`@pytest.fixture ` decorator and directly state the scope:: @pytest.fixture(scope="session") def db(request): # factory will only be invoked once per session - db = DataBase() request.addfinalizer(db.destroy) # destroy when session is finished return db This factory implementation does not need to call ``cached_setup()`` anymore because it will only be invoked once per session. Moreover, the ``request.addfinalizer()`` registers a finalizer according to the specified resource scope on which the factory function is operating. Direct parametrization of funcarg resource factories ---------------------------------------------------------- Previously, funcarg factories could not directly cause parametrization. You needed to specify a ``@parametrize`` decorator on your test function or implement a ``pytest_generate_tests`` hook to perform parametrization, i.e. calling a test multiple times with different value sets. pytest-2.3 introduces a decorator for use on the factory itself:: @pytest.fixture(params=["mysql", "pg"]) def db(request): ... # use request.param Here the factory will be invoked twice (with the respective "mysql" and "pg" values set as ``request.param`` attributes) and all of the tests requiring "db" will run twice as well. The "mysql" and "pg" values will also be used for reporting the test-invocation variants. This new way of parametrizing funcarg factories should in many cases allow to re-use already written factories because effectively ``request.param`` was already used when test functions/classes were parametrized via :py:func:`~_pytest.python.Metafunc.parametrize(indirect=True)` calls. Of course it's perfectly fine to combine parametrization and scoping:: @pytest.fixture(scope="session", params=["mysql", "pg"]) def db(request): if request.param == "mysql": db = MySQL() elif request.param == "pg": db = PG() request.addfinalizer(db.destroy) # destroy when session is finished return db This would execute all tests requiring the per-session "db" resource twice, receiving the values created by the two respective invocations to the factory function. No ``pytest_funcarg__`` prefix when using @fixture decorator ------------------------------------------------------------------- When using the ``@fixture`` decorator the name of the function denotes the name under which the resource can be accessed as a function argument:: @pytest.fixture() def db(request): ... The name under which the funcarg resource can be requested is ``db``. You can still use the "old" non-decorator way of specifying funcarg factories aka:: def pytest_funcarg__db(request): ... But it is then not possible to define scoping and parametrization. It is thus recommended to use the factory decorator. solving per-session setup / autouse fixtures -------------------------------------------------------------- pytest for a long time offered a pytest_configure and a pytest_sessionstart hook which are often used to setup global resources. This suffers from several problems: 1. in distributed testing the master process would setup test resources that are never needed because it only co-ordinates the test run activities of the slave processes. 2. if you only perform a collection (with "--collect-only") resource-setup will still be executed. 3. If a pytest_sessionstart is contained in some subdirectories conftest.py file, it will not be called. This stems from the fact that this hook is actually used for reporting, in particular the test-header with platform/custom information. Moreover, it was not easy to define a scoped setup from plugins or conftest files other than to implement a ``pytest_runtest_setup()`` hook and caring for scoping/caching yourself. And it's virtually impossible to do this with parametrization as ``pytest_runtest_setup()`` is called during test execution and parametrization happens at collection time. It follows that pytest_configure/session/runtest_setup are often not appropriate for implementing common fixture needs. Therefore, pytest-2.3 introduces :ref:`autouse fixtures` which fully integrate with the generic :ref:`fixture mechanism ` and obsolete many prior uses of pytest hooks. funcargs/fixture discovery now happens at collection time --------------------------------------------------------------------- Since pytest-2.3, discovery of fixture/funcarg factories are taken care of at collection time. This is more efficient especially for large test suites. Moreover, a call to "pytest --collect-only" should be able to in the future show a lot of setup-information and thus presents a nice method to get an overview of fixture management in your project. .. _`compatibility notes`: .. _`funcargscompat`: Conclusion and compatibility notes --------------------------------------------------------- **funcargs** were originally introduced to pytest-2.0. In pytest-2.3 the mechanism was extended and refined and is now described as fixtures: * previously funcarg factories were specified with a special ``pytest_funcarg__NAME`` prefix instead of using the ``@pytest.fixture`` decorator. * Factories received a ``request`` object which managed caching through ``request.cached_setup()`` calls and allowed using other funcargs via ``request.getfuncargvalue()`` calls. These intricate APIs made it hard to do proper parametrization and implement resource caching. The new :py:func:`pytest.fixture` decorator allows to declare the scope and let pytest figure things out for you. * if you used parametrization and funcarg factories which made use of ``request.cached_setup()`` it is recommended to invest a few minutes and simplify your fixture function code to use the :ref:`@pytest.fixture` decorator instead. This will also allow to take advantage of the automatic per-resource grouping of tests. pytest-4.6.9/doc/en/contact.rst0000664000372000037200000000317313604173400017262 0ustar travistravis00000000000000 .. _`contact channels`: .. _`contact`: Contact channels =================================== - `pytest issue tracker`_ to report bugs or suggest features (for version 2.0 and above). - `pytest on stackoverflow.com `_ to post questions with the tag ``pytest``. New Questions will usually be seen by pytest users or developers and answered quickly. - `Testing In Python`_: a mailing list for Python testing tools and discussion. - `pytest-dev at python.org (mailing list)`_ pytest specific announcements and discussions. - `pytest-commit at python.org (mailing list)`_: for commits and new issues - :doc:`contribution guide ` for help on submitting pull requests to GitHub. - ``#pylib`` on irc.freenode.net IRC channel for random questions. - private mail to Holger.Krekel at gmail com if you want to communicate sensitive issues - `merlinux.eu`_ offers pytest and tox-related professional teaching and consulting. .. _`pytest issue tracker`: https://github.com/pytest-dev/pytest/issues .. _`old issue tracker`: http://bitbucket.org/hpk42/py-trunk/issues/ .. _`merlinux.eu`: http://merlinux.eu .. _`get an account`: .. _tetamap: http://tetamap.wordpress.com .. _`@pylibcommit`: http://twitter.com/pylibcommit .. _`Testing in Python`: http://lists.idyll.org/listinfo/testing-in-python .. _FOAF: http://en.wikipedia.org/wiki/FOAF .. _`py-dev`: .. _`development mailing list`: .. _`pytest-dev at python.org (mailing list)`: http://mail.python.org/mailman/listinfo/pytest-dev .. _`pytest-commit at python.org (mailing list)`: http://mail.python.org/mailman/listinfo/pytest-commit pytest-4.6.9/doc/en/reference.rst0000664000372000037200000010465513604173400017574 0ustar travistravis00000000000000Reference ========= This page contains the full reference to pytest's API. .. contents:: :depth: 3 :local: Functions --------- pytest.approx ~~~~~~~~~~~~~ .. autofunction:: _pytest.python_api.approx pytest.fail ~~~~~~~~~~~ **Tutorial**: :ref:`skipping` .. autofunction:: _pytest.outcomes.fail pytest.skip ~~~~~~~~~~~ .. autofunction:: _pytest.outcomes.skip(msg, [allow_module_level=False]) pytest.importorskip ~~~~~~~~~~~~~~~~~~~ .. autofunction:: _pytest.outcomes.importorskip pytest.xfail ~~~~~~~~~~~~ .. autofunction:: _pytest.outcomes.xfail pytest.exit ~~~~~~~~~~~ .. autofunction:: _pytest.outcomes.exit pytest.main ~~~~~~~~~~~ .. autofunction:: _pytest.config.main pytest.param ~~~~~~~~~~~~ .. autofunction:: pytest.param(*values, [id], [marks]) pytest.raises ~~~~~~~~~~~~~ **Tutorial**: :ref:`assertraises`. .. autofunction:: pytest.raises(expected_exception: Exception, [match], [message]) :with: excinfo pytest.deprecated_call ~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`ensuring_function_triggers`. .. autofunction:: pytest.deprecated_call() :with: pytest.register_assert_rewrite ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`assertion-rewriting`. .. autofunction:: pytest.register_assert_rewrite pytest.warns ~~~~~~~~~~~~ **Tutorial**: :ref:`assertwarnings` .. autofunction:: pytest.warns(expected_warning: Exception, [match]) :with: pytest.freeze_includes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`freezing-pytest`. .. autofunction:: pytest.freeze_includes .. _`marks ref`: Marks ----- Marks can be used apply meta data to *test functions* (but not fixtures), which can then be accessed by fixtures or plugins. .. _`pytest.mark.filterwarnings ref`: pytest.mark.filterwarnings ~~~~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`filterwarnings`. Add warning filters to marked test items. .. py:function:: pytest.mark.filterwarnings(filter) :keyword str filter: A *warning specification string*, which is composed of contents of the tuple ``(action, message, category, module, lineno)`` as specified in `The Warnings filter `_ section of the Python documentation, separated by ``":"``. Optional fields can be omitted. Module names passed for filtering are not regex-escaped. For example: .. code-block:: python @pytest.mark.warnings("ignore:.*usage will be deprecated.*:DeprecationWarning") def test_foo(): ... .. _`pytest.mark.parametrize ref`: pytest.mark.parametrize ~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :doc:`parametrize`. .. automethod:: _pytest.python.Metafunc.parametrize .. _`pytest.mark.skip ref`: pytest.mark.skip ~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`skip`. Unconditionally skip a test function. .. py:function:: pytest.mark.skip(*, reason=None) :keyword str reason: Reason why the test function is being skipped. .. _`pytest.mark.skipif ref`: pytest.mark.skipif ~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`skipif`. Skip a test function if a condition is ``True``. .. py:function:: pytest.mark.skipif(condition, *, reason=None) :type condition: bool or str :param condition: ``True/False`` if the condition should be skipped or a :ref:`condition string `. :keyword str reason: Reason why the test function is being skipped. .. _`pytest.mark.usefixtures ref`: pytest.mark.usefixtures ~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`usefixtures`. Mark a test function as using the given fixture names. .. warning:: This mark has no effect when applied to a **fixture** function. .. py:function:: pytest.mark.usefixtures(*names) :param args: the names of the fixture to use, as strings .. _`pytest.mark.xfail ref`: pytest.mark.xfail ~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`xfail`. Marks a test function as *expected to fail*. .. py:function:: pytest.mark.xfail(condition=None, *, reason=None, raises=None, run=True, strict=False) :type condition: bool or str :param condition: Condition for marking the test function as xfail (``True/False`` or a :ref:`condition string `). :keyword str reason: Reason why the test function is marked as xfail. :keyword Exception raises: Exception subclass expected to be raised by the test function; other exceptions will fail the test. :keyword bool run: If the test function should actually be executed. If ``False``, the function will always xfail and will not be executed (useful if a function is segfaulting). :keyword bool strict: * If ``False`` (the default) the function will be shown in the terminal output as ``xfailed`` if it fails and as ``xpass`` if it passes. In both cases this will not cause the test suite to fail as a whole. This is particularly useful to mark *flaky* tests (tests that fail at random) to be tackled later. * If ``True``, the function will be shown in the terminal output as ``xfailed`` if it fails, but if it unexpectedly passes then it will **fail** the test suite. This is particularly useful to mark functions that are always failing and there should be a clear indication if they unexpectedly start to pass (for example a new release of a library fixes a known bug). custom marks ~~~~~~~~~~~~ Marks are created dynamically using the factory object ``pytest.mark`` and applied as a decorator. For example: .. code-block:: python @pytest.mark.timeout(10, "slow", method="thread") def test_function(): ... Will create and attach a :class:`Mark <_pytest.mark.structures.Mark>` object to the collected :class:`Item <_pytest.nodes.Item>`, which can then be accessed by fixtures or hooks with :meth:`Node.iter_markers <_pytest.nodes.Node.iter_markers>`. The ``mark`` object will have the following attributes: .. code-block:: python mark.args == (10, "slow") mark.kwargs == {"method": "thread"} Fixtures -------- **Tutorial**: :ref:`fixture`. Fixtures are requested by test functions or other fixtures by declaring them as argument names. Example of a test requiring a fixture: .. code-block:: python def test_output(capsys): print("hello") out, err = capsys.readouterr() assert out == "hello\n" Example of a fixture requiring another fixture: .. code-block:: python @pytest.fixture def db_session(tmpdir): fn = tmpdir / "db.file" return connect(str(fn)) For more details, consult the full :ref:`fixtures docs `. @pytest.fixture ~~~~~~~~~~~~~~~ .. autofunction:: pytest.fixture :decorator: .. _`cache-api`: config.cache ~~~~~~~~~~~~ **Tutorial**: :ref:`cache`. The ``config.cache`` object allows other plugins and fixtures to store and retrieve values across test runs. To access it from fixtures request ``pytestconfig`` into your fixture and get it with ``pytestconfig.cache``. Under the hood, the cache plugin uses the simple ``dumps``/``loads`` API of the :py:mod:`json` stdlib module. .. currentmodule:: _pytest.cacheprovider .. automethod:: Cache.get .. automethod:: Cache.set .. automethod:: Cache.makedir capsys ~~~~~~ **Tutorial**: :doc:`capture`. .. currentmodule:: _pytest.capture .. autofunction:: capsys() :no-auto-options: Returns an instance of :py:class:`CaptureFixture`. Example: .. code-block:: python def test_output(capsys): print("hello") captured = capsys.readouterr() assert captured.out == "hello\n" .. autoclass:: CaptureFixture() :members: capsysbinary ~~~~~~~~~~~~ **Tutorial**: :doc:`capture`. .. autofunction:: capsysbinary() :no-auto-options: Returns an instance of :py:class:`CaptureFixture`. Example: .. code-block:: python def test_output(capsysbinary): print("hello") captured = capsysbinary.readouterr() assert captured.out == b"hello\n" capfd ~~~~~~ **Tutorial**: :doc:`capture`. .. autofunction:: capfd() :no-auto-options: Returns an instance of :py:class:`CaptureFixture`. Example: .. code-block:: python def test_system_echo(capfd): os.system('echo "hello"') captured = capsys.readouterr() assert captured.out == "hello\n" capfdbinary ~~~~~~~~~~~~ **Tutorial**: :doc:`capture`. .. autofunction:: capfdbinary() :no-auto-options: Returns an instance of :py:class:`CaptureFixture`. Example: .. code-block:: python def test_system_echo(capfdbinary): os.system('echo "hello"') captured = capfdbinary.readouterr() assert captured.out == b"hello\n" doctest_namespace ~~~~~~~~~~~~~~~~~ **Tutorial**: :doc:`doctest`. .. autofunction:: _pytest.doctest.doctest_namespace() Usually this fixture is used in conjunction with another ``autouse`` fixture: .. code-block:: python @pytest.fixture(autouse=True) def add_np(doctest_namespace): doctest_namespace["np"] = numpy For more details: :ref:`doctest_namespace`. request ~~~~~~~ **Tutorial**: :ref:`request example`. The ``request`` fixture is a special fixture providing information of the requesting test function. .. autoclass:: _pytest.fixtures.FixtureRequest() :members: pytestconfig ~~~~~~~~~~~~ .. autofunction:: _pytest.fixtures.pytestconfig() record_property ~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`record_property example`. .. autofunction:: _pytest.junitxml.record_property() record_testsuite_property ~~~~~~~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`record_testsuite_property example`. .. autofunction:: _pytest.junitxml.record_testsuite_property() caplog ~~~~~~ **Tutorial**: :doc:`logging`. .. autofunction:: _pytest.logging.caplog() :no-auto-options: This returns a :class:`_pytest.logging.LogCaptureFixture` instance. .. autoclass:: _pytest.logging.LogCaptureFixture :members: monkeypatch ~~~~~~~~~~~ .. currentmodule:: _pytest.monkeypatch **Tutorial**: :doc:`monkeypatch`. .. autofunction:: _pytest.monkeypatch.monkeypatch() :no-auto-options: This returns a :class:`MonkeyPatch` instance. .. autoclass:: _pytest.monkeypatch.MonkeyPatch :members: testdir ~~~~~~~ .. currentmodule:: _pytest.pytester This fixture provides a :class:`Testdir` instance useful for black-box testing of test files, making it ideal to test plugins. To use it, include in your top-most ``conftest.py`` file:: pytest_plugins = 'pytester' .. autoclass:: Testdir() :members: .. autoclass:: RunResult() :members: .. autoclass:: LineMatcher() :members: recwarn ~~~~~~~ **Tutorial**: :ref:`assertwarnings` .. currentmodule:: _pytest.recwarn .. autofunction:: recwarn() :no-auto-options: .. autoclass:: _pytest.recwarn.WarningsRecorder() :members: Each recorded warning is an instance of :class:`warnings.WarningMessage`. .. note:: :class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1 .. note:: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated differently; see :ref:`ensuring_function_triggers`. tmp_path ~~~~~~~~ **Tutorial**: :doc:`tmpdir` .. currentmodule:: _pytest.tmpdir .. autofunction:: tmp_path() :no-auto-options: tmp_path_factory ~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`tmp_path_factory example` .. _`tmp_path_factory factory api`: ``tmp_path_factory`` instances have the following methods: .. currentmodule:: _pytest.tmpdir .. automethod:: TempPathFactory.mktemp .. automethod:: TempPathFactory.getbasetemp tmpdir ~~~~~~ **Tutorial**: :doc:`tmpdir` .. currentmodule:: _pytest.tmpdir .. autofunction:: tmpdir() :no-auto-options: tmpdir_factory ~~~~~~~~~~~~~~ **Tutorial**: :ref:`tmpdir factory example` .. _`tmpdir factory api`: ``tmpdir_factory`` instances have the following methods: .. currentmodule:: _pytest.tmpdir .. automethod:: TempdirFactory.mktemp .. automethod:: TempdirFactory.getbasetemp .. _`hook-reference`: Hooks ----- **Tutorial**: :doc:`writing_plugins`. .. currentmodule:: _pytest.hookspec Reference to all hooks which can be implemented by :ref:`conftest.py files ` and :ref:`plugins `. Bootstrapping hooks ~~~~~~~~~~~~~~~~~~~ Bootstrapping hooks called for plugins registered early enough (internal and setuptools plugins). .. autofunction:: pytest_load_initial_conftests .. autofunction:: pytest_cmdline_preparse .. autofunction:: pytest_cmdline_parse .. autofunction:: pytest_cmdline_main .. _`initialization-hooks`: Initialization hooks ~~~~~~~~~~~~~~~~~~~~ Initialization hooks called for plugins and ``conftest.py`` files. .. autofunction:: pytest_addoption .. autofunction:: pytest_addhooks .. autofunction:: pytest_configure .. autofunction:: pytest_unconfigure .. autofunction:: pytest_sessionstart .. autofunction:: pytest_sessionfinish .. autofunction:: pytest_plugin_registered Test running hooks ~~~~~~~~~~~~~~~~~~ All runtest related hooks receive a :py:class:`pytest.Item <_pytest.main.Item>` object. .. autofunction:: pytest_runtestloop .. autofunction:: pytest_runtest_protocol .. autofunction:: pytest_runtest_logstart .. autofunction:: pytest_runtest_logfinish .. autofunction:: pytest_runtest_setup .. autofunction:: pytest_runtest_call .. autofunction:: pytest_runtest_teardown .. autofunction:: pytest_runtest_makereport For deeper understanding you may look at the default implementation of these hooks in :py:mod:`_pytest.runner` and maybe also in :py:mod:`_pytest.pdb` which interacts with :py:mod:`_pytest.capture` and its input/output capturing in order to immediately drop into interactive debugging when a test failure occurs. The :py:mod:`_pytest.terminal` reported specifically uses the reporting hook to print information about a test run. .. autofunction:: pytest_pyfunc_call Collection hooks ~~~~~~~~~~~~~~~~ ``pytest`` calls the following hooks for collecting files and directories: .. autofunction:: pytest_collection .. autofunction:: pytest_ignore_collect .. autofunction:: pytest_collect_directory .. autofunction:: pytest_collect_file .. autofunction:: pytest_pycollect_makemodule For influencing the collection of objects in Python modules you can use the following hook: .. autofunction:: pytest_pycollect_makeitem .. autofunction:: pytest_generate_tests .. autofunction:: pytest_make_parametrize_id After collection is complete, you can modify the order of items, delete or otherwise amend the test items: .. autofunction:: pytest_collection_modifyitems .. autofunction:: pytest_collection_finish Reporting hooks ~~~~~~~~~~~~~~~ Session related reporting hooks: .. autofunction:: pytest_collectstart .. autofunction:: pytest_make_collect_report .. autofunction:: pytest_itemcollected .. autofunction:: pytest_collectreport .. autofunction:: pytest_deselected .. autofunction:: pytest_report_header .. autofunction:: pytest_report_collectionfinish .. autofunction:: pytest_report_teststatus .. autofunction:: pytest_terminal_summary .. autofunction:: pytest_fixture_setup .. autofunction:: pytest_fixture_post_finalizer .. autofunction:: pytest_warning_captured And here is the central hook for reporting about test execution: .. autofunction:: pytest_runtest_logreport You can also use this hook to customize assertion representation for some types: .. autofunction:: pytest_assertrepr_compare Debugging/Interaction hooks ~~~~~~~~~~~~~~~~~~~~~~~~~~~ There are few hooks which can be used for special reporting or interaction with exceptions: .. autofunction:: pytest_internalerror .. autofunction:: pytest_keyboard_interrupt .. autofunction:: pytest_exception_interact .. autofunction:: pytest_enter_pdb Objects ------- Full reference to objects accessible from :ref:`fixtures ` or :ref:`hooks `. CallInfo ~~~~~~~~ .. autoclass:: _pytest.runner.CallInfo() :members: Class ~~~~~ .. autoclass:: _pytest.python.Class() :members: :show-inheritance: Collector ~~~~~~~~~ .. autoclass:: _pytest.nodes.Collector() :members: :show-inheritance: Config ~~~~~~ .. autoclass:: _pytest.config.Config() :members: ExceptionInfo ~~~~~~~~~~~~~ .. autoclass:: _pytest._code.ExceptionInfo :members: FixtureDef ~~~~~~~~~~ .. autoclass:: _pytest.fixtures.FixtureDef() :members: :show-inheritance: FSCollector ~~~~~~~~~~~ .. autoclass:: _pytest.nodes.FSCollector() :members: :show-inheritance: Function ~~~~~~~~ .. autoclass:: _pytest.python.Function() :members: :show-inheritance: Item ~~~~ .. autoclass:: _pytest.nodes.Item() :members: :show-inheritance: MarkDecorator ~~~~~~~~~~~~~ .. autoclass:: _pytest.mark.MarkDecorator :members: MarkGenerator ~~~~~~~~~~~~~ .. autoclass:: _pytest.mark.MarkGenerator :members: Mark ~~~~ .. autoclass:: _pytest.mark.structures.Mark :members: Metafunc ~~~~~~~~ .. autoclass:: _pytest.python.Metafunc :members: Module ~~~~~~ .. autoclass:: _pytest.python.Module() :members: :show-inheritance: Node ~~~~ .. autoclass:: _pytest.nodes.Node() :members: Parser ~~~~~~ .. autoclass:: _pytest.config.argparsing.Parser() :members: PluginManager ~~~~~~~~~~~~~ .. autoclass:: pluggy.PluginManager() :members: PytestPluginManager ~~~~~~~~~~~~~~~~~~~ .. autoclass:: _pytest.config.PytestPluginManager() :members: :undoc-members: :show-inheritance: Session ~~~~~~~ .. autoclass:: _pytest.main.Session() :members: :show-inheritance: TestReport ~~~~~~~~~~ .. autoclass:: _pytest.runner.TestReport() :members: :inherited-members: _Result ~~~~~~~ .. autoclass:: pluggy.callers._Result :members: Special Variables ----------------- pytest treats some global variables in a special manner when defined in a test module. collect_ignore ~~~~~~~~~~~~~~ **Tutorial**: :ref:`customizing-test-collection` Can be declared in *conftest.py files* to exclude test directories or modules. Needs to be ``list[str]``. .. code-block:: python collect_ignore = ["setup.py"] collect_ignore_glob ~~~~~~~~~~~~~~~~~~~ **Tutorial**: :ref:`customizing-test-collection` Can be declared in *conftest.py files* to exclude test directories or modules with Unix shell-style wildcards. Needs to be ``list[str]`` where ``str`` can contain glob patterns. .. code-block:: python collect_ignore_glob = ["*_ignore.py"] pytest_plugins ~~~~~~~~~~~~~~ **Tutorial**: :ref:`available installable plugins` Can be declared at the **global** level in *test modules* and *conftest.py files* to register additional plugins. Can be either a ``str`` or ``Sequence[str]``. .. code-block:: python pytest_plugins = "myapp.testsupport.myplugin" .. code-block:: python pytest_plugins = ("myapp.testsupport.tools", "myapp.testsupport.regression") pytest_mark ~~~~~~~~~~~ **Tutorial**: :ref:`scoped-marking` Can be declared at the **global** level in *test modules* to apply one or more :ref:`marks ` to all test functions and methods. Can be either a single mark or a list of marks. .. code-block:: python import pytest pytestmark = pytest.mark.webtest .. code-block:: python import pytest pytestmark = [pytest.mark.integration, pytest.mark.slow] PYTEST_DONT_REWRITE (module docstring) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The text ``PYTEST_DONT_REWRITE`` can be add to any **module docstring** to disable :ref:`assertion rewriting ` for that module. Environment Variables --------------------- Environment variables that can be used to change pytest's behavior. PYTEST_ADDOPTS ~~~~~~~~~~~~~~ This contains a command-line (parsed by the py:mod:`shlex` module) that will be **prepended** to the command line given by the user, see :ref:`adding default options` for more information. PYTEST_DEBUG ~~~~~~~~~~~~ When set, pytest will print tracing and debug information. PYTEST_PLUGINS ~~~~~~~~~~~~~~ Contains comma-separated list of modules that should be loaded as plugins: .. code-block:: bash export PYTEST_PLUGINS=mymodule.plugin,xdist PYTEST_DISABLE_PLUGIN_AUTOLOAD ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ When set, disables plugin auto-loading through setuptools entrypoints. Only explicitly specified plugins will be loaded. PYTEST_CURRENT_TEST ~~~~~~~~~~~~~~~~~~~ This is not meant to be set by users, but is set by pytest internally with the name of the current test so other processes can inspect it, see :ref:`pytest current test env` for more information. .. _`ini options ref`: Configuration Options --------------------- Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``tox.ini`` or ``setup.cfg`` file, usually located at the root of your repository. All options must be under a ``[pytest]`` section (``[tool:pytest]`` for ``setup.cfg`` files). .. warning:: Usage of ``setup.cfg`` is not recommended unless for very simple use cases. ``.cfg`` files use a different parser than ``pytest.ini`` and ``tox.ini`` which might cause hard to track down problems. When possible, it is recommended to use the latter files to hold your pytest configuration. Configuration file options may be overwritten in the command-line by using ``-o/--override``, which can also be passed multiple times. The expected format is ``name=value``. For example:: pytest -o console_output_style=classic -o cache_dir=/tmp/mycache .. confval:: addopts Add the specified ``OPTS`` to the set of command line arguments as if they had been specified by the user. Example: if you have this ini file content: .. code-block:: ini # content of pytest.ini [pytest] addopts = --maxfail=2 -rf # exit after 2 failures, report fail info issuing ``pytest test_hello.py`` actually means:: pytest --maxfail=2 -rf test_hello.py Default is to add no options. .. confval:: cache_dir Sets a directory where stores content of cache plugin. Default directory is ``.pytest_cache`` which is created in :ref:`rootdir `. Directory may be relative or absolute path. If setting relative path, then directory is created relative to :ref:`rootdir `. Additionally path may contain environment variables, that will be expanded. For more information about cache plugin please refer to :ref:`cache_provider`. .. confval:: confcutdir Sets a directory where search upwards for ``conftest.py`` files stops. By default, pytest will stop searching for ``conftest.py`` files upwards from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any, or up to the file-system root. .. confval:: console_output_style Sets the console output style while running tests: * ``classic``: classic pytest output. * ``progress``: like classic pytest output, but with a progress indicator. * ``count``: like progress, but shows progress as the number of tests completed instead of a percent. The default is ``progress``, but you can fallback to ``classic`` if you prefer or the new mode is causing unexpected problems: .. code-block:: ini # content of pytest.ini [pytest] console_output_style = classic .. confval:: doctest_encoding Default encoding to use to decode text files with docstrings. :doc:`See how pytest handles doctests `. .. confval:: doctest_optionflags One or more doctest flag names from the standard ``doctest`` module. :doc:`See how pytest handles doctests `. .. confval:: empty_parameter_set_mark Allows to pick the action for empty parametersets in parameterization * ``skip`` skips tests with an empty parameterset (default) * ``xfail`` marks tests with an empty parameterset as xfail(run=False) * ``fail_at_collect`` raises an exception if parametrize collects an empty parameter set .. code-block:: ini # content of pytest.ini [pytest] empty_parameter_set_mark = xfail .. note:: The default value of this option is planned to change to ``xfail`` in future releases as this is considered less error prone, see `#3155 `_ for more details. .. confval:: filterwarnings Sets a list of filters and actions that should be taken for matched warnings. By default all warnings emitted during the test session will be displayed in a summary at the end of the test session. .. code-block:: ini # content of pytest.ini [pytest] filterwarnings = error ignore::DeprecationWarning This tells pytest to ignore deprecation warnings and turn all other warnings into errors. For more information please refer to :ref:`warnings`. .. confval:: junit_duration_report .. versionadded:: 4.1 Configures how durations are recorded into the JUnit XML report: * ``total`` (the default): duration times reported include setup, call, and teardown times. * ``call``: duration times reported include only call times, excluding setup and teardown. .. code-block:: ini [pytest] junit_duration_report = call .. confval:: junit_family .. versionadded:: 4.2 Configures the format of the generated JUnit XML file. The possible options are: * ``xunit1`` (or ``legacy``): produces old style output, compatible with the xunit 1.0 format. **This is the default**. * ``xunit2``: produces `xunit 2.0 style output `__, which should be more compatible with latest Jenkins versions. .. code-block:: ini [pytest] junit_family = xunit2 .. confval:: junit_logging .. versionadded:: 3.5 Configures if stdout/stderr should be written to the JUnit XML file. Valid values are ``system-out``, ``system-err``, and ``no`` (the default). .. code-block:: ini [pytest] junit_logging = system-out .. confval:: junit_log_passing_tests .. versionadded:: 4.6 If ``junit_logging != "no"``, configures if the captured output should be written to the JUnit XML file for **passing** tests. Default is ``True``. .. code-block:: ini [pytest] junit_log_passing_tests = False .. confval:: junit_suite_name To set the name of the root test suite xml item, you can configure the ``junit_suite_name`` option in your config file: .. code-block:: ini [pytest] junit_suite_name = my_suite .. confval:: log_cli_date_format Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for live logging. .. code-block:: ini [pytest] log_cli_date_format = %Y-%m-%d %H:%M:%S For more information, see :ref:`live_logs`. .. confval:: log_cli_format Sets a :py:mod:`logging`-compatible string used to format live logging messages. .. code-block:: ini [pytest] log_cli_format = %(asctime)s %(levelname)s %(message)s For more information, see :ref:`live_logs`. .. confval:: log_cli_level Sets the minimum log message level that should be captured for live logging. The integer value or the names of the levels can be used. .. code-block:: ini [pytest] log_cli_level = INFO For more information, see :ref:`live_logs`. .. confval:: log_date_format Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for logging capture. .. code-block:: ini [pytest] log_date_format = %Y-%m-%d %H:%M:%S For more information, see :ref:`logging`. .. confval:: log_file Sets a file name relative to the ``pytest.ini`` file where log messages should be written to, in addition to the other logging facilities that are active. .. code-block:: ini [pytest] log_file = logs/pytest-logs.txt For more information, see :ref:`logging`. .. confval:: log_file_date_format Sets a :py:func:`time.strftime`-compatible string that will be used when formatting dates for the logging file. .. code-block:: ini [pytest] log_file_date_format = %Y-%m-%d %H:%M:%S For more information, see :ref:`logging`. .. confval:: log_file_format Sets a :py:mod:`logging`-compatible string used to format logging messages redirected to the logging file. .. code-block:: ini [pytest] log_file_format = %(asctime)s %(levelname)s %(message)s For more information, see :ref:`logging`. .. confval:: log_file_level Sets the minimum log message level that should be captured for the logging file. The integer value or the names of the levels can be used. .. code-block:: ini [pytest] log_file_level = INFO For more information, see :ref:`logging`. .. confval:: log_format Sets a :py:mod:`logging`-compatible string used to format captured logging messages. .. code-block:: ini [pytest] log_format = %(asctime)s %(levelname)s %(message)s For more information, see :ref:`logging`. .. confval:: log_level Sets the minimum log message level that should be captured for logging capture. The integer value or the names of the levels can be used. .. code-block:: ini [pytest] log_level = INFO For more information, see :ref:`logging`. .. confval:: log_print If set to ``False``, will disable displaying captured logging messages for failed tests. .. code-block:: ini [pytest] log_print = False For more information, see :ref:`logging`. .. confval:: markers When the ``--strict-markers`` or ``--strict`` command-line arguments are used, only known markers - defined in code by core pytest or some plugin - are allowed. You can list additional markers in this setting to add them to the whitelist, in which case you probably want to add ``--strict-markers`` to ``addopts`` to avoid future regressions: .. code-block:: ini [pytest] addopts = --strict-markers markers = slow serial .. confval:: minversion Specifies a minimal pytest version required for running tests. .. code-block:: ini # content of pytest.ini [pytest] minversion = 3.0 # will fail if we run with pytest-2.8 .. confval:: norecursedirs Set the directory basename patterns to avoid when recursing for test discovery. The individual (fnmatch-style) patterns are applied to the basename of a directory to decide if to recurse into it. Pattern matching characters:: * matches everything ? matches any single character [seq] matches any character in seq [!seq] matches any char not in seq Default patterns are ``'.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv'``. Setting a ``norecursedirs`` replaces the default. Here is an example of how to avoid certain directories: .. code-block:: ini [pytest] norecursedirs = .svn _build tmp* This would tell ``pytest`` to not look into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory. Additionally, ``pytest`` will attempt to intelligently identify and ignore a virtualenv by the presence of an activation script. Any directory deemed to be the root of a virtual environment will not be considered during test collection unless ``‑‑collect‑in‑virtualenv`` is given. Note also that ``norecursedirs`` takes precedence over ``‑‑collect‑in‑virtualenv``; e.g. if you intend to run tests in a virtualenv with a base directory that matches ``'.*'`` you *must* override ``norecursedirs`` in addition to using the ``‑‑collect‑in‑virtualenv`` flag. .. confval:: python_classes One or more name prefixes or glob-style patterns determining which classes are considered for test collection. Search for multiple glob patterns by adding a space between patterns. By default, pytest will consider any class prefixed with ``Test`` as a test collection. Here is an example of how to collect tests from classes that end in ``Suite``: .. code-block:: ini [pytest] python_classes = *Suite Note that ``unittest.TestCase`` derived classes are always collected regardless of this option, as ``unittest``'s own collection framework is used to collect those tests. .. confval:: python_files One or more Glob-style file patterns determining which python files are considered as test modules. Search for multiple glob patterns by adding a space between patterns: .. code-block:: ini [pytest] python_files = test_*.py check_*.py example_*.py Or one per line: .. code-block:: ini [pytest] python_files = test_*.py check_*.py example_*.py By default, files matching ``test_*.py`` and ``*_test.py`` will be considered test modules. .. confval:: python_functions One or more name prefixes or glob-patterns determining which test functions and methods are considered tests. Search for multiple glob patterns by adding a space between patterns. By default, pytest will consider any function prefixed with ``test`` as a test. Here is an example of how to collect test functions and methods that end in ``_test``: .. code-block:: ini [pytest] python_functions = *_test Note that this has no effect on methods that live on a ``unittest .TestCase`` derived class, as ``unittest``'s own collection framework is used to collect those tests. See :ref:`change naming conventions` for more detailed examples. .. confval:: testpaths Sets list of directories that should be searched for tests when no specific directories, files or test ids are given in the command line when executing pytest from the :ref:`rootdir ` directory. Useful when all project tests are in a known location to speed up test collection and to avoid picking up undesired tests by accident. .. code-block:: ini [pytest] testpaths = testing doc This tells pytest to only look for tests in ``testing`` and ``doc`` directories when executing from the root directory. .. confval:: usefixtures List of fixtures that will be applied to all test functions; this is semantically the same to apply the ``@pytest.mark.usefixtures`` marker to all test functions. .. code-block:: ini [pytest] usefixtures = clean_db .. confval:: xfail_strict If set to ``True``, tests marked with ``@pytest.mark.xfail`` that actually succeed will by default fail the test suite. For more information, see :ref:`xfail strict tutorial`. .. code-block:: ini [pytest] xfail_strict = True pytest-4.6.9/doc/en/conf.py0000664000372000037200000002413213604173400016372 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # # pytest documentation build configuration file, created by # sphinx-quickstart on Fri Oct 8 17:54:28 2010. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The full version, including alpha/beta/rc tags. # The short X.Y version. import datetime import os import sys from _pytest import __version__ as version release = ".".join(version.split(".")[:2]) # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # sys.path.insert(0, os.path.abspath('.')) autodoc_member_order = "bysource" todo_include_todos = 1 # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ "pygments_pytest", "sphinx.ext.autodoc", "sphinx.ext.autosummary", "sphinx.ext.intersphinx", "sphinx.ext.todo", "sphinx.ext.viewcode", "sphinx_removed_in", "sphinxcontrib_trio", ] # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] # The suffix of source filenames. source_suffix = ".rst" # The encoding of source files. # source_encoding = 'utf-8-sig' # The master toctree document. master_doc = "contents" # General information about the project. project = u"pytest" year = datetime.datetime.utcnow().year copyright = u"2015–2020, holger krekel and pytest-dev team" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: # today = '' # Else, today_fmt is used as the format for a strftime call. # today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = [ "links.inc", "_build", "naming20.rst", "test/*", "old_*", "*attic*", "*/attic*", "funcargs.rst", "setup.rst", "example/remoteinterp.rst", ] # The reST default role (used for this markup: `text`) to use for all documents. # default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. # add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). add_module_names = False # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. # show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. # modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- sys.path.append(os.path.abspath("_themes")) html_theme_path = ["_themes"] # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = "flask" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. html_theme_options = {"index_logo": None} # Add any paths that contain custom themes here, relative to this directory. # html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". html_title = "pytest documentation" # A shorter title for the navigation bar. Default is the same as html_title. html_short_title = "pytest-%s" % release # The name of an image file (relative to this directory) to place at the top # of the sidebar. html_logo = "img/pytest1.png" # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. html_favicon = "img/pytest1favi.ico" # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". # html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. # html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. # html_use_smartypants = True # Custom sidebar templates, maps document names to template names. # html_sidebars = {} # html_sidebars = {'index': 'indexsidebar.html'} html_sidebars = { "index": [ "sidebarintro.html", "globaltoc.html", "links.html", "sourcelink.html", "searchbox.html", ], "**": [ "globaltoc.html", "relations.html", "links.html", "sourcelink.html", "searchbox.html", ], } # Additional templates that should be rendered to pages, maps page names to # template names. # html_additional_pages = {} # html_additional_pages = {'index': 'index.html'} # If false, no module index is generated. html_domain_indices = True # If false, no index is generated. html_use_index = False # If true, the index is split into individual pages for each letter. # html_split_index = False # If true, links to the reST sources are added to the pages. html_show_sourcelink = False # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. # html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. # html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. # html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). # html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = "pytestdoc" # -- Options for LaTeX output -------------------------------------------------- # The paper size ('letter' or 'a4'). # latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). # latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ( "contents", "pytest.tex", u"pytest Documentation", u"holger krekel, trainer and consultant, http://merlinux.eu", "manual", ) ] # The name of an image file (relative to this directory) to place at the top of # the title page. latex_logo = "img/pytest1.png" # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. # latex_use_parts = False # If true, show page references after internal links. # latex_show_pagerefs = False # If true, show URL addresses after external links. # latex_show_urls = False # Additional stuff for the LaTeX preamble. # latex_preamble = '' # Documents to append as an appendix to all manuals. # latex_appendices = [] # If false, no module index is generated. latex_domain_indices = False # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [("usage", "pytest", u"pytest usage", [u"holger krekel at merlinux eu"], 1)] # -- Options for Epub output --------------------------------------------------- # Bibliographic Dublin Core info. epub_title = u"pytest" epub_author = u"holger krekel at merlinux eu" epub_publisher = u"holger krekel at merlinux eu" epub_copyright = u"2013-2020, holger krekel et alii" # The language of the text. It defaults to the language option # or en if the language is not set. # epub_language = '' # The scheme of the identifier. Typical schemes are ISBN or URL. # epub_scheme = '' # The unique identifier of the text. This can be a ISBN number # or the project homepage. # epub_identifier = '' # A unique identification for the text. # epub_uid = '' # HTML files that should be inserted before the pages created by sphinx. # The format is a list of tuples containing the path and title. # epub_pre_files = [] # HTML files shat should be inserted after the pages created by sphinx. # The format is a list of tuples containing the path and title. # epub_post_files = [] # A list of files that should not be packed into the epub file. # epub_exclude_files = [] # The depth of the table of contents in toc.ncx. # epub_tocdepth = 3 # Allow duplicate toc entries. # epub_tocdup = True # -- Options for texinfo output ------------------------------------------------ texinfo_documents = [ ( master_doc, "pytest", "pytest Documentation", ( "Holger Krekel@*Benjamin Peterson@*Ronny Pfannschmidt@*" "Floris Bruynooghe@*others" ), "pytest", "simple powerful testing with Python", "Programming", 1, ) ] # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = {"python": ("https://docs.python.org/3", None)} def setup(app): # from sphinx.ext.autodoc import cut_lines # app.connect('autodoc-process-docstring', cut_lines(4, what=['module'])) app.add_object_type( "confval", "confval", objname="configuration value", indextemplate="pair: %s; configuration value", ) pytest-4.6.9/doc/en/unittest.rst0000664000372000037200000002317213604173400017507 0ustar travistravis00000000000000 .. _`unittest.TestCase`: .. _`unittest`: unittest.TestCase Support ========================= ``pytest`` supports running Python ``unittest``-based tests out of the box. It's meant for leveraging existing ``unittest``-based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features. To run an existing ``unittest``-style test suite using ``pytest``, type:: pytest tests pytest will automatically collect ``unittest.TestCase`` subclasses and their ``test`` methods in ``test_*.py`` or ``*_test.py`` files. Almost all ``unittest`` features are supported: * ``@unittest.skip`` style decorators; * ``setUp/tearDown``; * ``setUpClass/tearDownClass``; * ``setUpModule/tearDownModule``; .. _`load_tests protocol`: https://docs.python.org/3/library/unittest.html#load-tests-protocol .. _`subtests`: https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests Up to this point pytest does not have support for the following features: * `load_tests protocol`_; * `subtests`_; Benefits out of the box ----------------------- By running your test suite with pytest you can make use of several features, in most cases without having to modify existing code: * Obtain :ref:`more informative tracebacks `; * :ref:`stdout and stderr ` capturing; * :ref:`Test selection options ` using ``-k`` and ``-m`` flags; * :ref:`maxfail`; * :ref:`--pdb ` command-line option for debugging on test failures (see :ref:`note ` below); * Distribute tests to multiple CPUs using the `pytest-xdist `_ plugin; * Use :ref:`plain assert-statements ` instead of ``self.assert*`` functions (`unittest2pytest `__ is immensely helpful in this); pytest features in ``unittest.TestCase`` subclasses --------------------------------------------------- The following pytest features work in ``unittest.TestCase`` subclasses: * :ref:`Marks `: :ref:`skip `, :ref:`skipif `, :ref:`xfail `; * :ref:`Auto-use fixtures `; The following pytest features **do not** work, and probably never will due to different design philosophies: * :ref:`Fixtures ` (except for ``autouse`` fixtures, see :ref:`below `); * :ref:`Parametrization `; * :ref:`Custom hooks `; Third party plugins may or may not work well, depending on the plugin and the test suite. .. _mixing-fixtures: Mixing pytest fixtures into ``unittest.TestCase`` subclasses using marks ------------------------------------------------------------------------ Running your unittest with ``pytest`` allows you to use its :ref:`fixture mechanism ` with ``unittest.TestCase`` style tests. Assuming you have at least skimmed the pytest fixture features, let's jump-start into an example that integrates a pytest ``db_class`` fixture, setting up a class-cached database object, and then reference it from a unittest-style test:: # content of conftest.py # we define a fixture function below and it will be "used" by # referencing its name from tests import pytest @pytest.fixture(scope="class") def db_class(request): class DummyDB(object): pass # set a class attribute on the invoking test context request.cls.db = DummyDB() This defines a fixture function ``db_class`` which - if used - is called once for each test class and which sets the class-level ``db`` attribute to a ``DummyDB`` instance. The fixture function achieves this by receiving a special ``request`` object which gives access to :ref:`the requesting test context ` such as the ``cls`` attribute, denoting the class from which the fixture is used. This architecture de-couples fixture writing from actual test code and allows re-use of the fixture by a minimal reference, the fixture name. So let's write an actual ``unittest.TestCase`` class using our fixture definition:: # content of test_unittest_db.py import unittest import pytest @pytest.mark.usefixtures("db_class") class MyTest(unittest.TestCase): def test_method1(self): assert hasattr(self, "db") assert 0, self.db # fail for demo purposes def test_method2(self): assert 0, self.db # fail for demo purposes The ``@pytest.mark.usefixtures("db_class")`` class-decorator makes sure that the pytest fixture function ``db_class`` is called once per class. Due to the deliberately failing assert statements, we can take a look at the ``self.db`` values in the traceback: .. code-block:: pytest $ pytest test_unittest_db.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_unittest_db.py FF [100%] ================================= FAILURES ================================= ___________________________ MyTest.test_method1 ____________________________ self = def test_method1(self): assert hasattr(self, "db") > assert 0, self.db # fail for demo purposes E AssertionError: .DummyDB object at 0xdeadbeef> E assert 0 test_unittest_db.py:9: AssertionError ___________________________ MyTest.test_method2 ____________________________ self = def test_method2(self): > assert 0, self.db # fail for demo purposes E AssertionError: .DummyDB object at 0xdeadbeef> E assert 0 test_unittest_db.py:12: AssertionError ========================= 2 failed in 0.12 seconds ========================= This default pytest traceback shows that the two test methods share the same ``self.db`` instance which was our intention when writing the class-scoped fixture function above. Using autouse fixtures and accessing other fixtures --------------------------------------------------- Although it's usually better to explicitly declare use of fixtures you need for a given test, you may sometimes want to have fixtures that are automatically used in a given context. After all, the traditional style of unittest-setup mandates the use of this implicit fixture writing and chances are, you are used to it or like it. You can flag fixture functions with ``@pytest.fixture(autouse=True)`` and define the fixture function in the context where you want it used. Let's look at an ``initdir`` fixture which makes all test methods of a ``TestCase`` class execute in a temporary directory with a pre-initialized ``samplefile.ini``. Our ``initdir`` fixture itself uses the pytest builtin :ref:`tmpdir ` fixture to delegate the creation of a per-test temporary directory:: # content of test_unittest_cleandir.py import pytest import unittest class MyTest(unittest.TestCase): @pytest.fixture(autouse=True) def initdir(self, tmpdir): tmpdir.chdir() # change to pytest-provided temporary directory tmpdir.join("samplefile.ini").write("# testdata") def test_method(self): with open("samplefile.ini") as f: s = f.read() assert "testdata" in s Due to the ``autouse`` flag the ``initdir`` fixture function will be used for all methods of the class where it is defined. This is a shortcut for using a ``@pytest.mark.usefixtures("initdir")`` marker on the class like in the previous example. Running this test module ...: .. code-block:: pytest $ pytest -q test_unittest_cleandir.py . [100%] 1 passed in 0.12 seconds ... gives us one passed test because the ``initdir`` fixture function was executed ahead of the ``test_method``. .. note:: ``unittest.TestCase`` methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites. The above ``usefixtures`` and ``autouse`` examples should help to mix in pytest fixtures into unittest suites. You can also gradually move away from subclassing from ``unittest.TestCase`` to *plain asserts* and then start to benefit from the full pytest feature set step by step. .. _pdb-unittest-note: .. note:: Running tests from ``unittest.TestCase`` subclasses with ``--pdb`` will disable tearDown and cleanup methods for the case that an Exception occurs. This allows proper post mortem debugging for all applications which have significant logic in their tearDown machinery. However, supporting this feature has the following side effect: If people overwrite ``unittest.TestCase`` ``__call__`` or ``run``, they need to to overwrite ``debug`` in the same way (this is also true for standard unittest). .. note:: Due to architectural differences between the two frameworks, setup and teardown for ``unittest``-based tests is performed during the ``call`` phase of testing instead of in ``pytest``'s standard ``setup`` and ``teardown`` stages. This can be important to understand in some situations, particularly when reasoning about errors. For example, if a ``unittest``-based suite exhibits errors during setup, ``pytest`` will report no errors during its ``setup`` phase and will instead raise the error during ``call``. pytest-4.6.9/doc/en/tidelift.rst0000664000372000037200000000004313604173400017424 0ustar travistravis00000000000000 .. include:: ../../TIDELIFT.rst pytest-4.6.9/doc/en/pytest.ini0000664000372000037200000000011213604173400017114 0ustar travistravis00000000000000[pytest] # just defined to prevent the root level tox.ini from kicking in pytest-4.6.9/doc/en/example/0000775000372000037200000000000013604173421016527 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/reportingdemo.rst0000664000372000037200000005567213604173400022153 0ustar travistravis00000000000000.. _`tbreportdemo`: Demo of Python failure reports with pytest ========================================== Here is a nice run of several failures and how ``pytest`` presents things: .. code-block:: pytest assertion $ pytest failure_demo.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR/assertion collected 44 items failure_demo.py FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF [100%] ================================= FAILURES ================================= ___________________________ test_generative[3-6] ___________________________ param1 = 3, param2 = 6 @pytest.mark.parametrize("param1, param2", [(3, 6)]) def test_generative(param1, param2): > assert param1 * 2 < param2 E assert (3 * 2) < 6 failure_demo.py:21: AssertionError _________________________ TestFailing.test_simple __________________________ self = def test_simple(self): def f(): return 42 def g(): return 43 > assert f() == g() E assert 42 == 43 E + where 42 = .f at 0xdeadbeef>() E + and 43 = .g at 0xdeadbeef>() failure_demo.py:32: AssertionError ____________________ TestFailing.test_simple_multiline _____________________ self = def test_simple_multiline(self): > otherfunc_multi(42, 6 * 9) failure_demo.py:35: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 42, b = 54 def otherfunc_multi(a, b): > assert a == b E assert 42 == 54 failure_demo.py:16: AssertionError ___________________________ TestFailing.test_not ___________________________ self = def test_not(self): def f(): return 42 > assert not f() E assert not 42 E + where 42 = .f at 0xdeadbeef>() failure_demo.py:41: AssertionError _________________ TestSpecialisedExplanations.test_eq_text _________________ self = def test_eq_text(self): > assert "spam" == "eggs" E AssertionError: assert 'spam' == 'eggs' E - spam E + eggs failure_demo.py:46: AssertionError _____________ TestSpecialisedExplanations.test_eq_similar_text _____________ self = def test_eq_similar_text(self): > assert "foo 1 bar" == "foo 2 bar" E AssertionError: assert 'foo 1 bar' == 'foo 2 bar' E - foo 1 bar E ? ^ E + foo 2 bar E ? ^ failure_demo.py:49: AssertionError ____________ TestSpecialisedExplanations.test_eq_multiline_text ____________ self = def test_eq_multiline_text(self): > assert "foo\nspam\nbar" == "foo\neggs\nbar" E AssertionError: assert 'foo\nspam\nbar' == 'foo\neggs\nbar' E foo E - spam E + eggs E bar failure_demo.py:52: AssertionError ______________ TestSpecialisedExplanations.test_eq_long_text _______________ self = def test_eq_long_text(self): a = "1" * 100 + "a" + "2" * 100 b = "1" * 100 + "b" + "2" * 100 > assert a == b E AssertionError: assert '111111111111...2222222222222' == '1111111111111...2222222222222' E Skipping 90 identical leading characters in diff, use -v to show E Skipping 91 identical trailing characters in diff, use -v to show E - 1111111111a222222222 E ? ^ E + 1111111111b222222222 E ? ^ failure_demo.py:57: AssertionError _________ TestSpecialisedExplanations.test_eq_long_text_multiline __________ self = def test_eq_long_text_multiline(self): a = "1\n" * 100 + "a" + "2\n" * 100 b = "1\n" * 100 + "b" + "2\n" * 100 > assert a == b E AssertionError: assert '1\n1\n1\n1\n...n2\n2\n2\n2\n' == '1\n1\n1\n1\n1...n2\n2\n2\n2\n' E Skipping 190 identical leading characters in diff, use -v to show E Skipping 191 identical trailing characters in diff, use -v to show E 1 E 1 E 1 E 1 E 1... E E ...Full output truncated (7 lines hidden), use '-vv' to show failure_demo.py:62: AssertionError _________________ TestSpecialisedExplanations.test_eq_list _________________ self = def test_eq_list(self): > assert [0, 1, 2] == [0, 1, 3] E assert [0, 1, 2] == [0, 1, 3] E At index 2 diff: 2 != 3 E Use -v to get the full diff failure_demo.py:65: AssertionError ______________ TestSpecialisedExplanations.test_eq_list_long _______________ self = def test_eq_list_long(self): a = [0] * 100 + [1] + [3] * 100 b = [0] * 100 + [2] + [3] * 100 > assert a == b E assert [0, 0, 0, 0, 0, 0, ...] == [0, 0, 0, 0, 0, 0, ...] E At index 100 diff: 1 != 2 E Use -v to get the full diff failure_demo.py:70: AssertionError _________________ TestSpecialisedExplanations.test_eq_dict _________________ self = def test_eq_dict(self): > assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0} E AssertionError: assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0} E Omitting 1 identical items, use -vv to show E Differing items: E {'b': 1} != {'b': 2} E Left contains 1 more item: E {'c': 0} E Right contains 1 more item: E {'d': 0}... E E ...Full output truncated (2 lines hidden), use '-vv' to show failure_demo.py:73: AssertionError _________________ TestSpecialisedExplanations.test_eq_set __________________ self = def test_eq_set(self): > assert {0, 10, 11, 12} == {0, 20, 21} E AssertionError: assert {0, 10, 11, 12} == {0, 20, 21} E Extra items in the left set: E 10 E 11 E 12 E Extra items in the right set: E 20 E 21... E E ...Full output truncated (2 lines hidden), use '-vv' to show failure_demo.py:76: AssertionError _____________ TestSpecialisedExplanations.test_eq_longer_list ______________ self = def test_eq_longer_list(self): > assert [1, 2] == [1, 2, 3] E assert [1, 2] == [1, 2, 3] E Right contains one more item: 3 E Use -v to get the full diff failure_demo.py:79: AssertionError _________________ TestSpecialisedExplanations.test_in_list _________________ self = def test_in_list(self): > assert 1 in [0, 2, 3, 4, 5] E assert 1 in [0, 2, 3, 4, 5] failure_demo.py:82: AssertionError __________ TestSpecialisedExplanations.test_not_in_text_multiline __________ self = def test_not_in_text_multiline(self): text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail" > assert "foo" not in text E AssertionError: assert 'foo' not in 'some multiline\ntext\nw...ncludes foo\nand a\ntail' E 'foo' is contained here: E some multiline E text E which E includes foo E ? +++ E and a... E E ...Full output truncated (2 lines hidden), use '-vv' to show failure_demo.py:86: AssertionError ___________ TestSpecialisedExplanations.test_not_in_text_single ____________ self = def test_not_in_text_single(self): text = "single foo line" > assert "foo" not in text E AssertionError: assert 'foo' not in 'single foo line' E 'foo' is contained here: E single foo line E ? +++ failure_demo.py:90: AssertionError _________ TestSpecialisedExplanations.test_not_in_text_single_long _________ self = def test_not_in_text_single_long(self): text = "head " * 50 + "foo " + "tail " * 20 > assert "foo" not in text E AssertionError: assert 'foo' not in 'head head head head hea...ail tail tail tail tail ' E 'foo' is contained here: E head head foo tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? +++ failure_demo.py:94: AssertionError ______ TestSpecialisedExplanations.test_not_in_text_single_long_term _______ self = def test_not_in_text_single_long_term(self): text = "head " * 50 + "f" * 70 + "tail " * 20 > assert "f" * 70 not in text E AssertionError: assert 'fffffffffff...ffffffffffff' not in 'head head he...l tail tail ' E 'ffffffffffffffffff...fffffffffffffffffff' is contained here: E head head fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffftail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ failure_demo.py:98: AssertionError ______________ TestSpecialisedExplanations.test_eq_dataclass _______________ self = def test_eq_dataclass(self): from dataclasses import dataclass @dataclass class Foo(object): a: int b: str left = Foo(1, "b") right = Foo(1, "c") > assert left == right E AssertionError: assert TestSpecialis...oo(a=1, b='b') == TestSpecialise...oo(a=1, b='c') E Omitting 1 identical items, use -vv to show E Differing attributes: E b: 'b' != 'c' failure_demo.py:110: AssertionError ________________ TestSpecialisedExplanations.test_eq_attrs _________________ self = def test_eq_attrs(self): import attr @attr.s class Foo(object): a = attr.ib() b = attr.ib() left = Foo(1, "b") right = Foo(1, "c") > assert left == right E AssertionError: assert Foo(a=1, b='b') == Foo(a=1, b='c') E Omitting 1 identical items, use -vv to show E Differing attributes: E b: 'b' != 'c' failure_demo.py:122: AssertionError ______________________________ test_attribute ______________________________ def test_attribute(): class Foo(object): b = 1 i = Foo() > assert i.b == 2 E assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b failure_demo.py:130: AssertionError _________________________ test_attribute_instance __________________________ def test_attribute_instance(): class Foo(object): b = 1 > assert Foo().b == 2 E AssertionError: assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b E + where .Foo object at 0xdeadbeef> = .Foo'>() failure_demo.py:137: AssertionError __________________________ test_attribute_failure __________________________ def test_attribute_failure(): class Foo(object): def _get_b(self): raise Exception("Failed to get attrib") b = property(_get_b) i = Foo() > assert i.b == 2 failure_demo.py:148: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = .Foo object at 0xdeadbeef> def _get_b(self): > raise Exception("Failed to get attrib") E Exception: Failed to get attrib failure_demo.py:143: Exception _________________________ test_attribute_multiple __________________________ def test_attribute_multiple(): class Foo(object): b = 1 class Bar(object): b = 2 > assert Foo().b == Bar().b E AssertionError: assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b E + where .Foo object at 0xdeadbeef> = .Foo'>() E + and 2 = .Bar object at 0xdeadbeef>.b E + where .Bar object at 0xdeadbeef> = .Bar'>() failure_demo.py:158: AssertionError __________________________ TestRaises.test_raises __________________________ self = def test_raises(self): s = "qwe" > raises(TypeError, int, s) E ValueError: invalid literal for int() with base 10: 'qwe' failure_demo.py:168: ValueError ______________________ TestRaises.test_raises_doesnt _______________________ self = def test_raises_doesnt(self): > raises(IOError, int, "3") E Failed: DID NOT RAISE failure_demo.py:171: Failed __________________________ TestRaises.test_raise ___________________________ self = def test_raise(self): > raise ValueError("demo error") E ValueError: demo error failure_demo.py:174: ValueError ________________________ TestRaises.test_tupleerror ________________________ self = def test_tupleerror(self): > a, b = [1] # NOQA E ValueError: not enough values to unpack (expected 2, got 1) failure_demo.py:177: ValueError ______ TestRaises.test_reinterpret_fails_with_print_for_the_fun_of_it ______ self = def test_reinterpret_fails_with_print_for_the_fun_of_it(self): items = [1, 2, 3] print("items is %r" % items) > a, b = items.pop() E TypeError: 'int' object is not iterable failure_demo.py:182: TypeError --------------------------- Captured stdout call --------------------------- items is [1, 2, 3] ________________________ TestRaises.test_some_error ________________________ self = def test_some_error(self): > if namenotexi: # NOQA E NameError: name 'namenotexi' is not defined failure_demo.py:185: NameError ____________________ test_dynamic_compile_shows_nicely _____________________ def test_dynamic_compile_shows_nicely(): import imp import sys src = "def foo():\n assert 1 == 0\n" name = "abc-123" module = imp.new_module(name) code = _pytest._code.compile(src, name, "exec") exec(code, module.__dict__) sys.modules[name] = module > module.foo() failure_demo.py:203: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def foo(): > assert 1 == 0 E AssertionError <0-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:200>:2: AssertionError ____________________ TestMoreErrors.test_complex_error _____________________ self = def test_complex_error(self): def f(): return 44 def g(): return 43 > somefunc(f(), g()) failure_demo.py:214: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ failure_demo.py:12: in somefunc otherfunc(x, y) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 44, b = 43 def otherfunc(a, b): > assert a == b E assert 44 == 43 failure_demo.py:8: AssertionError ___________________ TestMoreErrors.test_z1_unpack_error ____________________ self = def test_z1_unpack_error(self): items = [] > a, b = items E ValueError: not enough values to unpack (expected 2, got 0) failure_demo.py:218: ValueError ____________________ TestMoreErrors.test_z2_type_error _____________________ self = def test_z2_type_error(self): items = 3 > a, b = items E TypeError: 'int' object is not iterable failure_demo.py:222: TypeError ______________________ TestMoreErrors.test_startswith ______________________ self = def test_startswith(self): s = "123" g = "456" > assert s.startswith(g) E AssertionError: assert False E + where False = ('456') E + where = '123'.startswith failure_demo.py:227: AssertionError __________________ TestMoreErrors.test_startswith_nested ___________________ self = def test_startswith_nested(self): def f(): return "123" def g(): return "456" > assert f().startswith(g()) E AssertionError: assert False E + where False = ('456') E + where = '123'.startswith E + where '123' = .f at 0xdeadbeef>() E + and '456' = .g at 0xdeadbeef>() failure_demo.py:236: AssertionError _____________________ TestMoreErrors.test_global_func ______________________ self = def test_global_func(self): > assert isinstance(globf(42), float) E assert False E + where False = isinstance(43, float) E + where 43 = globf(42) failure_demo.py:239: AssertionError _______________________ TestMoreErrors.test_instance _______________________ self = def test_instance(self): self.x = 6 * 7 > assert self.x != 42 E assert 42 != 42 E + where 42 = .x failure_demo.py:243: AssertionError _______________________ TestMoreErrors.test_compare ________________________ self = def test_compare(self): > assert globf(10) < 5 E assert 11 < 5 E + where 11 = globf(10) failure_demo.py:246: AssertionError _____________________ TestMoreErrors.test_try_finally ______________________ self = def test_try_finally(self): x = 1 try: > assert x == 0 E assert 1 == 0 failure_demo.py:251: AssertionError ___________________ TestCustomAssertMsg.test_single_line ___________________ self = def test_single_line(self): class A(object): a = 1 b = 2 > assert A.a == b, "A.a appears not to be b" E AssertionError: A.a appears not to be b E assert 1 == 2 E + where 1 = .A'>.a failure_demo.py:262: AssertionError ____________________ TestCustomAssertMsg.test_multiline ____________________ self = def test_multiline(self): class A(object): a = 1 b = 2 > assert ( A.a == b ), "A.a appears not to be b\nor does not appear to be b\none of those" E AssertionError: A.a appears not to be b E or does not appear to be b E one of those E assert 1 == 2 E + where 1 = .A'>.a failure_demo.py:269: AssertionError ___________________ TestCustomAssertMsg.test_custom_repr ___________________ self = def test_custom_repr(self): class JSON(object): a = 1 def __repr__(self): return "This is JSON\n{\n 'foo': 'bar'\n}" a = JSON() b = 2 > assert a.a == b, a E AssertionError: This is JSON E { E 'foo': 'bar' E } E assert 1 == 2 E + where 1 = This is JSON\n{\n 'foo': 'bar'\n}.a failure_demo.py:282: AssertionError ======================== 44 failed in 0.12 seconds ========================= pytest-4.6.9/doc/en/example/simple.rst0000664000372000037200000007021713604173400020556 0ustar travistravis00000000000000 Basic patterns and examples ========================================================== .. _request example: Pass different values to a test function, depending on command line options ---------------------------------------------------------------------------- .. regendoc:wipe Suppose we want to write a test that depends on a command line option. Here is a basic pattern to achieve this: .. code-block:: python # content of test_sample.py def test_answer(cmdopt): if cmdopt == "type1": print("first") elif cmdopt == "type2": print("second") assert 0 # to see what was printed For this to work we need to add a command line option and provide the ``cmdopt`` through a :ref:`fixture function `: .. code-block:: python # content of conftest.py import pytest def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @pytest.fixture def cmdopt(request): return request.config.getoption("--cmdopt") Let's run this without supplying our new option: .. code-block:: pytest $ pytest -q test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ cmdopt = 'type1' def test_answer(cmdopt): if cmdopt == "type1": print("first") elif cmdopt == "type2": print("second") > assert 0 # to see what was printed E assert 0 test_sample.py:6: AssertionError --------------------------- Captured stdout call --------------------------- first 1 failed in 0.12 seconds And now with supplying a command line option: .. code-block:: pytest $ pytest -q --cmdopt=type2 F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ cmdopt = 'type2' def test_answer(cmdopt): if cmdopt == "type1": print("first") elif cmdopt == "type2": print("second") > assert 0 # to see what was printed E assert 0 test_sample.py:6: AssertionError --------------------------- Captured stdout call --------------------------- second 1 failed in 0.12 seconds You can see that the command line option arrived in our test. This completes the basic pattern. However, one often rather wants to process command line options outside of the test and rather pass in different or more complex objects. Dynamically adding command line options -------------------------------------------------------------- .. regendoc:wipe Through :confval:`addopts` you can statically add command line options for your project. You can also dynamically modify the command line arguments before they get processed: .. code-block:: python # setuptools plugin import sys def pytest_load_initial_conftests(args): if "xdist" in sys.modules: # pytest-xdist plugin import multiprocessing num = max(multiprocessing.cpu_count() / 2, 1) args[:] = ["-n", str(num)] + args If you have the `xdist plugin `_ installed you will now always perform test runs using a number of subprocesses close to your CPU. Running in an empty directory with the above conftest.py: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 0 items ======================= no tests ran in 0.12 seconds ======================= .. _`excontrolskip`: Control skipping of tests according to command line option -------------------------------------------------------------- .. regendoc:wipe Here is a ``conftest.py`` file adding a ``--runslow`` command line option to control skipping of ``pytest.mark.slow`` marked tests: .. code-block:: python # content of conftest.py import pytest def pytest_addoption(parser): parser.addoption( "--runslow", action="store_true", default=False, help="run slow tests" ) def pytest_collection_modifyitems(config, items): if config.getoption("--runslow"): # --runslow given in cli: do not skip slow tests return skip_slow = pytest.mark.skip(reason="need --runslow option to run") for item in items: if "slow" in item.keywords: item.add_marker(skip_slow) We can now write a test module like this: .. code-block:: python # content of test_module.py import pytest def test_func_fast(): pass @pytest.mark.slow def test_func_slow(): pass and when running it will see a skipped "slow" test: .. code-block:: pytest $ pytest -rs # "-rs" means report details on the little 's' =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py .s [100%] ========================= short test summary info ========================== SKIPPED [1] test_module.py:8: need --runslow option to run =================== 1 passed, 1 skipped in 0.12 seconds ==================== Or run it including the ``slow`` marked test: .. code-block:: pytest $ pytest --runslow =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py .. [100%] ========================= 2 passed in 0.12 seconds ========================= Writing well integrated assertion helpers -------------------------------------------------- .. regendoc:wipe If you have a test helper function called from a test you can use the ``pytest.fail`` marker to fail a test with a certain message. The test support function will not show up in the traceback if you set the ``__tracebackhide__`` option somewhere in the helper function. Example: .. code-block:: python # content of test_checkconfig.py import pytest def checkconfig(x): __tracebackhide__ = True if not hasattr(x, "config"): pytest.fail("not configured: %s" % (x,)) def test_something(): checkconfig(42) The ``__tracebackhide__`` setting influences ``pytest`` showing of tracebacks: the ``checkconfig`` function will not be shown unless the ``--full-trace`` command line option is specified. Let's run our little function: .. code-block:: pytest $ pytest -q test_checkconfig.py F [100%] ================================= FAILURES ================================= ______________________________ test_something ______________________________ def test_something(): > checkconfig(42) E Failed: not configured: 42 test_checkconfig.py:11: Failed 1 failed in 0.12 seconds If you only want to hide certain exceptions, you can set ``__tracebackhide__`` to a callable which gets the ``ExceptionInfo`` object. You can for example use this to make sure unexpected exception types aren't hidden: .. code-block:: python import operator import pytest class ConfigException(Exception): pass def checkconfig(x): __tracebackhide__ = operator.methodcaller("errisinstance", ConfigException) if not hasattr(x, "config"): raise ConfigException("not configured: %s" % (x,)) def test_something(): checkconfig(42) This will avoid hiding the exception traceback on unrelated exceptions (i.e. bugs in assertion helpers). Detect if running from within a pytest run -------------------------------------------------------------- .. regendoc:wipe Usually it is a bad idea to make application code behave differently if called from a test. But if you absolutely must find out if your application code is running from a test you can do something like this: .. code-block:: python # content of conftest.py def pytest_configure(config): import sys sys._called_from_test = True def pytest_unconfigure(config): import sys del sys._called_from_test and then check for the ``sys._called_from_test`` flag: .. code-block:: python if hasattr(sys, "_called_from_test"): # called from within a test run ... else: # called "normally" ... accordingly in your application. It's also a good idea to use your own application module rather than ``sys`` for handling flag. Adding info to test report header -------------------------------------------------------------- .. regendoc:wipe It's easy to present extra information in a ``pytest`` run: .. code-block:: python # content of conftest.py def pytest_report_header(config): return "project deps: mylib-1.1" which will add the string to the test header accordingly: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache project deps: mylib-1.1 rootdir: $REGENDOC_TMPDIR collected 0 items ======================= no tests ran in 0.12 seconds ======================= .. regendoc:wipe It is also possible to return a list of strings which will be considered as several lines of information. You may consider ``config.getoption('verbose')`` in order to display more information if applicable: .. code-block:: python # content of conftest.py def pytest_report_header(config): if config.getoption("verbose") > 0: return ["info1: did you know that ...", "did you?"] which will add info only when run with "--v": .. code-block:: pytest $ pytest -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache info1: did you know that ... did you? rootdir: $REGENDOC_TMPDIR collecting ... collected 0 items ======================= no tests ran in 0.12 seconds ======================= and nothing when run plainly: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 0 items ======================= no tests ran in 0.12 seconds ======================= profiling test duration -------------------------- .. regendoc:wipe .. versionadded: 2.2 If you have a slow running large test suite you might want to find out which tests are the slowest. Let's make an artificial test suite: .. code-block:: python # content of test_some_are_slow.py import time def test_funcfast(): time.sleep(0.1) def test_funcslow1(): time.sleep(0.2) def test_funcslow2(): time.sleep(0.3) Now we can profile which test functions execute the slowest: .. code-block:: pytest $ pytest --durations=3 =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 3 items test_some_are_slow.py ... [100%] ========================= slowest 3 test durations ========================= 0.30s call test_some_are_slow.py::test_funcslow2 0.20s call test_some_are_slow.py::test_funcslow1 0.10s call test_some_are_slow.py::test_funcfast ========================= 3 passed in 0.12 seconds ========================= incremental testing - test steps --------------------------------------------------- .. regendoc:wipe Sometimes you may have a testing situation which consists of a series of test steps. If one step fails it makes no sense to execute further steps as they are all expected to fail anyway and their tracebacks add no insight. Here is a simple ``conftest.py`` file which introduces an ``incremental`` marker which is to be used on classes: .. code-block:: python # content of conftest.py import pytest def pytest_runtest_makereport(item, call): if "incremental" in item.keywords: if call.excinfo is not None: parent = item.parent parent._previousfailed = item def pytest_runtest_setup(item): if "incremental" in item.keywords: previousfailed = getattr(item.parent, "_previousfailed", None) if previousfailed is not None: pytest.xfail("previous test failed (%s)" % previousfailed.name) These two hook implementations work together to abort incremental-marked tests in a class. Here is a test module example: .. code-block:: python # content of test_step.py import pytest @pytest.mark.incremental class TestUserHandling(object): def test_login(self): pass def test_modification(self): assert 0 def test_deletion(self): pass def test_normal(): pass If we run this: .. code-block:: pytest $ pytest -rx =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items test_step.py .Fx. [100%] ================================= FAILURES ================================= ____________________ TestUserHandling.test_modification ____________________ self = def test_modification(self): > assert 0 E assert 0 test_step.py:11: AssertionError ========================= short test summary info ========================== XFAIL test_step.py::TestUserHandling::test_deletion reason: previous test failed (test_modification) ============== 1 failed, 2 passed, 1 xfailed in 0.12 seconds =============== We'll see that ``test_deletion`` was not executed because ``test_modification`` failed. It is reported as an "expected failure". Package/Directory-level fixtures (setups) ------------------------------------------------------- If you have nested test directories, you can have per-directory fixture scopes by placing fixture functions in a ``conftest.py`` file in that directory You can use all types of fixtures including :ref:`autouse fixtures ` which are the equivalent of xUnit's setup/teardown concept. It's however recommended to have explicit fixture references in your tests or test classes rather than relying on implicitly executing setup/teardown functions, especially if they are far away from the actual tests. Here is an example for making a ``db`` fixture available in a directory: .. code-block:: python # content of a/conftest.py import pytest class DB(object): pass @pytest.fixture(scope="session") def db(): return DB() and then a test module in that directory: .. code-block:: python # content of a/test_db.py def test_a1(db): assert 0, db # to show value another test module: .. code-block:: python # content of a/test_db2.py def test_a2(db): assert 0, db # to show value and then a module in a sister directory which will not see the ``db`` fixture: .. code-block:: python # content of b/test_error.py def test_root(db): # no db here, will error out pass We can run this: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 7 items test_step.py .Fx. [ 57%] a/test_db.py F [ 71%] a/test_db2.py F [ 85%] b/test_error.py E [100%] ================================== ERRORS ================================== _______________________ ERROR at setup of test_root ________________________ file $REGENDOC_TMPDIR/b/test_error.py, line 1 def test_root(db): # no db here, will error out E fixture 'db' not found > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. $REGENDOC_TMPDIR/b/test_error.py:1 ================================= FAILURES ================================= ____________________ TestUserHandling.test_modification ____________________ self = def test_modification(self): > assert 0 E assert 0 test_step.py:11: AssertionError _________________________________ test_a1 __________________________________ db = def test_a1(db): > assert 0, db # to show value E AssertionError: E assert 0 a/test_db.py:2: AssertionError _________________________________ test_a2 __________________________________ db = def test_a2(db): > assert 0, db # to show value E AssertionError: E assert 0 a/test_db2.py:2: AssertionError ========== 3 failed, 2 passed, 1 xfailed, 1 error in 0.12 seconds ========== The two test modules in the ``a`` directory see the same ``db`` fixture instance while the one test in the sister-directory ``b`` doesn't see it. We could of course also define a ``db`` fixture in that sister directory's ``conftest.py`` file. Note that each fixture is only instantiated if there is a test actually needing it (unless you use "autouse" fixture which are always executed ahead of the first test executing). post-process test reports / failures --------------------------------------- If you want to postprocess test reports and need access to the executing environment you can implement a hook that gets called when the test "report" object is about to be created. Here we write out all failing test calls and also access a fixture (if it was used by the test) in case you want to query/look at it during your post processing. In our case we just write some information out to a ``failures`` file: .. code-block:: python # content of conftest.py import pytest import os.path @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result() # we only look at actual failing test calls, not setup/teardown if rep.when == "call" and rep.failed: mode = "a" if os.path.exists("failures") else "w" with open("failures", mode) as f: # let's also access a fixture for the fun of it if "tmpdir" in item.fixturenames: extra = " (%s)" % item.funcargs["tmpdir"] else: extra = "" f.write(rep.nodeid + extra + "\n") if you then have failing tests: .. code-block:: python # content of test_module.py def test_fail1(tmpdir): assert 0 def test_fail2(): assert 0 and run them: .. code-block:: pytest $ pytest test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py FF [100%] ================================= FAILURES ================================= ________________________________ test_fail1 ________________________________ tmpdir = local('PYTEST_TMPDIR/test_fail10') def test_fail1(tmpdir): > assert 0 E assert 0 test_module.py:2: AssertionError ________________________________ test_fail2 ________________________________ def test_fail2(): > assert 0 E assert 0 test_module.py:6: AssertionError ========================= 2 failed in 0.12 seconds ========================= you will have a "failures" file which contains the failing test ids: .. code-block:: bash $ cat failures test_module.py::test_fail1 (PYTEST_TMPDIR/test_fail10) test_module.py::test_fail2 Making test result information available in fixtures ----------------------------------------------------------- .. regendoc:wipe If you want to make test result reports available in fixture finalizers here is a little example implemented via a local plugin: .. code-block:: python # content of conftest.py import pytest @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result() # set a report attribute for each phase of a call, which can # be "setup", "call", "teardown" setattr(item, "rep_" + rep.when, rep) @pytest.fixture def something(request): yield # request.node is an "item" because we use the default # "function" scope if request.node.rep_setup.failed: print("setting up a test failed!", request.node.nodeid) elif request.node.rep_setup.passed: if request.node.rep_call.failed: print("executing test failed", request.node.nodeid) if you then have failing tests: .. code-block:: python # content of test_module.py import pytest @pytest.fixture def other(): assert 0 def test_setup_fails(something, other): pass def test_call_fails(something): assert 0 def test_fail2(): assert 0 and run it: .. code-block:: pytest $ pytest -s test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 3 items test_module.py Esetting up a test failed! test_module.py::test_setup_fails Fexecuting test failed test_module.py::test_call_fails F ================================== ERRORS ================================== ____________________ ERROR at setup of test_setup_fails ____________________ @pytest.fixture def other(): > assert 0 E assert 0 test_module.py:7: AssertionError ================================= FAILURES ================================= _____________________________ test_call_fails ______________________________ something = None def test_call_fails(something): > assert 0 E assert 0 test_module.py:15: AssertionError ________________________________ test_fail2 ________________________________ def test_fail2(): > assert 0 E assert 0 test_module.py:19: AssertionError ==================== 2 failed, 1 error in 0.12 seconds ===================== You'll see that the fixture finalizers could use the precise reporting information. .. _pytest current test env: ``PYTEST_CURRENT_TEST`` environment variable -------------------------------------------- Sometimes a test session might get stuck and there might be no easy way to figure out which test got stuck, for example if pytest was run in quiet mode (``-q``) or you don't have access to the console output. This is particularly a problem if the problem helps only sporadically, the famous "flaky" kind of tests. ``pytest`` sets a ``PYTEST_CURRENT_TEST`` environment variable when running tests, which can be inspected by process monitoring utilities or libraries like `psutil `_ to discover which test got stuck if necessary: .. code-block:: python import psutil for pid in psutil.pids(): environ = psutil.Process(pid).environ() if "PYTEST_CURRENT_TEST" in environ: print(f'pytest process {pid} running: {environ["PYTEST_CURRENT_TEST"]}') During the test session pytest will set ``PYTEST_CURRENT_TEST`` to the current test :ref:`nodeid ` and the current stage, which can be ``setup``, ``call`` and ``teardown``. For example, when running a single test function named ``test_foo`` from ``foo_module.py``, ``PYTEST_CURRENT_TEST`` will be set to: #. ``foo_module.py::test_foo (setup)`` #. ``foo_module.py::test_foo (call)`` #. ``foo_module.py::test_foo (teardown)`` In that order. .. note:: The contents of ``PYTEST_CURRENT_TEST`` is meant to be human readable and the actual format can be changed between releases (even bug fixes) so it shouldn't be relied on for scripting or automation. .. _freezing-pytest: Freezing pytest --------------- If you freeze your application using a tool like `PyInstaller `_ in order to distribute it to your end-users, it is a good idea to also package your test runner and run your tests using the frozen application. This way packaging errors such as dependencies not being included into the executable can be detected early while also allowing you to send test files to users so they can run them in their machines, which can be useful to obtain more information about a hard to reproduce bug. Fortunately recent ``PyInstaller`` releases already have a custom hook for pytest, but if you are using another tool to freeze executables such as ``cx_freeze`` or ``py2exe``, you can use ``pytest.freeze_includes()`` to obtain the full list of internal pytest modules. How to configure the tools to find the internal modules varies from tool to tool, however. Instead of freezing the pytest runner as a separate executable, you can make your frozen program work as the pytest runner by some clever argument handling during program startup. This allows you to have a single executable, which is usually more convenient. Please note that the mechanism for plugin discovery used by pytest (setupttools entry points) doesn't work with frozen executables so pytest can't find any third party plugins automatically. To include third party plugins like ``pytest-timeout`` they must be imported explicitly and passed on to pytest.main. .. code-block:: python # contents of app_main.py import sys import pytest_timeout # Third party plugin if len(sys.argv) > 1 and sys.argv[1] == "--pytest": import pytest sys.exit(pytest.main(sys.argv[2:], plugins=[pytest_timeout])) else: # normal application execution: at this point argv can be parsed # by your argument-parsing library of choice as usual ... This allows you to execute tests using the frozen application with standard ``pytest`` command-line options: .. code-block:: bash ./app_main --pytest --verbose --tb=long --junitxml=results.xml test-suite/ pytest-4.6.9/doc/en/example/markers.rst0000664000372000037200000006337313604173400020736 0ustar travistravis00000000000000 .. _`mark examples`: Working with custom markers ================================================= Here are some examples using the :ref:`mark` mechanism. .. _`mark run`: Marking test functions and selecting them for a run ---------------------------------------------------- You can "mark" a test function with custom metadata like this: .. code-block:: python # content of test_server.py import pytest @pytest.mark.webtest def test_send_http(): pass # perform some webtest test for your app def test_something_quick(): pass def test_another(): pass class TestClass(object): def test_method(self): pass You can then restrict a test run to only run tests marked with ``webtest``: .. code-block:: pytest $ pytest -v -m webtest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 4 items / 3 deselected / 1 selected test_server.py::test_send_http PASSED [100%] ================== 1 passed, 3 deselected in 0.12 seconds ================== Or the inverse, running all tests except the webtest ones: .. code-block:: pytest $ pytest -v -m "not webtest" =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 4 items / 1 deselected / 3 selected test_server.py::test_something_quick PASSED [ 33%] test_server.py::test_another PASSED [ 66%] test_server.py::TestClass::test_method PASSED [100%] ================== 3 passed, 1 deselected in 0.12 seconds ================== Selecting tests based on their node ID -------------------------------------- You can provide one or more :ref:`node IDs ` as positional arguments to select only specified tests. This makes it easy to select tests based on their module, class, method, or function name: .. code-block:: pytest $ pytest -v test_server.py::TestClass::test_method =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 1 item test_server.py::TestClass::test_method PASSED [100%] ========================= 1 passed in 0.12 seconds ========================= You can also select on the class: .. code-block:: pytest $ pytest -v test_server.py::TestClass =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 1 item test_server.py::TestClass::test_method PASSED [100%] ========================= 1 passed in 0.12 seconds ========================= Or select multiple nodes: .. code-block:: pytest $ pytest -v test_server.py::TestClass test_server.py::test_send_http =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 2 items test_server.py::TestClass::test_method PASSED [ 50%] test_server.py::test_send_http PASSED [100%] ========================= 2 passed in 0.12 seconds ========================= .. _node-id: .. note:: Node IDs are of the form ``module.py::class::method`` or ``module.py::function``. Node IDs control which tests are collected, so ``module.py::class`` will select all test methods on the class. Nodes are also created for each parameter of a parametrized fixture or test, so selecting a parametrized test must include the parameter value, e.g. ``module.py::function[param]``. Node IDs for failing tests are displayed in the test summary info when running pytest with the ``-rf`` option. You can also construct Node IDs from the output of ``pytest --collectonly``. Using ``-k expr`` to select tests based on their name ------------------------------------------------------- .. versionadded: 2.0/2.3.4 You can use the ``-k`` command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that ``-m`` provides. This makes it easy to select tests based on their names: .. code-block:: pytest $ pytest -v -k http # running with the above defined example module =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 4 items / 3 deselected / 1 selected test_server.py::test_send_http PASSED [100%] ================== 1 passed, 3 deselected in 0.12 seconds ================== And you can also run all tests except the ones that match the keyword: .. code-block:: pytest $ pytest -k "not send_http" -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 4 items / 1 deselected / 3 selected test_server.py::test_something_quick PASSED [ 33%] test_server.py::test_another PASSED [ 66%] test_server.py::TestClass::test_method PASSED [100%] ================== 3 passed, 1 deselected in 0.12 seconds ================== Or to select "http" and "quick" tests: .. code-block:: pytest $ pytest -k "http or quick" -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 4 items / 2 deselected / 2 selected test_server.py::test_send_http PASSED [ 50%] test_server.py::test_something_quick PASSED [100%] ================== 2 passed, 2 deselected in 0.12 seconds ================== .. note:: If you are using expressions such as ``"X and Y"`` then both ``X`` and ``Y`` need to be simple non-keyword names. For example, ``"pass"`` or ``"from"`` will result in SyntaxErrors because ``"-k"`` evaluates the expression using Python's `eval`_ function. .. _`eval`: https://docs.python.org/3.6/library/functions.html#eval However, if the ``"-k"`` argument is a simple string, no such restrictions apply. Also ``"-k 'not STRING'"`` has no restrictions. You can also specify numbers like ``"-k 1.3"`` to match tests which are parametrized with the float ``"1.3"``. Registering markers ------------------------------------- .. ini-syntax for custom markers: Registering markers for your test suite is simple: .. code-block:: ini # content of pytest.ini [pytest] markers = webtest: mark a test as a webtest. You can ask which markers exist for your test suite - the list includes our just defined ``webtest`` markers: .. code-block:: pytest $ pytest --markers @pytest.mark.webtest: mark a test as a webtest. @pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test. @pytest.mark.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 https://docs.pytest.org/en/latest/skipping.html @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark 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 https://docs.pytest.org/en/latest/skipping.html @pytest.mark.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 https://docs.pytest.org/en/latest/parametrize.html for more info and examples. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/latest/fixture.html#usefixtures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. For an example on how to add and work with markers from a plugin, see :ref:`adding a custom marker from a plugin`. .. note:: It is recommended to explicitly register markers so that: * There is one place in your test suite defining your markers * Asking for existing markers via ``pytest --markers`` gives good output * Typos in function markers are treated as an error if you use the ``--strict-markers`` option. .. _`scoped-marking`: Marking whole classes or modules ---------------------------------------------------- You may use ``pytest.mark`` decorators with classes to apply markers to all of its test methods: .. code-block:: python # content of test_mark_classlevel.py import pytest @pytest.mark.webtest class TestClass(object): def test_startup(self): pass def test_startup_and_more(self): pass This is equivalent to directly applying the decorator to the two test functions. To remain backward-compatible with Python 2.4 you can also set a ``pytestmark`` attribute on a TestClass like this: .. code-block:: python import pytest class TestClass(object): pytestmark = pytest.mark.webtest or if you need to use multiple markers you can use a list: .. code-block:: python import pytest class TestClass(object): pytestmark = [pytest.mark.webtest, pytest.mark.slowtest] You can also set a module level marker:: import pytest pytestmark = pytest.mark.webtest or multiple markers:: pytestmark = [pytest.mark.webtest, pytest.mark.slowtest] in which case markers will be applied (in left-to-right order) to all functions and methods defined in the module. .. _`marking individual tests when using parametrize`: Marking individual tests when using parametrize ----------------------------------------------- When using parametrize, applying a mark will make it apply to each individual test. However it is also possible to apply a marker to an individual test instance: .. code-block:: python import pytest @pytest.mark.foo @pytest.mark.parametrize( ("n", "expected"), [(1, 2), pytest.param((1, 3), marks=pytest.mark.bar), (2, 3)] ) def test_increment(n, expected): assert n + 1 == expected In this example the mark "foo" will apply to each of the three tests, whereas the "bar" mark is only applied to the second test. Skip and xfail marks can also be applied in this way, see :ref:`skip/xfail with parametrize`. .. _`adding a custom marker from a plugin`: Custom marker and command line option to control test runs ---------------------------------------------------------- .. regendoc:wipe Plugins can provide custom markers and implement specific behaviour based on it. This is a self-contained example which adds a command line option and a parametrized test function marker to run tests specifies via named environments: .. code-block:: python # content of conftest.py import pytest def pytest_addoption(parser): parser.addoption( "-E", action="store", metavar="NAME", help="only run tests matching the environment NAME.", ) def pytest_configure(config): # register an additional marker config.addinivalue_line( "markers", "env(name): mark test to run only on named environment" ) def pytest_runtest_setup(item): envnames = [mark.args[0] for mark in item.iter_markers(name="env")] if envnames: if item.config.getoption("-E") not in envnames: pytest.skip("test requires env in %r" % envnames) A test file using this local plugin: .. code-block:: python # content of test_someenv.py import pytest @pytest.mark.env("stage1") def test_basic_db_operation(): pass and an example invocations specifying a different environment than what the test needs: .. code-block:: pytest $ pytest -E stage2 =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_someenv.py s [100%] ======================== 1 skipped in 0.12 seconds ========================= and here is one that specifies exactly the environment needed: .. code-block:: pytest $ pytest -E stage1 =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_someenv.py . [100%] ========================= 1 passed in 0.12 seconds ========================= The ``--markers`` option always gives you a list of available markers: .. code-block:: pytest $ pytest --markers @pytest.mark.env(name): mark test to run only on named environment @pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test. @pytest.mark.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 https://docs.pytest.org/en/latest/skipping.html @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark 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 https://docs.pytest.org/en/latest/skipping.html @pytest.mark.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 https://docs.pytest.org/en/latest/parametrize.html for more info and examples. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/latest/fixture.html#usefixtures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. .. _`passing callables to custom markers`: Passing a callable to custom markers -------------------------------------------- .. regendoc:wipe Below is the config file that will be used in the next examples: .. code-block:: python # content of conftest.py import sys def pytest_runtest_setup(item): for marker in item.iter_markers(name="my_marker"): print(marker) sys.stdout.flush() A custom marker can have its argument set, i.e. ``args`` and ``kwargs`` properties, defined by either invoking it as a callable or using ``pytest.mark.MARKER_NAME.with_args``. These two methods achieve the same effect most of the time. However, if there is a callable as the single positional argument with no keyword arguments, using the ``pytest.mark.MARKER_NAME(c)`` will not pass ``c`` as a positional argument but decorate ``c`` with the custom marker (see :ref:`MarkDecorator `). Fortunately, ``pytest.mark.MARKER_NAME.with_args`` comes to the rescue: .. code-block:: python # content of test_custom_marker.py import pytest def hello_world(*args, **kwargs): return "Hello World" @pytest.mark.my_marker.with_args(hello_world) def test_with_args(): pass The output is as follows: .. code-block:: pytest $ pytest -q -s Mark(name='my_marker', args=(,), kwargs={}) . 1 passed in 0.12 seconds We can see that the custom marker has its argument set extended with the function ``hello_world``. This is the key difference between creating a custom marker as a callable, which invokes ``__call__`` behind the scenes, and using ``with_args``. Reading markers which were set from multiple places ---------------------------------------------------- .. versionadded: 2.2.2 .. regendoc:wipe If you are heavily using markers in your test suite you may encounter the case where a marker is applied several times to a test function. From plugin code you can read over all such settings. Example: .. code-block:: python # content of test_mark_three_times.py import pytest pytestmark = pytest.mark.glob("module", x=1) @pytest.mark.glob("class", x=2) class TestClass(object): @pytest.mark.glob("function", x=3) def test_something(self): pass Here we have the marker "glob" applied three times to the same test function. From a conftest file we can read it like this: .. code-block:: python # content of conftest.py import sys def pytest_runtest_setup(item): for mark in item.iter_markers(name="glob"): print("glob args=%s kwargs=%s" % (mark.args, mark.kwargs)) sys.stdout.flush() Let's run this without capturing output and see what we get: .. code-block:: pytest $ pytest -q -s glob args=('function',) kwargs={'x': 3} glob args=('class',) kwargs={'x': 2} glob args=('module',) kwargs={'x': 1} . 1 passed in 0.12 seconds marking platform specific tests with pytest -------------------------------------------------------------- .. regendoc:wipe Consider you have a test suite which marks tests for particular platforms, namely ``pytest.mark.darwin``, ``pytest.mark.win32`` etc. and you also have tests that run on all platforms and have no specific marker. If you now want to have a way to only run the tests for your particular platform, you could use the following plugin: .. code-block:: python # content of conftest.py # import sys import pytest ALL = set("darwin linux win32".split()) def pytest_runtest_setup(item): supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers()) plat = sys.platform if supported_platforms and plat not in supported_platforms: pytest.skip("cannot run on platform %s" % (plat)) then tests will be skipped if they were specified for a different platform. Let's do a little test file to show how this looks like: .. code-block:: python # content of test_plat.py import pytest @pytest.mark.darwin def test_if_apple_is_evil(): pass @pytest.mark.linux def test_if_linux_works(): pass @pytest.mark.win32 def test_if_win32_crashes(): pass def test_runs_everywhere(): pass then you will see two tests skipped and two executed tests as expected: .. code-block:: pytest $ pytest -rs # this option reports skip reasons =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items test_plat.py s.s. [100%] ========================= short test summary info ========================== SKIPPED [2] $REGENDOC_TMPDIR/conftest.py:13: cannot run on platform linux =================== 2 passed, 2 skipped in 0.12 seconds ==================== Note that if you specify a platform via the marker-command line option like this: .. code-block:: pytest $ pytest -m linux =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items / 3 deselected / 1 selected test_plat.py . [100%] ================== 1 passed, 3 deselected in 0.12 seconds ================== then the unmarked-tests will not be run. It is thus a way to restrict the run to the specific tests. Automatically adding markers based on test names -------------------------------------------------------- .. regendoc:wipe If you a test suite where test function names indicate a certain type of test, you can implement a hook that automatically defines markers so that you can use the ``-m`` option with it. Let's look at this test module: .. code-block:: python # content of test_module.py def test_interface_simple(): assert 0 def test_interface_complex(): assert 0 def test_event_simple(): assert 0 def test_something_else(): assert 0 We want to dynamically define two markers and can do it in a ``conftest.py`` plugin: .. code-block:: python # content of conftest.py import pytest def pytest_collection_modifyitems(items): for item in items: if "interface" in item.nodeid: item.add_marker(pytest.mark.interface) elif "event" in item.nodeid: item.add_marker(pytest.mark.event) We can now use the ``-m option`` to select one set: .. code-block:: pytest $ pytest -m interface --tb=short =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items / 2 deselected / 2 selected test_module.py FF [100%] ================================= FAILURES ================================= __________________________ test_interface_simple ___________________________ test_module.py:4: in test_interface_simple assert 0 E assert 0 __________________________ test_interface_complex __________________________ test_module.py:8: in test_interface_complex assert 0 E assert 0 ================== 2 failed, 2 deselected in 0.12 seconds ================== or to select both "event" and "interface" tests: .. code-block:: pytest $ pytest -m "interface or event" --tb=short =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items / 1 deselected / 3 selected test_module.py FFF [100%] ================================= FAILURES ================================= __________________________ test_interface_simple ___________________________ test_module.py:4: in test_interface_simple assert 0 E assert 0 __________________________ test_interface_complex __________________________ test_module.py:8: in test_interface_complex assert 0 E assert 0 ____________________________ test_event_simple _____________________________ test_module.py:12: in test_event_simple assert 0 E assert 0 ================== 3 failed, 1 deselected in 0.12 seconds ================== pytest-4.6.9/doc/en/example/attic.rst0000664000372000037200000000524513604173400020370 0ustar travistravis00000000000000 .. _`accept example`: example: specifying and selecting acceptance tests -------------------------------------------------------------- .. sourcecode:: python # ./conftest.py def pytest_option(parser): group = parser.getgroup("myproject") group.addoption( "-A", dest="acceptance", action="store_true", help="run (slow) acceptance tests" ) def pytest_funcarg__accept(request): return AcceptFixture(request) class AcceptFixture(object): def __init__(self, request): if not request.config.getoption("acceptance"): pytest.skip("specify -A to run acceptance tests") self.tmpdir = request.config.mktemp(request.function.__name__, numbered=True) def run(self, *cmd): """ called by test code to execute an acceptance test. """ self.tmpdir.chdir() return subprocess.check_output(cmd).decode() and the actual test function example: .. sourcecode:: python def test_some_acceptance_aspect(accept): accept.tmpdir.mkdir("somesub") result = accept.run("ls", "-la") assert "somesub" in result If you run this test without specifying a command line option the test will get skipped with an appropriate message. Otherwise you can start to add convenience and test support methods to your AcceptFixture and drive running of tools or applications and provide ways to do assertions about the output. .. _`decorate a funcarg`: example: decorating a funcarg in a test module -------------------------------------------------------------- For larger scale setups it's sometimes useful to decorate a funcarg just for a particular test module. We can extend the `accept example`_ by putting this in our test module: .. sourcecode:: python def pytest_funcarg__accept(request): # call the next factory (living in our conftest.py) arg = request.getfuncargvalue("accept") # create a special layout in our tempdir arg.tmpdir.mkdir("special") return arg class TestSpecialAcceptance(object): def test_sometest(self, accept): assert accept.tmpdir.join("special").check() Our module level factory will be invoked first and it can ask its request object to call the next factory and then decorate its result. This mechanism allows us to stay ignorant of how/where the function argument is provided - in our example from a `conftest plugin`_. sidenote: the temporary directory used here are instances of the `py.path.local`_ class which provides many of the os.path methods in a convenient way. .. _`py.path.local`: ../path.html#local .. _`conftest plugin`: customize.html#conftestplugin pytest-4.6.9/doc/en/example/index.rst0000664000372000037200000000170413604173400020367 0ustar travistravis00000000000000 .. _examples: Examples and customization tricks ================================= Here is a (growing) list of examples. :ref:`Contact ` us if you need more examples or have questions. Also take a look at the :ref:`comprehensive documentation ` which contains many example snippets as well. Also, `pytest on stackoverflow.com `_ often comes with example answers. For basic examples, see - :doc:`../getting-started` for basic introductory examples - :ref:`assert` for basic assertion examples - :ref:`fixtures` for basic fixture/setup examples - :ref:`parametrize` for basic test function parametrization - :doc:`../unittest` for basic unittest integration - :doc:`../nose` for basic nosetests integration The following examples aim at various use cases you might encounter. .. toctree:: :maxdepth: 2 reportingdemo simple parametrize markers special pythoncollection nonpython pytest-4.6.9/doc/en/example/nonpython/0000775000372000037200000000000013604173421020563 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/nonpython/test_simple.yml0000664000372000037200000000011613604173400023631 0ustar travistravis00000000000000# test_simple.yml ok: sub1: sub1 hello: world: world some: other pytest-4.6.9/doc/en/example/nonpython/__init__.py0000664000372000037200000000000013604173400022657 0ustar travistravis00000000000000pytest-4.6.9/doc/en/example/nonpython/conftest.py0000664000372000037200000000262513604173400022764 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # content of conftest.py import pytest def pytest_collect_file(parent, path): if path.ext == ".yml" and path.basename.startswith("test"): return YamlFile(path, parent) class YamlFile(pytest.File): def collect(self): import yaml # we need a yaml parser, e.g. PyYAML raw = yaml.safe_load(self.fspath.open()) for name, spec in sorted(raw.items()): yield YamlItem(name, self, spec) class YamlItem(pytest.Item): def __init__(self, name, parent, spec): super(YamlItem, self).__init__(name, parent) self.spec = spec def runtest(self): for name, value in sorted(self.spec.items()): # some custom test execution (dumb example follows) if name != value: raise YamlException(self, name, value) def repr_failure(self, excinfo): """ called when self.runtest() raises an exception. """ if isinstance(excinfo.value, YamlException): return "\n".join( [ "usecase execution failed", " spec failed: %r: %r" % excinfo.value.args[1:3], " no further details known at this point.", ] ) def reportinfo(self): return self.fspath, 0, "usecase: %s" % self.name class YamlException(Exception): """ custom exception for error reporting. """ pytest-4.6.9/doc/en/example/special.rst0000664000372000037200000000372613604173400020706 0ustar travistravis00000000000000A session-fixture which can look at all collected tests ---------------------------------------------------------------- A session-scoped fixture effectively has access to all collected test items. Here is an example of a fixture function which walks all collected tests and looks if their test class defines a ``callme`` method and calls it:: # content of conftest.py import pytest @pytest.fixture(scope="session", autouse=True) def callattr_ahead_of_alltests(request): print("callattr_ahead_of_alltests called") seen = set([None]) session = request.node for item in session.items: cls = item.getparent(pytest.Class) if cls not in seen: if hasattr(cls.obj, "callme"): cls.obj.callme() seen.add(cls) test classes may now define a ``callme`` method which will be called ahead of running any tests:: # content of test_module.py class TestHello(object): @classmethod def callme(cls): print("callme called!") def test_method1(self): print("test_method1 called") def test_method2(self): print("test_method1 called") class TestOther(object): @classmethod def callme(cls): print("callme other called") def test_other(self): print("test other") # works with unittest as well ... import unittest class SomeTest(unittest.TestCase): @classmethod def callme(self): print("SomeTest callme called") def test_unit1(self): print("test_unit1 method called") If you run this without output capturing: .. code-block:: pytest $ pytest -q -s test_module.py callattr_ahead_of_alltests called callme called! callme other called SomeTest callme called test_method1 called .test_method1 called .test other .test_unit1 method called . 4 passed in 0.12 seconds pytest-4.6.9/doc/en/example/multipython.py0000664000372000037200000000401513604173400021472 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ module containing a parametrized tests testing cross-python serialization via the pickle module. """ import distutils.spawn import subprocess import textwrap import pytest pythonlist = ["python2.7", "python3.4", "python3.5"] @pytest.fixture(params=pythonlist) def python1(request, tmpdir): picklefile = tmpdir.join("data.pickle") return Python(request.param, picklefile) @pytest.fixture(params=pythonlist) def python2(request, python1): return Python(request.param, python1.picklefile) class Python(object): def __init__(self, version, picklefile): self.pythonpath = distutils.spawn.find_executable(version) if not self.pythonpath: pytest.skip("{!r} not found".format(version)) self.picklefile = picklefile def dumps(self, obj): dumpfile = self.picklefile.dirpath("dump.py") dumpfile.write( textwrap.dedent( r""" import pickle f = open({!r}, 'wb') s = pickle.dump({!r}, f, protocol=2) f.close() """.format( str(self.picklefile), obj ) ) ) subprocess.check_call((self.pythonpath, str(dumpfile))) def load_and_is_true(self, expression): loadfile = self.picklefile.dirpath("load.py") loadfile.write( textwrap.dedent( r""" import pickle f = open({!r}, 'rb') obj = pickle.load(f) f.close() res = eval({!r}) if not res: raise SystemExit(1) """.format( str(self.picklefile), expression ) ) ) print(loadfile) subprocess.check_call((self.pythonpath, str(loadfile))) @pytest.mark.parametrize("obj", [42, {}, {1: 3}]) def test_basic_objects(python1, python2, obj): python1.dumps(obj) python2.load_and_is_true("obj == %s" % obj) pytest-4.6.9/doc/en/example/py2py3/0000775000372000037200000000000013604173421017675 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/py2py3/test_py3.py0000664000372000037200000000020013604173400022006 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_exception_syntax(): try: 0 / 0 except ZeroDivisionError as e: assert e pytest-4.6.9/doc/en/example/py2py3/conftest.py0000664000372000037200000000053413604173400022073 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import pytest py3 = sys.version_info[0] >= 3 class DummyCollector(pytest.collect.File): def collect(self): return [] def pytest_pycollect_makemodule(path, parent): bn = path.basename if "py3" in bn and not py3 or ("py2" in bn and py3): return DummyCollector(path, parent=parent) pytest-4.6.9/doc/en/example/py2py3/test_py2.py0000664000372000037200000000014613604173400022016 0ustar travistravis00000000000000def test_exception_syntax(): try: 0 / 0 except ZeroDivisionError, e: assert e pytest-4.6.9/doc/en/example/parametrize.rst0000664000372000037200000005237013604173400021610 0ustar travistravis00000000000000 .. _paramexamples: Parametrizing tests ================================================= .. currentmodule:: _pytest.python ``pytest`` allows to easily parametrize test functions. For basic docs, see :ref:`parametrize-basics`. In the following we provide some examples using the builtin mechanisms. Generating parameters combinations, depending on command line ---------------------------------------------------------------------------- .. regendoc:wipe Let's say we want to execute a test with different computation parameters and the parameter range shall be determined by a command line argument. Let's first write a simple (do-nothing) computation test:: # content of test_compute.py def test_compute(param1): assert param1 < 4 Now we add a test configuration like this:: # content of conftest.py def pytest_addoption(parser): parser.addoption("--all", action="store_true", help="run all combinations") def pytest_generate_tests(metafunc): if 'param1' in metafunc.fixturenames: if metafunc.config.getoption('all'): end = 5 else: end = 2 metafunc.parametrize("param1", range(end)) This means that we only run 2 tests if we do not pass ``--all``: .. code-block:: pytest $ pytest -q test_compute.py .. [100%] 2 passed in 0.12 seconds We run only two computations, so we see two dots. let's run the full monty: .. code-block:: pytest $ pytest -q --all ....F [100%] ================================= FAILURES ================================= _____________________________ test_compute[4] ______________________________ param1 = 4 def test_compute(param1): > assert param1 < 4 E assert 4 < 4 test_compute.py:3: AssertionError 1 failed, 4 passed in 0.12 seconds As expected when running the full range of ``param1`` values we'll get an error on the last one. Different options for test IDs ------------------------------------ pytest will build a string that is the test ID for each set of values in a parametrized test. These IDs can be used with ``-k`` to select specific cases to run, and they will also identify the specific case when one is failing. Running pytest with ``--collect-only`` will show the generated IDs. Numbers, strings, booleans and None will have their usual string representation used in the test ID. For other objects, pytest will make a string based on the argument name:: # content of test_time.py import pytest from datetime import datetime, timedelta testdata = [ (datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)), (datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)), ] @pytest.mark.parametrize("a,b,expected", testdata) def test_timedistance_v0(a, b, expected): diff = a - b assert diff == expected @pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"]) def test_timedistance_v1(a, b, expected): diff = a - b assert diff == expected def idfn(val): if isinstance(val, (datetime,)): # note this wouldn't show any hours/minutes/seconds return val.strftime('%Y%m%d') @pytest.mark.parametrize("a,b,expected", testdata, ids=idfn) def test_timedistance_v2(a, b, expected): diff = a - b assert diff == expected @pytest.mark.parametrize("a,b,expected", [ pytest.param(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1), id='forward'), pytest.param(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1), id='backward'), ]) def test_timedistance_v3(a, b, expected): diff = a - b assert diff == expected In ``test_timedistance_v0``, we let pytest generate the test IDs. In ``test_timedistance_v1``, we specified ``ids`` as a list of strings which were used as the test IDs. These are succinct, but can be a pain to maintain. In ``test_timedistance_v2``, we specified ``ids`` as a function that can generate a string representation to make part of the test ID. So our ``datetime`` values use the label generated by ``idfn``, but because we didn't generate a label for ``timedelta`` objects, they are still using the default pytest representation: .. code-block:: pytest $ pytest test_time.py --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 8 items ======================= no tests ran in 0.12 seconds ======================= In ``test_timedistance_v3``, we used ``pytest.param`` to specify the test IDs together with the actual data, instead of listing them separately. A quick port of "testscenarios" ------------------------------------ .. _`test scenarios`: https://pypi.org/project/testscenarios/ Here is a quick port to run tests configured with `test scenarios`_, an add-on from Robert Collins for the standard unittest framework. We only have to work a bit to construct the correct arguments for pytest's :py:func:`Metafunc.parametrize`:: # content of test_scenarios.py def pytest_generate_tests(metafunc): idlist = [] argvalues = [] for scenario in metafunc.cls.scenarios: idlist.append(scenario[0]) items = scenario[1].items() argnames = [x[0] for x in items] argvalues.append(([x[1] for x in items])) metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class") scenario1 = ('basic', {'attribute': 'value'}) scenario2 = ('advanced', {'attribute': 'value2'}) class TestSampleWithScenarios(object): scenarios = [scenario1, scenario2] def test_demo1(self, attribute): assert isinstance(attribute, str) def test_demo2(self, attribute): assert isinstance(attribute, str) this is a fully self-contained example which you can run with: .. code-block:: pytest $ pytest test_scenarios.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items test_scenarios.py .... [100%] ========================= 4 passed in 0.12 seconds ========================= If you just collect tests you'll also nicely see 'advanced' and 'basic' as variants for the test function: .. code-block:: pytest $ pytest --collect-only test_scenarios.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 4 items ======================= no tests ran in 0.12 seconds ======================= Note that we told ``metafunc.parametrize()`` that your scenario values should be considered class-scoped. With pytest-2.3 this leads to a resource-based ordering. Deferring the setup of parametrized resources --------------------------------------------------- .. regendoc:wipe The parametrization of test functions happens at collection time. It is a good idea to setup expensive resources like DB connections or subprocess only when the actual test is run. Here is a simple example how you can achieve that, first the actual test requiring a ``db`` object:: # content of test_backends.py import pytest def test_db_initialized(db): # a dummy test if db.__class__.__name__ == "DB2": pytest.fail("deliberately failing for demo purposes") We can now add a test configuration that generates two invocations of the ``test_db_initialized`` function and also implements a factory that creates a database object for the actual test invocations:: # content of conftest.py import pytest def pytest_generate_tests(metafunc): if 'db' in metafunc.fixturenames: metafunc.parametrize("db", ['d1', 'd2'], indirect=True) class DB1(object): "one database object" class DB2(object): "alternative database object" @pytest.fixture def db(request): if request.param == "d1": return DB1() elif request.param == "d2": return DB2() else: raise ValueError("invalid internal test config") Let's first see how it looks like at collection time: .. code-block:: pytest $ pytest test_backends.py --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items ======================= no tests ran in 0.12 seconds ======================= And then when we run the test: .. code-block:: pytest $ pytest -q test_backends.py .F [100%] ================================= FAILURES ================================= _________________________ test_db_initialized[d2] __________________________ db = def test_db_initialized(db): # a dummy test if db.__class__.__name__ == "DB2": > pytest.fail("deliberately failing for demo purposes") E Failed: deliberately failing for demo purposes test_backends.py:6: Failed 1 failed, 1 passed in 0.12 seconds The first invocation with ``db == "DB1"`` passed while the second with ``db == "DB2"`` failed. Our ``db`` fixture function has instantiated each of the DB values during the setup phase while the ``pytest_generate_tests`` generated two according calls to the ``test_db_initialized`` during the collection phase. .. regendoc:wipe Apply indirect on particular arguments --------------------------------------------------- Very often parametrization uses more than one argument name. There is opportunity to apply ``indirect`` parameter on particular arguments. It can be done by passing list or tuple of arguments' names to ``indirect``. In the example below there is a function ``test_indirect`` which uses two fixtures: ``x`` and ``y``. Here we give to indirect the list, which contains the name of the fixture ``x``. The indirect parameter will be applied to this argument only, and the value ``a`` will be passed to respective fixture function:: # content of test_indirect_list.py import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.fixture(scope='function') def y(request): return request.param * 2 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x']) def test_indirect(x,y): assert x == 'aaa' assert y == 'b' The result of this test will be successful: .. code-block:: pytest $ pytest test_indirect_list.py --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item ======================= no tests ran in 0.12 seconds ======================= .. regendoc:wipe Parametrizing test methods through per-class configuration -------------------------------------------------------------- .. _`unittest parametrizer`: https://github.com/testing-cabal/unittest-ext/blob/master/params.py Here is an example ``pytest_generate_tests`` function implementing a parametrization scheme similar to Michael Foord's `unittest parametrizer`_ but in a lot less code:: # content of ./test_parametrize.py import pytest def pytest_generate_tests(metafunc): # called once per each test function funcarglist = metafunc.cls.params[metafunc.function.__name__] argnames = sorted(funcarglist[0]) metafunc.parametrize(argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist]) class TestClass(object): # a map specifying multiple argument sets for a test method params = { 'test_equals': [dict(a=1, b=2), dict(a=3, b=3), ], 'test_zerodivision': [dict(a=1, b=0), ], } def test_equals(self, a, b): assert a == b def test_zerodivision(self, a, b): with pytest.raises(ZeroDivisionError): a / b Our test generator looks up a class-level definition which specifies which argument sets to use for each test function. Let's run it: .. code-block:: pytest $ pytest -q F.. [100%] ================================= FAILURES ================================= ________________________ TestClass.test_equals[1-2] ________________________ self = , a = 1, b = 2 def test_equals(self, a, b): > assert a == b E assert 1 == 2 test_parametrize.py:18: AssertionError 1 failed, 2 passed in 0.12 seconds Indirect parametrization with multiple fixtures -------------------------------------------------------------- Here is a stripped down real-life example of using parametrized testing for testing serialization of objects between different python interpreters. We define a ``test_basic_objects`` function which is to be run with different sets of arguments for its three arguments: * ``python1``: first python interpreter, run to pickle-dump an object to a file * ``python2``: second interpreter, run to pickle-load an object from a file * ``obj``: object to be dumped/loaded .. literalinclude:: multipython.py Running it results in some skips if we don't have all the python interpreters installed and otherwise runs all combinations (5 interpreters times 5 interpreters times 3 objects to serialize/deserialize): .. code-block:: pytest . $ pytest -rs -q multipython.py ......sss......ssssssssssss [100%] ========================= short test summary info ========================== SKIPPED [15] $REGENDOC_TMPDIR/CWD/multipython.py:31: 'python3.5' not found 12 passed, 15 skipped in 0.12 seconds Indirect parametrization of optional implementations/imports -------------------------------------------------------------------- If you want to compare the outcomes of several implementations of a given API, you can write test functions that receive the already imported implementations and get skipped in case the implementation is not importable/available. Let's say we have a "base" implementation and the other (possibly optimized ones) need to provide similar results:: # content of conftest.py import pytest @pytest.fixture(scope="session") def basemod(request): return pytest.importorskip("base") @pytest.fixture(scope="session", params=["opt1", "opt2"]) def optmod(request): return pytest.importorskip(request.param) And then a base implementation of a simple function:: # content of base.py def func1(): return 1 And an optimized version:: # content of opt1.py def func1(): return 1.0001 And finally a little test module:: # content of test_module.py def test_func1(basemod, optmod): assert round(basemod.func1(), 3) == round(optmod.func1(), 3) If you run this with reporting for skips enabled: .. code-block:: pytest $ pytest -rs test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 2 items test_module.py .s [100%] ========================= short test summary info ========================== SKIPPED [1] $REGENDOC_TMPDIR/conftest.py:11: could not import 'opt2': No module named 'opt2' =================== 1 passed, 1 skipped in 0.12 seconds ==================== You'll see that we don't have an ``opt2`` module and thus the second test run of our ``test_func1`` was skipped. A few notes: - the fixture functions in the ``conftest.py`` file are "session-scoped" because we don't need to import more than once - if you have multiple test functions and a skipped import, you will see the ``[1]`` count increasing in the report - you can put :ref:`@pytest.mark.parametrize <@pytest.mark.parametrize>` style parametrization on the test functions to parametrize input/output values as well. Set marks or test ID for individual parametrized test -------------------------------------------------------------------- Use ``pytest.param`` to apply marks or set test ID to individual parametrized test. For example: .. code-block:: python # content of test_pytest_param_example.py import pytest @pytest.mark.parametrize( "test_input,expected", [ ("3+5", 8), pytest.param("1+7", 8, marks=pytest.mark.basic), pytest.param("2+4", 6, marks=pytest.mark.basic, id="basic_2+4"), pytest.param( "6*9", 42, marks=[pytest.mark.basic, pytest.mark.xfail], id="basic_6*9" ), ], ) def test_eval(test_input, expected): assert eval(test_input) == expected In this example, we have 4 parametrized tests. Except for the first test, we mark the rest three parametrized tests with the custom marker ``basic``, and for the fourth test we also use the built-in mark ``xfail`` to indicate this test is expected to fail. For explicitness, we set test ids for some tests. Then run ``pytest`` with verbose mode and with only the ``basic`` marker: .. code-block:: pytest $ pytest -v -m basic =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collecting ... collected 17 items / 14 deselected / 3 selected test_pytest_param_example.py::test_eval[1+7-8] PASSED [ 33%] test_pytest_param_example.py::test_eval[basic_2+4] PASSED [ 66%] test_pytest_param_example.py::test_eval[basic_6*9] XFAIL [100%] ============ 2 passed, 14 deselected, 1 xfailed in 0.12 seconds ============ As the result: - Four tests were collected - One test was deselected because it doesn't have the ``basic`` mark. - Three tests with the ``basic`` mark was selected. - The test ``test_eval[1+7-8]`` passed, but the name is autogenerated and confusing. - The test ``test_eval[basic_2+4]`` passed. - The test ``test_eval[basic_6*9]`` was expected to fail and did fail. .. _`parametrizing_conditional_raising`: Parametrizing conditional raising -------------------------------------------------------------------- Use :func:`pytest.raises` with the :ref:`pytest.mark.parametrize ref` decorator to write parametrized tests in which some tests raise exceptions and others do not. It is helpful to define a no-op context manager ``does_not_raise`` to serve as a complement to ``raises``. For example:: from contextlib import contextmanager import pytest @contextmanager def does_not_raise(): yield @pytest.mark.parametrize('example_input,expectation', [ (3, does_not_raise()), (2, does_not_raise()), (1, does_not_raise()), (0, pytest.raises(ZeroDivisionError)), ]) def test_division(example_input, expectation): """Test how much I know division.""" with expectation: assert (6 / example_input) is not None In the example above, the first three test cases should run unexceptionally, while the fourth should raise ``ZeroDivisionError``. If you're only supporting Python 3.7+, you can simply use ``nullcontext`` to define ``does_not_raise``:: from contextlib import nullcontext as does_not_raise Or, if you're supporting Python 3.3+ you can use:: from contextlib import ExitStack as does_not_raise Or, if desired, you can ``pip install contextlib2`` and use:: from contextlib2 import ExitStack as does_not_raise pytest-4.6.9/doc/en/example/costlysetup/0000775000372000037200000000000013604173421021125 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/costlysetup/sub_a/0000775000372000037200000000000013604173421022216 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/costlysetup/sub_a/test_quick.py0000664000372000037200000000007013604173400024735 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_quick(setup): pass pytest-4.6.9/doc/en/example/costlysetup/sub_a/__init__.py0000664000372000037200000000003213604173400024317 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # pytest-4.6.9/doc/en/example/costlysetup/sub_b/0000775000372000037200000000000013604173421022217 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/costlysetup/sub_b/test_two.py0000664000372000037200000000022713604173400024437 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_something(setup): assert setup.timecostly == 1 def test_something_more(setup): assert setup.timecostly == 1 pytest-4.6.9/doc/en/example/costlysetup/sub_b/__init__.py0000664000372000037200000000003213604173400024320 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # pytest-4.6.9/doc/en/example/costlysetup/conftest.py0000664000372000037200000000056013604173400023322 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture("session") def setup(request): setup = CostlySetup() yield setup setup.finalize() class CostlySetup(object): def __init__(self): import time print("performing costly setup") time.sleep(5) self.timecostly = 1 def finalize(self): del self.timecostly pytest-4.6.9/doc/en/example/xfail_demo.py0000664000372000037200000000073013604173400021205 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest xfail = pytest.mark.xfail @xfail def test_hello(): assert 0 @xfail(run=False) def test_hello2(): assert 0 @xfail("hasattr(os, 'sep')") def test_hello3(): assert 0 @xfail(reason="bug 110") def test_hello4(): assert 0 @xfail('pytest.__version__[0] != "17"') def test_hello5(): assert 0 def test_hello6(): pytest.xfail("reason") @xfail(raises=IndexError) def test_hello7(): x = [] x[1] = 1 pytest-4.6.9/doc/en/example/nonpython.rst0000664000372000037200000000731613604173400021321 0ustar travistravis00000000000000 .. _`non-python tests`: Working with non-python tests ==================================================== .. _`yaml plugin`: A basic example for specifying tests in Yaml files -------------------------------------------------------------- .. _`pytest-yamlwsgi`: http://bitbucket.org/aafshar/pytest-yamlwsgi/src/tip/pytest_yamlwsgi.py .. _`PyYAML`: https://pypi.org/project/PyYAML/ Here is an example ``conftest.py`` (extracted from Ali Afshnars special purpose `pytest-yamlwsgi`_ plugin). This ``conftest.py`` will collect ``test*.yml`` files and will execute the yaml-formatted content as custom tests: .. include:: nonpython/conftest.py :literal: You can create a simple example file: .. include:: nonpython/test_simple.yml :literal: and if you installed `PyYAML`_ or a compatible YAML-parser you can now execute the test specification: .. code-block:: pytest nonpython $ pytest test_simple.yml =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR/nonpython collected 2 items test_simple.yml F. [100%] ================================= FAILURES ================================= ______________________________ usecase: hello ______________________________ usecase execution failed spec failed: 'some': 'other' no further details known at this point. ==================== 1 failed, 1 passed in 0.12 seconds ==================== .. regendoc:wipe You get one dot for the passing ``sub1: sub1`` check and one failure. Obviously in the above ``conftest.py`` you'll want to implement a more interesting interpretation of the yaml-values. You can easily write your own domain specific testing language this way. .. note:: ``repr_failure(excinfo)`` is called for representing test failures. If you create custom collection nodes you can return an error representation string of your choice. It will be reported as a (red) string. ``reportinfo()`` is used for representing the test location and is also consulted when reporting in ``verbose`` mode: .. code-block:: pytest nonpython $ pytest -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR/nonpython collecting ... collected 2 items test_simple.yml::hello FAILED [ 50%] test_simple.yml::ok PASSED [100%] ================================= FAILURES ================================= ______________________________ usecase: hello ______________________________ usecase execution failed spec failed: 'some': 'other' no further details known at this point. ==================== 1 failed, 1 passed in 0.12 seconds ==================== .. regendoc:wipe While developing your custom test collection and execution it's also interesting to just look at the collection tree: .. code-block:: pytest nonpython $ pytest --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR/nonpython collected 2 items ======================= no tests ran in 0.12 seconds ======================= pytest-4.6.9/doc/en/example/conftest.py0000664000372000037200000000006713604173400020726 0ustar travistravis00000000000000# -*- coding: utf-8 -*- collect_ignore = ["nonpython"] pytest-4.6.9/doc/en/example/assertion/0000775000372000037200000000000013604173421020536 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/assertion/global_testmodule_config/0000775000372000037200000000000013604173421025570 5ustar travistravis00000000000000pytest-4.6.9/doc/en/example/assertion/global_testmodule_config/test_hello_world.py0000664000372000037200000000010413604173400031503 0ustar travistravis00000000000000# -*- coding: utf-8 -*- hello = "world" def test_func(): pass pytest-4.6.9/doc/en/example/assertion/global_testmodule_config/conftest.py0000664000372000037200000000055413604173400027770 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import py import pytest mydir = py.path.local(__file__).dirpath() def pytest_runtest_setup(item): if isinstance(item, pytest.Function): if not item.fspath.relto(mydir): return mod = item.getparent(pytest.Module).obj if hasattr(mod, "hello"): print("mod.hello {!r}".format(mod.hello)) pytest-4.6.9/doc/en/example/assertion/test_failures.py0000664000372000037200000000072413604173400023761 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import py failure_demo = py.path.local(__file__).dirpath("failure_demo.py") pytest_plugins = ("pytester",) def test_failure_demo_fails_properly(testdir): target = testdir.tmpdir.join(failure_demo.basename) failure_demo.copy(target) failure_demo.copy(testdir.tmpdir.join(failure_demo.basename)) result = testdir.runpytest(target, syspathinsert=True) result.stdout.fnmatch_lines(["*44 failed*"]) assert result.ret != 0 pytest-4.6.9/doc/en/example/assertion/failure_demo.py0000664000372000037200000001320213604173400023536 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import _pytest._code import pytest from pytest import raises def otherfunc(a, b): assert a == b def somefunc(x, y): otherfunc(x, y) def otherfunc_multi(a, b): assert a == b @pytest.mark.parametrize("param1, param2", [(3, 6)]) def test_generative(param1, param2): assert param1 * 2 < param2 class TestFailing(object): def test_simple(self): def f(): return 42 def g(): return 43 assert f() == g() def test_simple_multiline(self): otherfunc_multi(42, 6 * 9) def test_not(self): def f(): return 42 assert not f() class TestSpecialisedExplanations(object): def test_eq_text(self): assert "spam" == "eggs" def test_eq_similar_text(self): assert "foo 1 bar" == "foo 2 bar" def test_eq_multiline_text(self): assert "foo\nspam\nbar" == "foo\neggs\nbar" def test_eq_long_text(self): a = "1" * 100 + "a" + "2" * 100 b = "1" * 100 + "b" + "2" * 100 assert a == b def test_eq_long_text_multiline(self): a = "1\n" * 100 + "a" + "2\n" * 100 b = "1\n" * 100 + "b" + "2\n" * 100 assert a == b def test_eq_list(self): assert [0, 1, 2] == [0, 1, 3] def test_eq_list_long(self): a = [0] * 100 + [1] + [3] * 100 b = [0] * 100 + [2] + [3] * 100 assert a == b def test_eq_dict(self): assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0} def test_eq_set(self): assert {0, 10, 11, 12} == {0, 20, 21} def test_eq_longer_list(self): assert [1, 2] == [1, 2, 3] def test_in_list(self): assert 1 in [0, 2, 3, 4, 5] def test_not_in_text_multiline(self): text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail" assert "foo" not in text def test_not_in_text_single(self): text = "single foo line" assert "foo" not in text def test_not_in_text_single_long(self): text = "head " * 50 + "foo " + "tail " * 20 assert "foo" not in text def test_not_in_text_single_long_term(self): text = "head " * 50 + "f" * 70 + "tail " * 20 assert "f" * 70 not in text def test_eq_dataclass(self): from dataclasses import dataclass @dataclass class Foo(object): a: int b: str left = Foo(1, "b") right = Foo(1, "c") assert left == right def test_eq_attrs(self): import attr @attr.s class Foo(object): a = attr.ib() b = attr.ib() left = Foo(1, "b") right = Foo(1, "c") assert left == right def test_attribute(): class Foo(object): b = 1 i = Foo() assert i.b == 2 def test_attribute_instance(): class Foo(object): b = 1 assert Foo().b == 2 def test_attribute_failure(): class Foo(object): def _get_b(self): raise Exception("Failed to get attrib") b = property(_get_b) i = Foo() assert i.b == 2 def test_attribute_multiple(): class Foo(object): b = 1 class Bar(object): b = 2 assert Foo().b == Bar().b def globf(x): return x + 1 class TestRaises(object): def test_raises(self): s = "qwe" raises(TypeError, int, s) def test_raises_doesnt(self): raises(IOError, int, "3") def test_raise(self): raise ValueError("demo error") def test_tupleerror(self): a, b = [1] # NOQA def test_reinterpret_fails_with_print_for_the_fun_of_it(self): items = [1, 2, 3] print("items is %r" % items) a, b = items.pop() def test_some_error(self): if namenotexi: # NOQA pass def func1(self): assert 41 == 42 # thanks to Matthew Scott for this test def test_dynamic_compile_shows_nicely(): import imp import sys src = "def foo():\n assert 1 == 0\n" name = "abc-123" module = imp.new_module(name) code = _pytest._code.compile(src, name, "exec") exec(code, module.__dict__) sys.modules[name] = module module.foo() class TestMoreErrors(object): def test_complex_error(self): def f(): return 44 def g(): return 43 somefunc(f(), g()) def test_z1_unpack_error(self): items = [] a, b = items def test_z2_type_error(self): items = 3 a, b = items def test_startswith(self): s = "123" g = "456" assert s.startswith(g) def test_startswith_nested(self): def f(): return "123" def g(): return "456" assert f().startswith(g()) def test_global_func(self): assert isinstance(globf(42), float) def test_instance(self): self.x = 6 * 7 assert self.x != 42 def test_compare(self): assert globf(10) < 5 def test_try_finally(self): x = 1 try: assert x == 0 finally: x = 0 class TestCustomAssertMsg(object): def test_single_line(self): class A(object): a = 1 b = 2 assert A.a == b, "A.a appears not to be b" def test_multiline(self): class A(object): a = 1 b = 2 assert ( A.a == b ), "A.a appears not to be b\nor does not appear to be b\none of those" def test_custom_repr(self): class JSON(object): a = 1 def __repr__(self): return "This is JSON\n{\n 'foo': 'bar'\n}" a = JSON() b = 2 assert a.a == b, a pytest-4.6.9/doc/en/example/assertion/test_setup_flow_example.py0000664000372000037200000000240413604173400026046 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def setup_module(module): module.TestStateFullThing.classcount = 0 class TestStateFullThing(object): def setup_class(cls): cls.classcount += 1 def teardown_class(cls): cls.classcount -= 1 def setup_method(self, method): self.id = eval(method.__name__[5:]) def test_42(self): assert self.classcount == 1 assert self.id == 42 def test_23(self): assert self.classcount == 1 assert self.id == 23 def teardown_module(module): assert module.TestStateFullThing.classcount == 0 """ For this example the control flow happens as follows:: import test_setup_flow_example setup_module(test_setup_flow_example) setup_class(TestStateFullThing) instance = TestStateFullThing() setup_method(instance, instance.test_42) instance.test_42() setup_method(instance, instance.test_23) instance.test_23() teardown_class(TestStateFullThing) teardown_module(test_setup_flow_example) Note that ``setup_class(TestStateFullThing)`` is called and not ``TestStateFullThing.setup_class()`` which would require you to insert ``setup_class = classmethod(setup_class)`` to make your setup function callable. """ pytest-4.6.9/doc/en/example/pythoncollection.py0000664000372000037200000000035113604173400022472 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # run this with $ pytest --collect-only test_collectonly.py # def test_function(): pass class TestClass(object): def test_method(self): pass def test_anothermethod(self): pass pytest-4.6.9/doc/en/example/pythoncollection.rst0000664000372000037200000002302613604173400022656 0ustar travistravis00000000000000Changing standard (Python) test discovery =============================================== Ignore paths during test collection ----------------------------------- You can easily ignore certain test directories and modules during collection by passing the ``--ignore=path`` option on the cli. ``pytest`` allows multiple ``--ignore`` options. Example: .. code-block:: text tests/ |-- example | |-- test_example_01.py | |-- test_example_02.py | '-- test_example_03.py |-- foobar | |-- test_foobar_01.py | |-- test_foobar_02.py | '-- test_foobar_03.py '-- hello '-- world |-- test_world_01.py |-- test_world_02.py '-- test_world_03.py Now if you invoke ``pytest`` with ``--ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/``, you will see that ``pytest`` only collects test-modules, which do not match the patterns specified: .. code-block:: pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 5 items tests/example/test_example_01.py . [ 20%] tests/example/test_example_02.py . [ 40%] tests/example/test_example_03.py . [ 60%] tests/foobar/test_foobar_01.py . [ 80%] tests/foobar/test_foobar_02.py . [100%] ========================= 5 passed in 0.02 seconds ========================= The ``--ignore-glob`` option allows to ignore test file paths based on Unix shell-style wildcards. If you want to exclude test-modules that end with ``_01.py``, execute ``pytest`` with ``--ignore-glob='*_01.py'``. Deselect tests during test collection ------------------------------------- Tests can individually be deselected during collection by passing the ``--deselect=item`` option. For example, say ``tests/foobar/test_foobar_01.py`` contains ``test_a`` and ``test_b``. You can run all of the tests within ``tests/`` *except* for ``tests/foobar/test_foobar_01.py::test_a`` by invoking ``pytest`` with ``--deselect tests/foobar/test_foobar_01.py::test_a``. ``pytest`` allows multiple ``--deselect`` options. Keeping duplicate paths specified from command line ---------------------------------------------------- Default behavior of ``pytest`` is to ignore duplicate paths specified from the command line. Example: .. code-block:: pytest pytest path_a path_a ... collected 1 item ... Just collect tests once. To collect duplicate tests, use the ``--keep-duplicates`` option on the cli. Example: .. code-block:: pytest pytest --keep-duplicates path_a path_a ... collected 2 items ... As the collector just works on directories, if you specify twice a single test file, ``pytest`` will still collect it twice, no matter if the ``--keep-duplicates`` is not specified. Example: .. code-block:: pytest pytest test_a.py test_a.py ... collected 2 items ... Changing directory recursion ----------------------------------------------------- You can set the :confval:`norecursedirs` option in an ini-file, for example your ``pytest.ini`` in the project root directory: .. code-block:: ini # content of pytest.ini [pytest] norecursedirs = .svn _build tmp* This would tell ``pytest`` to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory. .. _`change naming conventions`: Changing naming conventions ----------------------------------------------------- You can configure different naming conventions by setting the :confval:`python_files`, :confval:`python_classes` and :confval:`python_functions` configuration options. Here is an example: .. code-block:: ini # content of pytest.ini # Example 1: have pytest look for "check" instead of "test" # can also be defined in tox.ini or setup.cfg file, although the section # name in setup.cfg files should be "tool:pytest" [pytest] python_files = check_*.py python_classes = Check python_functions = *_check This would make ``pytest`` look for tests in files that match the ``check_* .py`` glob-pattern, ``Check`` prefixes in classes, and functions and methods that match ``*_check``. For example, if we have:: # content of check_myapp.py class CheckMyApp(object): def simple_check(self): pass def complex_check(self): pass The test collection would look like this: .. code-block:: pytest $ pytest --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 2 items ======================= no tests ran in 0.12 seconds ======================= You can check for multiple glob patterns by adding a space between the patterns: .. code-block:: ini # Example 2: have pytest look for files with "test" and "example" # content of pytest.ini, tox.ini, or setup.cfg file (replace "pytest" # with "tool:pytest" for setup.cfg) [pytest] python_files = test_*.py example_*.py .. note:: the ``python_functions`` and ``python_classes`` options has no effect for ``unittest.TestCase`` test discovery because pytest delegates discovery of test case methods to unittest code. Interpreting cmdline arguments as Python packages ----------------------------------------------------- You can use the ``--pyargs`` option to make ``pytest`` try interpreting arguments as python package names, deriving their file system path and then running the test. For example if you have unittest2 installed you can type: .. code-block:: bash pytest --pyargs unittest2.test.test_skipping -q which would run the respective test module. Like with other options, through an ini-file and the :confval:`addopts` option you can make this change more permanently: .. code-block:: ini # content of pytest.ini [pytest] addopts = --pyargs Now a simple invocation of ``pytest NAME`` will check if NAME exists as an importable package/module and otherwise treat it as a filesystem path. Finding out what is collected ----------------------------------------------- You can always peek at the collection tree without running tests like this: .. code-block:: pytest . $ pytest --collect-only pythoncollection.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 3 items ======================= no tests ran in 0.12 seconds ======================= .. _customizing-test-collection: Customizing test collection --------------------------- .. regendoc:wipe You can easily instruct ``pytest`` to discover tests from every Python file: .. code-block:: ini # content of pytest.ini [pytest] python_files = *.py However, many projects will have a ``setup.py`` which they don't want to be imported. Moreover, there may files only importable by a specific python version. For such cases you can dynamically define files to be ignored by listing them in a ``conftest.py`` file:: # content of conftest.py import sys collect_ignore = ["setup.py"] if sys.version_info[0] > 2: collect_ignore.append("pkg/module_py2.py") and then if you have a module file like this:: # content of pkg/module_py2.py def test_only_on_python2(): try: assert 0 except Exception, e: pass and a ``setup.py`` dummy file like this:: # content of setup.py 0/0 # will raise exception if imported If you run with a Python 2 interpreter then you will find the one test and will leave out the ``setup.py`` file: .. code-block:: pytest #$ pytest --collect-only ====== test session starts ====== platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 1 items ====== no tests ran in 0.04 seconds ====== If you run with a Python 3 interpreter both the one test and the ``setup.py`` file will be left out: .. code-block:: pytest $ pytest --collect-only =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 0 items ======================= no tests ran in 0.12 seconds ======================= It's also possible to ignore files based on Unix shell-style wildcards by adding patterns to ``collect_ignore_glob``. The following example ``conftest.py`` ignores the file ``setup.py`` and in addition all files that end with ``*_py2.py`` when executed with a Python 3 interpreter:: # content of conftest.py import sys collect_ignore = ["setup.py"] if sys.version_info[0] > 2: collect_ignore_glob = ["*_py2.py"] pytest-4.6.9/doc/en/links.inc0000664000372000037200000000166413604173400016713 0ustar travistravis00000000000000 .. _`skipping plugin`: plugin/skipping.html .. _`funcargs mechanism`: funcargs.html .. _`doctest.py`: http://docs.python.org/library/doctest.html .. _`xUnit style setup`: xunit_setup.html .. _`pytest_nose`: plugin/nose.html .. _`reStructured Text`: http://docutils.sourceforge.net .. _`Python debugger`: http://docs.python.org/lib/module-pdb.html .. _nose: https://nose.readthedocs.io/en/latest/ .. _pytest: https://pypi.org/project/pytest/ .. _mercurial: http://mercurial.selenic.com/wiki/ .. _`setuptools`: https://pypi.org/project/setuptools/ .. _`easy_install`: .. _`distribute docs`: .. _`distribute`: https://pypi.org/project/distribute/ .. _`pip`: https://pypi.org/project/pip/ .. _`venv`: https://docs.python.org/3/library/venv.html/ .. _`virtualenv`: https://pypi.org/project/virtualenv/ .. _hudson: http://hudson-ci.org/ .. _jenkins: http://jenkins-ci.org/ .. _tox: http://testrun.org/tox .. _pylib: https://py.readthedocs.io/en/latest/ pytest-4.6.9/doc/en/backwards-compatibility.rst0000664000372000037200000000436613604173400022444 0ustar travistravis00000000000000.. _backwards-compatibility: Backwards Compatibility Policy ============================== Keeping backwards compatibility has a very high priority in the pytest project. Although we have deprecated functionality over the years, most of it is still supported. All deprecations in pytest were done because simpler or more efficient ways of accomplishing the same tasks have emerged, making the old way of doing things unnecessary. With the pytest 3.0 release we introduced a clear communication scheme for when we will actually remove the old busted joint and politely ask you to use the new hotness instead, while giving you enough time to adjust your tests or raise concerns if there are valid reasons to keep deprecated functionality around. To communicate changes we issue deprecation warnings using a custom warning hierarchy (see :ref:`internal-warnings`). These warnings may be suppressed using the standard means: ``-W`` command-line flag or ``filterwarnings`` ini options (see :ref:`warnings`), but we suggest to use these sparingly and temporarily, and heed the warnings when possible. We will only start the removal of deprecated functionality in major releases (e.g. if we deprecate something in 3.0 we will start to remove it in 4.0), and keep it around for at least two minor releases (e.g. if we deprecate something in 3.9 and 4.0 is the next release, we start to remove it in 5.0, not in 4.0). When the deprecation expires (e.g. 4.0 is released), we won't remove the deprecated functionality immediately, but will use the standard warning filters to turn them into **errors** by default. This approach makes it explicit that removal is imminent, and still gives you time to turn the deprecated feature into a warning instead of an error so it can be dealt with in your own time. In the next minor release (e.g. 4.1), the feature will be effectively removed. Deprecation Roadmap ------------------- Features currently deprecated and removed in previous releases can be found in :ref:`deprecations`. We track future deprecation and removal of features using milestones and the `deprecation `_ and `removal `_ labels on GitHub. pytest-4.6.9/doc/en/adopt.rst0000664000372000037200000001024613604173400016735 0ustar travistravis00000000000000:orphan: .. warnings about this file not being included in any toctree will be suppressed by :orphan: April 2015 is "adopt pytest month" ============================================= Are you an enthusiastic pytest user, the local testing guru in your workplace? Or are you considering using pytest for your open source project, but not sure how to get started? Then you may be interested in "adopt pytest month"! We will pair experienced pytest users with open source projects, for a month's effort of getting new development teams started with pytest. In 2015 we are trying this for the first time. In February and March 2015 we will gather volunteers on both sides, in April we will do the work, and in May we will evaluate how it went. This effort is being coordinated by Brianna Laugher. If you have any questions or comments, you can raise them on the `@pytestdotorg twitter account `_ the `issue tracker`_ or the `pytest-dev mailing list`_. .. _`issue tracker`: https://github.com/pytest-dev/pytest/issues/676 .. _`pytest-dev mailing list`: https://mail.python.org/mailman/listinfo/pytest-dev The ideal pytest helper ----------------------------------------- - will be able to commit 2-4 hours a week to working with their particular project (this might involve joining their mailing list, installing the software and exploring any existing tests, offering advice, writing some example tests) - feels confident in using pytest (e.g. has explored command line options, knows how to write parametrized tests, has an idea about conftest contents) - does not need to be an expert in every aspect! `Pytest helpers, sign up here`_! (preferably in February, hard deadline 22 March) .. _`Pytest helpers, sign up here`: http://goo.gl/forms/nxqAhqWt1P The ideal partner project ----------------------------------------- - is open source, and predominantly written in Python - has an automated/documented install process for developers - has more than one core developer - has at least one official release (e.g. is available on pypi) - has the support of the core development team, in trying out pytest adoption - has no tests... or 100% test coverage... or somewhere in between! `Partner projects, sign up here`_! (by 22 March) .. _`Partner projects, sign up here`: http://goo.gl/forms/ZGyqlHiwk3 What does it mean to "adopt pytest"? ----------------------------------------- There can be many different definitions of "success". Pytest can run many `nose and unittest`_ tests by default, so using pytest as your testrunner may be possible from day 1. Job done, right? Progressive success might look like: - tests can be run (by pytest) without errors (there may be failures) - tests can be run (by pytest) without failures - test runner is integrated into CI server - existing tests are rewritten to take advantage of pytest features - this can happen in several iterations, for example: - changing to native assert_ statements (pycmd_ has a script to help with that, ``pyconvert_unittest.py``) - changing `setUp/tearDown methods`_ to fixtures_ - adding markers_ - other changes to reduce boilerplate - assess needs for future tests to be written, e.g. new fixtures, distributed_ testing tweaks "Success" should also include that the development team feels comfortable with their knowledge of how to use pytest. In fact this is probably more important than anything else. So spending a lot of time on communication, giving examples, etc will probably be important - both in running the tests, and in writing them. It may be after the month is up, the partner project decides that pytest is not right for it. That's okay - hopefully the pytest team will also learn something about its weaknesses or deficiencies. .. _`nose and unittest`: faq.html#how-does-pytest-relate-to-nose-and-unittest .. _assert: asserts.html .. _pycmd: https://bitbucket.org/hpk42/pycmd/overview .. _`setUp/tearDown methods`: xunit_setup.html .. _fixtures: fixture.html .. _markers: markers.html .. _distributed: xdist.html Other ways to help ----------------------------------------- Promote! Do your favourite open source Python projects use pytest? If not, why not tell them about this page? pytest-4.6.9/doc/en/assert.rst0000664000372000037200000002457713604173400017143 0ustar travistravis00000000000000 The writing and reporting of assertions in tests ================================================== .. _`assertfeedback`: .. _`assert with the assert statement`: .. _`assert`: Asserting with the ``assert`` statement --------------------------------------------------------- ``pytest`` allows you to use the standard python ``assert`` for verifying expectations and values in Python tests. For example, you can write the following: .. code-block:: python # content of test_assert1.py def f(): return 3 def test_function(): assert f() == 4 to assert that your function returns a certain value. If this assertion fails you will see the return value of the function call: .. code-block:: pytest $ pytest test_assert1.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_assert1.py F [100%] ================================= FAILURES ================================= ______________________________ test_function _______________________________ def test_function(): > assert f() == 4 E assert 3 == 4 E + where 3 = f() test_assert1.py:6: AssertionError ========================= 1 failed in 0.12 seconds ========================= ``pytest`` has support for showing the values of the most common subexpressions including calls, attributes, comparisons, and binary and unary operators. (See :ref:`tbreportdemo`). This allows you to use the idiomatic python constructs without boilerplate code while not losing introspection information. However, if you specify a message with the assertion like this: .. code-block:: python assert a % 2 == 0, "value was odd, should be even" then no assertion introspection takes places at all and the message will be simply shown in the traceback. See :ref:`assert-details` for more information on assertion introspection. .. _`assertraises`: Assertions about expected exceptions ------------------------------------------ In order to write assertions about raised exceptions, you can use ``pytest.raises`` as a context manager like this: .. code-block:: python import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1 / 0 and if you need to have access to the actual exception info you may use: .. code-block:: python def test_recursion_depth(): with pytest.raises(RuntimeError) as excinfo: def f(): f() f() assert "maximum recursion" in str(excinfo.value) ``excinfo`` is a ``ExceptionInfo`` instance, which is a wrapper around the actual exception raised. The main attributes of interest are ``.type``, ``.value`` and ``.traceback``. You can pass a ``match`` keyword parameter to the context-manager to test that a regular expression matches on the string representation of an exception (similar to the ``TestCase.assertRaisesRegexp`` method from ``unittest``): .. code-block:: python import pytest def myfunc(): raise ValueError("Exception 123 raised") def test_match(): with pytest.raises(ValueError, match=r".* 123 .*"): myfunc() The regexp parameter of the ``match`` method is matched with the ``re.search`` function, so in the above example ``match='123'`` would have worked as well. There's an alternate form of the ``pytest.raises`` function where you pass a function that will be executed with the given ``*args`` and ``**kwargs`` and assert that the given exception is raised: .. code-block:: python pytest.raises(ExpectedException, func, *args, **kwargs) The reporter will provide you with helpful output in case of failures such as *no exception* or *wrong exception*. Note that it is also possible to specify a "raises" argument to ``pytest.mark.xfail``, which checks that the test is failing in a more specific way than just having any exception raised: .. code-block:: python @pytest.mark.xfail(raises=IndexError) def test_f(): f() Using ``pytest.raises`` is likely to be better for cases where you are testing exceptions your own code is deliberately raising, whereas using ``@pytest.mark.xfail`` with a check function is probably better for something like documenting unfixed bugs (where the test describes what "should" happen) or bugs in dependencies. .. _`assertwarns`: Assertions about expected warnings ----------------------------------------- You can check that code raises a particular warning using :ref:`pytest.warns `. .. _newreport: Making use of context-sensitive comparisons ------------------------------------------------- ``pytest`` has rich support for providing context-sensitive information when it encounters comparisons. For example: .. code-block:: python # content of test_assert2.py def test_set_comparison(): set1 = set("1308") set2 = set("8035") assert set1 == set2 if you run this module: .. code-block:: pytest $ pytest test_assert2.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_assert2.py F [100%] ================================= FAILURES ================================= ___________________________ test_set_comparison ____________________________ def test_set_comparison(): set1 = set("1308") set2 = set("8035") > assert set1 == set2 E AssertionError: assert {'0', '1', '3', '8'} == {'0', '3', '5', '8'} E Extra items in the left set: E '1' E Extra items in the right set: E '5' E Use -v to get the full diff test_assert2.py:6: AssertionError ========================= 1 failed in 0.12 seconds ========================= Special comparisons are done for a number of cases: * comparing long strings: a context diff is shown * comparing long sequences: first failing indices * comparing dicts: different entries See the :ref:`reporting demo ` for many more examples. Defining your own explanation for failed assertions --------------------------------------------------- It is possible to add your own detailed explanations by implementing the ``pytest_assertrepr_compare`` hook. .. autofunction:: _pytest.hookspec.pytest_assertrepr_compare :noindex: As an example consider adding the following hook in a :ref:`conftest.py ` file which provides an alternative explanation for ``Foo`` objects: .. code-block:: python # content of conftest.py from test_foocompare import Foo def pytest_assertrepr_compare(op, left, right): if isinstance(left, Foo) and isinstance(right, Foo) and op == "==": return ["Comparing Foo instances:", " vals: %s != %s" % (left.val, right.val)] now, given this test module: .. code-block:: python # content of test_foocompare.py class Foo(object): def __init__(self, val): self.val = val def __eq__(self, other): return self.val == other.val def test_compare(): f1 = Foo(1) f2 = Foo(2) assert f1 == f2 you can run the test module and get the custom output defined in the conftest file: .. code-block:: pytest $ pytest -q test_foocompare.py F [100%] ================================= FAILURES ================================= _______________________________ test_compare _______________________________ def test_compare(): f1 = Foo(1) f2 = Foo(2) > assert f1 == f2 E assert Comparing Foo instances: E vals: 1 != 2 test_foocompare.py:12: AssertionError 1 failed in 0.12 seconds .. _assert-details: .. _`assert introspection`: Assertion introspection details ------------------------------- Reporting details about a failing assertion is achieved by rewriting assert statements before they are run. Rewritten assert statements put introspection information into the assertion failure message. ``pytest`` only rewrites test modules directly discovered by its test collection process, so **asserts in supporting modules which are not themselves test modules will not be rewritten**. You can manually enable assertion rewriting for an imported module by calling `register_assert_rewrite `_ before you import it (a good place to do that is in your root ``conftest.py``). For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting `_. Assertion rewriting caches files on disk ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``pytest`` will write back the rewritten modules to disk for caching. You can disable this behavior (for example to avoid leaving stale ``.pyc`` files around in projects that move files around a lot) by adding this to the top of your ``conftest.py`` file: .. code-block:: python import sys sys.dont_write_bytecode = True Note that you still get the benefits of assertion introspection, the only change is that the ``.pyc`` files won't be cached on disk. Additionally, rewriting will silently skip caching if it cannot write new ``.pyc`` files, i.e. in a read-only filesystem or a zipfile. Disabling assert rewriting ~~~~~~~~~~~~~~~~~~~~~~~~~~ ``pytest`` rewrites test modules on import by using an import hook to write new ``pyc`` files. Most of the time this works transparently. However, if you are working with the import machinery yourself, the import hook may interfere. If this is the case you have two options: * Disable rewriting for a specific module by adding the string ``PYTEST_DONT_REWRITE`` to its docstring. * Disable rewriting for all modules by using ``--assert=plain``. Add assert rewriting as an alternate introspection technique. Introduce the ``--assert`` option. Deprecate ``--no-assert`` and ``--nomagic``. Removes the ``--no-assert`` and ``--nomagic`` options. Removes the ``--assert=reinterp`` option. pytest-4.6.9/doc/en/writing_plugins.rst0000664000372000037200000005420413604173400021054 0ustar travistravis00000000000000.. _plugins: .. _`writing-plugins`: Writing plugins =============== It is easy to implement `local conftest plugins`_ for your own project or `pip-installable plugins`_ that can be used throughout many projects, including third party projects. Please refer to :ref:`using plugins` if you only want to use but not write plugins. A plugin contains one or multiple hook functions. :ref:`Writing hooks ` explains the basics and details of how you can write a hook function yourself. ``pytest`` implements all aspects of configuration, collection, running and reporting by calling :ref:`well specified hooks ` of the following plugins: * builtin plugins: loaded from pytest's internal ``_pytest`` directory. * :ref:`external plugins `: modules discovered through `setuptools entry points`_ * `conftest.py plugins`_: modules auto-discovered in test directories In principle, each hook call is a ``1:N`` Python function call where ``N`` is the number of registered implementation functions for a given specification. All specifications and implementations follow the ``pytest_`` prefix naming convention, making them easy to distinguish and find. .. _`pluginorder`: Plugin discovery order at tool startup -------------------------------------- ``pytest`` loads plugin modules at tool startup in the following way: * by loading all builtin plugins * by loading all plugins registered through `setuptools entry points`_. * by pre-scanning the command line for the ``-p name`` option and loading the specified plugin before actual command line parsing. * by loading all :file:`conftest.py` files as inferred by the command line invocation: - if no test paths are specified use current dir as a test path - if exists, load ``conftest.py`` and ``test*/conftest.py`` relative to the directory part of the first test path. Note that pytest does not find ``conftest.py`` files in deeper nested sub directories at tool startup. It is usually a good idea to keep your ``conftest.py`` file in the top level test or project root directory. * by recursively loading all plugins specified by the ``pytest_plugins`` variable in ``conftest.py`` files .. _`pytest/plugin`: http://bitbucket.org/pytest-dev/pytest/src/tip/pytest/plugin/ .. _`conftest.py plugins`: .. _`localplugin`: .. _`local conftest plugins`: conftest.py: local per-directory plugins ---------------------------------------- Local ``conftest.py`` plugins contain directory-specific hook implementations. Hook Session and test running activities will invoke all hooks defined in ``conftest.py`` files closer to the root of the filesystem. Example of implementing the ``pytest_runtest_setup`` hook so that is called for tests in the ``a`` sub directory but not for other directories:: a/conftest.py: def pytest_runtest_setup(item): # called for running each test in 'a' directory print("setting up", item) a/test_sub.py: def test_sub(): pass test_flat.py: def test_flat(): pass Here is how you might run it::     pytest test_flat.py --capture=no # will not show "setting up" pytest a/test_sub.py --capture=no # will show "setting up" .. note:: If you have ``conftest.py`` files which do not reside in a python package directory (i.e. one containing an ``__init__.py``) then "import conftest" can be ambiguous because there might be other ``conftest.py`` files as well on your ``PYTHONPATH`` or ``sys.path``. It is thus good practice for projects to either put ``conftest.py`` under a package scope or to never import anything from a ``conftest.py`` file. See also: :ref:`pythonpath`. Writing your own plugin ----------------------- .. _`setuptools`: https://pypi.org/project/setuptools/ If you want to write a plugin, there are many real-life examples you can copy from: * a custom collection example plugin: :ref:`yaml plugin` * builtin plugins which provide pytest's own functionality * many `external plugins `_ providing additional features All of these plugins implement :ref:`hooks ` and/or :ref:`fixtures ` to extend and add functionality. .. note:: Make sure to check out the excellent `cookiecutter-pytest-plugin `_ project, which is a `cookiecutter template `_ for authoring plugins. The template provides an excellent starting point with a working plugin, tests running with tox, a comprehensive README file as well as a pre-configured entry-point. Also consider :ref:`contributing your plugin to pytest-dev` once it has some happy users other than yourself. .. _`setuptools entry points`: .. _`pip-installable plugins`: Making your plugin installable by others ---------------------------------------- If you want to make your plugin externally available, you may define a so-called entry point for your distribution so that ``pytest`` finds your plugin module. Entry points are a feature that is provided by `setuptools`_. pytest looks up the ``pytest11`` entrypoint to discover its plugins and you can thus make your plugin available by defining it in your setuptools-invocation: .. sourcecode:: python # sample ./setup.py file from setuptools import setup setup( name="myproject", packages=["myproject"], # the following makes a plugin available to pytest entry_points={"pytest11": ["name_of_plugin = myproject.pluginmodule"]}, # custom PyPI classifier for pytest plugins classifiers=["Framework :: Pytest"], ) If a package is installed this way, ``pytest`` will load ``myproject.pluginmodule`` as a plugin which can define :ref:`hooks `. .. note:: Make sure to include ``Framework :: Pytest`` in your list of `PyPI classifiers `_ to make it easy for users to find your plugin. .. _assertion-rewriting: Assertion Rewriting ------------------- One of the main features of ``pytest`` is the use of plain assert statements and the detailed introspection of expressions upon assertion failures. This is provided by "assertion rewriting" which modifies the parsed AST before it gets compiled to bytecode. This is done via a :pep:`302` import hook which gets installed early on when ``pytest`` starts up and will perform this rewriting when modules get imported. However since we do not want to test different bytecode then you will run in production this hook only rewrites test modules themselves as well as any modules which are part of plugins. Any other imported module will not be rewritten and normal assertion behaviour will happen. If you have assertion helpers in other modules where you would need assertion rewriting to be enabled you need to ask ``pytest`` explicitly to rewrite this module before it gets imported. .. autofunction:: pytest.register_assert_rewrite :noindex: This is especially important when you write a pytest plugin which is created using a package. The import hook only treats ``conftest.py`` files and any modules which are listed in the ``pytest11`` entrypoint as plugins. As an example consider the following package:: pytest_foo/__init__.py pytest_foo/plugin.py pytest_foo/helper.py With the following typical ``setup.py`` extract: .. code-block:: python setup(..., entry_points={"pytest11": ["foo = pytest_foo.plugin"]}, ...) In this case only ``pytest_foo/plugin.py`` will be rewritten. If the helper module also contains assert statements which need to be rewritten it needs to be marked as such, before it gets imported. This is easiest by marking it for rewriting inside the ``__init__.py`` module, which will always be imported first when a module inside a package is imported. This way ``plugin.py`` can still import ``helper.py`` normally. The contents of ``pytest_foo/__init__.py`` will then need to look like this: .. code-block:: python import pytest pytest.register_assert_rewrite("pytest_foo.helper") Requiring/Loading plugins in a test module or conftest file ----------------------------------------------------------- You can require plugins in a test module or a ``conftest.py`` file like this: .. code-block:: python pytest_plugins = ["name1", "name2"] When the test module or conftest plugin is loaded the specified plugins will be loaded as well. Any module can be blessed as a plugin, including internal application modules: .. code-block:: python pytest_plugins = "myapp.testsupport.myplugin" ``pytest_plugins`` variables are processed recursively, so note that in the example above if ``myapp.testsupport.myplugin`` also declares ``pytest_plugins``, the contents of the variable will also be loaded as plugins, and so on. .. _`requiring plugins in non-root conftests`: .. note:: Requiring plugins using a ``pytest_plugins`` variable in non-root ``conftest.py`` files is deprecated. This is important because ``conftest.py`` files implement per-directory hook implementations, but once a plugin is imported, it will affect the entire directory tree. In order to avoid confusion, defining ``pytest_plugins`` in any ``conftest.py`` file which is not located in the tests root directory is deprecated, and will raise a warning. This mechanism makes it easy to share fixtures within applications or even external applications without the need to create external plugins using the ``setuptools``'s entry point technique. Plugins imported by ``pytest_plugins`` will also automatically be marked for assertion rewriting (see :func:`pytest.register_assert_rewrite`). However for this to have any effect the module must not be imported already; if it was already imported at the time the ``pytest_plugins`` statement is processed, a warning will result and assertions inside the plugin will not be rewritten. To fix this you can either call :func:`pytest.register_assert_rewrite` yourself before the module is imported, or you can arrange the code to delay the importing until after the plugin is registered. Accessing another plugin by name -------------------------------- If a plugin wants to collaborate with code from another plugin it can obtain a reference through the plugin manager like this: .. sourcecode:: python plugin = config.pluginmanager.get_plugin("name_of_plugin") If you want to look at the names of existing plugins, use the ``--trace-config`` option. .. _registering-markers: Registering custom markers -------------------------- If your plugin uses any markers, you should register them so that they appear in pytest's help text and do not :ref:`cause spurious warnings `. For example, the following plugin would register ``cool_marker`` and ``mark_with`` for all users: .. code-block:: python def pytest_configure(config): config.addinivalue_line("markers", "cool_marker: this one is for cool tests.") config.addinivalue_line( "markers", "mark_with(arg, arg2): this marker takes arguments." ) Testing plugins --------------- pytest comes with a plugin named ``pytester`` that helps you write tests for your plugin code. The plugin is disabled by default, so you will have to enable it before you can use it. You can do so by adding the following line to a ``conftest.py`` file in your testing directory: .. code-block:: python # content of conftest.py pytest_plugins = ["pytester"] Alternatively you can invoke pytest with the ``-p pytester`` command line option. This will allow you to use the :py:class:`testdir <_pytest.pytester.Testdir>` fixture for testing your plugin code. Let's demonstrate what you can do with the plugin with an example. Imagine we developed a plugin that provides a fixture ``hello`` which yields a function and we can invoke this function with one optional parameter. It will return a string value of ``Hello World!`` if we do not supply a value or ``Hello {value}!`` if we do supply a string value. .. code-block:: python # -*- coding: utf-8 -*- import pytest def pytest_addoption(parser): group = parser.getgroup("helloworld") group.addoption( "--name", action="store", dest="name", default="World", help='Default "name" for hello().', ) @pytest.fixture def hello(request): name = request.config.getoption("name") def _hello(name=None): if not name: name = request.config.getoption("name") return "Hello {name}!".format(name=name) return _hello Now the ``testdir`` fixture provides a convenient API for creating temporary ``conftest.py`` files and test files. It also allows us to run the tests and return a result object, with which we can assert the tests' outcomes. .. code-block:: python def test_hello(testdir): """Make sure that our plugin works.""" # create a temporary conftest.py file testdir.makeconftest( """ import pytest @pytest.fixture(params=[ "Brianna", "Andreas", "Floris", ]) def name(request): return request.param """ ) # create a temporary pytest test file testdir.makepyfile( """ def test_hello_default(hello): assert hello() == "Hello World!" def test_hello_name(hello, name): assert hello(name) == "Hello {0}!".format(name) """ ) # run all tests with pytest result = testdir.runpytest() # check that all 4 tests passed result.assert_outcomes(passed=4) additionally it is possible to copy examples for an example folder before running pytest on it .. code-block:: ini # content of pytest.ini [pytest] pytester_example_dir = . .. code-block:: python # content of test_example.py def test_plugin(testdir): testdir.copy_example("test_example.py") testdir.runpytest("-k", "test_example") def test_example(): pass .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini collected 2 items test_example.py .. [100%] ============================= warnings summary ============================= test_example.py::test_plugin $REGENDOC_TMPDIR/test_example.py:4: PytestExperimentalApiWarning: testdir.copy_example is an experimental api that may change over time testdir.copy_example("test_example.py") -- Docs: https://docs.pytest.org/en/latest/warnings.html =================== 2 passed, 1 warnings in 0.12 seconds =================== For more information about the result object that ``runpytest()`` returns, and the methods that it provides please check out the :py:class:`RunResult <_pytest.pytester.RunResult>` documentation. .. _`writinghooks`: Writing hook functions ====================== .. _validation: hook function validation and execution -------------------------------------- pytest calls hook functions from registered plugins for any given hook specification. Let's look at a typical hook function for the ``pytest_collection_modifyitems(session, config, items)`` hook which pytest calls after collection of all test items is completed. When we implement a ``pytest_collection_modifyitems`` function in our plugin pytest will during registration verify that you use argument names which match the specification and bail out if not. Let's look at a possible implementation: .. code-block:: python def pytest_collection_modifyitems(config, items): # called after collection is completed # you can modify the ``items`` list ... Here, ``pytest`` will pass in ``config`` (the pytest config object) and ``items`` (the list of collected test items) but will not pass in the ``session`` argument because we didn't list it in the function signature. This dynamic "pruning" of arguments allows ``pytest`` to be "future-compatible": we can introduce new hook named parameters without breaking the signatures of existing hook implementations. It is one of the reasons for the general long-lived compatibility of pytest plugins. Note that hook functions other than ``pytest_runtest_*`` are not allowed to raise exceptions. Doing so will break the pytest run. .. _firstresult: firstresult: stop at first non-None result ------------------------------------------- Most calls to ``pytest`` hooks result in a **list of results** which contains all non-None results of the called hook functions. Some hook specifications use the ``firstresult=True`` option so that the hook call only executes until the first of N registered functions returns a non-None result which is then taken as result of the overall hook call. The remaining hook functions will not be called in this case. hookwrapper: executing around other hooks ------------------------------------------------- .. currentmodule:: _pytest.core pytest plugins can implement hook wrappers which wrap the execution of other hook implementations. A hook wrapper is a generator function which yields exactly once. When pytest invokes hooks it first executes hook wrappers and passes the same arguments as to the regular hooks. At the yield point of the hook wrapper pytest will execute the next hook implementations and return their result to the yield point in the form of a :py:class:`Result ` instance which encapsulates a result or exception info. The yield point itself will thus typically not raise exceptions (unless there are bugs). Here is an example definition of a hook wrapper: .. code-block:: python import pytest @pytest.hookimpl(hookwrapper=True) def pytest_pyfunc_call(pyfuncitem): do_something_before_next_hook_executes() outcome = yield # outcome.excinfo may be None or a (cls, val, tb) tuple res = outcome.get_result() # will raise if outcome was exception post_process_result(res) outcome.force_result(new_res) # to override the return value to the plugin system Note that hook wrappers don't return results themselves, they merely perform tracing or other side effects around the actual hook implementations. If the result of the underlying hook is a mutable object, they may modify that result but it's probably better to avoid it. For more information, consult the `pluggy documentation `_. Hook function ordering / call example ------------------------------------- For any given hook specification there may be more than one implementation and we thus generally view ``hook`` execution as a ``1:N`` function call where ``N`` is the number of registered functions. There are ways to influence if a hook implementation comes before or after others, i.e. the position in the ``N``-sized list of functions: .. code-block:: python # Plugin 1 @pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(items): # will execute as early as possible ... # Plugin 2 @pytest.hookimpl(trylast=True) def pytest_collection_modifyitems(items): # will execute as late as possible ... # Plugin 3 @pytest.hookimpl(hookwrapper=True) def pytest_collection_modifyitems(items): # will execute even before the tryfirst one above! outcome = yield # will execute after all non-hookwrappers executed Here is the order of execution: 1. Plugin3's pytest_collection_modifyitems called until the yield point because it is a hook wrapper. 2. Plugin1's pytest_collection_modifyitems is called because it is marked with ``tryfirst=True``. 3. Plugin2's pytest_collection_modifyitems is called because it is marked with ``trylast=True`` (but even without this mark it would come after Plugin1). 4. Plugin3's pytest_collection_modifyitems then executing the code after the yield point. The yield receives a :py:class:`Result ` instance which encapsulates the result from calling the non-wrappers. Wrappers shall not modify the result. It's possible to use ``tryfirst`` and ``trylast`` also in conjunction with ``hookwrapper=True`` in which case it will influence the ordering of hookwrappers among each other. Declaring new hooks ------------------------ .. currentmodule:: _pytest.hookspec Plugins and ``conftest.py`` files may declare new hooks that can then be implemented by other plugins in order to alter behaviour or interact with the new plugin: .. autofunction:: pytest_addhooks :noindex: Hooks are usually declared as do-nothing functions that contain only documentation describing when the hook will be called and what return values are expected. For an example, see `newhooks.py`_ from `xdist `_. .. _`newhooks.py`: https://github.com/pytest-dev/pytest-xdist/blob/974bd566c599dc6a9ea291838c6f226197208b46/xdist/newhooks.py Optionally using hooks from 3rd party plugins --------------------------------------------- Using new hooks from plugins as explained above might be a little tricky because of the standard :ref:`validation mechanism `: if you depend on a plugin that is not installed, validation will fail and the error message will not make much sense to your users. One approach is to defer the hook implementation to a new plugin instead of declaring the hook functions directly in your plugin module, for example: .. code-block:: python # contents of myplugin.py class DeferPlugin(object): """Simple plugin to defer pytest-xdist hook functions.""" def pytest_testnodedown(self, node, error): """standard xdist hook function. """ def pytest_configure(config): if config.pluginmanager.hasplugin("xdist"): config.pluginmanager.register(DeferPlugin()) This has the added benefit of allowing you to conditionally install hooks depending on which plugins are installed. pytest-4.6.9/doc/en/development_guide.rst0000664000372000037200000000446213604173400021330 0ustar travistravis00000000000000================= Development Guide ================= Some general guidelines regarding development in pytest for maintainers and contributors. Nothing here is set in stone and can't be changed, feel free to suggest improvements or changes in the workflow. Code Style ---------- * `PEP-8 `_ * `flake8 `_ for quality checks * `invoke `_ to automate development tasks Branches -------- We have two long term branches: * ``master``: contains the code for the next bugfix release. * ``features``: contains the code with new features for the next minor release. The official repository usually does not contain topic branches, developers and contributors should create topic branches in their own forks. Exceptions can be made for cases where more than one contributor is working on the same topic or where it makes sense to use some automatic capability of the main repository, such as automatic docs from `readthedocs `_ for a branch dealing with documentation refactoring. Issues ------ Any question, feature, bug or proposal is welcome as an issue. Users are encouraged to use them whenever they need. GitHub issues should use labels to categorize them. Labels should be created sporadically, to fill a niche; we should avoid creating labels just for the sake of creating them. Each label should include a description in the GitHub's interface stating its purpose. Labels are managed using `labels `_. All the labels in the repository are kept in ``.github/labels.toml``, so any changes should be via PRs to that file. After a PR is accepted and merged, one of the maintainers must manually synchronize the labels file with the GitHub repository. Temporary labels ~~~~~~~~~~~~~~~~ To classify issues for a special event it is encouraged to create a temporary label. This helps those involved to find the relevant issues to work on. Examples of that are sprints in Python events or global hacking events. * ``temporary: EP2017 sprint``: candidate issues or PRs tackled during the EuroPython 2017 Issues created at those events should have other relevant labels added as well. Those labels should be removed after they are no longer relevant. .. include:: ../../HOWTORELEASE.rst pytest-4.6.9/doc/en/xunit_setup.rst0000664000372000037200000000704013604173400020213 0ustar travistravis00000000000000 .. _`classic xunit`: .. _xunitsetup: classic xunit-style setup ======================================== This section describes a classic and popular way how you can implement fixtures (setup and teardown test state) on a per-module/class/function basis. .. note:: While these setup/teardown methods are simple and familiar to those coming from a ``unittest`` or nose ``background``, you may also consider using pytest's more powerful :ref:`fixture mechanism ` which leverages the concept of dependency injection, allowing for a more modular and more scalable approach for managing test state, especially for larger projects and for functional testing. You can mix both fixture mechanisms in the same file but test methods of ``unittest.TestCase`` subclasses cannot receive fixture arguments. Module level setup/teardown -------------------------------------- If you have multiple test functions and test classes in a single module you can optionally implement the following fixture methods which will usually be called once for all the functions:: def setup_module(module): """ setup any state specific to the execution of the given module.""" def teardown_module(module): """ teardown any state that was previously setup with a setup_module method. """ As of pytest-3.0, the ``module`` parameter is optional. Class level setup/teardown ---------------------------------- Similarly, the following methods are called at class level before and after all test methods of the class are called:: @classmethod def setup_class(cls): """ setup any state specific to the execution of the given class (which usually contains tests). """ @classmethod def teardown_class(cls): """ teardown any state that was previously setup with a call to setup_class. """ Method and function level setup/teardown ----------------------------------------------- Similarly, the following methods are called around each method invocation:: def setup_method(self, method): """ setup any state tied to the execution of the given method in a class. setup_method is invoked for every test method of a class. """ def teardown_method(self, method): """ teardown any state that was previously setup with a setup_method call. """ As of pytest-3.0, the ``method`` parameter is optional. If you would rather define test functions directly at module level you can also use the following functions to implement fixtures:: def setup_function(function): """ setup any state tied to the execution of the given function. Invoked for every test function in the module. """ def teardown_function(function): """ teardown any state that was previously setup with a setup_function call. """ As of pytest-3.0, the ``function`` parameter is optional. Remarks: * It is possible for setup/teardown pairs to be invoked multiple times per testing process. * teardown functions are not called if the corresponding setup function existed and failed/was skipped. * Prior to pytest-4.2, xunit-style functions did not obey the scope rules of fixtures, so it was possible, for example, for a ``setup_method`` to be called before a session-scoped autouse fixture. Now the xunit-style functions are integrated with the fixture mechanism and obey the proper scope rules of fixtures involved in the call. .. _`unittest.py module`: http://docs.python.org/library/unittest.html pytest-4.6.9/doc/en/license.rst0000664000372000037200000000254313604173400017251 0ustar travistravis00000000000000.. _license: License ------- Distributed under the terms of the `MIT`_ license, pytest is free and open source software. .. code-block:: text The MIT License (MIT) Copyright (c) 2004-2020 Holger Krekel and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. .. _`MIT`: https://github.com/pytest-dev/pytest/blob/master/LICENSE pytest-4.6.9/doc/en/contents.rst0000664000372000037200000000173613604173400017467 0ustar travistravis00000000000000.. _toc: Full pytest documentation =========================== `Download latest version as PDF `_ .. `Download latest version as EPUB `_ .. toctree:: :maxdepth: 2 getting-started usage existingtestsuite assert fixture mark monkeypatch tmpdir capture warnings doctest skipping parametrize cache unittest nose xunit_setup plugins writing_plugins logging reference goodpractices flaky pythonpath customize example/index bash-completion backwards-compatibility deprecations py27-py34-deprecation historical-notes license contributing development_guide talks projects faq contact tidelift .. only:: html .. toctree:: :maxdepth: 1 announce/index .. only:: html .. toctree:: :hidden: :maxdepth: 1 changelog pytest-4.6.9/doc/en/yieldfixture.rst0000664000372000037200000000073013604173400020340 0ustar travistravis00000000000000:orphan: .. _yieldfixture: "yield_fixture" functions --------------------------------------------------------------- .. important:: Since pytest-3.0, fixtures using the normal ``fixture`` decorator can use a ``yield`` statement to provide fixture values and execute teardown code, exactly like ``yield_fixture`` in previous versions. Marking functions as ``yield_fixture`` is still supported, but deprecated and should not be used in new code. pytest-4.6.9/doc/en/usage.rst0000664000372000037200000005637713604173400016751 0ustar travistravis00000000000000 .. _usage: Usage and Invocations ========================================== .. _cmdline: Calling pytest through ``python -m pytest`` ----------------------------------------------------- You can invoke testing through the Python interpreter from the command line: .. code-block:: text python -m pytest [...] This is almost equivalent to invoking the command line script ``pytest [...]`` directly, except that calling via ``python`` will also add the current directory to ``sys.path``. Possible exit codes -------------------------------------------------------------- Running ``pytest`` can result in six different exit codes: :Exit code 0: All tests were collected and passed successfully :Exit code 1: Tests were collected and run but some of the tests failed :Exit code 2: Test execution was interrupted by the user :Exit code 3: Internal error happened while executing tests :Exit code 4: pytest command line usage error :Exit code 5: No tests were collected Getting help on version, option names, environment variables -------------------------------------------------------------- .. code-block:: bash pytest --version # shows where pytest was imported from pytest --fixtures # show available builtin function arguments pytest -h | --help # show help on command line and config file options .. _maxfail: Stopping after the first (or N) failures --------------------------------------------------- To stop the testing process after the first (N) failures: .. code-block:: bash pytest -x # stop after first failure pytest --maxfail=2 # stop after two failures .. _select-tests: Specifying tests / selecting tests --------------------------------------------------- Pytest supports several ways to run and select tests from the command-line. **Run tests in a module** .. code-block:: bash pytest test_mod.py **Run tests in a directory** .. code-block:: bash pytest testing/ **Run tests by keyword expressions** .. code-block:: bash pytest -k "MyClass and not method" This will run tests which contain names that match the given *string expression*, which can include Python operators that use filenames, class names and function names as variables. The example above will run ``TestMyClass.test_something`` but not ``TestMyClass.test_method_simple``. .. _nodeids: **Run tests by node ids** Each collected test is assigned a unique ``nodeid`` which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by ``::`` characters. To run a specific test within a module: .. code-block:: bash pytest test_mod.py::test_func Another example specifying a test method in the command line: .. code-block:: bash pytest test_mod.py::TestClass::test_method **Run tests by marker expressions** .. code-block:: bash pytest -m slow Will run all tests which are decorated with the ``@pytest.mark.slow`` decorator. For more information see :ref:`marks `. **Run tests from packages** .. code-block:: bash pytest --pyargs pkg.testing This will import ``pkg.testing`` and use its filesystem location to find and run tests from. Modifying Python traceback printing ---------------------------------------------- Examples for modifying traceback printing: .. code-block:: bash pytest --showlocals # show local variables in tracebacks pytest -l # show local variables (shortcut) pytest --tb=auto # (default) 'long' tracebacks for the first and last # entry, but 'short' style for the other entries pytest --tb=long # exhaustive, informative traceback formatting pytest --tb=short # shorter traceback format pytest --tb=line # only one line per failure pytest --tb=native # Python standard library formatting pytest --tb=no # no traceback at all The ``--full-trace`` causes very long traces to be printed on error (longer than ``--tb=long``). It also ensures that a stack trace is printed on **KeyboardInterrupt** (Ctrl+C). This is very useful if the tests are taking too long and you interrupt them with Ctrl+C to find out where the tests are *hanging*. By default no output will be shown (because KeyboardInterrupt is caught by pytest). By using this option you make sure a trace is shown. .. _`pytest.detailed_failed_tests_usage`: Detailed summary report ----------------------- The ``-r`` flag can be used to display a "short test summary info" at the end of the test session, making it easy in large test suites to get a clear picture of all failures, skips, xfails, etc. Example: .. code-block:: python # content of test_example.py import pytest @pytest.fixture def error_fixture(): assert 0 def test_ok(): print("ok") def test_fail(): assert 0 def test_error(error_fixture): pass def test_skip(): pytest.skip("skipping this test") def test_xfail(): pytest.xfail("xfailing this test") @pytest.mark.xfail(reason="always xfail") def test_xpass(): pass .. code-block:: pytest $ pytest -ra =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 6 items test_example.py .FEsxX [100%] ================================== ERRORS ================================== _______________________ ERROR at setup of test_error _______________________ @pytest.fixture def error_fixture(): > assert 0 E assert 0 test_example.py:6: AssertionError ================================= FAILURES ================================= ________________________________ test_fail _________________________________ def test_fail(): > assert 0 E assert 0 test_example.py:14: AssertionError ========================= short test summary info ========================== SKIPPED [1] $REGENDOC_TMPDIR/test_example.py:23: skipping this test XFAIL test_example.py::test_xfail reason: xfailing this test XPASS test_example.py::test_xpass always xfail ERROR test_example.py::test_error - assert 0 FAILED test_example.py::test_fail - assert 0 = 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12 seconds = The ``-r`` options accepts a number of characters after it, with ``a`` used above meaning "all except passes". Here is the full list of available characters that can be used: - ``f`` - failed - ``E`` - error - ``s`` - skipped - ``x`` - xfailed - ``X`` - xpassed - ``p`` - passed - ``P`` - passed with output - ``a`` - all except ``pP`` - ``A`` - all More than one character can be used, so for example to only see failed and skipped tests, you can execute: .. code-block:: pytest $ pytest -rfs =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 6 items test_example.py .FEsxX [100%] ================================== ERRORS ================================== _______________________ ERROR at setup of test_error _______________________ @pytest.fixture def error_fixture(): > assert 0 E assert 0 test_example.py:6: AssertionError ================================= FAILURES ================================= ________________________________ test_fail _________________________________ def test_fail(): > assert 0 E assert 0 test_example.py:14: AssertionError ========================= short test summary info ========================== FAILED test_example.py::test_fail - assert 0 SKIPPED [1] $REGENDOC_TMPDIR/test_example.py:23: skipping this test = 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12 seconds = Using ``p`` lists the passing tests, whilst ``P`` adds an extra section "PASSES" with those tests that passed but had captured output: .. code-block:: pytest $ pytest -rpP =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 6 items test_example.py .FEsxX [100%] ================================== ERRORS ================================== _______________________ ERROR at setup of test_error _______________________ @pytest.fixture def error_fixture(): > assert 0 E assert 0 test_example.py:6: AssertionError ================================= FAILURES ================================= ________________________________ test_fail _________________________________ def test_fail(): > assert 0 E assert 0 test_example.py:14: AssertionError ================================== PASSES ================================== _________________________________ test_ok __________________________________ --------------------------- Captured stdout call --------------------------- ok ========================= short test summary info ========================== PASSED test_example.py::test_ok = 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12 seconds = .. _pdb-option: Dropping to PDB_ (Python Debugger) on failures ----------------------------------------------- .. _PDB: http://docs.python.org/library/pdb.html Python comes with a builtin Python debugger called PDB_. ``pytest`` allows one to drop into the PDB_ prompt via a command line option: .. code-block:: bash pytest --pdb This will invoke the Python debugger on every failure (or KeyboardInterrupt). Often you might only want to do this for the first failing test to understand a certain failure situation: .. code-block:: bash pytest -x --pdb # drop to PDB on first failure, then end test session pytest --pdb --maxfail=3 # drop to PDB for first three failures Note that on any failure the exception information is stored on ``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback``. In interactive use, this allows one to drop into postmortem debugging with any debug tool. One can also manually access the exception information, for example:: >>> import sys >>> sys.last_traceback.tb_lineno 42 >>> sys.last_value AssertionError('assert result == "ok"',) .. _trace-option: Dropping to PDB_ (Python Debugger) at the start of a test ---------------------------------------------------------- ``pytest`` allows one to drop into the PDB_ prompt immediately at the start of each test via a command line option: .. code-block:: bash pytest --trace This will invoke the Python debugger at the start of every test. .. _breakpoints: Setting breakpoints ------------------- .. versionadded: 2.4.0 To set a breakpoint in your code use the native Python ``import pdb;pdb.set_trace()`` call in your code and pytest automatically disables its output capture for that test: * Output capture in other tests is not affected. * Any prior test output that has already been captured and will be processed as such. * Output capture gets resumed when ending the debugger session (via the ``continue`` command). .. _`breakpoint-builtin`: Using the builtin breakpoint function ------------------------------------- Python 3.7 introduces a builtin ``breakpoint()`` function. Pytest supports the use of ``breakpoint()`` with the following behaviours: - When ``breakpoint()`` is called and ``PYTHONBREAKPOINT`` is set to the default value, pytest will use the custom internal PDB trace UI instead of the system default ``Pdb``. - When tests are complete, the system will default back to the system ``Pdb`` trace UI. - With ``--pdb`` passed to pytest, the custom internal Pdb trace UI is used with both ``breakpoint()`` and failed tests/unhandled exceptions. - ``--pdbcls`` can be used to specify a custom debugger class. .. _durations: Profiling test execution duration ------------------------------------- .. versionadded: 2.2 To get a list of the slowest 10 test durations: .. code-block:: bash pytest --durations=10 By default, pytest will not show test durations that are too small (<0.01s) unless ``-vv`` is passed on the command-line. Creating JUnitXML format files ---------------------------------------------------- To create result files which can be read by Jenkins_ or other Continuous integration servers, use this invocation: .. code-block:: bash pytest --junitxml=path to create an XML file at ``path``. To set the name of the root test suite xml item, you can configure the ``junit_suite_name`` option in your config file: .. code-block:: ini [pytest] junit_suite_name = my_suite .. versionadded:: 4.0 JUnit XML specification seems to indicate that ``"time"`` attribute should report total test execution times, including setup and teardown (`1 `_, `2 `_). It is the default pytest behavior. To report just call durations instead, configure the ``junit_duration_report`` option like this: .. code-block:: ini [pytest] junit_duration_report = call .. _record_property example: record_property ^^^^^^^^^^^^^^^ If you want to log additional information for a test, you can use the ``record_property`` fixture: .. code-block:: python def test_function(record_property): record_property("example_key", 1) assert True This will add an extra property ``example_key="1"`` to the generated ``testcase`` tag: .. code-block:: xml Alternatively, you can integrate this functionality with custom markers: .. code-block:: python # content of conftest.py def pytest_collection_modifyitems(session, config, items): for item in items: for marker in item.iter_markers(name="test_id"): test_id = marker.args[0] item.user_properties.append(("test_id", test_id)) And in your tests: .. code-block:: python # content of test_function.py import pytest @pytest.mark.test_id(1501) def test_function(): assert True Will result in: .. code-block:: xml .. warning:: Please note that using this feature will break schema verifications for the latest JUnitXML schema. This might be a problem when used with some CI servers. record_xml_attribute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To add an additional xml attribute to a testcase element, you can use ``record_xml_attribute`` fixture. This can also be used to override existing values: .. code-block:: python def test_function(record_xml_attribute): record_xml_attribute("assertions", "REQ-1234") record_xml_attribute("classname", "custom_classname") print("hello world") assert True Unlike ``record_property``, this will not add a new child element. Instead, this will add an attribute ``assertions="REQ-1234"`` inside the generated ``testcase`` tag and override the default ``classname`` with ``"classname=custom_classname"``: .. code-block:: xml hello world .. warning:: ``record_xml_attribute`` is an experimental feature, and its interface might be replaced by something more powerful and general in future versions. The functionality per-se will be kept, however. Using this over ``record_xml_property`` can help when using ci tools to parse the xml report. However, some parsers are quite strict about the elements and attributes that are allowed. Many tools use an xsd schema (like the example below) to validate incoming xml. Make sure you are using attribute names that are allowed by your parser. Below is the Scheme used by Jenkins to validate the XML report: .. code-block:: xml .. warning:: Please note that using this feature will break schema verifications for the latest JUnitXML schema. This might be a problem when used with some CI servers. .. _record_testsuite_property example: record_testsuite_property ^^^^^^^^^^^^^^^^^^^^^^^^^ .. versionadded:: 4.5 If you want to add a properties node at the test-suite level, which may contains properties that are relevant to all tests, you can use the ``record_testsuite_property`` session-scoped fixture: The ``record_testsuite_property`` session-scoped fixture can be used to add properties relevant to all tests. .. code-block:: python import pytest @pytest.fixture(scope="session", autouse=True) def log_global_env_facts(record_testsuite_property): record_testsuite_property("ARCH", "PPC") record_testsuite_property("STORAGE_TYPE", "CEPH") class TestMe(object): def test_foo(self): assert True The fixture is a callable which receives ``name`` and ``value`` of a ```` tag added at the test-suite level of the generated xml: .. code-block:: xml ``name`` must be a string, ``value`` will be converted to a string and properly xml-escaped. The generated XML is compatible with the latest ``xunit`` standard, contrary to `record_property`_ and `record_xml_attribute`_. Creating resultlog format files ---------------------------------------------------- This option is rarely used and is scheduled for removal in 5.0. See `the deprecation docs `__ for more information. To create plain-text machine-readable result files you can issue: .. code-block:: bash pytest --resultlog=path and look at the content at the ``path`` location. Such files are used e.g. by the `PyPy-test`_ web page to show test results over several revisions. .. _`PyPy-test`: http://buildbot.pypy.org/summary Sending test report to online pastebin service ----------------------------------------------------- **Creating a URL for each test failure**: .. code-block:: bash pytest --pastebin=failed This will submit test run information to a remote Paste service and provide a URL for each failure. You may select tests as usual or add for example ``-x`` if you only want to send one particular failure. **Creating a URL for a whole test session log**: .. code-block:: bash pytest --pastebin=all Currently only pasting to the http://bpaste.net service is implemented. Early loading plugins --------------------- You can early-load plugins (internal and external) explicitly in the command-line with the ``-p`` option:: pytest -p mypluginmodule The option receives a ``name`` parameter, which can be: * A full module dotted name, for example ``myproject.plugins``. This dotted name must be importable. * The entry-point name of a plugin. This is the name passed to ``setuptools`` when the plugin is registered. For example to early-load the `pytest-cov `__ plugin you can use:: pytest -p pytest_cov Disabling plugins ----------------- To disable loading specific plugins at invocation time, use the ``-p`` option together with the prefix ``no:``. Example: to disable loading the plugin ``doctest``, which is responsible for executing doctest tests from text files, invoke pytest like this: .. code-block:: bash pytest -p no:doctest .. _`pytest.main-usage`: Calling pytest from Python code ---------------------------------------------------- You can invoke ``pytest`` from Python code directly:: pytest.main() this acts as if you would call "pytest" from the command line. It will not raise ``SystemExit`` but return the exitcode instead. You can pass in options and arguments:: pytest.main(['-x', 'mytestdir']) You can specify additional plugins to ``pytest.main``:: # content of myinvoke.py import pytest class MyPlugin(object): def pytest_sessionfinish(self): print("*** test run reporting finishing") pytest.main(["-qq"], plugins=[MyPlugin()]) Running it will show that ``MyPlugin`` was added and its hook was invoked: .. code-block:: pytest $ python myinvoke.py .FEsxX. [100%]*** test run reporting finishing ================================== ERRORS ================================== _______________________ ERROR at setup of test_error _______________________ @pytest.fixture def error_fixture(): > assert 0 E assert 0 test_example.py:6: AssertionError ================================= FAILURES ================================= ________________________________ test_fail _________________________________ def test_fail(): > assert 0 E assert 0 test_example.py:14: AssertionError .. note:: Calling ``pytest.main()`` will result in importing your tests and any modules that they import. Due to the caching mechanism of python's import system, making subsequent calls to ``pytest.main()`` from the same process will not reflect changes to those files between the calls. For this reason, making multiple calls to ``pytest.main()`` from the same process (in order to re-run tests, for example) is not recommended. .. include:: links.inc pytest-4.6.9/doc/en/deprecations.rst0000664000372000037200000003432013604173400020305 0ustar travistravis00000000000000.. _deprecations: Deprecations and Removals ========================= This page lists all pytest features that are currently deprecated or have been removed in past major releases. The objective is to give users a clear rationale why a certain feature has been removed, and what alternatives should be used instead. .. contents:: :depth: 3 :local: Deprecated Features ------------------- Below is a complete list of all pytest features which are considered deprecated. Using those features will issue :class:`_pytest.warning_types.PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters `. .. _`raises message deprecated`: ``"message"`` parameter of ``pytest.raises`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. deprecated:: 4.1 It is a common mistake to think this parameter will match the exception message, while in fact it only serves to provide a custom message in case the ``pytest.raises`` check fails. To prevent users from making this mistake, and because it is believed to be little used, pytest is deprecating it without providing an alternative for the moment. If you have a valid use case for this parameter, consider that to obtain the same results you can just call ``pytest.fail`` manually at the end of the ``with`` statement. For example: .. code-block:: python with pytest.raises(TimeoutError, message="Client got unexpected message"): wait_for(websocket.recv(), 0.5) Becomes: .. code-block:: python with pytest.raises(TimeoutError): wait_for(websocket.recv(), 0.5) pytest.fail("Client got unexpected message") If you still have concerns about this deprecation and future removal, please comment on `issue #3974 `__. ``pytest.config`` global ~~~~~~~~~~~~~~~~~~~~~~~~ .. deprecated:: 4.1 The ``pytest.config`` global object is deprecated. Instead use ``request.config`` (via the ``request`` fixture) or if you are a plugin author use the ``pytest_configure(config)`` hook. Note that many hooks can also access the ``config`` object indirectly, through ``session.config`` or ``item.config`` for example. .. _raises-warns-exec: ``raises`` / ``warns`` with a string as the second argument ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. deprecated:: 4.1 Use the context manager form of these instead. When necessary, invoke ``exec`` directly. Example: .. code-block:: python pytest.raises(ZeroDivisionError, "1 / 0") pytest.raises(SyntaxError, "a $ b") pytest.warns(DeprecationWarning, "my_function()") pytest.warns(SyntaxWarning, "assert(1, 2)") Becomes: .. code-block:: python with pytest.raises(ZeroDivisionError): 1 / 0 with pytest.raises(SyntaxError): exec("a $ b") # exec is required for invalid syntax with pytest.warns(DeprecationWarning): my_function() with pytest.warns(SyntaxWarning): exec("assert(1, 2)") # exec is used to avoid a top-level warning Result log (``--result-log``) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The ``--resultlog`` command line option has been deprecated: it is little used and there are more modern and better alternatives, for example `pytest-tap `_. This feature will be effectively removed in pytest 4.0 as the team intends to include a better alternative in the core. If you have any concerns, please don't hesitate to `open an issue `__. Removed Features ---------------- As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after an appropriate period of deprecation has passed. Using ``Class`` in custom Collectors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Using objects named ``"Class"`` as a way to customize the type of nodes that are collected in ``Collector`` subclasses has been deprecated. Users instead should use ``pytest_pycollect_makeitem`` to customize node types during collection. This issue should affect only advanced plugins who create new collection types, so if you see this warning message please contact the authors so they can change the code. marks in ``pytest.mark.parametrize`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Applying marks to values of a ``pytest.mark.parametrize`` call is now deprecated. For example: .. code-block:: python @pytest.mark.parametrize( "a, b", [ (3, 9), pytest.mark.xfail(reason="flaky")(6, 36), (10, 100), (20, 200), (40, 400), (50, 500), ], ) def test_foo(a, b): ... This code applies the ``pytest.mark.xfail(reason="flaky")`` mark to the ``(6, 36)`` value of the above parametrization call. This was considered hard to read and understand, and also its implementation presented problems to the code preventing further internal improvements in the marks architecture. To update the code, use ``pytest.param``: .. code-block:: python @pytest.mark.parametrize( "a, b", [ (3, 9), pytest.param(6, 36, marks=pytest.mark.xfail(reason="flaky")), (10, 100), (20, 200), (40, 400), (50, 500), ], ) def test_foo(a, b): ... ``pytest_funcarg__`` prefix ~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 In very early pytest versions fixtures could be defined using the ``pytest_funcarg__`` prefix: .. code-block:: python def pytest_funcarg__data(): return SomeData() Switch over to the ``@pytest.fixture`` decorator: .. code-block:: python @pytest.fixture def data(): return SomeData() [pytest] section in setup.cfg files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 ``[pytest]`` sections in ``setup.cfg`` files should now be named ``[tool:pytest]`` to avoid conflicts with other distutils commands. Metafunc.addcall ~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 :meth:`_pytest.python.Metafunc.addcall` was a precursor to the current parametrized mechanism. Users should use :meth:`_pytest.python.Metafunc.parametrize` instead. Example: .. code-block:: python def pytest_generate_tests(metafunc): metafunc.addcall({"i": 1}, id="1") metafunc.addcall({"i": 2}, id="2") Becomes: .. code-block:: python def pytest_generate_tests(metafunc): metafunc.parametrize("i", [1, 2], ids=["1", "2"]) ``cached_setup`` ~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 ``request.cached_setup`` was the precursor of the setup/teardown mechanism available to fixtures. Example: .. code-block:: python @pytest.fixture def db_session(): return request.cached_setup( setup=Session.create, teardown=lambda session: session.close(), scope="module" ) This should be updated to make use of standard fixture mechanisms: .. code-block:: python @pytest.fixture(scope="module") def db_session(): session = Session.create() yield session session.close() You can consult `funcarg comparison section in the docs `_ for more information. pytest_plugins in non-top-level conftest files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Defining ``pytest_plugins`` is now deprecated in non-top-level conftest.py files because they will activate referenced plugins *globally*, which is surprising because for all other pytest features ``conftest.py`` files are only *active* for tests at or below it. ``Config.warn`` and ``Node.warn`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Those methods were part of the internal pytest warnings system, but since ``3.8`` pytest is using the builtin warning system for its own warnings, so those two functions are now deprecated. ``Config.warn`` should be replaced by calls to the standard ``warnings.warn``, example: .. code-block:: python config.warn("C1", "some warning") Becomes: .. code-block:: python warnings.warn(pytest.PytestWarning("some warning")) ``Node.warn`` now supports two signatures: * ``node.warn(PytestWarning("some message"))``: is now the **recommended** way to call this function. The warning instance must be a PytestWarning or subclass. * ``node.warn("CI", "some message")``: this code/message form has been **removed** and should be converted to the warning instance form above. record_xml_property ~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 The ``record_xml_property`` fixture is now deprecated in favor of the more generic ``record_property``, which can be used by other consumers (for example ``pytest-html``) to obtain custom information about the test run. This is just a matter of renaming the fixture as the API is the same: .. code-block:: python def test_foo(record_xml_property): ... Change to: .. code-block:: python def test_foo(record_property): ... Passing command-line string to ``pytest.main()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Passing a command-line string to ``pytest.main()`` is deprecated: .. code-block:: python pytest.main("-v -s") Pass a list instead: .. code-block:: python pytest.main(["-v", "-s"]) By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way. Calling fixtures directly ~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Calling a fixture function directly, as opposed to request them in a test function, is deprecated. For example: .. code-block:: python @pytest.fixture def cell(): return ... @pytest.fixture def full_cell(): cell = cell() cell.make_full() return cell This is a great source of confusion to new users, which will often call the fixture functions and request them from test functions interchangeably, which breaks the fixture resolution model. In those cases just request the function directly in the dependent fixture: .. code-block:: python @pytest.fixture def cell(): return ... @pytest.fixture def full_cell(cell): cell.make_full() return cell Alternatively if the fixture function is called multiple times inside a test (making it hard to apply the above pattern) or if you would like to make minimal changes to the code, you can create a fixture which calls the original function together with the ``name`` parameter: .. code-block:: python def cell(): return ... @pytest.fixture(name="cell") def cell_fixture(): return cell() ``yield`` tests ~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 pytest supported ``yield``-style tests, where a test function actually ``yield`` functions and values that are then turned into proper test methods. Example: .. code-block:: python def check(x, y): assert x ** x == y def test_squared(): yield check, 2, 4 yield check, 3, 9 This would result into two actual test functions being generated. This form of test function doesn't support fixtures properly, and users should switch to ``pytest.mark.parametrize``: .. code-block:: python @pytest.mark.parametrize("x, y", [(2, 4), (3, 9)]) def test_squared(x, y): assert x ** x == y Internal classes accessed through ``Node`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 Access of ``Module``, ``Function``, ``Class``, ``Instance``, ``File`` and ``Item`` through ``Node`` instances now issue this warning:: usage of Function.Module is deprecated, please use pytest.Module instead Users should just ``import pytest`` and access those objects using the ``pytest`` module. This has been documented as deprecated for years, but only now we are actually emitting deprecation warnings. ``Node.get_marker`` ~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 As part of a large :ref:`marker-revamp`, :meth:`_pytest.nodes.Node.get_marker` is deprecated. See :ref:`the documentation ` on tips on how to update your code. ``somefunction.markname`` ~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 As part of a large :ref:`marker-revamp` we already deprecated using ``MarkInfo`` the only correct way to get markers of an element is via ``node.iter_markers(name)``. ``pytest_namespace`` ~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 4.0 This hook is deprecated because it greatly complicates the pytest internals regarding configuration and initialization, making some bug fixes and refactorings impossible. Example of usage: .. code-block:: python class MySymbol: ... def pytest_namespace(): return {"my_symbol": MySymbol()} Plugin authors relying on this hook should instead require that users now import the plugin modules directly (with an appropriate public API). As a stopgap measure, plugin authors may still inject their names into pytest's namespace, usually during ``pytest_configure``: .. code-block:: python import pytest def pytest_configure(): pytest.my_symbol = MySymbol() Reinterpretation mode (``--assert=reinterp``) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 3.0 Reinterpretation mode has now been removed and only plain and rewrite mode are available, consequently the ``--assert=reinterp`` option is no longer available. This also means files imported from plugins or ``conftest.py`` will not benefit from improved assertions by default, you should use ``pytest.register_assert_rewrite()`` to explicitly turn on assertion rewriting for those files. Removed command-line options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 3.0 The following deprecated commandline options were removed: * ``--genscript``: no longer supported; * ``--no-assert``: use ``--assert=plain`` instead; * ``--nomagic``: use ``--assert=plain`` instead; * ``--report``: use ``-r`` instead; py.test-X* entry points ~~~~~~~~~~~~~~~~~~~~~~~ .. versionremoved:: 3.0 Removed all ``py.test-X*`` entry points. The versioned, suffixed entry points were never documented and a leftover from a pre-virtualenv era. These entry points also created broken entry points in wheels, so removing them also removes a source of confusion for users. pytest-4.6.9/doc/en/py27-py34-deprecation.rst0000664000372000037200000000267213604173400021523 0ustar travistravis00000000000000Python 2.7 and 3.4 support plan =============================== Python 2.7 EOL is fast approaching, with upstream support `ending in 2020 `__. Python 3.4's last release is scheduled for `March 2019 `__. pytest is one of the participating projects of the https://python3statement.org. The **pytest 4.6** series will be the last to support Python 2.7 and 3.4, and is scheduled to be released by **mid-2019**. **pytest 5.0** and onwards will support only Python 3.5+. Thanks to the `python_requires`_ ``setuptools`` option, Python 2.7 and Python 3.4 users using a modern ``pip`` version will install the last pytest ``4.6`` version automatically even if ``5.0`` or later are available on PyPI. While pytest ``5.0`` will be the new mainstream and development version, until **January 2020** the pytest core team plans to make bug-fix releases of the pytest ``4.6`` series by back-porting patches to the ``4.6-maintenance`` branch that affect Python 2 users. **After 2020**, the core team will no longer actively backport patches, but the ``4.6-maintenance`` branch will continue to exist so the community itself can contribute patches. The core team will be happy to accept those patches and make new ``4.6`` releases **until mid-2020**. .. _`python_requires`: https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires> pytest-4.6.9/doc/en/proposals/0000775000372000037200000000000013604173421017116 5ustar travistravis00000000000000pytest-4.6.9/doc/en/proposals/parametrize_with_fixtures.rst0000664000372000037200000001026713604173400025162 0ustar travistravis00000000000000:orphan: =================================== PROPOSAL: Parametrize with fixtures =================================== .. warning:: This document outlines a proposal around using fixtures as input of parametrized tests or fixtures. Problem ------- As a user I have functional tests that I would like to run against various scenarios. In this particular example we want to generate a new project based on a cookiecutter template. We want to test default values but also data that emulates user input. - use default values - emulate user input - specify 'author' - specify 'project_slug' - specify 'author' and 'project_slug' This is how a functional test could look like: .. code-block:: python import pytest @pytest.fixture def default_context(): return {"extra_context": {}} @pytest.fixture( params=[ {"author": "alice"}, {"project_slug": "helloworld"}, {"author": "bob", "project_slug": "foobar"}, ] ) def extra_context(request): return {"extra_context": request.param} @pytest.fixture(params=["default", "extra"]) def context(request): if request.param == "default": return request.getfuncargvalue("default_context") else: return request.getfuncargvalue("extra_context") def test_generate_project(cookies, context): """Call the cookiecutter API to generate a new project from a template. """ result = cookies.bake(extra_context=context) assert result.exit_code == 0 assert result.exception is None assert result.project.isdir() Issues ------ * By using ``request.getfuncargvalue()`` we rely on actual fixture function execution to know what fixtures are involved, due to its dynamic nature * More importantly, ``request.getfuncargvalue()`` cannot be combined with parametrized fixtures, such as ``extra_context`` * This is very inconvenient if you wish to extend an existing test suite by certain parameters for fixtures that are already used by tests pytest version 3.0 reports an error if you try to run above code:: Failed: The requested fixture has no parameter defined for the current test. Requested fixture 'extra_context' Proposed solution ----------------- A new function that can be used in modules can be used to dynamically define fixtures from existing ones. .. code-block:: python pytest.define_combined_fixture( name="context", fixtures=["default_context", "extra_context"] ) The new fixture ``context`` inherits the scope from the used fixtures and yield the following values. - ``{}`` - ``{'author': 'alice'}`` - ``{'project_slug': 'helloworld'}`` - ``{'author': 'bob', 'project_slug': 'foobar'}`` Alternative approach -------------------- A new helper function named ``fixture_request`` would tell pytest to yield all parameters marked as a fixture. .. note:: The `pytest-lazy-fixture `_ plugin implements a very similar solution to the proposal below, make sure to check it out. .. code-block:: python @pytest.fixture( params=[ pytest.fixture_request("default_context"), pytest.fixture_request("extra_context"), ] ) def context(request): """Returns all values for ``default_context``, one-by-one before it does the same for ``extra_context``. request.param: - {} - {'author': 'alice'} - {'project_slug': 'helloworld'} - {'author': 'bob', 'project_slug': 'foobar'} """ return request.param The same helper can be used in combination with ``pytest.mark.parametrize``. .. code-block:: python @pytest.mark.parametrize( "context, expected_response_code", [ (pytest.fixture_request("default_context"), 0), (pytest.fixture_request("extra_context"), 0), ], ) def test_generate_project(cookies, context, exit_code): """Call the cookiecutter API to generate a new project from a template. """ result = cookies.bake(extra_context=context) assert result.exit_code == exit_code pytest-4.6.9/doc/en/faq.rst0000664000372000037200000001533013604173400016374 0ustar travistravis00000000000000Some Issues and Questions ================================== .. note:: This FAQ is here only mostly for historic reasons. Checkout `pytest Q&A at Stackoverflow `_ for many questions and answers related to pytest and/or use :ref:`contact channels` to get help. On naming, nosetests, licensing and magic ------------------------------------------------ How does pytest relate to nose and unittest? +++++++++++++++++++++++++++++++++++++++++++++++++ ``pytest`` and nose_ share basic philosophy when it comes to running and writing Python tests. In fact, you can run many tests written for nose with ``pytest``. nose_ was originally created as a clone of ``pytest`` when ``pytest`` was in the ``0.8`` release cycle. Note that starting with pytest-2.0 support for running unittest test suites is majorly improved. how does pytest relate to twisted's trial? ++++++++++++++++++++++++++++++++++++++++++++++ Since some time ``pytest`` has builtin support for supporting tests written using trial. It does not itself start a reactor, however, and does not handle Deferreds returned from a test in pytest style. If you are using trial's unittest.TestCase chances are that you can just run your tests even if you return Deferreds. In addition, there also is a dedicated `pytest-twisted `_ plugin which allows you to return deferreds from pytest-style tests, allowing the use of :ref:`fixtures` and other features. how does pytest work with Django? ++++++++++++++++++++++++++++++++++++++++++++++ In 2012, some work is going into the `pytest-django plugin `_. It substitutes the usage of Django's ``manage.py test`` and allows the use of all pytest features_ most of which are not available from Django directly. .. _features: features.html What's this "magic" with pytest? (historic notes) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Around 2007 (version ``0.8``) some people thought that ``pytest`` was using too much "magic". It had been part of the `pylib`_ which contains a lot of unrelated python library code. Around 2010 there was a major cleanup refactoring, which removed unused or deprecated code and resulted in the new ``pytest`` PyPI package which strictly contains only test-related code. This release also brought a complete pluginification such that the core is around 300 lines of code and everything else is implemented in plugins. Thus ``pytest`` today is a small, universally runnable and customizable testing framework for Python. Note, however, that ``pytest`` uses metaprogramming techniques and reading its source is thus likely not something for Python beginners. A second "magic" issue was the assert statement debugging feature. Nowadays, ``pytest`` explicitly rewrites assert statements in test modules in order to provide more useful :ref:`assert feedback `. This completely avoids previous issues of confusing assertion-reporting. It also means, that you can use Python's ``-O`` optimization without losing assertions in test modules. You can also turn off all assertion interaction using the ``--assert=plain`` option. .. _`py namespaces`: index.html .. _`py/__init__.py`: http://bitbucket.org/hpk42/py-trunk/src/trunk/py/__init__.py Why can I use both ``pytest`` and ``py.test`` commands? +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pytest used to be part of the py package, which provided several developer utilities, all starting with ``py.``, thus providing nice TAB-completion. If you install ``pip install pycmd`` you get these tools from a separate package. Once ``pytest`` became a separate package, the ``py.test`` name was retained due to avoid a naming conflict with another tool. This conflict was eventually resolved, and the ``pytest`` command was therefore introduced. In future versions of pytest, we may deprecate and later remove the ``py.test`` command to avoid perpetuating the confusion. pytest fixtures, parametrized tests ------------------------------------------------------- .. _funcargs: funcargs.html Is using pytest fixtures versus xUnit setup a style question? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ For simple applications and for people experienced with nose_ or unittest-style test setup using `xUnit style setup`_ probably feels natural. For larger test suites, parametrized testing or setup of complex test resources using fixtures_ may feel more natural. Moreover, fixtures are ideal for writing advanced test support code (like e.g. the monkeypatch_, the tmpdir_ or capture_ fixtures) because the support code can register setup/teardown functions in a managed class/module/function scope. .. _monkeypatch: monkeypatch.html .. _tmpdir: tmpdir.html .. _capture: capture.html .. _fixtures: fixture.html .. _`why pytest_pyfuncarg__ methods?`: .. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration Can I yield multiple values from a fixture function? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ There are two conceptual reasons why yielding from a factory function is not possible: * If multiple factories yielded values there would be no natural place to determine the combination policy - in real-world examples some combinations often should not run. * Calling factories for obtaining test function arguments is part of setting up and running a test. At that point it is not possible to add new test calls to the test collection anymore. However, with pytest-2.3 you can use the :ref:`@pytest.fixture` decorator and specify ``params`` so that all tests depending on the factory-created resource will run multiple times with different parameters. You can also use the ``pytest_generate_tests`` hook to implement the `parametrization scheme of your choice`_. See also :ref:`paramexamples` for more examples. .. _`parametrization scheme of your choice`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/ pytest interaction with other packages --------------------------------------------------- Issues with pytest, multiprocess and setuptools? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ On Windows the multiprocess package will instantiate sub processes by pickling and thus implicitly re-import a lot of local modules. Unfortunately, setuptools-0.6.11 does not ``if __name__=='__main__'`` protect its generated command line script. This leads to infinite recursion when running a test that instantiates Processes. As of mid-2013, there shouldn't be a problem anymore when you use the standard setuptools (note that distribute has been merged back into setuptools which is now shipped directly with virtualenv). .. include:: links.inc pytest-4.6.9/doc/en/naming20.rst0000664000372000037200000000131613604173400017237 0ustar travistravis00000000000000 .. _naming20: New pytest names in 2.0 (flat is better than nested) ---------------------------------------------------- If you used older version of the ``py`` distribution (which included the py.test command line tool and Python name space) you accessed helpers and possibly collection classes through the ``py.test`` Python namespaces. The new ``pytest`` Python module flaty provides the same objects, following these renaming rules:: py.test.XYZ -> pytest.XYZ py.test.collect.XYZ -> pytest.XYZ py.test.cmdline.main -> pytest.main The old ``py.test.*`` ways to access functionality remain valid but you are encouraged to do global renaming according to the above rules in your test code. pytest-4.6.9/doc/en/index.rst0000664000372000037200000000505313604173400016735 0ustar travistravis00000000000000:orphan: .. _features: pytest: helps you write better programs ======================================= The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example of a simple test: .. code-block:: python # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 To execute it: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:6: AssertionError ========================= 1 failed in 0.12 seconds ========================= Due to ``pytest``'s detailed assertion introspection, only plain ``assert`` statements are used. See :ref:`Getting Started ` for more examples. Features -------- - Detailed info on failing :ref:`assert statements ` (no need to remember ``self.assert*`` names); - :ref:`Auto-discovery ` of test modules and functions; - :ref:`Modular fixtures ` for managing small or parametrized long-lived test resources; - Can run :ref:`unittest ` (including trial) and :ref:`nose ` test suites out of the box; - Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; Documentation ------------- Please see :ref:`Contents ` for full documentation, including installation, tutorials and PDF documents. Bugs/Requests ------------- Please use the `GitHub issue tracker `_ to submit bugs or request features. Changelog --------- Consult the :ref:`Changelog ` page for fixes and enhancements of each version. License ------- Copyright Holger Krekel and others, 2004-2020. Distributed under the terms of the `MIT`_ license, pytest is free and open source software. .. _`MIT`: https://github.com/pytest-dev/pytest/blob/master/LICENSE pytest-4.6.9/doc/en/skipping.rst0000664000372000037200000002572113604173400017456 0ustar travistravis00000000000000.. _`skip and xfail`: .. _skipping: Skip and xfail: dealing with tests that cannot succeed ====================================================== You can mark test functions that cannot be run on certain platforms or that you expect to fail so pytest can deal with them accordingly and present a summary of the test session, while keeping the test suite *green*. A **skip** means that you expect your test to pass only if some conditions are met, otherwise pytest should skip running the test altogether. Common examples are skipping windows-only tests on non-windows platforms, or skipping tests that depend on an external resource which is not available at the moment (for example a database). A **xfail** means that you expect a test to fail for some reason. A common example is a test for a feature not yet implemented, or a bug not yet fixed. When a test passes despite being expected to fail (marked with ``pytest.mark.xfail``), it's an **xpass** and will be reported in the test summary. ``pytest`` counts and lists *skip* and *xfail* tests separately. Detailed information about skipped/xfailed tests is not shown by default to avoid cluttering the output. You can use the ``-r`` option to see details corresponding to the "short" letters shown in the test progress: .. code-block:: bash pytest -rxXs # show extra info on xfailed, xpassed, and skipped tests More details on the ``-r`` option can be found by running ``pytest -h``. (See :ref:`how to change command line options defaults`) .. _skipif: .. _skip: .. _`condition booleans`: Skipping test functions ----------------------- The simplest way to skip a test function is to mark it with the ``skip`` decorator which may be passed an optional ``reason``: .. code-block:: python @pytest.mark.skip(reason="no way of currently testing this") def test_the_unknown(): ... Alternatively, it is also possible to skip imperatively during test execution or setup by calling the ``pytest.skip(reason)`` function: .. code-block:: python def test_function(): if not valid_config(): pytest.skip("unsupported configuration") The imperative method is useful when it is not possible to evaluate the skip condition during import time. It is also possible to skip the whole module using ``pytest.skip(reason, allow_module_level=True)`` at the module level: .. code-block:: python import sys import pytest if not sys.platform.startswith("win"): pytest.skip("skipping windows-only tests", allow_module_level=True) **Reference**: :ref:`pytest.mark.skip ref` ``skipif`` ~~~~~~~~~~ If you wish to skip something conditionally then you can use ``skipif`` instead. Here is an example of marking a test function to be skipped when run on an interpreter earlier than Python3.6: .. code-block:: python import sys @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher") def test_function(): ... If the condition evaluates to ``True`` during collection, the test function will be skipped, with the specified reason appearing in the summary when using ``-rs``. You can share ``skipif`` markers between modules. Consider this test module: .. code-block:: python # content of test_mymodule.py import mymodule minversion = pytest.mark.skipif( mymodule.__versioninfo__ < (1, 1), reason="at least mymodule-1.1 required" ) @minversion def test_function(): ... You can import the marker and reuse it in another test module: .. code-block:: python # test_myothermodule.py from test_mymodule import minversion @minversion def test_anotherfunction(): ... For larger test suites it's usually a good idea to have one file where you define the markers which you then consistently apply throughout your test suite. Alternatively, you can use :ref:`condition strings ` instead of booleans, but they can't be shared between modules easily so they are supported mainly for backward compatibility reasons. **Reference**: :ref:`pytest.mark.skipif ref` Skip all test functions of a class or module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can use the ``skipif`` marker (as any other marker) on classes: .. code-block:: python @pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") class TestPosixCalls(object): def test_function(self): "will not be setup or run under 'win32' platform" If the condition is ``True``, this marker will produce a skip result for each of the test methods of that class. If you want to skip all test functions of a module, you may use the ``pytestmark`` name on the global level: .. code-block:: python # test_module.py pytestmark = pytest.mark.skipif(...) If multiple ``skipif`` decorators are applied to a test function, it will be skipped if any of the skip conditions is true. .. _`whole class- or module level`: mark.html#scoped-marking Skipping files or directories ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sometimes you may need to skip an entire file or directory, for example if the tests rely on Python version-specific features or contain code that you do not wish pytest to run. In this case, you must exclude the files and directories from collection. Refer to :ref:`customizing-test-collection` for more information. Skipping on a missing import dependency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can use the following helper at module level or within a test or test setup function:: docutils = pytest.importorskip("docutils") If ``docutils`` cannot be imported here, this will lead to a skip outcome of the test. You can also skip based on the version number of a library:: docutils = pytest.importorskip("docutils", minversion="0.3") The version will be read from the specified module's ``__version__`` attribute. Summary ~~~~~~~ Here's a quick guide on how to skip tests in a module in different situations: 1. Skip all tests in a module unconditionally: .. code-block:: python pytestmark = pytest.mark.skip("all tests still WIP") 2. Skip all tests in a module based on some condition: .. code-block:: python pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="tests for linux only") 3. Skip all tests in a module if some import is missing: .. code-block:: python pexpect = pytest.importorskip("pexpect") .. _xfail: XFail: mark test functions as expected to fail ---------------------------------------------- You can use the ``xfail`` marker to indicate that you expect a test to fail:: @pytest.mark.xfail def test_function(): ... This test will be run but no traceback will be reported when it fails. Instead terminal reporting will list it in the "expected to fail" (``XFAIL``) or "unexpectedly passing" (``XPASS``) sections. Alternatively, you can also mark a test as ``XFAIL`` from within a test or setup function imperatively: .. code-block:: python def test_function(): if not valid_config(): pytest.xfail("failing configuration (but should work)") This will unconditionally make ``test_function`` ``XFAIL``. Note that no other code is executed after ``pytest.xfail`` call, differently from the marker. That's because it is implemented internally by raising a known exception. **Reference**: :ref:`pytest.mark.xfail ref` .. _`xfail strict tutorial`: ``strict`` parameter ~~~~~~~~~~~~~~~~~~~~ Both ``XFAIL`` and ``XPASS`` don't fail the test suite, unless the ``strict`` keyword-only parameter is passed as ``True``: .. code-block:: python @pytest.mark.xfail(strict=True) def test_function(): ... This will make ``XPASS`` ("unexpectedly passing") results from this test to fail the test suite. You can change the default value of the ``strict`` parameter using the ``xfail_strict`` ini option: .. code-block:: ini [pytest] xfail_strict=true ``reason`` parameter ~~~~~~~~~~~~~~~~~~~~ As with skipif_ you can also mark your expectation of a failure on a particular platform: .. code-block:: python @pytest.mark.xfail(sys.version_info >= (3, 6), reason="python3.6 api changes") def test_function(): ... ``raises`` parameter ~~~~~~~~~~~~~~~~~~~~ If you want to be more specific as to why the test is failing, you can specify a single exception, or a tuple of exceptions, in the ``raises`` argument. .. code-block:: python @pytest.mark.xfail(raises=RuntimeError) def test_function(): ... Then the test will be reported as a regular failure if it fails with an exception not mentioned in ``raises``. ``run`` parameter ~~~~~~~~~~~~~~~~~ If a test should be marked as xfail and reported as such but should not be even executed, use the ``run`` parameter as ``False``: .. code-block:: python @pytest.mark.xfail(run=False) def test_function(): ... This is specially useful for xfailing tests that are crashing the interpreter and should be investigated later. Ignoring xfail ~~~~~~~~~~~~~~ By specifying on the commandline: .. code-block:: bash pytest --runxfail you can force the running and reporting of an ``xfail`` marked test as if it weren't marked at all. This also causes ``pytest.xfail`` to produce no effect. Examples ~~~~~~~~ Here is a simple test file with the several usages: .. literalinclude:: example/xfail_demo.py Running it with the report-on-xfail option gives this output: .. code-block:: pytest example $ pytest -rx xfail_demo.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR/example collected 7 items xfail_demo.py xxxxxxx [100%] ========================= short test summary info ========================== XFAIL xfail_demo.py::test_hello XFAIL xfail_demo.py::test_hello2 reason: [NOTRUN] XFAIL xfail_demo.py::test_hello3 condition: hasattr(os, 'sep') XFAIL xfail_demo.py::test_hello4 bug 110 XFAIL xfail_demo.py::test_hello5 condition: pytest.__version__[0] != "17" XFAIL xfail_demo.py::test_hello6 reason: reason XFAIL xfail_demo.py::test_hello7 ======================== 7 xfailed in 0.12 seconds ========================= .. _`skip/xfail with parametrize`: Skip/xfail with parametrize --------------------------- It is possible to apply markers like skip and xfail to individual test instances when using parametrize: .. code-block:: python import pytest @pytest.mark.parametrize( ("n", "expected"), [ (1, 2), pytest.param(1, 0, marks=pytest.mark.xfail), pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), (2, 3), (3, 4), (4, 5), pytest.param( 10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k") ), ], ) def test_increment(n, expected): assert n + 1 == expected pytest-4.6.9/doc/en/historical-notes.rst0000664000372000037200000002603513604173400021120 0ustar travistravis00000000000000Historical Notes ================ This page lists features or behavior from previous versions of pytest which have changed over the years. They are kept here as a historical note so users looking at old code can find documentation related to them. .. _marker-revamp: Marker revamp and iteration --------------------------- .. versionchanged:: 3.6 pytest's marker implementation traditionally worked by simply updating the ``__dict__`` attribute of functions to cumulatively add markers. As a result, markers would unintentionally be passed along class hierarchies in surprising ways. Further, the API for retrieving them was inconsistent, as markers from parameterization would be stored differently than markers applied using the ``@pytest.mark`` decorator and markers added via ``node.add_marker``. This state of things made it technically next to impossible to use data from markers correctly without having a deep understanding of the internals, leading to subtle and hard to understand bugs in more advanced usages. Depending on how a marker got declared/changed one would get either a ``MarkerInfo`` which might contain markers from sibling classes, ``MarkDecorators`` when marks came from parameterization or from a ``node.add_marker`` call, discarding prior marks. Also ``MarkerInfo`` acts like a single mark, when it in fact represents a merged view on multiple marks with the same name. On top of that markers were not accessible in the same way for modules, classes, and functions/methods. In fact, markers were only accessible in functions, even if they were declared on classes/modules. A new API to access markers has been introduced in pytest 3.6 in order to solve the problems with the initial design, providing the :func:`_pytest.nodes.Node.iter_markers` method to iterate over markers in a consistent manner and reworking the internals, which solved a great deal of problems with the initial design. .. _update marker code: Updating code ~~~~~~~~~~~~~ The old ``Node.get_marker(name)`` function is considered deprecated because it returns an internal ``MarkerInfo`` object which contains the merged name, ``*args`` and ``**kwargs`` of all the markers which apply to that node. In general there are two scenarios on how markers should be handled: 1. Marks overwrite each other. Order matters but you only want to think of your mark as a single item. E.g. ``log_level('info')`` at a module level can be overwritten by ``log_level('debug')`` for a specific test. In this case, use ``Node.get_closest_marker(name)``: .. code-block:: python # replace this: marker = item.get_marker("log_level") if marker: level = marker.args[0] # by this: marker = item.get_closest_marker("log_level") if marker: level = marker.args[0] 2. Marks compose in an additive manner. E.g. ``skipif(condition)`` marks mean you just want to evaluate all of them, order doesn't even matter. You probably want to think of your marks as a set here. In this case iterate over each mark and handle their ``*args`` and ``**kwargs`` individually. .. code-block:: python # replace this skipif = item.get_marker("skipif") if skipif: for condition in skipif.args: # eval condition ... # by this: for skipif in item.iter_markers("skipif"): condition = skipif.args[0] # eval condition If you are unsure or have any questions, please consider opening `an issue `_. Related issues ~~~~~~~~~~~~~~ Here is a non-exhaustive list of issues fixed by the new implementation: * Marks don't pick up nested classes (`#199 `_). * Markers stain on all related classes (`#568 `_). * Combining marks - args and kwargs calculation (`#2897 `_). * ``request.node.get_marker('name')`` returns ``None`` for markers applied in classes (`#902 `_). * Marks applied in parametrize are stored as markdecorator (`#2400 `_). * Fix marker interaction in a backward incompatible way (`#1670 `_). * Refactor marks to get rid of the current "marks transfer" mechanism (`#2363 `_). * Introduce FunctionDefinition node, use it in generate_tests (`#2522 `_). * Remove named marker attributes and collect markers in items (`#891 `_). * skipif mark from parametrize hides module level skipif mark (`#1540 `_). * skipif + parametrize not skipping tests (`#1296 `_). * Marker transfer incompatible with inheritance (`#535 `_). More details can be found in the `original PR `_. .. note:: in a future major relase of pytest we will introduce class based markers, at which point markers will no longer be limited to instances of :py:class:`Mark`. cache plugin integrated into the core ------------------------------------- The functionality of the :ref:`core cache ` plugin was previously distributed as a third party plugin named ``pytest-cache``. The core plugin is compatible regarding command line options and API usage except that you can only store/receive data between test runs that is json-serializable. funcargs and ``pytest_funcarg__`` --------------------------------- In versions prior to 2.3 there was no ``@pytest.fixture`` marker and you had to use a magic ``pytest_funcarg__NAME`` prefix for the fixture factory. This remains and will remain supported but is not anymore advertised as the primary means of declaring fixture functions. ``@pytest.yield_fixture`` decorator ----------------------------------- Prior to version 2.10, in order to use a ``yield`` statement to execute teardown code one had to mark a fixture using the ``yield_fixture`` marker. From 2.10 onward, normal fixtures can use ``yield`` directly so the ``yield_fixture`` decorator is no longer needed and considered deprecated. ``[pytest]`` header in ``setup.cfg`` ------------------------------------ Prior to 3.0, the supported section name was ``[pytest]``. Due to how this may collide with some distutils commands, the recommended section name for ``setup.cfg`` files is now ``[tool:pytest]``. Note that for ``pytest.ini`` and ``tox.ini`` files the section name is ``[pytest]``. Applying marks to ``@pytest.mark.parametrize`` parameters --------------------------------------------------------- Prior to version 3.1 the supported mechanism for marking values used the syntax: .. code-block:: python import pytest @pytest.mark.parametrize( "test_input,expected", [("3+5", 8), ("2+4", 6), pytest.mark.xfail(("6*9", 42))] ) def test_eval(test_input, expected): assert eval(test_input) == expected This was an initial hack to support the feature but soon was demonstrated to be incomplete, broken for passing functions or applying multiple marks with the same name but different parameters. The old syntax is planned to be removed in pytest-4.0. ``@pytest.mark.parametrize`` argument names as a tuple ------------------------------------------------------ In versions prior to 2.4 one needed to specify the argument names as a tuple. This remains valid but the simpler ``"name1,name2,..."`` comma-separated-string syntax is now advertised first because it's easier to write and produces less line noise. setup: is now an "autouse fixture" ---------------------------------- During development prior to the pytest-2.3 release the name ``pytest.setup`` was used but before the release it was renamed and moved to become part of the general fixture mechanism, namely :ref:`autouse fixtures` .. _string conditions: Conditions as strings instead of booleans ----------------------------------------- Prior to pytest-2.4 the only way to specify skipif/xfail conditions was to use strings: .. code-block:: python import sys @pytest.mark.skipif("sys.version_info >= (3,3)") def test_function(): ... During test function setup the skipif condition is evaluated by calling ``eval('sys.version_info >= (3,0)', namespace)``. The namespace contains all the module globals, and ``os`` and ``sys`` as a minimum. Since pytest-2.4 :ref:`boolean conditions ` are considered preferable because markers can then be freely imported between test modules. With strings you need to import not only the marker but all variables used by the marker, which violates encapsulation. The reason for specifying the condition as a string was that ``pytest`` can report a summary of skip conditions based purely on the condition string. With conditions as booleans you are required to specify a ``reason`` string. Note that string conditions will remain fully supported and you are free to use them if you have no need for cross-importing markers. The evaluation of a condition string in ``pytest.mark.skipif(conditionstring)`` or ``pytest.mark.xfail(conditionstring)`` takes place in a namespace dictionary which is constructed as follows: * the namespace is initialized by putting the ``sys`` and ``os`` modules and the pytest ``config`` object into it. * updated with the module globals of the test function for which the expression is applied. The pytest ``config`` object allows you to skip based on a test configuration value which you might have added: .. code-block:: python @pytest.mark.skipif("not config.getvalue('db')") def test_function(): ... The equivalent with "boolean conditions" is: .. code-block:: python @pytest.mark.skipif(not pytest.config.getvalue("db"), reason="--db was not specified") def test_function(): pass .. note:: You cannot use ``pytest.config.getvalue()`` in code imported before pytest's argument parsing takes place. For example, ``conftest.py`` files are imported before command line parsing and thus ``config.getvalue()`` will not execute correctly. ``pytest.set_trace()`` ---------------------- Previous to version 2.4 to set a break point in code one needed to use ``pytest.set_trace()``: .. code-block:: python import pytest def test_function(): ... pytest.set_trace() # invoke PDB debugger and tracing This is no longer needed and one can use the native ``import pdb;pdb.set_trace()`` call directly. For more details see :ref:`breakpoints`. "compat" properties ------------------- Access of ``Module``, ``Function``, ``Class``, ``Instance``, ``File`` and ``Item`` through ``Node`` instances have long been documented as deprecated, but started to emit warnings from pytest ``3.9`` and onward. Users should just ``import pytest`` and access those objects using the ``pytest`` module. pytest-4.6.9/doc/en/parametrize.rst0000664000372000037200000002061113604173400020146 0ustar travistravis00000000000000 .. _`test generators`: .. _`parametrizing-tests`: .. _`parametrized test functions`: .. _`parametrize`: .. _`parametrize-basics`: Parametrizing fixtures and test functions ========================================================================== pytest enables test parametrization at several levels: - :py:func:`pytest.fixture` allows one to :ref:`parametrize fixture functions `. * `@pytest.mark.parametrize`_ allows one to define multiple sets of arguments and fixtures at the test function or class. * `pytest_generate_tests`_ allows one to define custom parametrization schemes or extensions. .. _parametrizemark: .. _`@pytest.mark.parametrize`: ``@pytest.mark.parametrize``: parametrizing test functions --------------------------------------------------------------------- .. regendoc: wipe Several improvements. The builtin :ref:`pytest.mark.parametrize ref` decorator enables parametrization of arguments for a test function. Here is a typical example of a test function that implements checking that a certain input leads to an expected output: .. code-block:: python # content of test_expectation.py import pytest @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)]) def test_eval(test_input, expected): assert eval(test_input) == expected Here, the ``@parametrize`` decorator defines three different ``(test_input,expected)`` tuples so that the ``test_eval`` function will run three times using them in turn: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 3 items test_expectation.py ..F [100%] ================================= FAILURES ================================= ____________________________ test_eval[6*9-42] _____________________________ test_input = '6*9', expected = 42 @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)]) def test_eval(test_input, expected): > assert eval(test_input) == expected E AssertionError: assert 54 == 42 E + where 54 = eval('6*9') test_expectation.py:6: AssertionError ==================== 1 failed, 2 passed in 0.12 seconds ==================== .. note:: pytest by default escapes any non-ascii characters used in unicode strings for the parametrization because it has several downsides. If however you would like to use unicode strings in parametrization and see them in the terminal as is (non-escaped), use this option in your ``pytest.ini``: .. code-block:: ini [pytest] disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True Keep in mind however that this might cause unwanted side effects and even bugs depending on the OS used and plugins currently installed, so use it at your own risk. As designed in this example, only one pair of input/output values fails the simple test function. And as usual with test function arguments, you can see the ``input`` and ``output`` values in the traceback. Note that you could also use the parametrize marker on a class or a module (see :ref:`mark`) which would invoke several functions with the argument sets. It is also possible to mark individual test instances within parametrize, for example with the builtin ``mark.xfail``: .. code-block:: python # content of test_expectation.py import pytest @pytest.mark.parametrize( "test_input,expected", [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)], ) def test_eval(test_input, expected): assert eval(test_input) == expected Let's run this: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 3 items test_expectation.py ..x [100%] =================== 2 passed, 1 xfailed in 0.12 seconds ==================== The one parameter set which caused a failure previously now shows up as an "xfailed (expected to fail)" test. In case the values provided to ``parametrize`` result in an empty list - for example, if they're dynamically generated by some function - the behaviour of pytest is defined by the :confval:`empty_parameter_set_mark` option. To get all combinations of multiple parametrized arguments you can stack ``parametrize`` decorators: .. code-block:: python import pytest @pytest.mark.parametrize("x", [0, 1]) @pytest.mark.parametrize("y", [2, 3]) def test_foo(x, y): pass This will run the test with the arguments set to ``x=0/y=2``, ``x=1/y=2``, ``x=0/y=3``, and ``x=1/y=3`` exhausting parameters in the order of the decorators. .. _`pytest_generate_tests`: Basic ``pytest_generate_tests`` example --------------------------------------------- Sometimes you may want to implement your own parametrization scheme or implement some dynamism for determining the parameters or scope of a fixture. For this, you can use the ``pytest_generate_tests`` hook which is called when collecting a test function. Through the passed in ``metafunc`` object you can inspect the requesting test context and, most importantly, you can call ``metafunc.parametrize()`` to cause parametrization. For example, let's say we want to run a test taking string inputs which we want to set via a new ``pytest`` command line option. Let's first write a simple test accepting a ``stringinput`` fixture function argument: .. code-block:: python # content of test_strings.py def test_valid_string(stringinput): assert stringinput.isalpha() Now we add a ``conftest.py`` file containing the addition of a command line option and the parametrization of our test function: .. code-block:: python # content of conftest.py def pytest_addoption(parser): parser.addoption( "--stringinput", action="append", default=[], help="list of stringinputs to pass to test functions", ) def pytest_generate_tests(metafunc): if "stringinput" in metafunc.fixturenames: metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput")) If we now pass two stringinput values, our test will run twice: .. code-block:: pytest $ pytest -q --stringinput="hello" --stringinput="world" test_strings.py .. [100%] 2 passed in 0.12 seconds Let's also run with a stringinput that will lead to a failing test: .. code-block:: pytest $ pytest -q --stringinput="!" test_strings.py F [100%] ================================= FAILURES ================================= ___________________________ test_valid_string[!] ___________________________ stringinput = '!' def test_valid_string(stringinput): > assert stringinput.isalpha() E AssertionError: assert False E + where False = () E + where = '!'.isalpha test_strings.py:4: AssertionError 1 failed in 0.12 seconds As expected our test function fails. If you don't specify a stringinput it will be skipped because ``metafunc.parametrize()`` will be called with an empty parameter list: .. code-block:: pytest $ pytest -q -rs test_strings.py s [100%] ========================= short test summary info ========================== SKIPPED [1] test_strings.py: got empty parameter set ['stringinput'], function test_valid_string at $REGENDOC_TMPDIR/test_strings.py:2 1 skipped in 0.12 seconds Note that when calling ``metafunc.parametrize`` multiple times with different parameter sets, all parameter names across those sets cannot be duplicated, otherwise an error will be raised. More examples ------------- For further examples, you might want to look at :ref:`more parametrization examples `. pytest-4.6.9/doc/en/getting-started.rst0000664000372000037200000001602113604173400020730 0ustar travistravis00000000000000Installation and Getting Started =================================== **Pythons**: Python 2.7, 3.4, 3.5, 3.6, 3.7, Jython, PyPy-2.3 **Platforms**: Unix/Posix and Windows **PyPI package name**: `pytest `_ **Documentation as PDF**: `download latest `_ ``pytest`` is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. Get started in minutes with a small unit test or complex functional test for your application or library. .. _`getstarted`: .. _`installation`: Install ``pytest`` ---------------------------------------- 1. Run the following command in your command line: .. code-block:: bash pip install -U pytest 2. Check that you installed the correct version: .. code-block:: bash $ pytest --version This is pytest version 4.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py .. _`simpletest`: Create your first test ---------------------------------------------------------- Create a simple test function with just four lines of code:: # content of test_sample.py def func(x): return x + 1 def test_answer(): assert func(3) == 5 That’s it. You can now execute the test function: .. code-block:: pytest $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:5: AssertionError ========================= 1 failed in 0.12 seconds ========================= This test returns a failure report because ``func(3)`` does not return ``5``. .. note:: You can use the ``assert`` statement to verify test expectations. pytest’s `Advanced assertion introspection `_ will intelligently report intermediate values of the assert expression so you can avoid the many names `of JUnit legacy methods `_. Run multiple tests ---------------------------------------------------------- ``pytest`` will run all files of the form test_*.py or \*_test.py in the current directory and its subdirectories. More generally, it follows :ref:`standard test discovery rules `. Assert that a certain exception is raised -------------------------------------------------------------- Use the :ref:`raises ` helper to assert that some code raises an exception:: # content of test_sysexit.py import pytest def f(): raise SystemExit(1) def test_mytest(): with pytest.raises(SystemExit): f() Execute the test function with “quiet” reporting mode: .. code-block:: pytest $ pytest -q test_sysexit.py . [100%] 1 passed in 0.12 seconds Group multiple tests in a class -------------------------------------------------------------- Once you develop multiple tests, you may want to group them into a class. pytest makes it easy to create a class containing more than one test:: # content of test_class.py class TestClass(object): def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check') ``pytest`` discovers all tests following its :ref:`Conventions for Python test discovery `, so it finds both ``test_`` prefixed functions. There is no need to subclass anything. We can simply run the module by passing its filename: .. code-block:: pytest $ pytest -q test_class.py .F [100%] ================================= FAILURES ================================= ____________________________ TestClass.test_two ____________________________ self = def test_two(self): x = "hello" > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check') test_class.py:8: AssertionError 1 failed, 1 passed in 0.12 seconds The first test passed and the second failed. You can easily see the intermediate values in the assertion to help you understand the reason for the failure. Request a unique temporary directory for functional tests -------------------------------------------------------------- ``pytest`` provides `Builtin fixtures/function arguments `_ to request arbitrary resources, like a unique temporary directory:: # content of test_tmpdir.py def test_needsfiles(tmpdir): print(tmpdir) assert 0 List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory: .. code-block:: pytest $ pytest -q test_tmpdir.py F [100%] ================================= FAILURES ================================= _____________________________ test_needsfiles ______________________________ tmpdir = local('PYTEST_TMPDIR/test_needsfiles0') def test_needsfiles(tmpdir): print(tmpdir) > assert 0 E assert 0 test_tmpdir.py:3: AssertionError --------------------------- Captured stdout call --------------------------- PYTEST_TMPDIR/test_needsfiles0 1 failed in 0.12 seconds More info on tmpdir handling is available at :ref:`Temporary directories and files `. Find out what kind of builtin :ref:`pytest fixtures ` exist with the command: .. code-block:: bash pytest --fixtures # shows builtin and custom fixtures Note that this command omits fixtures with leading ``_`` unless the ``-v`` option is added. Continue reading ------------------------------------- Check out additional pytest resources to help you customize tests for your unique workflow: * ":ref:`cmdline`" for command line invocation examples * ":ref:`existingtestsuite`" for working with pre-existing tests * ":ref:`mark`" for information on the ``pytest.mark`` mechanism * ":ref:`fixtures`" for providing a functional baseline to your tests * ":ref:`plugins`" for managing and writing plugins * ":ref:`goodpractices`" for virtualenv and test layouts .. include:: links.inc pytest-4.6.9/doc/en/tmpdir.rst0000664000372000037200000001314213604173400017123 0ustar travistravis00000000000000 .. _`tmpdir handling`: .. _tmpdir: Temporary directories and files ================================================ The ``tmp_path`` fixture ------------------------ You can use the ``tmp_path`` fixture which will provide a temporary directory unique to the test invocation, created in the `base temporary directory`_. ``tmp_path`` is a ``pathlib/pathlib2.Path`` object. Here is an example test usage: .. code-block:: python # content of test_tmp_path.py import os CONTENT = u"content" def test_create_file(tmp_path): d = tmp_path / "sub" d.mkdir() p = d / "hello.txt" p.write_text(CONTENT) assert p.read_text() == CONTENT assert len(list(tmp_path.iterdir())) == 1 assert 0 Running this would result in a passed test except for the last ``assert 0`` line which we use to look at values: .. code-block:: pytest $ pytest test_tmp_path.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_tmp_path.py F [100%] ================================= FAILURES ================================= _____________________________ test_create_file _____________________________ tmp_path = PosixPath('PYTEST_TMPDIR/test_create_file0') def test_create_file(tmp_path): d = tmp_path / "sub" d.mkdir() p = d / "hello.txt" p.write_text(CONTENT) assert p.read_text() == CONTENT assert len(list(tmp_path.iterdir())) == 1 > assert 0 E assert 0 test_tmp_path.py:13: AssertionError ========================= 1 failed in 0.12 seconds ========================= .. _`tmp_path_factory example`: The ``tmp_path_factory`` fixture -------------------------------- The ``tmp_path_factory`` is a session-scoped fixture which can be used to create arbitrary temporary directories from any other fixture or test. It is intended to replace ``tmpdir_factory``, and returns :class:`pathlib.Path` instances. See :ref:`tmp_path_factory API ` for details. The 'tmpdir' fixture -------------------- You can use the ``tmpdir`` fixture which will provide a temporary directory unique to the test invocation, created in the `base temporary directory`_. ``tmpdir`` is a `py.path.local`_ object which offers ``os.path`` methods and more. Here is an example test usage:: # content of test_tmpdir.py import os def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 assert 0 Running this would result in a passed test except for the last ``assert 0`` line which we use to look at values: .. code-block:: pytest $ pytest test_tmpdir.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_tmpdir.py F [100%] ================================= FAILURES ================================= _____________________________ test_create_file _____________________________ tmpdir = local('PYTEST_TMPDIR/test_create_file0') def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 > assert 0 E assert 0 test_tmpdir.py:7: AssertionError ========================= 1 failed in 0.12 seconds ========================= .. _`tmpdir factory example`: The 'tmpdir_factory' fixture ---------------------------- The ``tmpdir_factory`` is a session-scoped fixture which can be used to create arbitrary temporary directories from any other fixture or test. For example, suppose your test suite needs a large image on disk, which is generated procedurally. Instead of computing the same image for each test that uses it into its own ``tmpdir``, you can generate it once per-session to save time: .. code-block:: python # contents of conftest.py import pytest @pytest.fixture(scope="session") def image_file(tmpdir_factory): img = compute_expensive_image() fn = tmpdir_factory.mktemp("data").join("img.png") img.save(str(fn)) return fn # contents of test_image.py def test_histogram(image_file): img = load_image(image_file) # compute and test histogram See :ref:`tmpdir_factory API ` for details. .. _`base temporary directory`: The default base temporary directory ----------------------------------------------- Temporary directories are by default created as sub-directories of the system temporary directory. The base name will be ``pytest-NUM`` where ``NUM`` will be incremented with each test run. Moreover, entries older than 3 temporary directories will be removed. You can override the default temporary directory setting like this: .. code-block:: bash pytest --basetemp=mydir When distributing tests on the local machine, ``pytest`` takes care to configure a basetemp directory for the sub processes such that all temporary data lands below a single per-test run basetemp directory. .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html pytest-4.6.9/doc/en/conftest.py0000664000372000037200000000006513604173400017271 0ustar travistravis00000000000000# -*- coding: utf-8 -*- collect_ignore = ["conf.py"] pytest-4.6.9/doc/en/builtin.rst0000664000372000037200000001434113604173400017274 0ustar travistravis00000000000000:orphan: .. _`pytest helpers`: Pytest API and builtin fixtures ================================================ Most of the information of this page has been moved over to :ref:`reference`. For information on plugin hooks and objects, see :ref:`plugins`. For information on the ``pytest.mark`` mechanism, see :ref:`mark`. For information about fixtures, see :ref:`fixtures`. To see a complete list of available fixtures (add ``-v`` to also see fixtures with leading ``_``), type : .. code-block:: pytest $ pytest -q --fixtures cache Return a cache object that can persist state between testing sessions. cache.get(key, default) cache.set(key, value) Keys must be a ``/`` separated value, where the first part is usually the name of your plugin or application to avoid clashes with other cache users. Values can be any object handled by the json stdlib module. capsys Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsys.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. capsysbinary Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsysbinary.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``bytes`` objects. capfd Enable text capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. capfdbinary Enable bytes capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``byte`` objects. doctest_namespace [session scope] Fixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. pytestconfig [session scope] Session-scoped fixture that returns the :class:`_pytest.config.Config` object. Example:: def test_foo(pytestconfig): if pytestconfig.getoption("verbose") > 0: ... record_property Add an extra properties the calling test. User properties become part of the test report and are available to the configured reporters, like JUnit XML. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded. Example:: def test_function(record_property): record_property("example_key", 1) record_xml_attribute Add extra xml attributes to the tag for the calling test. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded record_testsuite_property [session scope] Records a new ```` tag as child of the root ````. This is suitable to writing global information regarding the entire test suite, and is compatible with ``xunit2`` JUnit family. This is a ``session``-scoped fixture which is called with ``(name, value)``. Example: .. code-block:: python def test_foo(record_testsuite_property): record_testsuite_property("ARCH", "PPC") record_testsuite_property("STORAGE_TYPE", "CEPH") ``name`` must be a string, ``value`` will be converted to a string and properly xml-escaped. caplog Access and control log capturing. Captured logs are available through the following properties/methods:: * caplog.text -> string containing formatted log output * caplog.records -> list of logging.LogRecord instances * caplog.record_tuples -> list of (logger_name, level, message) tuples * caplog.clear() -> clear captured records and formatted log output string monkeypatch The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.delattr(obj, name, raising=True) monkeypatch.setitem(mapping, name, value) monkeypatch.delitem(obj, name, raising=True) monkeypatch.setenv(name, value, prepend=False) monkeypatch.delenv(name, raising=True) monkeypatch.syspath_prepend(path) monkeypatch.chdir(path) All modifications will be undone after the requesting test function or fixture has finished. The ``raising`` parameter determines if a KeyError or AttributeError will be raised if the set/deletion operation has no target. recwarn Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. See http://docs.python.org/library/warnings.html for information on warning categories. tmpdir_factory [session scope] Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session. tmp_path_factory [session scope] Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session. tmpdir Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html tmp_path Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a :class:`pathlib.Path` object. .. note:: in python < 3.6 this is a pathlib2.Path no tests ran in 0.12 seconds You can also interactively ask for help, e.g. by typing on the Python interactive prompt something like:: import pytest help(pytest) pytest-4.6.9/doc/en/pythonpath.rst0000664000372000037200000000505713604173400020030 0ustar travistravis00000000000000.. _pythonpath: pytest import mechanisms and ``sys.path``/``PYTHONPATH`` ======================================================== Here's a list of scenarios where pytest may need to change ``sys.path`` in order to import test modules or ``conftest.py`` files. Test modules / ``conftest.py`` files inside packages ---------------------------------------------------- Consider this file and directory layout:: root/ |- foo/ |- __init__.py |- conftest.py |- bar/ |- __init__.py |- tests/ |- __init__.py |- test_foo.py When executing:: pytest root/ pytest will find ``foo/bar/tests/test_foo.py`` and realize it is part of a package given that there's an ``__init__.py`` file in the same folder. It will then search upwards until it can find the last folder which still contains an ``__init__.py`` file in order to find the package *root* (in this case ``foo/``). To load the module, it will insert ``root/`` to the front of ``sys.path`` (if not there already) in order to load ``test_foo.py`` as the *module* ``foo.bar.tests.test_foo``. The same logic applies to the ``conftest.py`` file: it will be imported as ``foo.conftest`` module. Preserving the full package name is important when tests live in a package to avoid problems and allow test modules to have duplicated names. This is also discussed in details in :ref:`test discovery`. Standalone test modules / ``conftest.py`` files ----------------------------------------------- Consider this file and directory layout:: root/ |- foo/ |- conftest.py |- bar/ |- tests/ |- test_foo.py When executing:: pytest root/ pytest will find ``foo/bar/tests/test_foo.py`` and realize it is NOT part of a package given that there's no ``__init__.py`` file in the same folder. It will then add ``root/foo/bar/tests`` to ``sys.path`` in order to import ``test_foo.py`` as the *module* ``test_foo``. The same is done with the ``conftest.py`` file by adding ``root/foo`` to ``sys.path`` to import it as ``conftest``. For this reason this layout cannot have test modules with the same name, as they all will be imported in the global import namespace. This is also discussed in details in :ref:`test discovery`. Invoking ``pytest`` versus ``python -m pytest`` ----------------------------------------------- Running pytest with ``python -m pytest [...]`` instead of ``pytest [...]`` yields nearly equivalent behaviour, except that the former call will add the current directory to ``sys.path``. See also :ref:`cmdline`. pytest-4.6.9/doc/en/flaky.rst0000664000372000037200000001611413604173400016734 0ustar travistravis00000000000000 Flaky tests ----------- A "flaky" test is one that exhibits intermittent or sporadic failure, that seems to have non-deterministic behaviour. Sometimes it passes, sometimes it fails, and it's not clear why. This page discusses pytest features that can help and other general strategies for identifying, fixing or mitigating them. Why flaky tests are a problem ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Flaky tests are particularly troublesome when a continuous integration (CI) server is being used, so that all tests must pass before a new code change can be merged. If the test result is not a reliable signal -- that a test failure means the code change broke the test -- developers can become mistrustful of the test results, which can lead to overlooking genuine failures. It is also a source of wasted time as developers must re-run test suites and investigate spurious failures. Potential root causes ^^^^^^^^^^^^^^^^^^^^^ System state ~~~~~~~~~~~~ Broadly speaking, a flaky test indicates that the test relies on some system state that is not being appropriately controlled - the test environment is not sufficiently isolated. Higher level tests are more likely to be flaky as they rely on more state. Flaky tests sometimes appear when a test suite is run in parallel (such as use of pytest-xdist). This can indicate a test is reliant on test ordering. - Perhaps a different test is failing to clean up after itself and leaving behind data which causes the flaky test to fail. - The flaky test is reliant on data from a previous test that doesn't clean up after itself, and in parallel runs that previous test is not always present - Tests that modify global state typically cannot be run in parallel. Overly strict assertion ~~~~~~~~~~~~~~~~~~~~~~~ Overly strict assertions can cause problems with floating point comparison as well as timing issues. `pytest.approx `_ is useful here. Pytest features ^^^^^^^^^^^^^^^ Xfail strict ~~~~~~~~~~~~ :ref:`pytest.mark.xfail ref` with ``strict=False`` can be used to mark a test so that its failure does not cause the whole build to break. This could be considered like a manual quarantine, and is rather dangerous to use permanently. PYTEST_CURRENT_TEST ~~~~~~~~~~~~~~~~~~~ :ref:`pytest current test env` may be useful for figuring out "which test got stuck". Plugins ~~~~~~~ Rerunning any failed tests can mitigate the negative effects of flaky tests by giving them additional chances to pass, so that the overall build does not fail. Several pytest plugins support this: * `flaky `_ * `pytest-flakefinder `_ - `blog post `_ * `pytest-rerunfailures `_ * `pytest-replay `_: This plugin helps to reproduce locally crashes or flaky tests observed during CI runs. Plugins to deliberately randomize tests can help expose tests with state problems: * `pytest-random-order `_ * `pytest-randomly `_ Other general strategies ^^^^^^^^^^^^^^^^^^^^^^^^ Split up test suites ~~~~~~~~~~~~~~~~~~~~ It can be common to split a single test suite into two, such as unit vs integration, and only use the unit test suite as a CI gate. This also helps keep build times manageable as high level tests tend to be slower. However, it means it does become possible for code that breaks the build to be merged, so extra vigilance is needed for monitoring the integration test results. Video/screenshot on failure ~~~~~~~~~~~~~~~~~~~~~~~~~~~ For UI tests these are important for understanding what the state of the UI was when the test failed. pytest-splinter can be used with plugins like pytest-bdd and can `save a screenshot on test failure `_, which can help to isolate the cause. Delete or rewrite the test ~~~~~~~~~~~~~~~~~~~~~~~~~~ If the functionality is covered by other tests, perhaps the test can be removed. If not, perhaps it can be rewritten at a lower level which will remove the flakiness or make its source more apparent. Quarantine ~~~~~~~~~~ Mark Lapierre discusses the `Pros and Cons of Quarantined Tests `_ in a post from 2018. CI tools that rerun on failure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Azure Pipelines (the Azure cloud CI/CD tool, formerly Visual Studio Team Services or VSTS) has a feature to `identify flaky tests `_ and rerun failed tests. Research ^^^^^^^^ This is a limited list, please submit an issue or pull request to expand it! * Gao, Zebao, Yalan Liang, Myra B. Cohen, Atif M. Memon, and Zhen Wang. "Making system user interactive tests repeatable: When and what should we control?." In *Software Engineering (ICSE), 2015 IEEE/ACM 37th IEEE International Conference on*, vol. 1, pp. 55-65. IEEE, 2015. `PDF `__ * Palomba, Fabio, and Andy Zaidman. "Does refactoring of test smells induce fixing flaky tests?." In *Software Maintenance and Evolution (ICSME), 2017 IEEE International Conference on*, pp. 1-12. IEEE, 2017. `PDF in Google Drive `__ * Bell, Jonathan, Owolabi Legunsen, Michael Hilton, Lamyaa Eloussi, Tifany Yung, and Darko Marinov. "DeFlaker: Automatically detecting flaky tests." In *Proceedings of the 2018 International Conference on Software Engineering*. 2018. `PDF `__ Resources ^^^^^^^^^ * `Eradicating Non-Determinism in Tests `_ by Martin Fowler, 2011 * `No more flaky tests on the Go team `_ by Pavan Sudarshan, 2012 * `The Build That Cried Broken: Building Trust in your Continuous Integration Tests `_ talk (video) by `Angie Jones `_ at SeleniumConf Austin 2017 * `Test and Code Podcast: Flaky Tests and How to Deal with Them `_ by Brian Okken and Anthony Shaw, 2018 * Microsoft: * `How we approach testing VSTS to enable continuous delivery `_ by Brian Harry MS, 2017 * `Eliminating Flaky Tests `_ blog and talk (video) by Munil Shah, 2017 * Google: * `Flaky Tests at Google and How We Mitigate Them `_ by John Micco, 2016 * `Where do Google's flaky tests come from? `_ by Jeff Listfield, 2017 pytest-4.6.9/doc/en/bash-completion.rst0000664000372000037200000000140313604173400020705 0ustar travistravis00000000000000 .. _bash_completion: Setting up bash completion ========================== When using bash as your shell, ``pytest`` can use argcomplete (https://argcomplete.readthedocs.io/) for auto-completion. For this ``argcomplete`` needs to be installed **and** enabled. Install argcomplete using: .. code-block:: bash sudo pip install 'argcomplete>=0.5.7' For global activation of all argcomplete enabled python applications run: .. code-block:: bash sudo activate-global-python-argcomplete For permanent (but not global) ``pytest`` activation, use: .. code-block:: bash register-python-argcomplete pytest >> ~/.bashrc For one-time activation of argcomplete for ``pytest`` only, use: .. code-block:: bash eval "$(register-python-argcomplete pytest)" pytest-4.6.9/doc/en/_themes/0000775000372000037200000000000013604173421016520 5ustar travistravis00000000000000pytest-4.6.9/doc/en/_themes/flask/0000775000372000037200000000000013604173421017620 5ustar travistravis00000000000000pytest-4.6.9/doc/en/_themes/flask/static/0000775000372000037200000000000013604173421021107 5ustar travistravis00000000000000pytest-4.6.9/doc/en/_themes/flask/static/flasky.css_t0000664000372000037200000002121613604173400023434 0ustar travistravis00000000000000/* * flasky.css_t * ~~~~~~~~~~~~ * * :copyright: Copyright 2010 by Armin Ronacher. * :license: Flask Design License, see LICENSE for details. */ {% set page_width = '1020px' %} {% set sidebar_width = '220px' %} /* orange of logo is #d67c29 but we use black for links for now */ {% set link_color = '#000' %} {% set link_hover_color = '#000' %} {% set base_font = 'sans-serif' %} {% set header_font = 'serif' %} @import url("basic.css"); /* -- page layout ----------------------------------------------------------- */ body { font-family: {{ base_font }}; font-size: 17px; background-color: white; color: #000; margin: 0; padding: 0; } div.document { width: {{ page_width }}; margin: 30px auto 0 auto; } div.documentwrapper { float: left; width: 100%; } div.bodywrapper { margin: 0 0 0 {{ sidebar_width }}; } div.sphinxsidebar { width: {{ sidebar_width }}; } hr { border: 0; border-top: 1px solid #B1B4B6; } div.body { background-color: #ffffff; color: #3E4349; padding: 0 30px 0 30px; } img.floatingflask { padding: 0 0 10px 10px; float: right; } div.footer { width: {{ page_width }}; margin: 20px auto 30px auto; font-size: 14px; color: #888; text-align: right; } div.footer a { color: #888; } div.related { display: none; } div.sphinxsidebar a { color: #444; text-decoration: none; border-bottom: 1px dotted #999; } div.sphinxsidebar a:hover { border-bottom: 1px solid #999; } div.sphinxsidebar { font-size: 14px; line-height: 1.5; } div.sphinxsidebarwrapper { padding: 18px 10px; } div.sphinxsidebarwrapper p.logo { padding: 0 0 20px 0; margin: 0; text-align: center; } div.sphinxsidebar h3, div.sphinxsidebar h4 { font-family: {{ header_font }}; color: #444; font-size: 24px; font-weight: normal; margin: 0 0 5px 0; padding: 0; } div.sphinxsidebar h4 { font-size: 20px; } div.sphinxsidebar h3 a { color: #444; } div.sphinxsidebar p.logo a, div.sphinxsidebar h3 a, div.sphinxsidebar p.logo a:hover, div.sphinxsidebar h3 a:hover { border: none; } div.sphinxsidebar p { color: #555; margin: 10px 0; } div.sphinxsidebar ul { margin: 10px 0; padding: 0; color: #000; } div.sphinxsidebar input { border: 1px solid #ccc; font-family: {{ base_font }}; font-size: 1em; } /* -- body styles ----------------------------------------------------------- */ a { color: {{ link_color }}; text-decoration: underline; } a:hover { color: {{ link_hover_color }}; text-decoration: underline; } a.reference.internal em { font-style: normal; } div.body h1, div.body h2, div.body h3, div.body h4, div.body h5, div.body h6 { font-family: {{ header_font }}; font-weight: normal; margin: 30px 0px 10px 0px; padding: 0; } {% if theme_index_logo %} div.indexwrapper h1 { text-indent: -999999px; background: url({{ theme_index_logo }}) no-repeat center center; height: {{ theme_index_logo_height }}; } {% else %} div.indexwrapper div.body h1 { font-size: 200%; } {% endif %} div.body h1 { margin-top: 0; padding-top: 0; font-size: 240%; } div.body h2 { font-size: 180%; } div.body h3 { font-size: 150%; } div.body h4 { font-size: 130%; } div.body h5 { font-size: 100%; } div.body h6 { font-size: 100%; } a.headerlink { color: #ddd; padding: 0 4px; text-decoration: none; } a.headerlink:hover { color: #444; background: #eaeaea; } div.body p, div.body dd, div.body li { line-height: 1.4em; } div.admonition { background: #fafafa; margin: 20px -30px; padding: 10px 30px; border-top: 1px solid #ccc; border-bottom: 1px solid #ccc; } div.admonition tt.xref, div.admonition a tt { border-bottom: 1px solid #fafafa; } dd div.admonition { margin-left: -60px; padding-left: 60px; } div.admonition p.admonition-title { font-family: {{ header_font }}; font-weight: normal; font-size: 24px; margin: 0 0 10px 0; padding: 0; line-height: 1; } div.admonition p.last { margin-bottom: 0; } div.highlight { background-color: white; } dt:target, .highlight { background: #FAF3E8; } div.note { background-color: #eee; border: 1px solid #ccc; } div.seealso { background-color: #ffc; border: 1px solid #ff6; } div.topic { background-color: #eee; } p.admonition-title { display: inline; } p.admonition-title:after { content: ":"; } pre, tt, code { font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace; font-size: 0.9em; background: #eee; } img.screenshot { } tt.descname, tt.descclassname { font-size: 0.95em; } tt.descname { padding-right: 0.08em; } img.screenshot { -moz-box-shadow: 2px 2px 4px #eee; -webkit-box-shadow: 2px 2px 4px #eee; box-shadow: 2px 2px 4px #eee; } table.docutils { border: 1px solid #888; -moz-box-shadow: 2px 2px 4px #eee; -webkit-box-shadow: 2px 2px 4px #eee; box-shadow: 2px 2px 4px #eee; } table.docutils td, table.docutils th { border: 1px solid #888; padding: 0.25em 0.7em; } table.field-list, table.footnote { border: none; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; } table.footnote { margin: 15px 0; width: 100%; border: 1px solid #eee; background: #fdfdfd; font-size: 0.9em; } table.footnote + table.footnote { margin-top: -15px; border-top: none; } table.field-list th { padding: 0 0.8em 0 0; } table.field-list td { padding: 0; } table.footnote td.label { width: 0px; padding: 0.3em 0 0.3em 0.5em; } table.footnote td { padding: 0.3em 0.5em; } dl { margin: 0; padding: 0; } dl dd { margin-left: 30px; } blockquote { margin: 0 0 0 30px; padding: 0; } ul, ol { margin: 10px 0 10px 30px; padding: 0; } pre { background: #eee; padding: 7px 30px; margin: 15px -30px; line-height: 1.3em; } dl pre, blockquote pre, li pre { margin-left: -60px; padding-left: 60px; } dl dl pre { margin-left: -90px; padding-left: 90px; } tt { background-color: #ecf0f3; color: #222; /* padding: 1px 2px; */ } tt.xref, a tt { background-color: #FBFBFB; border-bottom: 1px solid white; } a.reference { text-decoration: none; border-bottom: 1px dotted {{ link_color }}; } a.reference:hover { border-bottom: 1px solid {{ link_hover_color }}; } a.footnote-reference { text-decoration: none; font-size: 0.7em; vertical-align: top; border-bottom: 1px dotted {{ link_color }}; } a.footnote-reference:hover { border-bottom: 1px solid {{ link_hover_color }}; } a:hover tt { background: #EEE; } @media screen and (max-width: 870px) { div.sphinxsidebar { display: none; } div.document { width: 100%; } div.documentwrapper { margin-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; } div.bodywrapper { margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; } ul { margin-left: 0; } .document { width: auto; } .footer { width: auto; } .bodywrapper { margin: 0; } .footer { width: auto; } .github { display: none; } } @media screen and (max-width: 875px) { body { margin: 0; padding: 20px 30px; } div.documentwrapper { float: none; background: white; } div.sphinxsidebar { display: block; float: none; width: 102.5%; margin: 50px -30px -20px -30px; padding: 10px 20px; background: #333; color: white; } div.sphinxsidebar h3, div.sphinxsidebar h4, div.sphinxsidebar p, div.sphinxsidebar h3 a, div.sphinxsidebar ul { color: white; } div.sphinxsidebar a { color: #aaa; } div.sphinxsidebar p.logo { display: none; } div.document { width: 100%; margin: 0; } div.related { display: block; margin: 0; padding: 10px 0 20px 0; } div.related ul, div.related ul li { margin: 0; padding: 0; } div.footer { display: none; } div.bodywrapper { margin: 0; } div.body { min-height: 0; padding: 0; } .rtd_doc_footer { display: none; } .document { width: auto; } .footer { width: auto; } .footer { width: auto; } .github { display: none; } } /* misc. */ .revsys-inline { display: none!important; } pytest-4.6.9/doc/en/_themes/flask/theme.conf0000664000372000037200000000024313604173400021565 0ustar travistravis00000000000000[theme] inherit = basic stylesheet = flasky.css pygments_style = flask_theme_support.FlaskyStyle [options] index_logo = '' index_logo_height = 120px touch_icon = pytest-4.6.9/doc/en/_themes/flask/relations.html0000664000372000037200000000111613604173400022502 0ustar travistravis00000000000000

Related Topics

pytest-4.6.9/doc/en/_themes/flask/layout.html0000664000372000037200000000126513604173400022024 0ustar travistravis00000000000000{%- extends "basic/layout.html" %} {%- block extrahead %} {{ super() }} {% if theme_touch_icon %} {% endif %} {% endblock %} {%- block relbar2 %}{% endblock %} {% block header %} {{ super() }} {% if pagename == 'index' %}
{% endif %} {% endblock %} {%- block footer %} {% if pagename == 'index' %}
{% endif %} {%- endblock %} pytest-4.6.9/doc/en/_themes/.gitignore0000664000372000037200000000002613604173400020503 0ustar travistravis00000000000000*.pyc *.pyo .DS_Store pytest-4.6.9/doc/en/_themes/README0000664000372000037200000000210513604173400017373 0ustar travistravis00000000000000Flask Sphinx Styles =================== This repository contains sphinx styles for Flask and Flask related projects. To use this style in your Sphinx documentation, follow this guide: 1. put this folder as _themes into your docs folder. Alternatively you can also use git submodules to check out the contents there. 2. add this to your conf.py: sys.path.append(os.path.abspath('_themes')) html_theme_path = ['_themes'] html_theme = 'flask' The following themes exist: - 'flask' - the standard flask documentation theme for large projects - 'flask_small' - small one-page theme. Intended to be used by very small addon libraries for flask. The following options exist for the flask_small theme: [options] index_logo = '' filename of a picture in _static to be used as replacement for the h1 in the index.rst file. index_logo_height = 120px height of the index logo github_fork = '' repository name on github for the "fork me" badge pytest-4.6.9/doc/en/_themes/LICENSE0000664000372000037200000000337513604173400017532 0ustar travistravis00000000000000Copyright (c) 2010 by Armin Ronacher. Some rights reserved. Redistribution and use in source and binary forms of the theme, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. We kindly ask you to only use these themes in an unmodified manner just for Flask and Flask-related products, not for unrelated projects. If you like the visual style and want to use it for your own projects, please consider making some larger changes to the themes (such as changing font faces, sizes, colors or margins). THIS THEME IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS THEME, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pytest-4.6.9/doc/en/_themes/flask_theme_support.py0000664000372000037200000001010213604173400023137 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # flasky extensions. flasky pygments style based on tango style from pygments.style import Style from pygments.token import Comment from pygments.token import Error from pygments.token import Generic from pygments.token import Keyword from pygments.token import Literal from pygments.token import Name from pygments.token import Number from pygments.token import Operator from pygments.token import Other from pygments.token import Punctuation from pygments.token import String from pygments.token import Whitespace class FlaskyStyle(Style): background_color = "#f8f8f8" default_style = "" styles = { # No corresponding class for the following: # Text: "", # class: '' Whitespace: "underline #f8f8f8", # class: 'w' Error: "#a40000 border:#ef2929", # class: 'err' Other: "#000000", # class 'x' Comment: "italic #8f5902", # class: 'c' Comment.Preproc: "noitalic", # class: 'cp' Keyword: "bold #004461", # class: 'k' Keyword.Constant: "bold #004461", # class: 'kc' Keyword.Declaration: "bold #004461", # class: 'kd' Keyword.Namespace: "bold #004461", # class: 'kn' Keyword.Pseudo: "bold #004461", # class: 'kp' Keyword.Reserved: "bold #004461", # class: 'kr' Keyword.Type: "bold #004461", # class: 'kt' Operator: "#582800", # class: 'o' Operator.Word: "bold #004461", # class: 'ow' - like keywords Punctuation: "bold #000000", # class: 'p' # because special names such as Name.Class, Name.Function, etc. # are not recognized as such later in the parsing, we choose them # to look the same as ordinary variables. Name: "#000000", # class: 'n' Name.Attribute: "#c4a000", # class: 'na' - to be revised Name.Builtin: "#004461", # class: 'nb' Name.Builtin.Pseudo: "#3465a4", # class: 'bp' Name.Class: "#000000", # class: 'nc' - to be revised Name.Constant: "#000000", # class: 'no' - to be revised Name.Decorator: "#888", # class: 'nd' - to be revised Name.Entity: "#ce5c00", # class: 'ni' Name.Exception: "bold #cc0000", # class: 'ne' Name.Function: "#000000", # class: 'nf' Name.Property: "#000000", # class: 'py' Name.Label: "#f57900", # class: 'nl' Name.Namespace: "#000000", # class: 'nn' - to be revised Name.Other: "#000000", # class: 'nx' Name.Tag: "bold #004461", # class: 'nt' - like a keyword Name.Variable: "#000000", # class: 'nv' - to be revised Name.Variable.Class: "#000000", # class: 'vc' - to be revised Name.Variable.Global: "#000000", # class: 'vg' - to be revised Name.Variable.Instance: "#000000", # class: 'vi' - to be revised Number: "#990000", # class: 'm' Literal: "#000000", # class: 'l' Literal.Date: "#000000", # class: 'ld' String: "#4e9a06", # class: 's' String.Backtick: "#4e9a06", # class: 'sb' String.Char: "#4e9a06", # class: 'sc' String.Doc: "italic #8f5902", # class: 'sd' - like a comment String.Double: "#4e9a06", # class: 's2' String.Escape: "#4e9a06", # class: 'se' String.Heredoc: "#4e9a06", # class: 'sh' String.Interpol: "#4e9a06", # class: 'si' String.Other: "#4e9a06", # class: 'sx' String.Regex: "#4e9a06", # class: 'sr' String.Single: "#4e9a06", # class: 's1' String.Symbol: "#4e9a06", # class: 'ss' Generic: "#000000", # class: 'g' Generic.Deleted: "#a40000", # class: 'gd' Generic.Emph: "italic #000000", # class: 'ge' Generic.Error: "#ef2929", # class: 'gr' Generic.Heading: "bold #000080", # class: 'gh' Generic.Inserted: "#00A000", # class: 'gi' Generic.Output: "#888", # class: 'go' Generic.Prompt: "#745334", # class: 'gp' Generic.Strong: "bold #000000", # class: 'gs' Generic.Subheading: "bold #800080", # class: 'gu' Generic.Traceback: "bold #a40000", # class: 'gt' } pytest-4.6.9/doc/en/customize.rst0000664000372000037200000001403713604173400017652 0ustar travistravis00000000000000Configuration ============= Command line options and configuration file settings ----------------------------------------------------------------- You can get help on command line options and values in INI-style configurations files by using the general help option: .. code-block:: bash pytest -h # prints options _and_ config file settings This will display command line and configuration file settings which were registered by installed plugins. .. _rootdir: .. _inifiles: Initialization: determining rootdir and inifile ----------------------------------------------- pytest determines a ``rootdir`` for each test run which depends on the command line arguments (specified test files, paths) and on the existence of *ini-files*. The determined ``rootdir`` and *ini-file* are printed as part of the pytest header during startup. Here's a summary what ``pytest`` uses ``rootdir`` for: * Construct *nodeids* during collection; each test is assigned a unique *nodeid* which is rooted at the ``rootdir`` and takes in account full path, class name, function name and parametrization (if any). * Is used by plugins as a stable location to store project/test run specific information; for example, the internal :ref:`cache ` plugin creates a ``.pytest_cache`` subdirectory in ``rootdir`` to store its cross-test run state. Important to emphasize that ``rootdir`` is **NOT** used to modify ``sys.path``/``PYTHONPATH`` or influence how modules are imported. See :ref:`pythonpath` for more details. ``--rootdir=path`` command-line option can be used to force a specific directory. The directory passed may contain environment variables when it is used in conjunction with ``addopts`` in a ``pytest.ini`` file. Finding the ``rootdir`` ~~~~~~~~~~~~~~~~~~~~~~~ Here is the algorithm which finds the rootdir from ``args``: - determine the common ancestor directory for the specified ``args`` that are recognised as paths that exist in the file system. If no such paths are found, the common ancestor directory is set to the current working directory. - look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the ancestor directory and upwards. If one is matched, it becomes the ini-file and its directory becomes the rootdir. - if no ini-file was found, look for ``setup.py`` upwards from the common ancestor directory to determine the ``rootdir``. - if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` in each of the specified ``args`` and upwards. If one is matched, it becomes the ini-file and its directory becomes the rootdir. - if no ini-file was found, use the already determined common ancestor as root directory. This allows the use of pytest in structures that are not part of a package and don't have any particular ini-file configuration. If no ``args`` are given, pytest collects test below the current working directory and also starts determining the rootdir from there. :warning: custom pytest plugin commandline arguments may include a path, as in ``pytest --log-output ../../test.log args``. Then ``args`` is mandatory, otherwise pytest uses the folder of test.log for rootdir determination (see also `issue 1435 `_). A dot ``.`` for referencing to the current working directory is also possible. Note that an existing ``pytest.ini`` file will always be considered a match, whereas ``tox.ini`` and ``setup.cfg`` will only match if they contain a ``[pytest]`` or ``[tool:pytest]`` section, respectively. Options from multiple ini-files candidates are never merged - the first one wins (``pytest.ini`` always wins, even if it does not contain a ``[pytest]`` section). The ``config`` object will subsequently carry these attributes: - ``config.rootdir``: the determined root directory, guaranteed to exist. - ``config.inifile``: the determined ini-file, may be ``None``. The rootdir is used as a reference directory for constructing test addresses ("nodeids") and can be used also by plugins for storing per-testrun information. Example: .. code-block:: bash pytest path/to/testdir path/other/ will determine the common ancestor as ``path`` and then check for ini-files as follows: .. code-block:: text # first look for pytest.ini files path/pytest.ini path/setup.cfg # must also contain [tool:pytest] section to match path/tox.ini # must also contain [pytest] section to match pytest.ini ... # all the way down to the root # now look for setup.py path/setup.py setup.py ... # all the way down to the root .. _`how to change command line options defaults`: .. _`adding default options`: How to change command line options defaults ------------------------------------------------ It can be tedious to type the same series of command line options every time you use ``pytest``. For example, if you always want to see detailed info on skipped and xfailed tests, as well as have terser "dot" progress output, you can write it into a configuration file: .. code-block:: ini # content of pytest.ini or tox.ini # setup.cfg files should use [tool:pytest] section instead [pytest] addopts = -ra -q Alternatively, you can set a ``PYTEST_ADDOPTS`` environment variable to add command line options while the environment is in use: .. code-block:: bash export PYTEST_ADDOPTS="-v" Here's how the command-line is built in the presence of ``addopts`` or the environment variable: .. code-block:: text $PYTEST_ADDOPTS So if the user executes in the command-line: .. code-block:: bash pytest -m slow The actual command line executed is: .. code-block:: bash pytest -ra -q -v -m slow Note that as usual for other command-line applications, in case of conflicting options the last one wins, so the example above will show verbose output because ``-v`` overwrites ``-q``. Builtin configuration file options ---------------------------------------------- For the full list of options consult the :ref:`reference documentation `. pytest-4.6.9/doc/en/monkeypatch.rst0000664000372000037200000001231513604173400020147 0ustar travistravis00000000000000 Monkeypatching/mocking modules and environments ================================================================ .. currentmodule:: _pytest.monkeypatch Sometimes tests need to invoke functionality which depends on global settings or which invokes code which cannot be easily tested such as network access. The ``monkeypatch`` fixture helps you to safely set/delete an attribute, dictionary item or environment variable or to modify ``sys.path`` for importing. See the `monkeypatch blog post`_ for some introduction material and a discussion of its motivation. .. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ Simple example: monkeypatching functions ---------------------------------------- If you want to pretend that ``os.expanduser`` returns a certain directory, you can use the :py:meth:`monkeypatch.setattr` method to patch this function before calling into a function which uses it:: # content of test_module.py import os.path def getssh(): # pseudo application code return os.path.join(os.path.expanduser("~admin"), '.ssh') def test_mytest(monkeypatch): def mockreturn(path): return '/abc' monkeypatch.setattr(os.path, 'expanduser', mockreturn) x = getssh() assert x == '/abc/.ssh' Here our test function monkeypatches ``os.path.expanduser`` and then calls into a function that calls it. After the test function finishes the ``os.path.expanduser`` modification will be undone. Global patch example: preventing "requests" from remote operations ------------------------------------------------------------------ If you want to prevent the "requests" library from performing http requests in all your tests, you can do:: # content of conftest.py import pytest @pytest.fixture(autouse=True) def no_requests(monkeypatch): monkeypatch.delattr("requests.sessions.Session.request") This autouse fixture will be executed for each test function and it will delete the method ``request.session.Session.request`` so that any attempts within tests to create http requests will fail. .. note:: Be advised that it is not recommended to patch builtin functions such as ``open``, ``compile``, etc., because it might break pytest's internals. If that's unavoidable, passing ``--tb=native``, ``--assert=plain`` and ``--capture=no`` might help although there's no guarantee. .. note:: Mind that patching ``stdlib`` functions and some third-party libraries used by pytest might break pytest itself, therefore in those cases it is recommended to use :meth:`MonkeyPatch.context` to limit the patching to the block you want tested: .. code-block:: python import functools def test_partial(monkeypatch): with monkeypatch.context() as m: m.setattr(functools, "partial", 3) assert functools.partial == 3 See issue `#3290 `_ for details. Monkeypatching environment variables ------------------------------------ If you are working with environment variables you often need to safely change the values or delete them from the system for testing purposes. ``Monkeypatch`` provides a mechanism to do this using the ``setenv`` and ``delenv`` method. Our example code to test: .. code-block:: python # contents of our original code file e.g. code.py import os def get_os_user_lower(): """Simple retrieval function. Returns lowercase USER or raises EnvironmentError.""" username = os.getenv("USER") if username is None: raise EnvironmentError("USER environment is not set.") return username.lower() There are two potential paths. First, the ``USER`` environment variable is set to a value. Second, the ``USER`` environment variable does not exist. Using ``monkeypatch`` both paths can be safely tested without impacting the running environment: .. code-block:: python # contents of our test file e.g. test_code.py import pytest def test_upper_to_lower(monkeypatch): """Set the USER env var to assert the behavior.""" monkeypatch.setenv("USER", "TestingUser") assert get_os_user_lower() == "testinguser" def test_raise_exception(monkeypatch): """Remove the USER env var and assert EnvironmentError is raised.""" monkeypatch.delenv("USER", raising=False) with pytest.raises(EnvironmentError): _ = get_os_user_lower() This behavior can be moved into ``fixture`` structures and shared across tests: .. code-block:: python import pytest @pytest.fixture def mock_env_user(monkeypatch): monkeypatch.setenv("USER", "TestingUser") @pytest.fixture def mock_env_missing(monkeypatch): monkeypatch.delenv("USER", raising=False) # Notice the tests reference the fixtures for mocks def test_upper_to_lower(mock_env_user): assert get_os_user_lower() == "testinguser" def test_raise_exception(mock_env_missing): with pytest.raises(EnvironmentError): _ = get_os_user_lower() .. currentmodule:: _pytest.monkeypatch API Reference ------------- Consult the docs for the :class:`MonkeyPatch` class. pytest-4.6.9/doc/en/funcargs.rst0000664000372000037200000000067113604173400017437 0ustar travistravis00000000000000 ======================================================= funcargs: resource injection and parametrization ======================================================= pytest-2.3 introduces major refinements to fixture management of which the funcarg mechanism introduced with pytest-2.0 remains a core part. The documentation has been refactored as well and you can read on here: - :ref:`fixtures` - :ref:`parametrize` - :ref:`funcargcompare` pytest-4.6.9/changelog/0000775000372000037200000000000013604173421015654 5ustar travistravis00000000000000pytest-4.6.9/changelog/_template.rst0000664000372000037200000000165313604173400020362 0ustar travistravis00000000000000{% for section in sections %} {% set underline = "-" %} {% if section %} {{section}} {{ underline * section|length }}{% set underline = "~" %} {% endif %} {% if sections[section] %} {% for category, val in definitions.items() if category in sections[section] %} {{ definitions[category]['name'] }} {{ underline * definitions[category]['name']|length }} {% if definitions[category]['showcontent'] %} {% for text, values in sections[section][category]|dictsort(by='value') %} {% set issue_joiner = joiner(', ') %} - {% for value in values|sort %}{{ issue_joiner() }}`{{ value }} `_{% endfor %}: {{ text }} {% endfor %} {% else %} - {{ sections[section][category]['']|sort|join(', ') }} {% endif %} {% if sections[section][category]|length == 0 %} No significant changes. {% else %} {% endif %} {% endfor %} {% else %} No significant changes. {% endif %} {% endfor %} pytest-4.6.9/changelog/README.rst0000664000372000037200000000322713604173400017344 0ustar travistravis00000000000000This directory contains "newsfragments" which are short files that contain a small **ReST**-formatted text that will be added to the next ``CHANGELOG``. The ``CHANGELOG`` will be read by users, so this description should be aimed to pytest users instead of describing internal changes which are only relevant to the developers. Make sure to use full sentences with correct case and punctuation, for example:: Fix issue with non-ascii messages from the ``warnings`` module. Each file should be named like ``..rst``, where ```` is an issue number, and ```` is one of: * ``feature``: new user facing features, like new command-line options and new behavior. * ``bugfix``: fixes a reported bug. * ``doc``: documentation improvement, like rewording an entire session or adding missing docs. * ``deprecation``: feature deprecation. * ``removal``: feature removal. * ``vendor``: changes in packages vendored in pytest. * ``trivial``: fixing a small typo or internal change that might be noteworthy. So for example: ``123.feature.rst``, ``456.bugfix.rst``. If your PR fixes an issue, use that number here. If there is no issue, then after you submit the PR and get the PR number you can add a changelog using that instead. If you are not sure what issue type to use, don't hesitate to ask in your PR. ``towncrier`` preserves multiple paragraphs and formatting (code blocks, lists, and so on), but for entries other than ``features`` it is usually better to stick to a single paragraph to keep it concise. You can install ``towncrier`` and then run ``towncrier --draft`` if you want to get a preview of how your change will look in the final release notes. pytest-4.6.9/src/0000775000372000037200000000000013604173421014514 5ustar travistravis00000000000000pytest-4.6.9/src/pytest.egg-info/0000775000372000037200000000000013604173421017536 5ustar travistravis00000000000000pytest-4.6.9/src/pytest.egg-info/PKG-INFO0000664000372000037200000001545413604173421020644 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: pytest Version: 4.6.9 Summary: pytest: simple powerful testing with Python Home-page: https://docs.pytest.org/en/latest/ Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others License: MIT license Project-URL: Source, https://github.com/pytest-dev/pytest Project-URL: Tracker, https://github.com/pytest-dev/pytest/issues Description: .. image:: https://docs.pytest.org/en/latest/_static/pytest1.png :target: https://docs.pytest.org/en/latest/ :align: center :alt: pytest ------ .. image:: https://img.shields.io/pypi/v/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest .. image:: https://img.shields.io/pypi/pyversions/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://codecov.io/gh/pytest-dev/pytest/branch/master/graph/badge.svg :target: https://codecov.io/gh/pytest-dev/pytest :alt: Code coverage Status .. image:: https://travis-ci.org/pytest-dev/pytest.svg?branch=master :target: https://travis-ci.org/pytest-dev/pytest .. image:: https://dev.azure.com/pytest-dev/pytest/_apis/build/status/pytest-CI?branchName=master :target: https://dev.azure.com/pytest-dev/pytest .. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/python/black .. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg :target: https://www.codetriage.com/pytest-dev/pytest The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example of a simple test: .. code-block:: python # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 To execute it:: $ pytest ============================= test session starts ============================= collected 1 items test_sample.py F ================================== FAILURES =================================== _________________________________ test_answer _________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:5: AssertionError ========================== 1 failed in 0.04 seconds =========================== Due to ``pytest``'s detailed assertion introspection, only plain ``assert`` statements are used. See `getting-started `_ for more examples. Features -------- - Detailed info on failing `assert statements `_ (no need to remember ``self.assert*`` names); - `Auto-discovery `_ of test modules and functions; - `Modular fixtures `_ for managing small or parametrized long-lived test resources; - Can run `unittest `_ (or trial), `nose `_ test suites out of the box; - Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; Documentation ------------- For full documentation, including installation, tutorials and PDF documents, please see https://docs.pytest.org/en/latest/. Bugs/Requests ------------- Please use the `GitHub issue tracker `_ to submit bugs or request features. Changelog --------- Consult the `Changelog `__ page for fixes and enhancements of each version. Support pytest -------------- You can support pytest by obtaining a `Tideflift subscription`_. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools. .. _`Tideflift subscription`: https://tidelift.com/subscription/pkg/pypi-pytest?utm_source=pypi-pytest&utm_medium=referral&utm_campaign=readme Security ^^^^^^^^ pytest has never been associated with a security vunerability, but in any case, to report a security vulnerability please use the `Tidelift security contact `_. Tidelift will coordinate the fix and disclosure. License ------- Copyright Holger Krekel and others, 2004-2020. Distributed under the terms of the `MIT`_ license, pytest is free and open source software. .. _`MIT`: https://github.com/pytest-dev/pytest/blob/master/LICENSE Keywords: test,unittest Platform: unix Platform: linux Platform: osx Platform: cygwin Platform: win32 Classifier: Development Status :: 6 - Mature Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: POSIX Classifier: Operating System :: Microsoft :: Windows Classifier: Operating System :: MacOS :: MacOS X Classifier: Topic :: Software Development :: Testing Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: Utilities Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Requires-Python: !=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7 Provides-Extra: testing pytest-4.6.9/src/pytest.egg-info/dependency_links.txt0000664000372000037200000000000113604173421023604 0ustar travistravis00000000000000 pytest-4.6.9/src/pytest.egg-info/top_level.txt0000664000372000037200000000001713604173421022266 0ustar travistravis00000000000000_pytest pytest pytest-4.6.9/src/pytest.egg-info/not-zip-safe0000664000372000037200000000000113604173421021764 0ustar travistravis00000000000000 pytest-4.6.9/src/pytest.egg-info/SOURCES.txt0000664000372000037200000003502113604173421021423 0ustar travistravis00000000000000.coveragerc .gitattributes .gitignore .pre-commit-config.yaml .travis.yml AUTHORS CHANGELOG.rst CITATION CONTRIBUTING.rst HOWTORELEASE.rst LICENSE README.rst TIDELIFT.rst azure-pipelines.yml codecov.yml pyproject.toml setup.cfg setup.py tox.ini .github/FUNDING.yml .github/ISSUE_TEMPLATE.md .github/PULL_REQUEST_TEMPLATE.md .github/config.yml .github/labels.toml bench/bench.py bench/bench_argcomplete.py bench/empty.py bench/manyparam.py bench/skip.py changelog/README.rst changelog/_template.rst doc/en/Makefile doc/en/adopt.rst doc/en/assert.rst doc/en/backwards-compatibility.rst doc/en/bash-completion.rst doc/en/builtin.rst doc/en/cache.rst doc/en/capture.rst doc/en/changelog.rst doc/en/conf.py doc/en/conftest.py doc/en/contact.rst doc/en/contents.rst doc/en/contributing.rst doc/en/customize.rst doc/en/deprecations.rst doc/en/development_guide.rst doc/en/doctest.rst doc/en/existingtestsuite.rst doc/en/faq.rst doc/en/fixture.rst doc/en/flaky.rst doc/en/funcarg_compare.rst doc/en/funcargs.rst doc/en/getting-started.rst doc/en/goodpractices.rst doc/en/historical-notes.rst doc/en/index.rst doc/en/license.rst doc/en/links.inc doc/en/logging.rst doc/en/mark.rst doc/en/monkeypatch.rst doc/en/naming20.rst doc/en/nose.rst doc/en/parametrize.rst doc/en/plugins.rst doc/en/projects.rst doc/en/py27-py34-deprecation.rst doc/en/pytest.ini doc/en/pythonpath.rst doc/en/recwarn.rst doc/en/reference.rst doc/en/requirements.txt doc/en/skipping.rst doc/en/talks.rst doc/en/tidelift.rst doc/en/tmpdir.rst doc/en/unittest.rst doc/en/usage.rst doc/en/warnings.rst doc/en/writing_plugins.rst doc/en/xunit_setup.rst doc/en/yieldfixture.rst doc/en/_templates/globaltoc.html doc/en/_templates/layout.html doc/en/_templates/links.html doc/en/_templates/sidebarintro.html doc/en/_themes/.gitignore doc/en/_themes/LICENSE doc/en/_themes/README doc/en/_themes/flask_theme_support.py doc/en/_themes/flask/layout.html doc/en/_themes/flask/relations.html doc/en/_themes/flask/theme.conf doc/en/_themes/flask/static/flasky.css_t doc/en/announce/index.rst doc/en/announce/release-2.0.0.rst doc/en/announce/release-2.0.1.rst doc/en/announce/release-2.0.2.rst doc/en/announce/release-2.0.3.rst doc/en/announce/release-2.1.0.rst doc/en/announce/release-2.1.1.rst doc/en/announce/release-2.1.2.rst doc/en/announce/release-2.1.3.rst doc/en/announce/release-2.2.0.rst doc/en/announce/release-2.2.1.rst doc/en/announce/release-2.2.2.rst doc/en/announce/release-2.2.4.rst doc/en/announce/release-2.3.0.rst doc/en/announce/release-2.3.1.rst doc/en/announce/release-2.3.2.rst doc/en/announce/release-2.3.3.rst doc/en/announce/release-2.3.4.rst doc/en/announce/release-2.3.5.rst doc/en/announce/release-2.4.0.rst doc/en/announce/release-2.4.1.rst doc/en/announce/release-2.4.2.rst doc/en/announce/release-2.5.0.rst doc/en/announce/release-2.5.1.rst doc/en/announce/release-2.5.2.rst doc/en/announce/release-2.6.0.rst doc/en/announce/release-2.6.1.rst doc/en/announce/release-2.6.2.rst doc/en/announce/release-2.6.3.rst doc/en/announce/release-2.7.0.rst doc/en/announce/release-2.7.1.rst doc/en/announce/release-2.7.2.rst doc/en/announce/release-2.8.2.rst doc/en/announce/release-2.8.3.rst doc/en/announce/release-2.8.4.rst doc/en/announce/release-2.8.5.rst doc/en/announce/release-2.8.6.rst doc/en/announce/release-2.8.7.rst doc/en/announce/release-2.9.0.rst doc/en/announce/release-2.9.1.rst doc/en/announce/release-2.9.2.rst doc/en/announce/release-3.0.0.rst doc/en/announce/release-3.0.1.rst doc/en/announce/release-3.0.2.rst doc/en/announce/release-3.0.3.rst doc/en/announce/release-3.0.4.rst doc/en/announce/release-3.0.5.rst doc/en/announce/release-3.0.6.rst doc/en/announce/release-3.0.7.rst doc/en/announce/release-3.1.0.rst doc/en/announce/release-3.1.1.rst doc/en/announce/release-3.1.2.rst doc/en/announce/release-3.1.3.rst doc/en/announce/release-3.10.0.rst doc/en/announce/release-3.10.1.rst doc/en/announce/release-3.2.0.rst doc/en/announce/release-3.2.1.rst doc/en/announce/release-3.2.2.rst doc/en/announce/release-3.2.3.rst doc/en/announce/release-3.2.4.rst doc/en/announce/release-3.2.5.rst doc/en/announce/release-3.3.0.rst doc/en/announce/release-3.3.1.rst doc/en/announce/release-3.3.2.rst doc/en/announce/release-3.4.0.rst doc/en/announce/release-3.4.1.rst doc/en/announce/release-3.4.2.rst doc/en/announce/release-3.5.0.rst doc/en/announce/release-3.5.1.rst doc/en/announce/release-3.6.0.rst doc/en/announce/release-3.6.1.rst doc/en/announce/release-3.6.2.rst doc/en/announce/release-3.6.3.rst doc/en/announce/release-3.6.4.rst doc/en/announce/release-3.7.0.rst doc/en/announce/release-3.7.1.rst doc/en/announce/release-3.7.2.rst doc/en/announce/release-3.7.3.rst doc/en/announce/release-3.7.4.rst doc/en/announce/release-3.8.0.rst doc/en/announce/release-3.8.1.rst doc/en/announce/release-3.8.2.rst doc/en/announce/release-3.9.0.rst doc/en/announce/release-3.9.1.rst doc/en/announce/release-3.9.2.rst doc/en/announce/release-3.9.3.rst doc/en/announce/release-4.0.0.rst doc/en/announce/release-4.0.1.rst doc/en/announce/release-4.0.2.rst doc/en/announce/release-4.1.0.rst doc/en/announce/release-4.1.1.rst doc/en/announce/release-4.2.0.rst doc/en/announce/release-4.2.1.rst doc/en/announce/release-4.3.0.rst doc/en/announce/release-4.3.1.rst doc/en/announce/release-4.4.0.rst doc/en/announce/release-4.4.1.rst doc/en/announce/release-4.4.2.rst doc/en/announce/release-4.5.0.rst doc/en/announce/release-4.6.0.rst doc/en/announce/release-4.6.1.rst doc/en/announce/release-4.6.2.rst doc/en/announce/release-4.6.3.rst doc/en/announce/release-4.6.4.rst doc/en/announce/release-4.6.5.rst doc/en/announce/release-4.6.6.rst doc/en/announce/release-4.6.7.rst doc/en/announce/release-4.6.8.rst doc/en/announce/release-4.6.9.rst doc/en/announce/sprint2016.rst doc/en/example/attic.rst doc/en/example/conftest.py doc/en/example/index.rst doc/en/example/markers.rst doc/en/example/multipython.py doc/en/example/nonpython.rst doc/en/example/parametrize.rst doc/en/example/pythoncollection.py doc/en/example/pythoncollection.rst doc/en/example/reportingdemo.rst doc/en/example/simple.rst doc/en/example/special.rst doc/en/example/xfail_demo.py doc/en/example/assertion/failure_demo.py doc/en/example/assertion/test_failures.py doc/en/example/assertion/test_setup_flow_example.py doc/en/example/assertion/global_testmodule_config/conftest.py doc/en/example/assertion/global_testmodule_config/test_hello_world.py doc/en/example/costlysetup/conftest.py doc/en/example/costlysetup/sub_a/__init__.py doc/en/example/costlysetup/sub_a/test_quick.py doc/en/example/costlysetup/sub_b/__init__.py doc/en/example/costlysetup/sub_b/test_two.py doc/en/example/nonpython/__init__.py doc/en/example/nonpython/conftest.py doc/en/example/nonpython/test_simple.yml doc/en/example/py2py3/conftest.py doc/en/example/py2py3/test_py2.py doc/en/example/py2py3/test_py3.py doc/en/img/cramer2.png doc/en/img/freiburg2.jpg doc/en/img/gaynor3.png doc/en/img/keleshev.png doc/en/img/pullrequest.png doc/en/img/pylib.png doc/en/img/pytest1.png doc/en/img/pytest1favi.ico doc/en/img/theuni.png doc/en/proposals/parametrize_with_fixtures.rst extra/get_issues.py extra/setup-py.test/setup.py scripts/publish_gh_release_notes.py scripts/release.minor.rst scripts/release.patch.rst scripts/release.py scripts/retry.cmd scripts/setup-coverage-vars.bat scripts/upload-coverage.bat src/pytest.py src/_pytest/__init__.py src/_pytest/_argcomplete.py src/_pytest/_version.py src/_pytest/cacheprovider.py src/_pytest/capture.py src/_pytest/compat.py src/_pytest/debugging.py src/_pytest/deprecated.py src/_pytest/doctest.py src/_pytest/fixtures.py src/_pytest/freeze_support.py src/_pytest/helpconfig.py src/_pytest/hookspec.py src/_pytest/junitxml.py src/_pytest/logging.py src/_pytest/main.py src/_pytest/monkeypatch.py src/_pytest/nodes.py src/_pytest/nose.py src/_pytest/outcomes.py src/_pytest/pastebin.py src/_pytest/pathlib.py src/_pytest/pytester.py src/_pytest/python.py src/_pytest/python_api.py src/_pytest/recwarn.py src/_pytest/reports.py src/_pytest/resultlog.py src/_pytest/runner.py src/_pytest/setuponly.py src/_pytest/setupplan.py src/_pytest/skipping.py src/_pytest/stepwise.py src/_pytest/terminal.py src/_pytest/tmpdir.py src/_pytest/unittest.py src/_pytest/warning_types.py src/_pytest/warnings.py src/_pytest/_code/__init__.py src/_pytest/_code/_py2traceback.py src/_pytest/_code/code.py src/_pytest/_code/source.py src/_pytest/_io/__init__.py src/_pytest/_io/saferepr.py src/_pytest/assertion/__init__.py src/_pytest/assertion/rewrite.py src/_pytest/assertion/truncate.py src/_pytest/assertion/util.py src/_pytest/config/__init__.py src/_pytest/config/argparsing.py src/_pytest/config/exceptions.py src/_pytest/config/findpaths.py src/_pytest/mark/__init__.py src/_pytest/mark/evaluate.py src/_pytest/mark/legacy.py src/_pytest/mark/structures.py src/pytest.egg-info/PKG-INFO src/pytest.egg-info/SOURCES.txt src/pytest.egg-info/dependency_links.txt src/pytest.egg-info/entry_points.txt src/pytest.egg-info/not-zip-safe src/pytest.egg-info/requires.txt src/pytest.egg-info/top_level.txt testing/acceptance_test.py testing/conftest.py testing/deprecated_test.py testing/test_argcomplete.py testing/test_assertion.py testing/test_assertrewrite.py testing/test_cacheprovider.py testing/test_capture.py testing/test_collection.py testing/test_compat.py testing/test_config.py testing/test_conftest.py testing/test_doctest.py testing/test_entry_points.py testing/test_helpconfig.py testing/test_junitxml.py testing/test_mark.py testing/test_modimport.py testing/test_monkeypatch.py testing/test_nodes.py testing/test_nose.py testing/test_parseopt.py testing/test_pastebin.py testing/test_pathlib.py testing/test_pdb.py testing/test_pluginmanager.py testing/test_pytester.py testing/test_recwarn.py testing/test_reports.py testing/test_resultlog.py testing/test_runner.py testing/test_runner_xunit.py testing/test_session.py testing/test_skipping.py testing/test_stepwise.py testing/test_terminal.py testing/test_tmpdir.py testing/test_unittest.py testing/test_warnings.py testing/code/test_code.py testing/code/test_excinfo.py testing/code/test_source.py testing/example_scripts/README.rst testing/example_scripts/issue_519.py testing/example_scripts/acceptance/fixture_mock_integration.py testing/example_scripts/collect/collect_init_tests/pytest.ini testing/example_scripts/collect/collect_init_tests/tests/__init__.py testing/example_scripts/collect/collect_init_tests/tests/test_foo.py testing/example_scripts/collect/package_infinite_recursion/conftest.py testing/example_scripts/collect/package_infinite_recursion/tests/__init__.py testing/example_scripts/collect/package_infinite_recursion/tests/test_basic.py testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py testing/example_scripts/config/collect_pytest_prefix/conftest.py testing/example_scripts/config/collect_pytest_prefix/test_foo.py testing/example_scripts/conftest_usageerror/conftest.py testing/example_scripts/dataclasses/test_compare_dataclasses.py testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py testing/example_scripts/deprecated/test_fixture_named_request.py testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py testing/example_scripts/fixtures/custom_item/conftest.py testing/example_scripts/fixtures/custom_item/foo/__init__.py testing/example_scripts/fixtures/custom_item/foo/test_foo.py testing/example_scripts/fixtures/fill_fixtures/test_detect_recursive_dependency_error.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_module_class.py testing/example_scripts/fixtures/fill_fixtures/test_funcarg_basic.py testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookup_classlevel.py testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookup_modulelevel.py testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookupfails.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/__init__.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/test_in_sub1.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/__init__.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/conftest.py testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/test_in_sub2.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/conftest.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/__init__.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/conftest.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/test_spam.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/conftest.py testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/test_extend_fixture_conftest_module.py testing/example_scripts/issue88_initial_file_multinodes/conftest.py testing/example_scripts/issue88_initial_file_multinodes/test_hello.py testing/example_scripts/marks/marks_considered_keywords/conftest.py testing/example_scripts/marks/marks_considered_keywords/test_marks_as_keywords.py testing/example_scripts/perf_examples/collect_stats/.gitignore testing/example_scripts/perf_examples/collect_stats/generate_folders.py testing/example_scripts/perf_examples/collect_stats/template_test.py testing/example_scripts/tmpdir/tmpdir_fixture.py testing/example_scripts/unittest/test_parametrized_fixture_error_message.py testing/example_scripts/unittest/test_setup_skip.py testing/example_scripts/unittest/test_setup_skip_class.py testing/example_scripts/unittest/test_setup_skip_module.py testing/example_scripts/warnings/test_group_warnings_by_message.py testing/examples/test_issue519.py testing/freeze/.gitignore testing/freeze/create_executable.py testing/freeze/runtests_script.py testing/freeze/tox_run.py testing/freeze/tests/test_doctest.txt testing/freeze/tests/test_trivial.py testing/io/test_saferepr.py testing/logging/test_fixture.py testing/logging/test_formatter.py testing/logging/test_reporting.py testing/python/approx.py testing/python/collect.py testing/python/fixtures.py testing/python/integration.py testing/python/metafunc.py testing/python/raises.py testing/python/setup_only.py testing/python/setup_plan.py testing/python/show_fixtures_per_test.pypytest-4.6.9/src/pytest.egg-info/requires.txt0000664000372000037200000000106313604173421022136 0ustar travistravis00000000000000py>=1.5.0 six>=1.10.0 packaging attrs>=17.4.0 atomicwrites>=1.0 pluggy<1.0,>=0.12 wcwidth [:python_version < "3.0"] funcsigs>=1.0 [:python_version < "3.6"] pathlib2>=2.2.0 [:python_version < "3.8"] importlib-metadata>=0.12 [:python_version <= "2.7"] more-itertools<6.0.0,>=4.0.0 [:python_version > "2.7"] more-itertools>=4.0.0 [:sys_platform == "win32" and python_version != "3.4"] colorama [:sys_platform == "win32" and python_version == "3.4"] colorama<=0.4.1 [testing] argcomplete hypothesis>=3.56 nose requests [testing:python_version == "2.7"] mock pytest-4.6.9/src/pytest.egg-info/entry_points.txt0000664000372000037200000000007613604173421023037 0ustar travistravis00000000000000[console_scripts] py.test = pytest:main pytest = pytest:main pytest-4.6.9/src/_pytest/0000775000372000037200000000000013604173421016203 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/doctest.py0000664000372000037200000004566513604173400020237 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ discover and run doctests in modules and test files.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import inspect import platform import sys import traceback import warnings from contextlib import contextmanager import pytest from _pytest._code.code import ExceptionInfo from _pytest._code.code import ReprFileLocation from _pytest._code.code import TerminalRepr from _pytest.compat import safe_getattr from _pytest.fixtures import FixtureRequest from _pytest.outcomes import Skipped from _pytest.warning_types import PytestWarning DOCTEST_REPORT_CHOICE_NONE = "none" DOCTEST_REPORT_CHOICE_CDIFF = "cdiff" DOCTEST_REPORT_CHOICE_NDIFF = "ndiff" DOCTEST_REPORT_CHOICE_UDIFF = "udiff" DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = "only_first_failure" DOCTEST_REPORT_CHOICES = ( DOCTEST_REPORT_CHOICE_NONE, DOCTEST_REPORT_CHOICE_CDIFF, DOCTEST_REPORT_CHOICE_NDIFF, DOCTEST_REPORT_CHOICE_UDIFF, DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE, ) # Lazy definition of runner class RUNNER_CLASS = None def pytest_addoption(parser): parser.addini( "doctest_optionflags", "option flags for doctests", type="args", default=["ELLIPSIS"], ) parser.addini( "doctest_encoding", "encoding used for doctest files", default="utf-8" ) group = parser.getgroup("collect") group.addoption( "--doctest-modules", action="store_true", default=False, help="run doctests in all .py modules", dest="doctestmodules", ) group.addoption( "--doctest-report", type=str.lower, default="udiff", help="choose another output format for diffs on doctest failure", choices=DOCTEST_REPORT_CHOICES, dest="doctestreport", ) group.addoption( "--doctest-glob", action="append", default=[], metavar="pat", help="doctests file matching pattern, default: test*.txt", dest="doctestglob", ) group.addoption( "--doctest-ignore-import-errors", action="store_true", default=False, help="ignore doctest ImportErrors", dest="doctest_ignore_import_errors", ) group.addoption( "--doctest-continue-on-failure", action="store_true", default=False, help="for a given doctest, continue to run after the first failure", dest="doctest_continue_on_failure", ) def pytest_collect_file(path, parent): config = parent.config if path.ext == ".py": if config.option.doctestmodules and not _is_setup_py(config, path, parent): return DoctestModule(path, parent) elif _is_doctest(config, path, parent): return DoctestTextfile(path, parent) def _is_setup_py(config, path, parent): if path.basename != "setup.py": return False contents = path.read() return "setuptools" in contents or "distutils" in contents def _is_doctest(config, path, parent): if path.ext in (".txt", ".rst") and parent.session.isinitpath(path): return True globs = config.getoption("doctestglob") or ["test*.txt"] for glob in globs: if path.check(fnmatch=glob): return True return False class ReprFailDoctest(TerminalRepr): def __init__(self, reprlocation_lines): # List of (reprlocation, lines) tuples self.reprlocation_lines = reprlocation_lines def toterminal(self, tw): for reprlocation, lines in self.reprlocation_lines: for line in lines: tw.line(line) reprlocation.toterminal(tw) class MultipleDoctestFailures(Exception): def __init__(self, failures): super(MultipleDoctestFailures, self).__init__() self.failures = failures def _init_runner_class(): import doctest class PytestDoctestRunner(doctest.DebugRunner): """ Runner to collect failures. Note that the out variable in this case is a list instead of a stdout-like object """ def __init__( self, checker=None, verbose=None, optionflags=0, continue_on_failure=True ): doctest.DebugRunner.__init__( self, checker=checker, verbose=verbose, optionflags=optionflags ) self.continue_on_failure = continue_on_failure def report_failure(self, out, test, example, got): failure = doctest.DocTestFailure(test, example, got) if self.continue_on_failure: out.append(failure) else: raise failure def report_unexpected_exception(self, out, test, example, exc_info): if isinstance(exc_info[1], Skipped): raise exc_info[1] failure = doctest.UnexpectedException(test, example, exc_info) if self.continue_on_failure: out.append(failure) else: raise failure return PytestDoctestRunner def _get_runner(checker=None, verbose=None, optionflags=0, continue_on_failure=True): # We need this in order to do a lazy import on doctest global RUNNER_CLASS if RUNNER_CLASS is None: RUNNER_CLASS = _init_runner_class() return RUNNER_CLASS( checker=checker, verbose=verbose, optionflags=optionflags, continue_on_failure=continue_on_failure, ) class DoctestItem(pytest.Item): def __init__(self, name, parent, runner=None, dtest=None): super(DoctestItem, self).__init__(name, parent) self.runner = runner self.dtest = dtest self.obj = None self.fixture_request = None def setup(self): if self.dtest is not None: self.fixture_request = _setup_fixtures(self) globs = dict(getfixture=self.fixture_request.getfixturevalue) for name, value in self.fixture_request.getfixturevalue( "doctest_namespace" ).items(): globs[name] = value self.dtest.globs.update(globs) def runtest(self): _check_all_skipped(self.dtest) self._disable_output_capturing_for_darwin() failures = [] self.runner.run(self.dtest, out=failures) if failures: raise MultipleDoctestFailures(failures) def _disable_output_capturing_for_darwin(self): """ Disable output capturing. Otherwise, stdout is lost to doctest (#985) """ if platform.system() != "Darwin": return capman = self.config.pluginmanager.getplugin("capturemanager") if capman: capman.suspend_global_capture(in_=True) out, err = capman.read_global_capture() sys.stdout.write(out) sys.stderr.write(err) def repr_failure(self, excinfo): import doctest failures = None if excinfo.errisinstance((doctest.DocTestFailure, doctest.UnexpectedException)): failures = [excinfo.value] elif excinfo.errisinstance(MultipleDoctestFailures): failures = excinfo.value.failures if failures is not None: reprlocation_lines = [] for failure in failures: example = failure.example test = failure.test filename = test.filename if test.lineno is None: lineno = None else: lineno = test.lineno + example.lineno + 1 message = type(failure).__name__ reprlocation = ReprFileLocation(filename, lineno, message) checker = _get_checker() report_choice = _get_report_choice( self.config.getoption("doctestreport") ) if lineno is not None: lines = failure.test.docstring.splitlines(False) # add line numbers to the left of the error message lines = [ "%03d %s" % (i + test.lineno + 1, x) for (i, x) in enumerate(lines) ] # trim docstring error lines to 10 lines = lines[max(example.lineno - 9, 0) : example.lineno + 1] else: lines = [ "EXAMPLE LOCATION UNKNOWN, not showing all tests of that example" ] indent = ">>>" for line in example.source.splitlines(): lines.append("??? %s %s" % (indent, line)) indent = "..." if isinstance(failure, doctest.DocTestFailure): lines += checker.output_difference( example, failure.got, report_choice ).split("\n") else: inner_excinfo = ExceptionInfo(failure.exc_info) lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)] lines += traceback.format_exception(*failure.exc_info) reprlocation_lines.append((reprlocation, lines)) return ReprFailDoctest(reprlocation_lines) else: return super(DoctestItem, self).repr_failure(excinfo) def reportinfo(self): return self.fspath, self.dtest.lineno, "[doctest] %s" % self.name def _get_flag_lookup(): import doctest return dict( DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1, DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE, NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE, ELLIPSIS=doctest.ELLIPSIS, IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL, COMPARISON_FLAGS=doctest.COMPARISON_FLAGS, ALLOW_UNICODE=_get_allow_unicode_flag(), ALLOW_BYTES=_get_allow_bytes_flag(), ) def get_optionflags(parent): optionflags_str = parent.config.getini("doctest_optionflags") flag_lookup_table = _get_flag_lookup() flag_acc = 0 for flag in optionflags_str: flag_acc |= flag_lookup_table[flag] return flag_acc def _get_continue_on_failure(config): continue_on_failure = config.getvalue("doctest_continue_on_failure") if continue_on_failure: # We need to turn off this if we use pdb since we should stop at # the first failure if config.getvalue("usepdb"): continue_on_failure = False return continue_on_failure class DoctestTextfile(pytest.Module): obj = None def collect(self): import doctest # inspired by doctest.testfile; ideally we would use it directly, # but it doesn't support passing a custom checker encoding = self.config.getini("doctest_encoding") text = self.fspath.read_text(encoding) filename = str(self.fspath) name = self.fspath.basename globs = {"__name__": "__main__"} optionflags = get_optionflags(self) runner = _get_runner( verbose=0, optionflags=optionflags, checker=_get_checker(), continue_on_failure=_get_continue_on_failure(self.config), ) _fix_spoof_python2(runner, encoding) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) if test.examples: yield DoctestItem(test.name, self, runner, test) def _check_all_skipped(test): """raises pytest.skip() if all examples in the given DocTest have the SKIP option set. """ import doctest all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples) if all_skipped: pytest.skip("all tests skipped by +SKIP option") def _is_mocked(obj): """ returns if a object is possibly a mock object by checking the existence of a highly improbable attribute """ return ( safe_getattr(obj, "pytest_mock_example_attribute_that_shouldnt_exist", None) is not None ) @contextmanager def _patch_unwrap_mock_aware(): """ contextmanager which replaces ``inspect.unwrap`` with a version that's aware of mock objects and doesn't recurse on them """ real_unwrap = getattr(inspect, "unwrap", None) if real_unwrap is None: yield else: def _mock_aware_unwrap(obj, stop=None): try: if stop is None or stop is _is_mocked: return real_unwrap(obj, stop=_is_mocked) return real_unwrap(obj, stop=lambda obj: _is_mocked(obj) or stop(obj)) except Exception as e: warnings.warn( "Got %r when unwrapping %r. This is usually caused " "by a violation of Python's object protocol; see e.g. " "https://github.com/pytest-dev/pytest/issues/5080" % (e, obj), PytestWarning, ) raise inspect.unwrap = _mock_aware_unwrap try: yield finally: inspect.unwrap = real_unwrap class DoctestModule(pytest.Module): def collect(self): import doctest class MockAwareDocTestFinder(doctest.DocTestFinder): """ a hackish doctest finder that overrides stdlib internals to fix a stdlib bug https://github.com/pytest-dev/pytest/issues/3456 https://bugs.python.org/issue25532 """ def _find(self, tests, obj, name, module, source_lines, globs, seen): if _is_mocked(obj): return with _patch_unwrap_mock_aware(): doctest.DocTestFinder._find( self, tests, obj, name, module, source_lines, globs, seen ) if self.fspath.basename == "conftest.py": module = self.config.pluginmanager._importconftest(self.fspath) else: try: module = self.fspath.pyimport() except ImportError: if self.config.getvalue("doctest_ignore_import_errors"): pytest.skip("unable to import module %r" % self.fspath) else: raise # uses internal doctest module parsing mechanism finder = MockAwareDocTestFinder() optionflags = get_optionflags(self) runner = _get_runner( verbose=0, optionflags=optionflags, checker=_get_checker(), continue_on_failure=_get_continue_on_failure(self.config), ) for test in finder.find(module, module.__name__): if test.examples: # skip empty doctests yield DoctestItem(test.name, self, runner, test) def _setup_fixtures(doctest_item): """ Used by DoctestTextfile and DoctestItem to setup fixture information. """ def func(): pass doctest_item.funcargs = {} fm = doctest_item.session._fixturemanager doctest_item._fixtureinfo = fm.getfixtureinfo( node=doctest_item, func=func, cls=None, funcargs=False ) fixture_request = FixtureRequest(doctest_item) fixture_request._fillfixtures() return fixture_request def _get_checker(): """ Returns a doctest.OutputChecker subclass that takes in account the ALLOW_UNICODE option to ignore u'' prefixes in strings and ALLOW_BYTES to strip b'' prefixes. Useful when the same doctest should run in Python 2 and Python 3. An inner class is used to avoid importing "doctest" at the module level. """ if hasattr(_get_checker, "LiteralsOutputChecker"): return _get_checker.LiteralsOutputChecker() import doctest import re class LiteralsOutputChecker(doctest.OutputChecker): """ Copied from doctest_nose_plugin.py from the nltk project: https://github.com/nltk/nltk Further extended to also support byte literals. """ _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE) def check_output(self, want, got, optionflags): res = doctest.OutputChecker.check_output(self, want, got, optionflags) if res: return True allow_unicode = optionflags & _get_allow_unicode_flag() allow_bytes = optionflags & _get_allow_bytes_flag() if not allow_unicode and not allow_bytes: return False else: # pragma: no cover def remove_prefixes(regex, txt): return re.sub(regex, r"\1\2", txt) if allow_unicode: want = remove_prefixes(self._unicode_literal_re, want) got = remove_prefixes(self._unicode_literal_re, got) if allow_bytes: want = remove_prefixes(self._bytes_literal_re, want) got = remove_prefixes(self._bytes_literal_re, got) res = doctest.OutputChecker.check_output(self, want, got, optionflags) return res _get_checker.LiteralsOutputChecker = LiteralsOutputChecker return _get_checker.LiteralsOutputChecker() def _get_allow_unicode_flag(): """ Registers and returns the ALLOW_UNICODE flag. """ import doctest return doctest.register_optionflag("ALLOW_UNICODE") def _get_allow_bytes_flag(): """ Registers and returns the ALLOW_BYTES flag. """ import doctest return doctest.register_optionflag("ALLOW_BYTES") def _get_report_choice(key): """ This function returns the actual `doctest` module flag value, we want to do it as late as possible to avoid importing `doctest` and all its dependencies when parsing options, as it adds overhead and breaks tests. """ import doctest return { DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF, DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF, DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF, DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE, DOCTEST_REPORT_CHOICE_NONE: 0, }[key] def _fix_spoof_python2(runner, encoding): """ Installs a "SpoofOut" into the given DebugRunner so it properly deals with unicode output. This should patch only doctests for text files because they don't have a way to declare their encoding. Doctests in docstrings from Python modules don't have the same problem given that Python already decoded the strings. This fixes the problem related in issue #2434. """ from _pytest.compat import _PY2 if not _PY2: return from doctest import _SpoofOut class UnicodeSpoof(_SpoofOut): def getvalue(self): result = _SpoofOut.getvalue(self) if encoding and isinstance(result, bytes): result = result.decode(encoding) return result runner._fakeout = UnicodeSpoof() @pytest.fixture(scope="session") def doctest_namespace(): """ Fixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. """ return dict() pytest-4.6.9/src/_pytest/recwarn.py0000664000372000037200000002220313604173400020212 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ recording warnings during test function execution. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import inspect import re import sys import warnings import six import _pytest._code from _pytest.deprecated import PYTEST_WARNS_UNKNOWN_KWARGS from _pytest.deprecated import WARNS_EXEC from _pytest.fixtures import yield_fixture from _pytest.outcomes import fail @yield_fixture def recwarn(): """Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. See http://docs.python.org/library/warnings.html for information on warning categories. """ wrec = WarningsRecorder() with wrec: warnings.simplefilter("default") yield wrec def deprecated_call(func=None, *args, **kwargs): """context manager that can be used to ensure a block of code triggers a ``DeprecationWarning`` or ``PendingDeprecationWarning``:: >>> import warnings >>> def api_call_v2(): ... warnings.warn('use v3 of this api', DeprecationWarning) ... return 200 >>> with deprecated_call(): ... assert api_call_v2() == 200 ``deprecated_call`` can also be used by passing a function and ``*args`` and ``*kwargs``, in which case it will ensure calling ``func(*args, **kwargs)`` produces one of the warnings types above. """ __tracebackhide__ = True if func is not None: args = (func,) + args return warns((DeprecationWarning, PendingDeprecationWarning), *args, **kwargs) def warns(expected_warning, *args, **kwargs): r"""Assert that code raises a particular class of warning. Specifically, the parameter ``expected_warning`` can be a warning class or sequence of warning classes, and the inside the ``with`` block must issue a warning of that class or classes. This helper produces a list of :class:`warnings.WarningMessage` objects, one for each warning raised. This function can be used as a context manager, or any of the other ways ``pytest.raises`` can be used:: >>> with warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning) In the context manager form you may use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with warns(UserWarning, match='must be 0 or None'): ... warnings.warn("value must be 0 or None", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("value must be 42", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("this is not here", UserWarning) Traceback (most recent call last): ... Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted... """ __tracebackhide__ = True if not args: match_expr = kwargs.pop("match", None) if kwargs: warnings.warn( PYTEST_WARNS_UNKNOWN_KWARGS.format(args=sorted(kwargs)), stacklevel=2 ) return WarningsChecker(expected_warning, match_expr=match_expr) elif isinstance(args[0], str): warnings.warn(WARNS_EXEC, stacklevel=2) (code,) = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) with WarningsChecker(expected_warning): code = _pytest._code.Source(code).compile() exec(code, frame.f_globals, loc) else: func = args[0] with WarningsChecker(expected_warning): return func(*args[1:], **kwargs) class WarningsRecorder(warnings.catch_warnings): """A context manager to record raised warnings. Adapted from `warnings.catch_warnings`. """ def __init__(self): super(WarningsRecorder, self).__init__(record=True) self._entered = False self._list = [] @property def list(self): """The list of recorded warnings.""" return self._list def __getitem__(self, i): """Get a recorded warning by index.""" return self._list[i] def __iter__(self): """Iterate through the recorded warnings.""" return iter(self._list) def __len__(self): """The number of recorded warnings.""" return len(self._list) def pop(self, cls=Warning): """Pop the first recorded warning, raise exception if not exists.""" for i, w in enumerate(self._list): if issubclass(w.category, cls): return self._list.pop(i) __tracebackhide__ = True raise AssertionError("%r not found in warning list" % cls) def clear(self): """Clear the list of recorded warnings.""" self._list[:] = [] def __enter__(self): if self._entered: __tracebackhide__ = True raise RuntimeError("Cannot enter %r twice" % self) self._list = super(WarningsRecorder, self).__enter__() warnings.simplefilter("always") # python3 keeps track of a "filter version", when the filters are # updated previously seen warnings can be re-warned. python2 has no # concept of this so we must reset the warnings registry manually. # trivial patching of `warnings.warn` seems to be enough somehow? if six.PY2: def warn(message, category=None, stacklevel=1): # duplicate the stdlib logic due to # bad handing in the c version of warnings if isinstance(message, Warning): category = message.__class__ # Check category argument if category is None: category = UserWarning assert issubclass(category, Warning) # emulate resetting the warn registry f_globals = sys._getframe(stacklevel).f_globals if "__warningregistry__" in f_globals: orig = f_globals["__warningregistry__"] f_globals["__warningregistry__"] = None try: return self._saved_warn(message, category, stacklevel + 1) finally: f_globals["__warningregistry__"] = orig else: return self._saved_warn(message, category, stacklevel + 1) warnings.warn, self._saved_warn = warn, warnings.warn return self def __exit__(self, *exc_info): if not self._entered: __tracebackhide__ = True raise RuntimeError("Cannot exit %r without entering first" % self) # see above where `self._saved_warn` is assigned if six.PY2: warnings.warn = self._saved_warn super(WarningsRecorder, self).__exit__(*exc_info) # Built-in catch_warnings does not reset entered state so we do it # manually here for this context manager to become reusable. self._entered = False class WarningsChecker(WarningsRecorder): def __init__(self, expected_warning=None, match_expr=None): super(WarningsChecker, self).__init__() msg = "exceptions must be old-style classes or derived from Warning, not %s" if isinstance(expected_warning, tuple): for exc in expected_warning: if not inspect.isclass(exc): raise TypeError(msg % type(exc)) elif inspect.isclass(expected_warning): expected_warning = (expected_warning,) elif expected_warning is not None: raise TypeError(msg % type(expected_warning)) self.expected_warning = expected_warning self.match_expr = match_expr def __exit__(self, *exc_info): super(WarningsChecker, self).__exit__(*exc_info) __tracebackhide__ = True # only check if we're not currently handling an exception if all(a is None for a in exc_info): if self.expected_warning is not None: if not any(issubclass(r.category, self.expected_warning) for r in self): __tracebackhide__ = True fail( "DID NOT WARN. No warnings of type {} was emitted. " "The list of emitted warnings is: {}.".format( self.expected_warning, [each.message for each in self] ) ) elif self.match_expr is not None: for r in self: if issubclass(r.category, self.expected_warning): if re.compile(self.match_expr).search(str(r.message)): break else: fail( "DID NOT WARN. No warnings of type {} matching" " ('{}') was emitted. The list of emitted warnings" " is: {}.".format( self.expected_warning, self.match_expr, [each.message for each in self], ) ) pytest-4.6.9/src/_pytest/pytester.py0000664000372000037200000013477613604173400020453 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """(disabled by default) support for testing pytest and pytest plugins.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import codecs import gc import os import platform import re import subprocess import sys import time import traceback from fnmatch import fnmatch from weakref import WeakKeyDictionary import py import six import pytest from _pytest._code import Source from _pytest._io.saferepr import saferepr from _pytest.assertion.rewrite import AssertionRewritingHook from _pytest.capture import MultiCapture from _pytest.capture import SysCapture from _pytest.compat import safe_str from _pytest.compat import Sequence from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_OK from _pytest.main import Session from _pytest.monkeypatch import MonkeyPatch from _pytest.pathlib import Path IGNORE_PAM = [ # filenames added when obtaining details about the current user u"/var/lib/sss/mc/passwd" ] def pytest_addoption(parser): parser.addoption( "--lsof", action="store_true", dest="lsof", default=False, help="run FD checks if lsof is available", ) parser.addoption( "--runpytest", default="inprocess", dest="runpytest", choices=("inprocess", "subprocess"), help=( "run pytest sub runs in tests using an 'inprocess' " "or 'subprocess' (python -m main) method" ), ) parser.addini( "pytester_example_dir", help="directory to take the pytester example files from" ) def pytest_configure(config): if config.getvalue("lsof"): checker = LsofFdLeakChecker() if checker.matching_platform(): config.pluginmanager.register(checker) config.addinivalue_line( "markers", "pytester_example_path(*path_segments): join the given path " "segments to `pytester_example_dir` for this test.", ) def raise_on_kwargs(kwargs): __tracebackhide__ = True if kwargs: # pragma: no branch raise TypeError( "Unexpected keyword arguments: {}".format(", ".join(sorted(kwargs))) ) class LsofFdLeakChecker(object): def get_open_files(self): out = self._exec_lsof() open_files = self._parse_lsof_output(out) return open_files def _exec_lsof(self): pid = os.getpid() # py3: use subprocess.DEVNULL directly. with open(os.devnull, "wb") as devnull: return subprocess.check_output( ("lsof", "-Ffn0", "-p", str(pid)), stderr=devnull ).decode() def _parse_lsof_output(self, out): def isopen(line): return line.startswith("f") and ( "deleted" not in line and "mem" not in line and "txt" not in line and "cwd" not in line ) open_files = [] for line in out.split("\n"): if isopen(line): fields = line.split("\0") fd = fields[0][1:] filename = fields[1][1:] if filename in IGNORE_PAM: continue if filename.startswith("/"): open_files.append((fd, filename)) return open_files def matching_platform(self): try: subprocess.check_output(("lsof", "-v")) except (OSError, subprocess.CalledProcessError): return False else: return True @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_protocol(self, item): lines1 = self.get_open_files() yield if hasattr(sys, "pypy_version_info"): gc.collect() lines2 = self.get_open_files() new_fds = {t[0] for t in lines2} - {t[0] for t in lines1} leaked_files = [t for t in lines2 if t[0] in new_fds] if leaked_files: error = [] error.append("***** %s FD leakage detected" % len(leaked_files)) error.extend([str(f) for f in leaked_files]) error.append("*** Before:") error.extend([str(f) for f in lines1]) error.append("*** After:") error.extend([str(f) for f in lines2]) error.append(error[0]) error.append("*** function %s:%s: %s " % item.location) error.append("See issue #2366") item.warn(pytest.PytestWarning("\n".join(error))) # used at least by pytest-xdist plugin @pytest.fixture def _pytest(request): """Return a helper which offers a gethookrecorder(hook) method which returns a HookRecorder instance which helps to make assertions about called hooks. """ return PytestArg(request) class PytestArg(object): def __init__(self, request): self.request = request def gethookrecorder(self, hook): hookrecorder = HookRecorder(hook._pm) self.request.addfinalizer(hookrecorder.finish_recording) return hookrecorder def get_public_names(values): """Only return names from iterator values without a leading underscore.""" return [x for x in values if x[0] != "_"] class ParsedCall(object): def __init__(self, name, kwargs): self.__dict__.update(kwargs) self._name = name def __repr__(self): d = self.__dict__.copy() del d["_name"] return "" % (self._name, d) class HookRecorder(object): """Record all hooks called in a plugin manager. This wraps all the hook calls in the plugin manager, recording each call before propagating the normal calls. """ def __init__(self, pluginmanager): self._pluginmanager = pluginmanager self.calls = [] def before(hook_name, hook_impls, kwargs): self.calls.append(ParsedCall(hook_name, kwargs)) def after(outcome, hook_name, hook_impls, kwargs): pass self._undo_wrapping = pluginmanager.add_hookcall_monitoring(before, after) def finish_recording(self): self._undo_wrapping() def getcalls(self, names): if isinstance(names, str): names = names.split() return [call for call in self.calls if call._name in names] def assert_contains(self, entries): __tracebackhide__ = True i = 0 entries = list(entries) backlocals = sys._getframe(1).f_locals while entries: name, check = entries.pop(0) for ind, call in enumerate(self.calls[i:]): if call._name == name: print("NAMEMATCH", name, call) if eval(check, backlocals, call.__dict__): print("CHECKERMATCH", repr(check), "->", call) else: print("NOCHECKERMATCH", repr(check), "-", call) continue i += ind + 1 break print("NONAMEMATCH", name, "with", call) else: pytest.fail("could not find %r check %r" % (name, check)) def popcall(self, name): __tracebackhide__ = True for i, call in enumerate(self.calls): if call._name == name: del self.calls[i] return call lines = ["could not find call %r, in:" % (name,)] lines.extend([" %s" % x for x in self.calls]) pytest.fail("\n".join(lines)) def getcall(self, name): values = self.getcalls(name) assert len(values) == 1, (name, values) return values[0] # functionality for test reports def getreports(self, names="pytest_runtest_logreport pytest_collectreport"): return [x.report for x in self.getcalls(names)] def matchreport( self, inamepart="", names="pytest_runtest_logreport pytest_collectreport", when=None, ): """return a testreport whose dotted import path matches""" values = [] for rep in self.getreports(names=names): if not when and rep.when != "call" and rep.passed: # setup/teardown passing reports - let's ignore those continue if when and rep.when != when: continue if not inamepart or inamepart in rep.nodeid.split("::"): values.append(rep) if not values: raise ValueError( "could not find test report matching %r: " "no test reports at all!" % (inamepart,) ) if len(values) > 1: raise ValueError( "found 2 or more testreports matching %r: %s" % (inamepart, values) ) return values[0] def getfailures(self, names="pytest_runtest_logreport pytest_collectreport"): return [rep for rep in self.getreports(names) if rep.failed] def getfailedcollections(self): return self.getfailures("pytest_collectreport") def listoutcomes(self): passed = [] skipped = [] failed = [] for rep in self.getreports("pytest_collectreport pytest_runtest_logreport"): if rep.passed: if rep.when == "call": passed.append(rep) elif rep.skipped: skipped.append(rep) else: assert rep.failed, "Unexpected outcome: {!r}".format(rep) failed.append(rep) return passed, skipped, failed def countoutcomes(self): return [len(x) for x in self.listoutcomes()] def assertoutcome(self, passed=0, skipped=0, failed=0): realpassed, realskipped, realfailed = self.listoutcomes() assert passed == len(realpassed) assert skipped == len(realskipped) assert failed == len(realfailed) def clear(self): self.calls[:] = [] @pytest.fixture def linecomp(request): return LineComp() @pytest.fixture(name="LineMatcher") def LineMatcher_fixture(request): return LineMatcher @pytest.fixture def testdir(request, tmpdir_factory): return Testdir(request, tmpdir_factory) @pytest.fixture def _sys_snapshot(): snappaths = SysPathsSnapshot() snapmods = SysModulesSnapshot() yield snapmods.restore() snappaths.restore() @pytest.fixture def _config_for_test(): from _pytest.config import get_config config = get_config() yield config config._ensure_unconfigure() # cleanup, e.g. capman closing tmpfiles. rex_outcome = re.compile(r"(\d+) ([\w-]+)") class RunResult(object): """The result of running a command. Attributes: :ret: the return value :outlines: list of lines captured from stdout :errlines: list of lines captures from stderr :stdout: :py:class:`LineMatcher` of stdout, use ``stdout.str()`` to reconstruct stdout or the commonly used ``stdout.fnmatch_lines()`` method :stderr: :py:class:`LineMatcher` of stderr :duration: duration in seconds """ def __init__(self, ret, outlines, errlines, duration): self.ret = ret self.outlines = outlines self.errlines = errlines self.stdout = LineMatcher(outlines) self.stderr = LineMatcher(errlines) self.duration = duration def __repr__(self): return ( "" % (self.ret, len(self.stdout.lines), len(self.stderr.lines), self.duration) ) def parseoutcomes(self): """Return a dictionary of outcomestring->num from parsing the terminal output that the test process produced. """ for line in reversed(self.outlines): if "seconds" in line: outcomes = rex_outcome.findall(line) if outcomes: d = {} for num, cat in outcomes: d[cat] = int(num) return d raise ValueError("Pytest terminal report not found") def assert_outcomes( self, passed=0, skipped=0, failed=0, error=0, xpassed=0, xfailed=0 ): """Assert that the specified outcomes appear with the respective numbers (0 means it didn't occur) in the text output from a test run. """ d = self.parseoutcomes() obtained = { "passed": d.get("passed", 0), "skipped": d.get("skipped", 0), "failed": d.get("failed", 0), "error": d.get("error", 0), "xpassed": d.get("xpassed", 0), "xfailed": d.get("xfailed", 0), } expected = { "passed": passed, "skipped": skipped, "failed": failed, "error": error, "xpassed": xpassed, "xfailed": xfailed, } assert obtained == expected class CwdSnapshot(object): def __init__(self): self.__saved = os.getcwd() def restore(self): os.chdir(self.__saved) class SysModulesSnapshot(object): def __init__(self, preserve=None): self.__preserve = preserve self.__saved = dict(sys.modules) def restore(self): if self.__preserve: self.__saved.update( (k, m) for k, m in sys.modules.items() if self.__preserve(k) ) sys.modules.clear() sys.modules.update(self.__saved) class SysPathsSnapshot(object): def __init__(self): self.__saved = list(sys.path), list(sys.meta_path) def restore(self): sys.path[:], sys.meta_path[:] = self.__saved class Testdir(object): """Temporary test directory with tools to test/run pytest itself. This is based on the ``tmpdir`` fixture but provides a number of methods which aid with testing pytest itself. Unless :py:meth:`chdir` is used all methods will use :py:attr:`tmpdir` as their current working directory. Attributes: :tmpdir: The :py:class:`py.path.local` instance of the temporary directory. :plugins: A list of plugins to use with :py:meth:`parseconfig` and :py:meth:`runpytest`. Initially this is an empty list but plugins can be added to the list. The type of items to add to the list depends on the method using them so refer to them for details. """ CLOSE_STDIN = object class TimeoutExpired(Exception): pass def __init__(self, request, tmpdir_factory): self.request = request self._mod_collections = WeakKeyDictionary() name = request.function.__name__ self.tmpdir = tmpdir_factory.mktemp(name, numbered=True) self.test_tmproot = tmpdir_factory.mktemp("tmp-" + name, numbered=True) self.plugins = [] self._cwd_snapshot = CwdSnapshot() self._sys_path_snapshot = SysPathsSnapshot() self._sys_modules_snapshot = self.__take_sys_modules_snapshot() self.chdir() self.request.addfinalizer(self.finalize) method = self.request.config.getoption("--runpytest") if method == "inprocess": self._runpytest_method = self.runpytest_inprocess elif method == "subprocess": self._runpytest_method = self.runpytest_subprocess mp = self.monkeypatch = MonkeyPatch() mp.setenv("PYTEST_DEBUG_TEMPROOT", str(self.test_tmproot)) # Ensure no unexpected caching via tox. mp.delenv("TOX_ENV_DIR", raising=False) # Discard outer pytest options. mp.delenv("PYTEST_ADDOPTS", raising=False) # Environment (updates) for inner runs. tmphome = str(self.tmpdir) self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome} def __repr__(self): return "" % (self.tmpdir,) def __str__(self): return str(self.tmpdir) def finalize(self): """Clean up global state artifacts. Some methods modify the global interpreter state and this tries to clean this up. It does not remove the temporary directory however so it can be looked at after the test run has finished. """ self._sys_modules_snapshot.restore() self._sys_path_snapshot.restore() self._cwd_snapshot.restore() self.monkeypatch.undo() def __take_sys_modules_snapshot(self): # some zope modules used by twisted-related tests keep internal state # and can't be deleted; we had some trouble in the past with # `zope.interface` for example def preserve_module(name): return name.startswith("zope") return SysModulesSnapshot(preserve=preserve_module) def make_hook_recorder(self, pluginmanager): """Create a new :py:class:`HookRecorder` for a PluginManager.""" pluginmanager.reprec = reprec = HookRecorder(pluginmanager) self.request.addfinalizer(reprec.finish_recording) return reprec def chdir(self): """Cd into the temporary directory. This is done automatically upon instantiation. """ self.tmpdir.chdir() def _makefile(self, ext, args, kwargs, encoding="utf-8"): items = list(kwargs.items()) def to_text(s): return s.decode(encoding) if isinstance(s, bytes) else six.text_type(s) if args: source = u"\n".join(to_text(x) for x in args) basename = self.request.function.__name__ items.insert(0, (basename, source)) ret = None for basename, value in items: p = self.tmpdir.join(basename).new(ext=ext) p.dirpath().ensure_dir() source = Source(value) source = u"\n".join(to_text(line) for line in source.lines) p.write(source.strip().encode(encoding), "wb") if ret is None: ret = p return ret def makefile(self, ext, *args, **kwargs): r"""Create new file(s) in the testdir. :param str ext: The extension the file(s) should use, including the dot, e.g. `.py`. :param list[str] args: All args will be treated as strings and joined using newlines. The result will be written as contents to the file. The name of the file will be based on the test function requesting this fixture. :param kwargs: Each keyword is the name of a file, while the value of it will be written as contents of the file. Examples: .. code-block:: python testdir.makefile(".txt", "line1", "line2") testdir.makefile(".ini", pytest="[pytest]\naddopts=-rs\n") """ return self._makefile(ext, args, kwargs) def makeconftest(self, source): """Write a contest.py file with 'source' as contents.""" return self.makepyfile(conftest=source) def makeini(self, source): """Write a tox.ini file with 'source' as contents.""" return self.makefile(".ini", tox=source) def getinicfg(self, source): """Return the pytest section from the tox.ini config file.""" p = self.makeini(source) return py.iniconfig.IniConfig(p)["pytest"] def makepyfile(self, *args, **kwargs): """Shortcut for .makefile() with a .py extension.""" return self._makefile(".py", args, kwargs) def maketxtfile(self, *args, **kwargs): """Shortcut for .makefile() with a .txt extension.""" return self._makefile(".txt", args, kwargs) def syspathinsert(self, path=None): """Prepend a directory to sys.path, defaults to :py:attr:`tmpdir`. This is undone automatically when this object dies at the end of each test. """ if path is None: path = self.tmpdir self.monkeypatch.syspath_prepend(str(path)) def mkdir(self, name): """Create a new (sub)directory.""" return self.tmpdir.mkdir(name) def mkpydir(self, name): """Create a new python package. This creates a (sub)directory with an empty ``__init__.py`` file so it gets recognised as a python package. """ p = self.mkdir(name) p.ensure("__init__.py") return p def copy_example(self, name=None): import warnings from _pytest.warning_types import PYTESTER_COPY_EXAMPLE warnings.warn(PYTESTER_COPY_EXAMPLE, stacklevel=2) example_dir = self.request.config.getini("pytester_example_dir") if example_dir is None: raise ValueError("pytester_example_dir is unset, can't copy examples") example_dir = self.request.config.rootdir.join(example_dir) for extra_element in self.request.node.iter_markers("pytester_example_path"): assert extra_element.args example_dir = example_dir.join(*extra_element.args) if name is None: func_name = self.request.function.__name__ maybe_dir = example_dir / func_name maybe_file = example_dir / (func_name + ".py") if maybe_dir.isdir(): example_path = maybe_dir elif maybe_file.isfile(): example_path = maybe_file else: raise LookupError( "{} cant be found as module or package in {}".format( func_name, example_dir.bestrelpath(self.request.config.rootdir) ) ) else: example_path = example_dir.join(name) if example_path.isdir() and not example_path.join("__init__.py").isfile(): example_path.copy(self.tmpdir) return self.tmpdir elif example_path.isfile(): result = self.tmpdir.join(example_path.basename) example_path.copy(result) return result else: raise LookupError( 'example "{}" is not found as a file or directory'.format(example_path) ) Session = Session def getnode(self, config, arg): """Return the collection node of a file. :param config: :py:class:`_pytest.config.Config` instance, see :py:meth:`parseconfig` and :py:meth:`parseconfigure` to create the configuration :param arg: a :py:class:`py.path.local` instance of the file """ session = Session(config) assert "::" not in str(arg) p = py.path.local(arg) config.hook.pytest_sessionstart(session=session) res = session.perform_collect([str(p)], genitems=False)[0] config.hook.pytest_sessionfinish(session=session, exitstatus=EXIT_OK) return res def getpathnode(self, path): """Return the collection node of a file. This is like :py:meth:`getnode` but uses :py:meth:`parseconfigure` to create the (configured) pytest Config instance. :param path: a :py:class:`py.path.local` instance of the file """ config = self.parseconfigure(path) session = Session(config) x = session.fspath.bestrelpath(path) config.hook.pytest_sessionstart(session=session) res = session.perform_collect([x], genitems=False)[0] config.hook.pytest_sessionfinish(session=session, exitstatus=EXIT_OK) return res def genitems(self, colitems): """Generate all test items from a collection node. This recurses into the collection node and returns a list of all the test items contained within. """ session = colitems[0].session result = [] for colitem in colitems: result.extend(session.genitems(colitem)) return result def runitem(self, source): """Run the "test_func" Item. The calling test instance (class containing the test method) must provide a ``.getrunner()`` method which should return a runner which can run the test protocol for a single item, e.g. :py:func:`_pytest.runner.runtestprotocol`. """ # used from runner functional tests item = self.getitem(source) # the test class where we are called from wants to provide the runner testclassinstance = self.request.instance runner = testclassinstance.getrunner() return runner(item) def inline_runsource(self, source, *cmdlineargs): """Run a test module in process using ``pytest.main()``. This run writes "source" into a temporary file and runs ``pytest.main()`` on it, returning a :py:class:`HookRecorder` instance for the result. :param source: the source code of the test module :param cmdlineargs: any extra command line arguments to use :return: :py:class:`HookRecorder` instance of the result """ p = self.makepyfile(source) values = list(cmdlineargs) + [p] return self.inline_run(*values) def inline_genitems(self, *args): """Run ``pytest.main(['--collectonly'])`` in-process. Runs the :py:func:`pytest.main` function to run all of pytest inside the test process itself like :py:meth:`inline_run`, but returns a tuple of the collected items and a :py:class:`HookRecorder` instance. """ rec = self.inline_run("--collect-only", *args) items = [x.item for x in rec.getcalls("pytest_itemcollected")] return items, rec def inline_run(self, *args, **kwargs): """Run ``pytest.main()`` in-process, returning a HookRecorder. Runs the :py:func:`pytest.main` function to run all of pytest inside the test process itself. This means it can return a :py:class:`HookRecorder` instance which gives more detailed results from that run than can be done by matching stdout/stderr from :py:meth:`runpytest`. :param args: command line arguments to pass to :py:func:`pytest.main` :param plugins: (keyword-only) extra plugin instances the ``pytest.main()`` instance should use :return: a :py:class:`HookRecorder` instance """ plugins = kwargs.pop("plugins", []) no_reraise_ctrlc = kwargs.pop("no_reraise_ctrlc", None) raise_on_kwargs(kwargs) finalizers = [] try: # Do not load user config (during runs only). mp_run = MonkeyPatch() for k, v in self._env_run_update.items(): mp_run.setenv(k, v) finalizers.append(mp_run.undo) # When running pytest inline any plugins active in the main test # process are already imported. So this disables the warning which # will trigger to say they can no longer be rewritten, which is # fine as they have already been rewritten. orig_warn = AssertionRewritingHook._warn_already_imported def revert_warn_already_imported(): AssertionRewritingHook._warn_already_imported = orig_warn finalizers.append(revert_warn_already_imported) AssertionRewritingHook._warn_already_imported = lambda *a: None # Any sys.module or sys.path changes done while running pytest # inline should be reverted after the test run completes to avoid # clashing with later inline tests run within the same pytest test, # e.g. just because they use matching test module names. finalizers.append(self.__take_sys_modules_snapshot().restore) finalizers.append(SysPathsSnapshot().restore) # Important note: # - our tests should not leave any other references/registrations # laying around other than possibly loaded test modules # referenced from sys.modules, as nothing will clean those up # automatically rec = [] class Collect(object): def pytest_configure(x, config): rec.append(self.make_hook_recorder(config.pluginmanager)) plugins.append(Collect()) ret = pytest.main(list(args), plugins=plugins) if len(rec) == 1: reprec = rec.pop() else: class reprec(object): pass reprec.ret = ret # typically we reraise keyboard interrupts from the child run # because it's our user requesting interruption of the testing if ret == EXIT_INTERRUPTED and not no_reraise_ctrlc: calls = reprec.getcalls("pytest_keyboard_interrupt") if calls and calls[-1].excinfo.type == KeyboardInterrupt: raise KeyboardInterrupt() return reprec finally: for finalizer in finalizers: finalizer() def runpytest_inprocess(self, *args, **kwargs): """Return result of running pytest in-process, providing a similar interface to what self.runpytest() provides. """ syspathinsert = kwargs.pop("syspathinsert", False) if syspathinsert: self.syspathinsert() now = time.time() capture = MultiCapture(Capture=SysCapture) capture.start_capturing() try: try: reprec = self.inline_run(*args, **kwargs) except SystemExit as e: class reprec(object): ret = e.args[0] except Exception: traceback.print_exc() class reprec(object): ret = 3 finally: out, err = capture.readouterr() capture.stop_capturing() sys.stdout.write(out) sys.stderr.write(err) res = RunResult(reprec.ret, out.split("\n"), err.split("\n"), time.time() - now) res.reprec = reprec return res def runpytest(self, *args, **kwargs): """Run pytest inline or in a subprocess, depending on the command line option "--runpytest" and return a :py:class:`RunResult`. """ args = self._ensure_basetemp(args) return self._runpytest_method(*args, **kwargs) def _ensure_basetemp(self, args): args = list(args) for x in args: if safe_str(x).startswith("--basetemp"): break else: args.append("--basetemp=%s" % self.tmpdir.dirpath("basetemp")) return args def parseconfig(self, *args): """Return a new pytest Config instance from given commandline args. This invokes the pytest bootstrapping code in _pytest.config to create a new :py:class:`_pytest.core.PluginManager` and call the pytest_cmdline_parse hook to create a new :py:class:`_pytest.config.Config` instance. If :py:attr:`plugins` has been populated they should be plugin modules to be registered with the PluginManager. """ args = self._ensure_basetemp(args) import _pytest.config config = _pytest.config._prepareconfig(args, self.plugins) # we don't know what the test will do with this half-setup config # object and thus we make sure it gets unconfigured properly in any # case (otherwise capturing could still be active, for example) self.request.addfinalizer(config._ensure_unconfigure) return config def parseconfigure(self, *args): """Return a new pytest configured Config instance. This returns a new :py:class:`_pytest.config.Config` instance like :py:meth:`parseconfig`, but also calls the pytest_configure hook. """ config = self.parseconfig(*args) config._do_configure() self.request.addfinalizer(config._ensure_unconfigure) return config def getitem(self, source, funcname="test_func"): """Return the test item for a test function. This writes the source to a python file and runs pytest's collection on the resulting module, returning the test item for the requested function name. :param source: the module source :param funcname: the name of the test function for which to return a test item """ items = self.getitems(source) for item in items: if item.name == funcname: return item assert 0, "%r item not found in module:\n%s\nitems: %s" % ( funcname, source, items, ) def getitems(self, source): """Return all test items collected from the module. This writes the source to a python file and runs pytest's collection on the resulting module, returning all test items contained within. """ modcol = self.getmodulecol(source) return self.genitems([modcol]) def getmodulecol(self, source, configargs=(), withinit=False): """Return the module collection node for ``source``. This writes ``source`` to a file using :py:meth:`makepyfile` and then runs the pytest collection on it, returning the collection node for the test module. :param source: the source code of the module to collect :param configargs: any extra arguments to pass to :py:meth:`parseconfigure` :param withinit: whether to also write an ``__init__.py`` file to the same directory to ensure it is a package """ if isinstance(source, Path): path = self.tmpdir.join(str(source)) assert not withinit, "not supported for paths" else: kw = {self.request.function.__name__: Source(source).strip()} path = self.makepyfile(**kw) if withinit: self.makepyfile(__init__="#") self.config = config = self.parseconfigure(path, *configargs) return self.getnode(config, path) def collect_by_name(self, modcol, name): """Return the collection node for name from the module collection. This will search a module collection node for a collection node matching the given name. :param modcol: a module collection node; see :py:meth:`getmodulecol` :param name: the name of the node to return """ if modcol not in self._mod_collections: self._mod_collections[modcol] = list(modcol.collect()) for colitem in self._mod_collections[modcol]: if colitem.name == name: return colitem def popen( self, cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=CLOSE_STDIN, **kw ): """Invoke subprocess.Popen. This calls subprocess.Popen making sure the current working directory is in the PYTHONPATH. You probably want to use :py:meth:`run` instead. """ env = os.environ.copy() env["PYTHONPATH"] = os.pathsep.join( filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) ) env.update(self._env_run_update) kw["env"] = env if stdin is Testdir.CLOSE_STDIN: kw["stdin"] = subprocess.PIPE elif isinstance(stdin, bytes): kw["stdin"] = subprocess.PIPE else: kw["stdin"] = stdin popen = subprocess.Popen(cmdargs, stdout=stdout, stderr=stderr, **kw) if stdin is Testdir.CLOSE_STDIN: popen.stdin.close() elif isinstance(stdin, bytes): popen.stdin.write(stdin) return popen def run(self, *cmdargs, **kwargs): """Run a command with arguments. Run a process using subprocess.Popen saving the stdout and stderr. :param args: the sequence of arguments to pass to `subprocess.Popen()` :param timeout: the period in seconds after which to timeout and raise :py:class:`Testdir.TimeoutExpired` :param stdin: optional standard input. Bytes are being send, closing the pipe, otherwise it is passed through to ``popen``. Defaults to ``CLOSE_STDIN``, which translates to using a pipe (``subprocess.PIPE``) that gets closed. Returns a :py:class:`RunResult`. """ __tracebackhide__ = True timeout = kwargs.pop("timeout", None) stdin = kwargs.pop("stdin", Testdir.CLOSE_STDIN) raise_on_kwargs(kwargs) cmdargs = [ str(arg) if isinstance(arg, py.path.local) else arg for arg in cmdargs ] p1 = self.tmpdir.join("stdout") p2 = self.tmpdir.join("stderr") print("running:", *cmdargs) print(" in:", py.path.local()) f1 = codecs.open(str(p1), "w", encoding="utf8") f2 = codecs.open(str(p2), "w", encoding="utf8") try: now = time.time() popen = self.popen( cmdargs, stdin=stdin, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32"), ) if isinstance(stdin, bytes): popen.stdin.close() def handle_timeout(): __tracebackhide__ = True timeout_message = ( "{seconds} second timeout expired running:" " {command}".format(seconds=timeout, command=cmdargs) ) popen.kill() popen.wait() raise self.TimeoutExpired(timeout_message) if timeout is None: ret = popen.wait() elif not six.PY2: try: ret = popen.wait(timeout) except subprocess.TimeoutExpired: handle_timeout() else: end = time.time() + timeout resolution = min(0.1, timeout / 10) while True: ret = popen.poll() if ret is not None: break if time.time() > end: handle_timeout() time.sleep(resolution) finally: f1.close() f2.close() f1 = codecs.open(str(p1), "r", encoding="utf8") f2 = codecs.open(str(p2), "r", encoding="utf8") try: out = f1.read().splitlines() err = f2.read().splitlines() finally: f1.close() f2.close() self._dump_lines(out, sys.stdout) self._dump_lines(err, sys.stderr) return RunResult(ret, out, err, time.time() - now) def _dump_lines(self, lines, fp): try: for line in lines: print(line, file=fp) except UnicodeEncodeError: print("couldn't print to %s because of encoding" % (fp,)) def _getpytestargs(self): return sys.executable, "-mpytest" def runpython(self, script): """Run a python script using sys.executable as interpreter. Returns a :py:class:`RunResult`. """ return self.run(sys.executable, script) def runpython_c(self, command): """Run python -c "command", return a :py:class:`RunResult`.""" return self.run(sys.executable, "-c", command) def runpytest_subprocess(self, *args, **kwargs): """Run pytest as a subprocess with given arguments. Any plugins added to the :py:attr:`plugins` list will be added using the ``-p`` command line option. Additionally ``--basetemp`` is used to put any temporary files and directories in a numbered directory prefixed with "runpytest-" to not conflict with the normal numbered pytest location for temporary files and directories. :param args: the sequence of arguments to pass to the pytest subprocess :param timeout: the period in seconds after which to timeout and raise :py:class:`Testdir.TimeoutExpired` Returns a :py:class:`RunResult`. """ __tracebackhide__ = True timeout = kwargs.pop("timeout", None) raise_on_kwargs(kwargs) p = py.path.local.make_numbered_dir( prefix="runpytest-", keep=None, rootdir=self.tmpdir ) args = ("--basetemp=%s" % p,) + args plugins = [x for x in self.plugins if isinstance(x, str)] if plugins: args = ("-p", plugins[0]) + args args = self._getpytestargs() + args return self.run(*args, timeout=timeout) def spawn_pytest(self, string, expect_timeout=10.0): """Run pytest using pexpect. This makes sure to use the right pytest and sets up the temporary directory locations. The pexpect child is returned. """ basetemp = self.tmpdir.mkdir("temp-pexpect") invoke = " ".join(map(str, self._getpytestargs())) cmd = "%s --basetemp=%s %s" % (invoke, basetemp, string) return self.spawn(cmd, expect_timeout=expect_timeout) def spawn(self, cmd, expect_timeout=10.0): """Run a command using pexpect. The pexpect child is returned. """ pexpect = pytest.importorskip("pexpect", "3.0") if hasattr(sys, "pypy_version_info") and "64" in platform.machine(): pytest.skip("pypy-64 bit not supported") if sys.platform.startswith("freebsd"): pytest.xfail("pexpect does not work reliably on freebsd") logfile = self.tmpdir.join("spawn.out").open("wb") # Do not load user config. env = os.environ.copy() env.update(self._env_run_update) child = pexpect.spawn(cmd, logfile=logfile, env=env) self.request.addfinalizer(logfile.close) child.timeout = expect_timeout return child def getdecoded(out): try: return out.decode("utf-8") except UnicodeDecodeError: return "INTERNAL not-utf8-decodeable, truncated string:\n%s" % (saferepr(out),) class LineComp(object): def __init__(self): self.stringio = py.io.TextIO() def assert_contains_lines(self, lines2): """Assert that lines2 are contained (linearly) in lines1. Return a list of extralines found. """ __tracebackhide__ = True val = self.stringio.getvalue() self.stringio.truncate(0) self.stringio.seek(0) lines1 = val.split("\n") return LineMatcher(lines1).fnmatch_lines(lines2) class LineMatcher(object): """Flexible matching of text. This is a convenience class to test large texts like the output of commands. The constructor takes a list of lines without their trailing newlines, i.e. ``text.splitlines()``. """ def __init__(self, lines): self.lines = lines self._log_output = [] def str(self): """Return the entire original text.""" return "\n".join(self.lines) def _getlines(self, lines2): if isinstance(lines2, str): lines2 = Source(lines2) if isinstance(lines2, Source): lines2 = lines2.strip().lines return lines2 def fnmatch_lines_random(self, lines2): """Check lines exist in the output using in any order. Lines are checked using ``fnmatch.fnmatch``. The argument is a list of lines which have to occur in the output, in any order. """ self._match_lines_random(lines2, fnmatch) def re_match_lines_random(self, lines2): """Check lines exist in the output using ``re.match``, in any order. The argument is a list of lines which have to occur in the output, in any order. """ self._match_lines_random(lines2, lambda name, pat: re.match(pat, name)) def _match_lines_random(self, lines2, match_func): """Check lines exist in the output. The argument is a list of lines which have to occur in the output, in any order. Each line can contain glob whildcards. """ lines2 = self._getlines(lines2) for line in lines2: for x in self.lines: if line == x or match_func(x, line): self._log("matched: ", repr(line)) break else: self._log("line %r not found in output" % line) raise ValueError(self._log_text) def get_lines_after(self, fnline): """Return all lines following the given line in the text. The given line can contain glob wildcards. """ for i, line in enumerate(self.lines): if fnline == line or fnmatch(line, fnline): return self.lines[i + 1 :] raise ValueError("line %r not found in output" % fnline) def _log(self, *args): self._log_output.append(" ".join(str(x) for x in args)) @property def _log_text(self): return "\n".join(self._log_output) def fnmatch_lines(self, lines2): """Search captured text for matching lines using ``fnmatch.fnmatch``. The argument is a list of lines which have to match and can use glob wildcards. If they do not match a pytest.fail() is called. The matches and non-matches are also printed on stdout. """ __tracebackhide__ = True self._match_lines(lines2, fnmatch, "fnmatch") def re_match_lines(self, lines2): """Search captured text for matching lines using ``re.match``. The argument is a list of lines which have to match using ``re.match``. If they do not match a pytest.fail() is called. The matches and non-matches are also printed on stdout. """ __tracebackhide__ = True self._match_lines(lines2, lambda name, pat: re.match(pat, name), "re.match") def _match_lines(self, lines2, match_func, match_nickname): """Underlying implementation of ``fnmatch_lines`` and ``re_match_lines``. :param list[str] lines2: list of string patterns to match. The actual format depends on ``match_func`` :param match_func: a callable ``match_func(line, pattern)`` where line is the captured line from stdout/stderr and pattern is the matching pattern :param str match_nickname: the nickname for the match function that will be logged to stdout when a match occurs """ assert isinstance(lines2, Sequence) lines2 = self._getlines(lines2) lines1 = self.lines[:] nextline = None extralines = [] __tracebackhide__ = True for line in lines2: nomatchprinted = False while lines1: nextline = lines1.pop(0) if line == nextline: self._log("exact match:", repr(line)) break elif match_func(nextline, line): self._log("%s:" % match_nickname, repr(line)) self._log(" with:", repr(nextline)) break else: if not nomatchprinted: self._log("nomatch:", repr(line)) nomatchprinted = True self._log(" and:", repr(nextline)) extralines.append(nextline) else: self._log("remains unmatched: %r" % (line,)) pytest.fail(self._log_text) pytest-4.6.9/src/_pytest/deprecated.py0000664000372000037200000000764313604173400020664 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ This module contains deprecation messages and bits of code used elsewhere in the codebase that is planned to be removed in the next pytest release. Keeping it in a central location makes it easy to track what is deprecated and should be removed when the time comes. All constants defined in this module should be either PytestWarning instances or UnformattedWarning in case of warnings which need to format their messages. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from _pytest.warning_types import PytestDeprecationWarning from _pytest.warning_types import RemovedInPytest4Warning from _pytest.warning_types import UnformattedWarning YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored" FIXTURE_FUNCTION_CALL = ( 'Fixture "{name}" called directly. Fixtures are not meant to be called directly,\n' "but are created automatically when test functions request them as parameters.\n" "See https://docs.pytest.org/en/latest/fixture.html for more information about fixtures, and\n" "https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly about how to update your code." ) FIXTURE_NAMED_REQUEST = PytestDeprecationWarning( "'request' is a reserved name for fixtures and will raise an error in future versions" ) CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead." GETFUNCARGVALUE = RemovedInPytest4Warning( "getfuncargvalue is deprecated, use getfixturevalue" ) RAISES_MESSAGE_PARAMETER = PytestDeprecationWarning( "The 'message' parameter is deprecated.\n" "(did you mean to use `match='some regex'` to check the exception message?)\n" "Please see:\n" " https://docs.pytest.org/en/4.6-maintenance/deprecations.html#message-parameter-of-pytest-raises" ) RESULT_LOG = PytestDeprecationWarning( "--result-log is deprecated and scheduled for removal in pytest 5.0.\n" "See https://docs.pytest.org/en/latest/deprecations.html#result-log-result-log for more information." ) RAISES_EXEC = PytestDeprecationWarning( "raises(..., 'code(as_a_string)') is deprecated, use the context manager form or use `exec()` directly\n\n" "See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec" ) WARNS_EXEC = PytestDeprecationWarning( "warns(..., 'code(as_a_string)') is deprecated, use the context manager form or use `exec()` directly.\n\n" "See https://docs.pytest.org/en/latest/deprecations.html#raises-warns-exec" ) PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = ( "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported " "because it affects the entire directory tree in a non-explicit way.\n" " {}\n" "Please move it to a top level conftest file at the rootdir:\n" " {}\n" "For more information, visit:\n" " https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files" ) PYTEST_CONFIG_GLOBAL = PytestDeprecationWarning( "the `pytest.config` global is deprecated. Please use `request.config` " "or `pytest_configure` (if you're a pytest plugin) instead." ) PYTEST_ENSURETEMP = RemovedInPytest4Warning( "pytest/tmpdir_factory.ensuretemp is deprecated, \n" "please use the tmp_path fixture or tmp_path_factory.mktemp" ) PYTEST_LOGWARNING = PytestDeprecationWarning( "pytest_logwarning is deprecated, no longer being called, and will be removed soon\n" "please use pytest_warning_captured instead" ) PYTEST_WARNS_UNKNOWN_KWARGS = UnformattedWarning( PytestDeprecationWarning, "pytest.warns() got unexpected keyword arguments: {args!r}.\n" "This will be an error in future versions.", ) PYTEST_PARAM_UNKNOWN_KWARGS = UnformattedWarning( PytestDeprecationWarning, "pytest.param() got unexpected keyword arguments: {args!r}.\n" "This will be an error in future versions.", ) pytest-4.6.9/src/_pytest/freeze_support.py0000664000372000037200000000235713604173400021635 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Provides a function to report all internal modules for using freezing tools pytest """ from __future__ import absolute_import from __future__ import division from __future__ import print_function def freeze_includes(): """ Returns a list of module names used by pytest that should be included by cx_freeze. """ import py import _pytest result = list(_iter_all_modules(py)) result += list(_iter_all_modules(_pytest)) return result def _iter_all_modules(package, prefix=""): """ Iterates over the names of all modules that can be found in the given package, recursively. Example: _iter_all_modules(_pytest) -> ['_pytest.assertion.newinterpret', '_pytest.capture', '_pytest.core', ... ] """ import os import pkgutil if type(package) is not str: path, prefix = package.__path__[0], package.__name__ + "." else: path = package for _, name, is_package in pkgutil.iter_modules([path]): if is_package: for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."): yield prefix + m else: yield prefix + name pytest-4.6.9/src/_pytest/python.py0000664000372000037200000015372613604173400020111 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Python test discovery, setup and run of test functions. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import fnmatch import inspect import os import sys import warnings from functools import partial from textwrap import dedent import py import six import _pytest from _pytest import deprecated from _pytest import fixtures from _pytest import nodes from _pytest._code import filter_traceback from _pytest.compat import ascii_escaped from _pytest.compat import enum from _pytest.compat import get_default_arg_names from _pytest.compat import get_real_func from _pytest.compat import getfslineno from _pytest.compat import getimfunc from _pytest.compat import getlocation from _pytest.compat import is_generator from _pytest.compat import isclass from _pytest.compat import isfunction from _pytest.compat import NOTSET from _pytest.compat import REGEX_TYPE from _pytest.compat import safe_getattr from _pytest.compat import safe_isclass from _pytest.compat import safe_str from _pytest.compat import STRING_TYPES from _pytest.config import hookimpl from _pytest.main import FSHookProxy from _pytest.mark import MARK_GEN from _pytest.mark.structures import get_unpacked_marks from _pytest.mark.structures import normalize_mark_list from _pytest.outcomes import fail from _pytest.outcomes import skip from _pytest.pathlib import parts from _pytest.warning_types import PytestCollectionWarning from _pytest.warning_types import PytestUnhandledCoroutineWarning def pyobj_property(name): def get(self): node = self.getparent(getattr(__import__("pytest"), name)) if node is not None: return node.obj doc = "python %s object this node was collected from (can be None)." % ( name.lower(), ) return property(get, None, None, doc) def pytest_addoption(parser): group = parser.getgroup("general") group.addoption( "--fixtures", "--funcargs", action="store_true", dest="showfixtures", default=False, help="show available fixtures, sorted by plugin appearance " "(fixtures with leading '_' are only shown with '-v')", ) group.addoption( "--fixtures-per-test", action="store_true", dest="show_fixtures_per_test", default=False, help="show fixtures per test", ) parser.addini( "python_files", type="args", # NOTE: default is also used in AssertionRewritingHook. default=["test_*.py", "*_test.py"], help="glob-style file patterns for Python test module discovery", ) parser.addini( "python_classes", type="args", default=["Test"], help="prefixes or glob names for Python test class discovery", ) parser.addini( "python_functions", type="args", default=["test"], help="prefixes or glob names for Python test function and method discovery", ) parser.addini( "disable_test_id_escaping_and_forfeit_all_rights_to_community_support", type="bool", default=False, help="disable string escape non-ascii characters, might cause unwanted " "side effects(use at your own risk)", ) group.addoption( "--import-mode", default="prepend", choices=["prepend", "append"], dest="importmode", help="prepend/append to sys.path when importing test modules, " "default is to prepend.", ) def pytest_cmdline_main(config): if config.option.showfixtures: showfixtures(config) return 0 if config.option.show_fixtures_per_test: show_fixtures_per_test(config) return 0 def pytest_generate_tests(metafunc): # those alternative spellings are common - raise a specific error to alert # the user alt_spellings = ["parameterize", "parametrise", "parameterise"] for mark_name in alt_spellings: if metafunc.definition.get_closest_marker(mark_name): msg = "{0} has '{1}' mark, spelling should be 'parametrize'" fail(msg.format(metafunc.function.__name__, mark_name), pytrace=False) for marker in metafunc.definition.iter_markers(name="parametrize"): metafunc.parametrize(*marker.args, **marker.kwargs) 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 https://docs.pytest.org/en/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 " "https://docs.pytest.org/en/latest/fixture.html#usefixtures ", ) @hookimpl(trylast=True) def pytest_pyfunc_call(pyfuncitem): testfunction = pyfuncitem.obj iscoroutinefunction = getattr(inspect, "iscoroutinefunction", None) if iscoroutinefunction is not None and iscoroutinefunction(testfunction): msg = "Coroutine functions are not natively supported and have been skipped.\n" msg += "You need to install a suitable plugin for your async framework, for example:\n" msg += " - pytest-asyncio\n" msg += " - pytest-trio\n" msg += " - pytest-tornasync" warnings.warn(PytestUnhandledCoroutineWarning(msg.format(pyfuncitem.nodeid))) skip(msg="coroutine function and no async plugin installed (see warnings)") funcargs = pyfuncitem.funcargs testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} testfunction(**testargs) return True def pytest_collect_file(path, parent): ext = path.ext if ext == ".py": if not parent.session.isinitpath(path): if not path_matches_patterns( path, parent.config.getini("python_files") + ["__init__.py"] ): return ihook = parent.session.gethookproxy(path) return ihook.pytest_pycollect_makemodule(path=path, parent=parent) def path_matches_patterns(path, patterns): """Returns True if the given py.path.local matches one of the patterns in the list of globs given""" return any(path.fnmatch(pattern) for pattern in patterns) def pytest_pycollect_makemodule(path, parent): if path.basename == "__init__.py": return Package(path, parent) return Module(path, parent) @hookimpl(hookwrapper=True) def pytest_pycollect_makeitem(collector, name, obj): outcome = yield res = outcome.get_result() if res is not None: return # nothing was collected elsewhere, let's do it here if safe_isclass(obj): if collector.istestclass(obj, name): outcome.force_result(Class(name, parent=collector)) elif collector.istestfunction(obj, name): # mock seems to store unbound methods (issue473), normalize it obj = getattr(obj, "__func__", obj) # We need to try and unwrap the function if it's a functools.partial # or a funtools.wrapped. # We musn't if it's been wrapped with mock.patch (python 2 only) if not (isfunction(obj) or isfunction(get_real_func(obj))): filename, lineno = getfslineno(obj) warnings.warn_explicit( message=PytestCollectionWarning( "cannot collect %r because it is not a function." % name ), category=None, filename=str(filename), lineno=lineno + 1, ) elif getattr(obj, "__test__", True): if is_generator(obj): res = Function(name, parent=collector) reason = deprecated.YIELD_TESTS.format(name=name) res.add_marker(MARK_GEN.xfail(run=False, reason=reason)) res.warn(PytestCollectionWarning(reason)) else: res = list(collector._genfunctions(name, obj)) outcome.force_result(res) def pytest_make_parametrize_id(config, val, argname=None): return None class PyobjContext(object): module = pyobj_property("Module") cls = pyobj_property("Class") instance = pyobj_property("Instance") class PyobjMixin(PyobjContext): _ALLOW_MARKERS = True def __init__(self, *k, **kw): super(PyobjMixin, self).__init__(*k, **kw) @property def obj(self): """Underlying Python object.""" obj = getattr(self, "_obj", None) if obj is None: self._obj = obj = self._getobj() # XXX evil hack # used to avoid Instance collector marker duplication if self._ALLOW_MARKERS: self.own_markers.extend(get_unpacked_marks(self.obj)) return obj @obj.setter def obj(self, value): self._obj = value def _getobj(self): """Gets the underlying Python object. May be overwritten by subclasses.""" return getattr(self.parent.obj, self.name) def getmodpath(self, stopatmodule=True, includemodule=False): """ return python path relative to the containing module. """ chain = self.listchain() chain.reverse() parts = [] for node in chain: if isinstance(node, Instance): continue name = node.name if isinstance(node, Module): name = os.path.splitext(name)[0] if stopatmodule: if includemodule: parts.append(name) break parts.append(name) parts.reverse() s = ".".join(parts) return s.replace(".[", "[") def reportinfo(self): # XXX caching? obj = self.obj compat_co_firstlineno = getattr(obj, "compat_co_firstlineno", None) if isinstance(compat_co_firstlineno, int): # nose compatibility fspath = sys.modules[obj.__module__].__file__ if fspath.endswith(".pyc"): fspath = fspath[:-1] lineno = compat_co_firstlineno else: fspath, lineno = getfslineno(obj) modpath = self.getmodpath() assert isinstance(lineno, int) return fspath, lineno, modpath class PyCollector(PyobjMixin, nodes.Collector): def funcnamefilter(self, name): return self._matches_prefix_or_glob_option("python_functions", name) def isnosetest(self, obj): """ Look for the __test__ attribute, which is applied by the @nose.tools.istest decorator """ # We explicitly check for "is True" here to not mistakenly treat # classes with a custom __getattr__ returning something truthy (like a # function) as test classes. return safe_getattr(obj, "__test__", False) is True def classnamefilter(self, name): return self._matches_prefix_or_glob_option("python_classes", name) def istestfunction(self, obj, name): if self.funcnamefilter(name) or self.isnosetest(obj): if isinstance(obj, staticmethod): # static methods need to be unwrapped obj = safe_getattr(obj, "__func__", False) return ( safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None ) else: return False def istestclass(self, obj, name): return self.classnamefilter(name) or self.isnosetest(obj) def _matches_prefix_or_glob_option(self, option_name, name): """ checks if the given name matches the prefix or glob-pattern defined in ini configuration. """ for option in self.config.getini(option_name): if name.startswith(option): return True # check that name looks like a glob-string before calling fnmatch # because this is called for every name in each collected module, # and fnmatch is somewhat expensive to call elif ("*" in option or "?" in option or "[" in option) and fnmatch.fnmatch( name, option ): return True return False def collect(self): if not getattr(self.obj, "__test__", True): return [] # NB. we avoid random getattrs and peek in the __dict__ instead # (XXX originally introduced from a PyPy need, still true?) dicts = [getattr(self.obj, "__dict__", {})] for basecls in inspect.getmro(self.obj.__class__): dicts.append(basecls.__dict__) seen = {} values = [] for dic in dicts: for name, obj in list(dic.items()): if name in seen: continue seen[name] = True res = self._makeitem(name, obj) if res is None: continue if not isinstance(res, list): res = [res] values.extend(res) values.sort(key=lambda item: item.reportinfo()[:2]) return values def _makeitem(self, name, obj): # assert self.ihook.fspath == self.fspath, self return self.ihook.pytest_pycollect_makeitem(collector=self, name=name, obj=obj) def _genfunctions(self, name, funcobj): module = self.getparent(Module).obj clscol = self.getparent(Class) cls = clscol and clscol.obj or None fm = self.session._fixturemanager definition = FunctionDefinition(name=name, parent=self, callobj=funcobj) fixtureinfo = fm.getfixtureinfo(definition, funcobj, cls) metafunc = Metafunc( definition, fixtureinfo, self.config, cls=cls, module=module ) methods = [] if hasattr(module, "pytest_generate_tests"): methods.append(module.pytest_generate_tests) if hasattr(cls, "pytest_generate_tests"): methods.append(cls().pytest_generate_tests) if methods: self.ihook.pytest_generate_tests.call_extra( methods, dict(metafunc=metafunc) ) else: self.ihook.pytest_generate_tests(metafunc=metafunc) if not metafunc._calls: yield Function(name, parent=self, fixtureinfo=fixtureinfo) else: # add funcargs() as fixturedefs to fixtureinfo.arg2fixturedefs fixtures.add_funcarg_pseudo_fixture_def(self, metafunc, fm) # add_funcarg_pseudo_fixture_def may have shadowed some fixtures # with direct parametrization, so make sure we update what the # function really needs. fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = "%s[%s]" % (name, callspec.id) yield Function( name=subname, parent=self, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, ) class Module(nodes.File, PyCollector): """ Collector for test classes and functions. """ def _getobj(self): return self._importtestmodule() def collect(self): self._inject_setup_module_fixture() self._inject_setup_function_fixture() self.session._fixturemanager.parsefactories(self) return super(Module, self).collect() def _inject_setup_module_fixture(self): """Injects a hidden autouse, module scoped fixture into the collected module object that invokes setUpModule/tearDownModule if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517). """ setup_module = _get_non_fixture_func(self.obj, "setUpModule") if setup_module is None: setup_module = _get_non_fixture_func(self.obj, "setup_module") teardown_module = _get_non_fixture_func(self.obj, "tearDownModule") if teardown_module is None: teardown_module = _get_non_fixture_func(self.obj, "teardown_module") if setup_module is None and teardown_module is None: return @fixtures.fixture(autouse=True, scope="module") def xunit_setup_module_fixture(request): if setup_module is not None: _call_with_optional_argument(setup_module, request.module) yield if teardown_module is not None: _call_with_optional_argument(teardown_module, request.module) self.obj.__pytest_setup_module = xunit_setup_module_fixture def _inject_setup_function_fixture(self): """Injects a hidden autouse, function scoped fixture into the collected module object that invokes setup_function/teardown_function if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517). """ setup_function = _get_non_fixture_func(self.obj, "setup_function") teardown_function = _get_non_fixture_func(self.obj, "teardown_function") if setup_function is None and teardown_function is None: return @fixtures.fixture(autouse=True, scope="function") def xunit_setup_function_fixture(request): if request.instance is not None: # in this case we are bound to an instance, so we need to let # setup_method handle this yield return if setup_function is not None: _call_with_optional_argument(setup_function, request.function) yield if teardown_function is not None: _call_with_optional_argument(teardown_function, request.function) self.obj.__pytest_setup_function = xunit_setup_function_fixture def _importtestmodule(self): # we assume we are only called once per module importmode = self.config.getoption("--import-mode") try: mod = self.fspath.pyimport(ensuresyspath=importmode) except SyntaxError: raise self.CollectError( _pytest._code.ExceptionInfo.from_current().getrepr(style="short") ) except self.fspath.ImportMismatchError: e = sys.exc_info()[1] raise self.CollectError( "import file mismatch:\n" "imported module %r has this __file__ attribute:\n" " %s\n" "which is not the same as the test file we want to collect:\n" " %s\n" "HINT: remove __pycache__ / .pyc files and/or use a " "unique basename for your test file modules" % e.args ) except ImportError: from _pytest._code.code import ExceptionInfo exc_info = ExceptionInfo.from_current() if self.config.getoption("verbose") < 2: exc_info.traceback = exc_info.traceback.filter(filter_traceback) exc_repr = ( exc_info.getrepr(style="short") if exc_info.traceback else exc_info.exconly() ) formatted_tb = safe_str(exc_repr) raise self.CollectError( "ImportError while importing test module '{fspath}'.\n" "Hint: make sure your test modules/packages have valid Python names.\n" "Traceback:\n" "{traceback}".format(fspath=self.fspath, traceback=formatted_tb) ) except _pytest.runner.Skipped as e: if e.allow_module_level: raise raise self.CollectError( "Using pytest.skip outside of a test is not allowed. " "To decorate a test function, use the @pytest.mark.skip " "or @pytest.mark.skipif decorators instead, and to skip a " "module use `pytestmark = pytest.mark.{skip,skipif}." ) self.config.pluginmanager.consider_module(mod) return mod class Package(Module): def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None): session = parent.session nodes.FSCollector.__init__( self, fspath, parent=parent, config=config, session=session, nodeid=nodeid ) self.name = fspath.dirname self.trace = session.trace self._norecursepatterns = session._norecursepatterns self.fspath = fspath def setup(self): # not using fixtures to call setup_module here because autouse fixtures # from packages are not called automatically (#4085) setup_module = _get_non_fixture_func(self.obj, "setUpModule") if setup_module is None: setup_module = _get_non_fixture_func(self.obj, "setup_module") if setup_module is not None: _call_with_optional_argument(setup_module, self.obj) teardown_module = _get_non_fixture_func(self.obj, "tearDownModule") if teardown_module is None: teardown_module = _get_non_fixture_func(self.obj, "teardown_module") if teardown_module is not None: func = partial(_call_with_optional_argument, teardown_module, self.obj) self.addfinalizer(func) def _recurse(self, dirpath): if dirpath.basename == "__pycache__": return False ihook = self.gethookproxy(dirpath.dirpath()) if ihook.pytest_ignore_collect(path=dirpath, config=self.config): return for pat in self._norecursepatterns: if dirpath.check(fnmatch=pat): return False ihook = self.gethookproxy(dirpath) ihook.pytest_collect_directory(path=dirpath, parent=self) return True def gethookproxy(self, fspath): # check if we have the common case of running # hooks with all conftest.py filesall conftest.py pm = self.config.pluginmanager my_conftestmodules = pm._getconftestmodules(fspath) remove_mods = pm._conftest_plugins.difference(my_conftestmodules) if remove_mods: # one or more conftests are not in use at this fspath proxy = FSHookProxy(fspath, pm, remove_mods) else: # all plugis are active for this fspath proxy = self.config.hook return proxy def _collectfile(self, path, handle_dupes=True): assert path.isfile(), "%r is not a file (isdir=%r, exists=%r, islink=%r)" % ( path, path.isdir(), path.exists(), path.islink(), ) ihook = self.gethookproxy(path) if not self.isinitpath(path): if ihook.pytest_ignore_collect(path=path, config=self.config): return () if handle_dupes: keepduplicates = self.config.getoption("keepduplicates") if not keepduplicates: duplicate_paths = self.config.pluginmanager._duplicatepaths if path in duplicate_paths: return () else: duplicate_paths.add(path) if self.fspath == path: # __init__.py return [self] return ihook.pytest_collect_file(path=path, parent=self) def isinitpath(self, path): return path in self.session._initialpaths def collect(self): this_path = self.fspath.dirpath() init_module = this_path.join("__init__.py") if init_module.check(file=1) and path_matches_patterns( init_module, self.config.getini("python_files") ): yield Module(init_module, self) pkg_prefixes = set() for path in this_path.visit(rec=self._recurse, bf=True, sort=True): # We will visit our own __init__.py file, in which case we skip it. is_file = path.isfile() if is_file: if path.basename == "__init__.py" and path.dirpath() == this_path: continue parts_ = parts(path.strpath) if any( pkg_prefix in parts_ and pkg_prefix.join("__init__.py") != path for pkg_prefix in pkg_prefixes ): continue if is_file: for x in self._collectfile(path): yield x elif not path.isdir(): # Broken symlink or invalid/missing file. continue elif path.join("__init__.py").check(file=1): pkg_prefixes.add(path) def _get_xunit_setup_teardown(holder, attr_name, param_obj=None): """ Return a callable to perform xunit-style setup or teardown if the function exists in the ``holder`` object. The ``param_obj`` parameter is the parameter which will be passed to the function when the callable is called without arguments, defaults to the ``holder`` object. Return ``None`` if a suitable callable is not found. """ # TODO: only needed because of Package! param_obj = param_obj if param_obj is not None else holder result = _get_non_fixture_func(holder, attr_name) if result is not None: arg_count = result.__code__.co_argcount if inspect.ismethod(result): arg_count -= 1 if arg_count: return lambda: result(param_obj) else: return result def _call_with_optional_argument(func, arg): """Call the given function with the given argument if func accepts one argument, otherwise calls func without arguments""" arg_count = func.__code__.co_argcount if inspect.ismethod(func): arg_count -= 1 if arg_count: func(arg) else: func() def _get_non_fixture_func(obj, name): """Return the attribute from the given object to be used as a setup/teardown xunit-style function, but only if not marked as a fixture to avoid calling it twice. """ meth = getattr(obj, name, None) if fixtures.getfixturemarker(meth) is None: return meth class Class(PyCollector): """ Collector for test methods. """ def collect(self): if not safe_getattr(self.obj, "__test__", True): return [] if hasinit(self.obj): self.warn( PytestCollectionWarning( "cannot collect test class %r because it has a " "__init__ constructor (from: %s)" % (self.obj.__name__, self.parent.nodeid) ) ) return [] elif hasnew(self.obj): self.warn( PytestCollectionWarning( "cannot collect test class %r because it has a " "__new__ constructor (from: %s)" % (self.obj.__name__, self.parent.nodeid) ) ) return [] self._inject_setup_class_fixture() self._inject_setup_method_fixture() return [Instance(name="()", parent=self)] def _inject_setup_class_fixture(self): """Injects a hidden autouse, class scoped fixture into the collected class object that invokes setup_class/teardown_class if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517). """ setup_class = _get_non_fixture_func(self.obj, "setup_class") teardown_class = getattr(self.obj, "teardown_class", None) if setup_class is None and teardown_class is None: return @fixtures.fixture(autouse=True, scope="class") def xunit_setup_class_fixture(cls): if setup_class is not None: func = getimfunc(setup_class) _call_with_optional_argument(func, self.obj) yield if teardown_class is not None: func = getimfunc(teardown_class) _call_with_optional_argument(func, self.obj) self.obj.__pytest_setup_class = xunit_setup_class_fixture def _inject_setup_method_fixture(self): """Injects a hidden autouse, function scoped fixture into the collected class object that invokes setup_method/teardown_method if either or both are available. Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with other fixtures (#517). """ setup_method = _get_non_fixture_func(self.obj, "setup_method") teardown_method = getattr(self.obj, "teardown_method", None) if setup_method is None and teardown_method is None: return @fixtures.fixture(autouse=True, scope="function") def xunit_setup_method_fixture(self, request): method = request.function if setup_method is not None: func = getattr(self, "setup_method") _call_with_optional_argument(func, method) yield if teardown_method is not None: func = getattr(self, "teardown_method") _call_with_optional_argument(func, method) self.obj.__pytest_setup_method = xunit_setup_method_fixture class Instance(PyCollector): _ALLOW_MARKERS = False # hack, destroy later # instances share the object with their parents in a way # that duplicates markers instances if not taken out # can be removed at node structure reorganization time def _getobj(self): return self.parent.obj() def collect(self): self.session._fixturemanager.parsefactories(self) return super(Instance, self).collect() def newinstance(self): self.obj = self._getobj() return self.obj class FunctionMixin(PyobjMixin): """ mixin for the code common to Function and Generator. """ def setup(self): """ perform setup for this test function. """ if isinstance(self.parent, Instance): self.parent.newinstance() self.obj = self._getobj() def _prunetraceback(self, excinfo): if hasattr(self, "_obj") and not self.config.getoption("fulltrace", False): code = _pytest._code.Code(get_real_func(self.obj)) path, firstlineno = code.path, code.firstlineno traceback = excinfo.traceback ntraceback = traceback.cut(path=path, firstlineno=firstlineno) if ntraceback == traceback: ntraceback = ntraceback.cut(path=path) if ntraceback == traceback: ntraceback = ntraceback.filter(filter_traceback) if not ntraceback: ntraceback = traceback excinfo.traceback = ntraceback.filter() # issue364: mark all but first and last frames to # only show a single-line message for each frame if self.config.getoption("tbstyle", "auto") == "auto": if len(excinfo.traceback) > 2: for entry in excinfo.traceback[1:-1]: entry.set_repr_style("short") def repr_failure(self, excinfo, outerr=None): assert outerr is None, "XXX outerr usage is deprecated" style = self.config.getoption("tbstyle", "auto") if style == "auto": style = "long" return self._repr_failure_py(excinfo, style=style) def hasinit(obj): init = getattr(obj, "__init__", None) if init: return init != object.__init__ def hasnew(obj): new = getattr(obj, "__new__", None) if new: return new != object.__new__ class CallSpec2(object): def __init__(self, metafunc): self.metafunc = metafunc self.funcargs = {} self._idlist = [] self.params = {} self._globalid = NOTSET self._globalparam = NOTSET self._arg2scopenum = {} # used for sorting parametrized resources self.marks = [] self.indices = {} def copy(self): cs = CallSpec2(self.metafunc) cs.funcargs.update(self.funcargs) cs.params.update(self.params) cs.marks.extend(self.marks) cs.indices.update(self.indices) cs._arg2scopenum.update(self._arg2scopenum) cs._idlist = list(self._idlist) cs._globalid = self._globalid cs._globalparam = self._globalparam return cs def _checkargnotcontained(self, arg): if arg in self.params or arg in self.funcargs: raise ValueError("duplicate %r" % (arg,)) def getparam(self, name): try: return self.params[name] except KeyError: if self._globalparam is NOTSET: raise ValueError(name) return self._globalparam @property def id(self): return "-".join(map(str, filter(None, self._idlist))) def setmulti2(self, valtypes, argnames, valset, id, marks, scopenum, param_index): for arg, val in zip(argnames, valset): self._checkargnotcontained(arg) valtype_for_arg = valtypes[arg] getattr(self, valtype_for_arg)[arg] = val self.indices[arg] = param_index self._arg2scopenum[arg] = scopenum self._idlist.append(id) self.marks.extend(normalize_mark_list(marks)) def setall(self, funcargs, id, param): for x in funcargs: self._checkargnotcontained(x) self.funcargs.update(funcargs) if id is not NOTSET: self._idlist.append(id) if param is not NOTSET: assert self._globalparam is NOTSET self._globalparam = param for arg in funcargs: self._arg2scopenum[arg] = fixtures.scopenum_function class Metafunc(fixtures.FuncargnamesCompatAttr): """ Metafunc objects are passed to the :func:`pytest_generate_tests <_pytest.hookspec.pytest_generate_tests>` hook. They help to inspect a test function and to generate tests according to test configuration or values specified in the class or module where a test function is defined. """ def __init__(self, definition, fixtureinfo, config, cls=None, module=None): assert ( isinstance(definition, FunctionDefinition) or type(definition).__name__ == "DefinitionMock" ) self.definition = definition #: access to the :class:`_pytest.config.Config` object for the test session self.config = config #: the module object where the test function is defined in. self.module = module #: underlying python test function self.function = definition.obj #: set of fixture names required by the test function self.fixturenames = fixtureinfo.names_closure #: class object where the test function is defined in or ``None``. self.cls = cls self._calls = [] self._ids = set() self._arg2fixturedefs = fixtureinfo.name2fixturedefs def parametrize(self, argnames, argvalues, indirect=False, ids=None, scope=None): """ Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather at test setup time. :arg argnames: a comma-separated string denoting one or more argument names, or a list/tuple of argument strings. :arg argvalues: The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname. :arg indirect: The list of argnames or boolean. A list of arguments' names (subset of argnames). If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time. :arg ids: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If None is given as id of specific test, the automatically generated id for that argument will be used. If callable, it should take one argument (a single argvalue) and return a string or return None. If None, the automatically generated id for that argument will be used. If no ids are provided they will be generated automatically from the argvalues. :arg scope: if specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. """ from _pytest.fixtures import scope2index from _pytest.mark import ParameterSet argnames, parameters = ParameterSet._for_parametrize( argnames, argvalues, self.function, self.config, function_definition=self.definition, ) del argvalues if scope is None: scope = _find_parametrized_scope(argnames, self._arg2fixturedefs, indirect) self._validate_if_using_arg_names(argnames, indirect) arg_values_types = self._resolve_arg_value_types(argnames, indirect) ids = self._resolve_arg_ids(argnames, ids, parameters, item=self.definition) scopenum = scope2index( scope, descr="parametrize() call in {}".format(self.function.__name__) ) # create the new calls: if we are parametrize() multiple times (by applying the decorator # more than once) then we accumulate those calls generating the cartesian product # of all calls newcalls = [] for callspec in self._calls or [CallSpec2(self)]: for param_index, (param_id, param_set) in enumerate(zip(ids, parameters)): newcallspec = callspec.copy() newcallspec.setmulti2( arg_values_types, argnames, param_set.values, param_id, param_set.marks, scopenum, param_index, ) newcalls.append(newcallspec) self._calls = newcalls def _resolve_arg_ids(self, argnames, ids, parameters, item): """Resolves the actual ids for the given argnames, based on the ``ids`` parameter given to ``parametrize``. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param ids: the ids parameter of the parametrized call (see docs). :param List[ParameterSet] parameters: the list of parameter values, same size as ``argnames``. :param Item item: the item that generated this parametrized call. :rtype: List[str] :return: the list of ids for each argname given """ from _pytest._io.saferepr import saferepr idfn = None if callable(ids): idfn = ids ids = None if ids: func_name = self.function.__name__ if len(ids) != len(parameters): msg = "In {}: {} parameter sets specified, with different number of ids: {}" fail(msg.format(func_name, len(parameters), len(ids)), pytrace=False) for id_value in ids: if id_value is not None and not isinstance(id_value, six.string_types): msg = "In {}: ids must be list of strings, found: {} (type: {!r})" fail( msg.format(func_name, saferepr(id_value), type(id_value)), pytrace=False, ) ids = idmaker(argnames, parameters, idfn, ids, self.config, item=item) return ids def _resolve_arg_value_types(self, argnames, indirect): """Resolves if each parametrized argument must be considered a parameter to a fixture or a "funcarg" to the function, based on the ``indirect`` parameter of the parametrized() call. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param indirect: same ``indirect`` parameter of ``parametrize()``. :rtype: Dict[str, str] A dict mapping each arg name to either: * "params" if the argname should be the parameter of a fixture of the same name. * "funcargs" if the argname should be a parameter to the parametrized test function. """ valtypes = {} if indirect is True: valtypes = dict.fromkeys(argnames, "params") elif indirect is False: valtypes = dict.fromkeys(argnames, "funcargs") elif isinstance(indirect, (tuple, list)): valtypes = dict.fromkeys(argnames, "funcargs") for arg in indirect: if arg not in argnames: fail( "In {}: indirect fixture '{}' doesn't exist".format( self.function.__name__, arg ), pytrace=False, ) valtypes[arg] = "params" return valtypes def _validate_if_using_arg_names(self, argnames, indirect): """ Check if all argnames are being used, by default values, or directly/indirectly. :param List[str] argnames: list of argument names passed to ``parametrize()``. :param indirect: same ``indirect`` parameter of ``parametrize()``. :raise ValueError: if validation fails. """ default_arg_names = set(get_default_arg_names(self.function)) func_name = self.function.__name__ for arg in argnames: if arg not in self.fixturenames: if arg in default_arg_names: fail( "In {}: function already takes an argument '{}' with a default value".format( func_name, arg ), pytrace=False, ) else: if isinstance(indirect, (tuple, list)): name = "fixture" if arg in indirect else "argument" else: name = "fixture" if indirect else "argument" fail( "In {}: function uses no {} '{}'".format(func_name, name, arg), pytrace=False, ) def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): """Find the most appropriate scope for a parametrized call based on its arguments. When there's at least one direct argument, always use "function" scope. When a test function is parametrized and all its arguments are indirect (e.g. fixtures), return the most narrow scope based on the fixtures used. Related to issue #1832, based on code posted by @Kingdread. """ from _pytest.fixtures import scopes if isinstance(indirect, (list, tuple)): all_arguments_are_fixtures = len(indirect) == len(argnames) else: all_arguments_are_fixtures = bool(indirect) if all_arguments_are_fixtures: fixturedefs = arg2fixturedefs or {} used_scopes = [ fixturedef[0].scope for name, fixturedef in fixturedefs.items() if name in argnames ] if used_scopes: # Takes the most narrow scope from used fixtures for scope in reversed(scopes): if scope in used_scopes: return scope return "function" def _ascii_escaped_by_config(val, config): if config is None: escape_option = False else: escape_option = config.getini( "disable_test_id_escaping_and_forfeit_all_rights_to_community_support" ) return val if escape_option else ascii_escaped(val) def _idval(val, argname, idx, idfn, item, config): if idfn: try: generated_id = idfn(val) if generated_id is not None: val = generated_id except Exception as e: # See issue https://github.com/pytest-dev/pytest/issues/2169 msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n" msg = msg.format(item.nodeid, argname, idx) # we only append the exception type and message because on Python 2 reraise does nothing msg += " {}: {}\n".format(type(e).__name__, e) six.raise_from(ValueError(msg), e) elif config: hook_id = config.hook.pytest_make_parametrize_id( config=config, val=val, argname=argname ) if hook_id: return hook_id if isinstance(val, STRING_TYPES): return _ascii_escaped_by_config(val, config) elif val is None or isinstance(val, (float, int, bool)): return str(val) elif isinstance(val, REGEX_TYPE): return ascii_escaped(val.pattern) elif enum is not None and isinstance(val, enum.Enum): return str(val) elif (isclass(val) or isfunction(val)) and hasattr(val, "__name__"): return val.__name__ return str(argname) + str(idx) def _idvalset(idx, parameterset, argnames, idfn, ids, item, config): if parameterset.id is not None: return parameterset.id if ids is None or (idx >= len(ids) or ids[idx] is None): this_id = [ _idval(val, argname, idx, idfn, item=item, config=config) for val, argname in zip(parameterset.values, argnames) ] return "-".join(this_id) else: return _ascii_escaped_by_config(ids[idx], config) def idmaker(argnames, parametersets, idfn=None, ids=None, config=None, item=None): ids = [ _idvalset(valindex, parameterset, argnames, idfn, ids, config=config, item=item) for valindex, parameterset in enumerate(parametersets) ] if len(set(ids)) != len(ids): # The ids are not unique duplicates = [testid for testid in ids if ids.count(testid) > 1] counters = collections.defaultdict(lambda: 0) for index, testid in enumerate(ids): if testid in duplicates: ids[index] = testid + str(counters[testid]) counters[testid] += 1 return ids def show_fixtures_per_test(config): from _pytest.main import wrap_session return wrap_session(config, _show_fixtures_per_test) def _show_fixtures_per_test(config, session): import _pytest.config session.perform_collect() curdir = py.path.local() tw = _pytest.config.create_terminal_writer(config) verbose = config.getvalue("verbose") def get_best_relpath(func): loc = getlocation(func, curdir) return curdir.bestrelpath(loc) def write_fixture(fixture_def): argname = fixture_def.argname if verbose <= 0 and argname.startswith("_"): return if verbose > 0: bestrel = get_best_relpath(fixture_def.func) funcargspec = "{} -- {}".format(argname, bestrel) else: funcargspec = argname tw.line(funcargspec, green=True) fixture_doc = fixture_def.func.__doc__ if fixture_doc: write_docstring(tw, fixture_doc) else: tw.line(" no docstring available", red=True) def write_item(item): try: info = item._fixtureinfo except AttributeError: # doctests items have no _fixtureinfo attribute return if not info.name2fixturedefs: # this test item does not use any fixtures return tw.line() tw.sep("-", "fixtures used by {}".format(item.name)) tw.sep("-", "({})".format(get_best_relpath(item.function))) # dict key not used in loop but needed for sorting for _, fixturedefs in sorted(info.name2fixturedefs.items()): assert fixturedefs is not None if not fixturedefs: continue # last item is expected to be the one used by the test item write_fixture(fixturedefs[-1]) for session_item in session.items: write_item(session_item) def showfixtures(config): from _pytest.main import wrap_session return wrap_session(config, _showfixtures_main) def _showfixtures_main(config, session): import _pytest.config session.perform_collect() curdir = py.path.local() tw = _pytest.config.create_terminal_writer(config) verbose = config.getvalue("verbose") fm = session._fixturemanager available = [] seen = set() for argname, fixturedefs in fm._arg2fixturedefs.items(): assert fixturedefs is not None if not fixturedefs: continue for fixturedef in fixturedefs: loc = getlocation(fixturedef.func, curdir) if (fixturedef.argname, loc) in seen: continue seen.add((fixturedef.argname, loc)) available.append( ( len(fixturedef.baseid), fixturedef.func.__module__, curdir.bestrelpath(loc), fixturedef.argname, fixturedef, ) ) available.sort() currentmodule = None for baseid, module, bestrel, argname, fixturedef in available: if currentmodule != module: if not module.startswith("_pytest."): tw.line() tw.sep("-", "fixtures defined from %s" % (module,)) currentmodule = module if verbose <= 0 and argname[0] == "_": continue tw.write(argname, green=True) if fixturedef.scope != "function": tw.write(" [%s scope]" % fixturedef.scope, cyan=True) if verbose > 0: tw.write(" -- %s" % bestrel, yellow=True) tw.write("\n") loc = getlocation(fixturedef.func, curdir) doc = fixturedef.func.__doc__ or "" if doc: write_docstring(tw, doc) else: tw.line(" %s: no docstring available" % (loc,), red=True) tw.line() def write_docstring(tw, doc, indent=" "): doc = doc.rstrip() if "\n" in doc: firstline, rest = doc.split("\n", 1) else: firstline, rest = doc, "" if firstline.strip(): tw.line(indent + firstline.strip()) if rest: for line in dedent(rest).split("\n"): tw.write(indent + line + "\n") class Function(FunctionMixin, nodes.Item, fixtures.FuncargnamesCompatAttr): """ a Function Item is responsible for setting up and executing a Python test function. """ # disable since functions handle it themselves _ALLOW_MARKERS = False def __init__( self, name, parent, args=None, config=None, callspec=None, callobj=NOTSET, keywords=None, session=None, fixtureinfo=None, originalname=None, ): super(Function, self).__init__(name, parent, config=config, session=session) self._args = args if callobj is not NOTSET: self.obj = callobj self.keywords.update(self.obj.__dict__) self.own_markers.extend(get_unpacked_marks(self.obj)) if callspec: self.callspec = callspec # this is total hostile and a mess # keywords are broken by design by now # this will be redeemed later for mark in callspec.marks: # feel free to cry, this was broken for years before # and keywords cant fix it per design self.keywords[mark.name] = mark self.own_markers.extend(normalize_mark_list(callspec.marks)) if keywords: self.keywords.update(keywords) # todo: this is a hell of a hack # https://github.com/pytest-dev/pytest/issues/4569 self.keywords.update( dict.fromkeys( [ mark.name for mark in self.iter_markers() if mark.name not in self.keywords ], True, ) ) if fixtureinfo is None: fixtureinfo = self.session._fixturemanager.getfixtureinfo( self, self.obj, self.cls, funcargs=True ) self._fixtureinfo = fixtureinfo self.fixturenames = fixtureinfo.names_closure self._initrequest() #: original function name, without any decorations (for example #: parametrization adds a ``"[...]"`` suffix to function names). #: #: .. versionadded:: 3.0 self.originalname = originalname def _initrequest(self): self.funcargs = {} self._request = fixtures.FixtureRequest(self) @property def function(self): "underlying python 'function' object" return getimfunc(self.obj) def _getobj(self): name = self.name i = name.find("[") # parametrization if i != -1: name = name[:i] return getattr(self.parent.obj, name) @property def _pyfuncitem(self): "(compatonly) for code expecting pytest-2.2 style request objects" return self def runtest(self): """ execute the underlying test function. """ self.ihook.pytest_pyfunc_call(pyfuncitem=self) def setup(self): super(Function, self).setup() fixtures.fillfixtures(self) class FunctionDefinition(Function): """ internal hack until we get actual definition nodes instead of the crappy metafunc hack """ def runtest(self): raise RuntimeError("function definitions are not supposed to be used") setup = runtest pytest-4.6.9/src/_pytest/python_api.py0000664000372000037200000006660013604173400020734 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import import math import pprint import sys import warnings from decimal import Decimal from numbers import Number from more_itertools.more import always_iterable from six.moves import filterfalse from six.moves import zip import _pytest._code from _pytest import deprecated from _pytest.compat import isclass from _pytest.compat import Iterable from _pytest.compat import Mapping from _pytest.compat import Sized from _pytest.compat import STRING_TYPES from _pytest.outcomes import fail BASE_TYPE = (type, STRING_TYPES) def _cmp_raises_type_error(self, other): """__cmp__ implementation which raises TypeError. Used by Approx base classes to implement only == and != and raise a TypeError for other comparisons. Needed in Python 2 only, Python 3 all it takes is not implementing the other operators at all. """ __tracebackhide__ = True raise TypeError( "Comparison operators other than == and != not supported by approx objects" ) def _non_numeric_type_error(value, at): at_str = " at {}".format(at) if at else "" return TypeError( "cannot make approximate comparisons to non-numeric values: {!r} {}".format( value, at_str ) ) # builtin pytest.approx helper class ApproxBase(object): """ Provide shared utilities for making approximate comparisons between numbers or sequences of numbers. """ # Tell numpy to use our `__eq__` operator instead of its. __array_ufunc__ = None __array_priority__ = 100 def __init__(self, expected, rel=None, abs=None, nan_ok=False): __tracebackhide__ = True self.expected = expected self.abs = abs self.rel = rel self.nan_ok = nan_ok self._check_type() def __repr__(self): raise NotImplementedError def __eq__(self, actual): return all( a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual) ) __hash__ = None def __ne__(self, actual): return not (actual == self) if sys.version_info[0] == 2: __cmp__ = _cmp_raises_type_error def _approx_scalar(self, x): return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok) def _yield_comparisons(self, actual): """ Yield all the pairs of numbers to be compared. This is used to implement the `__eq__` method. """ raise NotImplementedError def _check_type(self): """ Raise a TypeError if the expected value is not a valid type. """ # This is only a concern if the expected value is a sequence. In every # other case, the approx() function ensures that the expected value has # a numeric type. For this reason, the default is to do nothing. The # classes that deal with sequences should reimplement this method to # raise if there are any non-numeric elements in the sequence. pass def _recursive_list_map(f, x): if isinstance(x, list): return list(_recursive_list_map(f, xi) for xi in x) else: return f(x) class ApproxNumpy(ApproxBase): """ Perform approximate comparisons where the expected value is numpy array. """ def __repr__(self): list_scalars = _recursive_list_map(self._approx_scalar, self.expected.tolist()) return "approx({!r})".format(list_scalars) if sys.version_info[0] == 2: __cmp__ = _cmp_raises_type_error def __eq__(self, actual): import numpy as np # self.expected is supposed to always be an array here if not np.isscalar(actual): try: actual = np.asarray(actual) except: # noqa raise TypeError("cannot compare '{}' to numpy.ndarray".format(actual)) if not np.isscalar(actual) and actual.shape != self.expected.shape: return False return ApproxBase.__eq__(self, actual) def _yield_comparisons(self, actual): import numpy as np # `actual` can either be a numpy array or a scalar, it is treated in # `__eq__` before being passed to `ApproxBase.__eq__`, which is the # only method that calls this one. if np.isscalar(actual): for i in np.ndindex(self.expected.shape): yield actual, self.expected[i].item() else: for i in np.ndindex(self.expected.shape): yield actual[i].item(), self.expected[i].item() class ApproxMapping(ApproxBase): """ Perform approximate comparisons where the expected value is a mapping with numeric values (the keys can be anything). """ def __repr__(self): return "approx({!r})".format( {k: self._approx_scalar(v) for k, v in self.expected.items()} ) def __eq__(self, actual): if set(actual.keys()) != set(self.expected.keys()): return False return ApproxBase.__eq__(self, actual) def _yield_comparisons(self, actual): for k in self.expected.keys(): yield actual[k], self.expected[k] def _check_type(self): __tracebackhide__ = True for key, value in self.expected.items(): if isinstance(value, type(self.expected)): msg = "pytest.approx() does not support nested dictionaries: key={!r} value={!r}\n full mapping={}" raise TypeError(msg.format(key, value, pprint.pformat(self.expected))) elif not isinstance(value, Number): raise _non_numeric_type_error(self.expected, at="key={!r}".format(key)) class ApproxSequencelike(ApproxBase): """ Perform approximate comparisons where the expected value is a sequence of numbers. """ def __repr__(self): seq_type = type(self.expected) if seq_type not in (tuple, list, set): seq_type = list return "approx({!r})".format( seq_type(self._approx_scalar(x) for x in self.expected) ) def __eq__(self, actual): if len(actual) != len(self.expected): return False return ApproxBase.__eq__(self, actual) def _yield_comparisons(self, actual): return zip(actual, self.expected) def _check_type(self): __tracebackhide__ = True for index, x in enumerate(self.expected): if isinstance(x, type(self.expected)): msg = "pytest.approx() does not support nested data structures: {!r} at index {}\n full sequence: {}" raise TypeError(msg.format(x, index, pprint.pformat(self.expected))) elif not isinstance(x, Number): raise _non_numeric_type_error( self.expected, at="index {}".format(index) ) class ApproxScalar(ApproxBase): """ Perform approximate comparisons where the expected value is a single number. """ DEFAULT_ABSOLUTE_TOLERANCE = 1e-12 DEFAULT_RELATIVE_TOLERANCE = 1e-6 def __repr__(self): """ Return a string communicating both the expected value and the tolerance for the comparison being made, e.g. '1.0 +- 1e-6'. Use the unicode plus/minus symbol if this is python3 (it's too hard to get right for python2). """ if isinstance(self.expected, complex): return str(self.expected) # Infinities aren't compared using tolerances, so don't show a # tolerance. if math.isinf(self.expected): return str(self.expected) # If a sensible tolerance can't be calculated, self.tolerance will # raise a ValueError. In this case, display '???'. try: vetted_tolerance = "{:.1e}".format(self.tolerance) except ValueError: vetted_tolerance = "???" if sys.version_info[0] == 2: return "{} +- {}".format(self.expected, vetted_tolerance) else: return u"{} \u00b1 {}".format(self.expected, vetted_tolerance) def __eq__(self, actual): """ Return true if the given value is equal to the expected value within the pre-specified tolerance. """ if _is_numpy_array(actual): # Call ``__eq__()`` manually to prevent infinite-recursion with # numpy<1.13. See #3748. return all(self.__eq__(a) for a in actual.flat) # Short-circuit exact equality. if actual == self.expected: return True # Allow the user to control whether NaNs are considered equal to each # other or not. The abs() calls are for compatibility with complex # numbers. if math.isnan(abs(self.expected)): return self.nan_ok and math.isnan(abs(actual)) # Infinity shouldn't be approximately equal to anything but itself, but # if there's a relative tolerance, it will be infinite and infinity # will seem approximately equal to everything. The equal-to-itself # case would have been short circuited above, so here we can just # return false if the expected value is infinite. The abs() call is # for compatibility with complex numbers. if math.isinf(abs(self.expected)): return False # Return true if the two numbers are within the tolerance. return abs(self.expected - actual) <= self.tolerance __hash__ = None @property def tolerance(self): """ Return the tolerance for the comparison. This could be either an absolute tolerance or a relative tolerance, depending on what the user specified or which would be larger. """ def set_default(x, default): return x if x is not None else default # Figure out what the absolute tolerance should be. ``self.abs`` is # either None or a value specified by the user. absolute_tolerance = set_default(self.abs, self.DEFAULT_ABSOLUTE_TOLERANCE) if absolute_tolerance < 0: raise ValueError( "absolute tolerance can't be negative: {}".format(absolute_tolerance) ) if math.isnan(absolute_tolerance): raise ValueError("absolute tolerance can't be NaN.") # If the user specified an absolute tolerance but not a relative one, # just return the absolute tolerance. if self.rel is None: if self.abs is not None: return absolute_tolerance # Figure out what the relative tolerance should be. ``self.rel`` is # either None or a value specified by the user. This is done after # we've made sure the user didn't ask for an absolute tolerance only, # because we don't want to raise errors about the relative tolerance if # we aren't even going to use it. relative_tolerance = set_default( self.rel, self.DEFAULT_RELATIVE_TOLERANCE ) * abs(self.expected) if relative_tolerance < 0: raise ValueError( "relative tolerance can't be negative: {}".format(absolute_tolerance) ) if math.isnan(relative_tolerance): raise ValueError("relative tolerance can't be NaN.") # Return the larger of the relative and absolute tolerances. return max(relative_tolerance, absolute_tolerance) class ApproxDecimal(ApproxScalar): """ Perform approximate comparisons where the expected value is a decimal. """ DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12") DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6") def approx(expected, rel=None, abs=None, nan_ok=False): """ Assert that two numbers (or two sets of numbers) are equal to each other within some tolerance. Due to the `intricacies of floating-point arithmetic`__, numbers that we would intuitively expect to be equal are not always so:: >>> 0.1 + 0.2 == 0.3 False __ https://docs.python.org/3/tutorial/floatingpoint.html This problem is commonly encountered when writing tests, e.g. when making sure that floating-point values are what you expect them to be. One way to deal with this problem is to assert that two floating-point numbers are equal to within some appropriate tolerance:: >>> abs((0.1 + 0.2) - 0.3) < 1e-6 True However, comparisons like this are tedious to write and difficult to understand. Furthermore, absolute comparisons like the one above are usually discouraged because there's no tolerance that works well for all situations. ``1e-6`` is good for numbers around ``1``, but too small for very big numbers and too big for very small ones. It's better to express the tolerance as a fraction of the expected value, but relative comparisons like that are even more difficult to write correctly and concisely. The ``approx`` class performs floating-point comparisons using a syntax that's as intuitive as possible:: >>> from pytest import approx >>> 0.1 + 0.2 == approx(0.3) True The same syntax also works for sequences of numbers:: >>> (0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6)) True Dictionary *values*:: >>> {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6}) True ``numpy`` arrays:: >>> import numpy as np # doctest: +SKIP >>> np.array([0.1, 0.2]) + np.array([0.2, 0.4]) == approx(np.array([0.3, 0.6])) # doctest: +SKIP True And for a ``numpy`` array against a scalar:: >>> import numpy as np # doctest: +SKIP >>> np.array([0.1, 0.2]) + np.array([0.2, 0.1]) == approx(0.3) # doctest: +SKIP True By default, ``approx`` considers numbers within a relative tolerance of ``1e-6`` (i.e. one part in a million) of its expected value to be equal. This treatment would lead to surprising results if the expected value was ``0.0``, because nothing but ``0.0`` itself is relatively close to ``0.0``. To handle this case less surprisingly, ``approx`` also considers numbers within an absolute tolerance of ``1e-12`` of its expected value to be equal. Infinity and NaN are special cases. Infinity is only considered equal to itself, regardless of the relative tolerance. NaN is not considered equal to anything by default, but you can make it be equal to itself by setting the ``nan_ok`` argument to True. (This is meant to facilitate comparing arrays that use NaN to mean "no data".) Both the relative and absolute tolerances can be changed by passing arguments to the ``approx`` constructor:: >>> 1.0001 == approx(1) False >>> 1.0001 == approx(1, rel=1e-3) True >>> 1.0001 == approx(1, abs=1e-3) True If you specify ``abs`` but not ``rel``, the comparison will not consider the relative tolerance at all. In other words, two numbers that are within the default relative tolerance of ``1e-6`` will still be considered unequal if they exceed the specified absolute tolerance. If you specify both ``abs`` and ``rel``, the numbers will be considered equal if either tolerance is met:: >>> 1 + 1e-8 == approx(1) True >>> 1 + 1e-8 == approx(1, abs=1e-12) False >>> 1 + 1e-8 == approx(1, rel=1e-6, abs=1e-12) True If you're thinking about using ``approx``, then you might want to know how it compares to other good ways of comparing floating-point numbers. All of these algorithms are based on relative and absolute tolerances and should agree for the most part, but they do have meaningful differences: - ``math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)``: True if the relative tolerance is met w.r.t. either ``a`` or ``b`` or if the absolute tolerance is met. Because the relative tolerance is calculated w.r.t. both ``a`` and ``b``, this test is symmetric (i.e. neither ``a`` nor ``b`` is a "reference value"). You have to specify an absolute tolerance if you want to compare to ``0.0`` because there is no tolerance by default. Only available in python>=3.5. `More information...`__ __ https://docs.python.org/3/library/math.html#math.isclose - ``numpy.isclose(a, b, rtol=1e-5, atol=1e-8)``: True if the difference between ``a`` and ``b`` is less that the sum of the relative tolerance w.r.t. ``b`` and the absolute tolerance. Because the relative tolerance is only calculated w.r.t. ``b``, this test is asymmetric and you can think of ``b`` as the reference value. Support for comparing sequences is provided by ``numpy.allclose``. `More information...`__ __ http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.isclose.html - ``unittest.TestCase.assertAlmostEqual(a, b)``: True if ``a`` and ``b`` are within an absolute tolerance of ``1e-7``. No relative tolerance is considered and the absolute tolerance cannot be changed, so this function is not appropriate for very large or very small numbers. Also, it's only available in subclasses of ``unittest.TestCase`` and it's ugly because it doesn't follow PEP8. `More information...`__ __ https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual - ``a == pytest.approx(b, rel=1e-6, abs=1e-12)``: True if the relative tolerance is met w.r.t. ``b`` or if the absolute tolerance is met. Because the relative tolerance is only calculated w.r.t. ``b``, this test is asymmetric and you can think of ``b`` as the reference value. In the special case that you explicitly specify an absolute tolerance but not a relative tolerance, only the absolute tolerance is considered. .. warning:: .. versionchanged:: 3.2 In order to avoid inconsistent behavior, ``TypeError`` is raised for ``>``, ``>=``, ``<`` and ``<=`` comparisons. The example below illustrates the problem:: assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10) assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10) In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)`` to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used to comparison. This is because the call hierarchy of rich comparisons follows a fixed behavior. `More information...`__ __ https://docs.python.org/3/reference/datamodel.html#object.__ge__ """ # Delegate the comparison to a class that knows how to deal with the type # of the expected value (e.g. int, float, list, dict, numpy.array, etc). # # The primary responsibility of these classes is to implement ``__eq__()`` # and ``__repr__()``. The former is used to actually check if some # "actual" value is equivalent to the given expected value within the # allowed tolerance. The latter is used to show the user the expected # value and tolerance, in the case that a test failed. # # The actual logic for making approximate comparisons can be found in # ApproxScalar, which is used to compare individual numbers. All of the # other Approx classes eventually delegate to this class. The ApproxBase # class provides some convenient methods and overloads, but isn't really # essential. __tracebackhide__ = True if isinstance(expected, Decimal): cls = ApproxDecimal elif isinstance(expected, Number): cls = ApproxScalar elif isinstance(expected, Mapping): cls = ApproxMapping elif _is_numpy_array(expected): cls = ApproxNumpy elif ( isinstance(expected, Iterable) and isinstance(expected, Sized) and not isinstance(expected, STRING_TYPES) ): cls = ApproxSequencelike else: raise _non_numeric_type_error(expected, at=None) return cls(expected, rel, abs, nan_ok) def _is_numpy_array(obj): """ Return true if the given object is a numpy array. Make a special effort to avoid importing numpy unless it's really necessary. """ import sys np = sys.modules.get("numpy") if np is not None: return isinstance(obj, np.ndarray) return False # builtin pytest.raises helper def raises(expected_exception, *args, **kwargs): r""" Assert that a code block/function call raises ``expected_exception`` or raise a failure exception otherwise. :kwparam match: if specified, a string containing a regular expression, or a regular expression object, that is tested against the string representation of the exception using ``re.search``. To match a literal string that may contain `special characters`__, the pattern can first be escaped with ``re.escape``. __ https://docs.python.org/3/library/re.html#regular-expression-syntax :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message if the exception is not raised. See :ref:`the deprecation docs ` for a workaround. .. currentmodule:: _pytest._code Use ``pytest.raises`` as a context manager, which will capture the exception of the given type:: >>> with raises(ZeroDivisionError): ... 1/0 If the code block does not raise the expected exception (``ZeroDivisionError`` in the example above), or no exception at all, the check will fail instead. You can also use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with raises(ValueError, match='must be 0 or None'): ... raise ValueError("value must be 0 or None") >>> with raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the details of the captured exception:: >>> with raises(ValueError) as exc_info: ... raise ValueError("value must be 42") >>> assert exc_info.type is ValueError >>> assert exc_info.value.args[0] == "value must be 42" .. deprecated:: 4.1 In the context manager form you may use the keyword argument ``message`` to specify a custom failure message that will be displayed in case the ``pytest.raises`` check fails. This has been deprecated as it is considered error prone as users often mean to use ``match`` instead. See :ref:`the deprecation docs ` for a workaround. .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to note that normal context manager rules apply and that the exception raised *must* be the final line in the scope of the context manager. Lines of code after that, within the scope of the context manager will not be executed. For example:: >>> value = 15 >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... assert exc_info.type is ValueError # this will not execute Instead, the following approach must be taken (note the difference in scope):: >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... >>> assert exc_info.type is ValueError **Using with** ``pytest.mark.parametrize`` When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests such that some runs raise an exception and others do not. See :ref:`parametrizing_conditional_raising` for an example. **Legacy form** It is possible to specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) or you can specify an arbitrary callable with arguments:: >>> def f(x): return 1/x ... >>> raises(ZeroDivisionError, f, 0) >>> raises(ZeroDivisionError, f, x=0) The form above is fully supported but discouraged for new code because the context manager form is regarded as more readable and less error-prone. .. note:: Similar to caught exception objects in Python, explicitly clearing local references to returned ``ExceptionInfo`` objects can help the Python interpreter speed up its garbage collection. Clearing those references breaks a reference cycle (``ExceptionInfo`` --> caught exception --> frame stack raising the exception --> current frame stack --> local variables --> ``ExceptionInfo``) which makes Python keep all objects referenced from that cycle (including all local variables in the current frame) alive until the next cyclic garbage collection run. See the official Python ``try`` statement documentation for more detailed information. """ __tracebackhide__ = True for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ( "exceptions must be old-style classes or" " derived from BaseException, not %s" ) raise TypeError(msg % type(exc)) message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: if "message" in kwargs: message = kwargs.pop("message") warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2) if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: msg = "Unexpected keyword arguments passed to pytest.raises: " msg += ", ".join(sorted(kwargs)) raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): warnings.warn(deprecated.RAISES_EXEC, stacklevel=2) (code,) = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: code = _pytest._code.Source(code).compile(_genframe=frame) exec(code, frame.f_globals, loc) # XXX didn't mean f_globals == f_locals something special? # this is destroyed here ... except expected_exception: return _pytest._code.ExceptionInfo.from_current() else: func = args[0] try: func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo.from_current() fail(message) raises.Exception = fail.Exception class RaisesContext(object): def __init__(self, expected_exception, message, match_expr): self.expected_exception = expected_exception self.message = message self.match_expr = match_expr self.excinfo = None def __enter__(self): self.excinfo = _pytest._code.ExceptionInfo.for_later() return self.excinfo def __exit__(self, *tp): __tracebackhide__ = True if tp[0] is None: fail(self.message) 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() if self.match_expr is not None and suppress_exception: self.excinfo.match(self.match_expr) return suppress_exception pytest-4.6.9/src/_pytest/runner.py0000664000372000037200000002726713604173400020101 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ basic collect and runtest protocol implementations """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import bdb import os import sys from time import time import attr import six from .reports import CollectErrorRepr from .reports import CollectReport from .reports import TestReport from _pytest._code.code import ExceptionInfo from _pytest.outcomes import Exit from _pytest.outcomes import Skipped from _pytest.outcomes import TEST_OUTCOME # # pytest plugin hooks def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") group.addoption( "--durations", action="store", type=int, default=None, metavar="N", help="show N slowest setup/test durations (N=0 for all).", ), def pytest_terminal_summary(terminalreporter): durations = terminalreporter.config.option.durations verbose = terminalreporter.config.getvalue("verbose") if durations is None: return tr = terminalreporter dlist = [] for replist in tr.stats.values(): for rep in replist: if hasattr(rep, "duration"): dlist.append(rep) if not dlist: return dlist.sort(key=lambda x: x.duration) dlist.reverse() if not durations: tr.write_sep("=", "slowest test durations") else: tr.write_sep("=", "slowest %s test durations" % durations) dlist = dlist[:durations] for rep in dlist: if verbose < 2 and rep.duration < 0.005: tr.write_line("") tr.write_line("(0.00 durations hidden. Use -vv to show these durations.)") break tr.write_line("%02.2fs %-8s %s" % (rep.duration, rep.when, rep.nodeid)) def pytest_sessionstart(session): session._setupstate = SetupState() def pytest_sessionfinish(session): session._setupstate.teardown_all() def pytest_runtest_protocol(item, nextitem): item.ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location) runtestprotocol(item, nextitem=nextitem) item.ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location) return True def runtestprotocol(item, log=True, nextitem=None): hasrequest = hasattr(item, "_request") if hasrequest and not item._request: item._initrequest() rep = call_and_report(item, "setup", log) reports = [rep] if rep.passed: if item.config.getoption("setupshow", False): show_test_item(item) if not item.config.getoption("setuponly", False): reports.append(call_and_report(item, "call", log)) reports.append(call_and_report(item, "teardown", log, nextitem=nextitem)) # after all teardown hooks have been called # want funcargs and request info to go away if hasrequest: item._request = False item.funcargs = None return reports def show_test_item(item): """Show test function, parameters and the fixtures of the test item.""" tw = item.config.get_terminal_writer() tw.line() tw.write(" " * 8) tw.write(item._nodeid) used_fixtures = sorted(item._fixtureinfo.name2fixturedefs.keys()) if used_fixtures: tw.write(" (fixtures used: {})".format(", ".join(used_fixtures))) def pytest_runtest_setup(item): _update_current_test_var(item, "setup") item.session._setupstate.prepare(item) def pytest_runtest_call(item): _update_current_test_var(item, "call") sys.last_type, sys.last_value, sys.last_traceback = (None, None, None) try: item.runtest() except Exception: # Store trace info to allow postmortem debugging type, value, tb = sys.exc_info() tb = tb.tb_next # Skip *this* frame sys.last_type = type sys.last_value = value sys.last_traceback = tb del type, value, tb # Get rid of these in this frame raise def pytest_runtest_teardown(item, nextitem): _update_current_test_var(item, "teardown") item.session._setupstate.teardown_exact(item, nextitem) _update_current_test_var(item, None) def _update_current_test_var(item, when): """ Update PYTEST_CURRENT_TEST to reflect the current item and stage. If ``when`` is None, delete PYTEST_CURRENT_TEST from the environment. """ var_name = "PYTEST_CURRENT_TEST" if when: value = "{} ({})".format(item.nodeid, when) # don't allow null bytes on environment variables (see #2644, #2957) value = value.replace("\x00", "(null)") os.environ[var_name] = value else: os.environ.pop(var_name) def pytest_report_teststatus(report): if report.when in ("setup", "teardown"): if report.failed: # category, shortletter, verbose-word return "error", "E", "ERROR" elif report.skipped: return "skipped", "s", "SKIPPED" else: return "", "", "" # # Implementation def call_and_report(item, when, log=True, **kwds): call = call_runtest_hook(item, when, **kwds) hook = item.ihook report = hook.pytest_runtest_makereport(item=item, call=call) if log: hook.pytest_runtest_logreport(report=report) if check_interactive_exception(call, report): hook.pytest_exception_interact(node=item, call=call, report=report) return report def check_interactive_exception(call, report): return call.excinfo and not ( hasattr(report, "wasxfail") or call.excinfo.errisinstance(Skipped) or call.excinfo.errisinstance(bdb.BdbQuit) ) def call_runtest_hook(item, when, **kwds): hookname = "pytest_runtest_" + when ihook = getattr(item.ihook, hookname) reraise = (Exit,) if not item.config.getoption("usepdb", False): reraise += (KeyboardInterrupt,) return CallInfo.from_call( lambda: ihook(item=item, **kwds), when=when, reraise=reraise ) @attr.s(repr=False) class CallInfo(object): """ Result/Exception info a function invocation. """ _result = attr.ib() # Optional[ExceptionInfo] excinfo = attr.ib() start = attr.ib() stop = attr.ib() when = attr.ib() @property def result(self): if self.excinfo is not None: raise AttributeError("{!r} has no valid result".format(self)) return self._result @classmethod def from_call(cls, func, when, reraise=None): #: context of invocation: one of "setup", "call", #: "teardown", "memocollect" start = time() excinfo = None try: result = func() except: # noqa excinfo = ExceptionInfo.from_current() if reraise is not None and excinfo.errisinstance(reraise): raise result = None stop = time() return cls(start=start, stop=stop, when=when, result=result, excinfo=excinfo) def __repr__(self): if self.excinfo is not None: status = "exception" value = self.excinfo.value else: # TODO: investigate unification value = repr(self._result) status = "result" return "".format( when=self.when, value=value, status=status ) def pytest_runtest_makereport(item, call): return TestReport.from_item_and_call(item, call) def pytest_make_collect_report(collector): call = CallInfo.from_call(lambda: list(collector.collect()), "collect") longrepr = None if not call.excinfo: outcome = "passed" else: from _pytest import nose skip_exceptions = (Skipped,) + nose.get_skip_exceptions() if call.excinfo.errisinstance(skip_exceptions): outcome = "skipped" r = collector._repr_failure_py(call.excinfo, "line").reprcrash longrepr = (str(r.path), r.lineno, r.message) else: outcome = "failed" errorinfo = collector.repr_failure(call.excinfo) if not hasattr(errorinfo, "toterminal"): errorinfo = CollectErrorRepr(errorinfo) longrepr = errorinfo rep = CollectReport( collector.nodeid, outcome, longrepr, getattr(call, "result", None) ) rep.call = call # see collect_one_node return rep class SetupState(object): """ shared state for setting up/tearing down test items or collectors. """ def __init__(self): self.stack = [] self._finalizers = {} def addfinalizer(self, finalizer, colitem): """ attach a finalizer to the given colitem. if colitem is None, this will add a finalizer that is called at the end of teardown_all(). """ assert colitem and not isinstance(colitem, tuple) assert callable(finalizer) # assert colitem in self.stack # some unit tests don't setup stack :/ self._finalizers.setdefault(colitem, []).append(finalizer) def _pop_and_teardown(self): colitem = self.stack.pop() self._teardown_with_finalization(colitem) def _callfinalizers(self, colitem): finalizers = self._finalizers.pop(colitem, None) exc = None while finalizers: fin = finalizers.pop() try: fin() except TEST_OUTCOME: # XXX Only first exception will be seen by user, # ideally all should be reported. if exc is None: exc = sys.exc_info() if exc: six.reraise(*exc) def _teardown_with_finalization(self, colitem): self._callfinalizers(colitem) if hasattr(colitem, "teardown"): colitem.teardown() for colitem in self._finalizers: assert ( colitem is None or colitem in self.stack or isinstance(colitem, tuple) ) def teardown_all(self): while self.stack: self._pop_and_teardown() for key in list(self._finalizers): self._teardown_with_finalization(key) assert not self._finalizers def teardown_exact(self, item, nextitem): needed_collectors = nextitem and nextitem.listchain() or [] self._teardown_towards(needed_collectors) def _teardown_towards(self, needed_collectors): exc = None while self.stack: if self.stack == needed_collectors[: len(self.stack)]: break try: self._pop_and_teardown() except TEST_OUTCOME: # XXX Only first exception will be seen by user, # ideally all should be reported. if exc is None: exc = sys.exc_info() if exc: six.reraise(*exc) def prepare(self, colitem): """ setup objects along the collector chain to the test-method and teardown previously setup objects.""" needed_collectors = colitem.listchain() self._teardown_towards(needed_collectors) # check if the last collection node has raised an error for col in self.stack: if hasattr(col, "_prepare_exc"): six.reraise(*col._prepare_exc) for col in needed_collectors[len(self.stack) :]: self.stack.append(col) try: col.setup() except TEST_OUTCOME: col._prepare_exc = sys.exc_info() raise def collect_one_node(collector): ihook = collector.ihook ihook.pytest_collectstart(collector=collector) rep = ihook.pytest_make_collect_report(collector=collector) call = rep.__dict__.pop("call", None) if call and check_interactive_exception(call, rep): ihook.pytest_exception_interact(node=collector, call=call, report=rep) return rep pytest-4.6.9/src/_pytest/unittest.py0000664000372000037200000002277613604173400020447 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ discovery and running of std-library "unittest" style tests. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import traceback import _pytest._code import pytest from _pytest.compat import getimfunc from _pytest.config import hookimpl from _pytest.outcomes import fail from _pytest.outcomes import skip from _pytest.outcomes import xfail from _pytest.python import Class from _pytest.python import Function def pytest_pycollect_makeitem(collector, name, obj): # has unittest been imported and is obj a subclass of its TestCase? try: if not issubclass(obj, sys.modules["unittest"].TestCase): return except Exception: return # yes, so let's collect it return UnitTestCase(name, parent=collector) class UnitTestCase(Class): # marker for fixturemanger.getfixtureinfo() # to declare that our children do not support funcargs nofuncargs = True def collect(self): from unittest import TestLoader cls = self.obj if not getattr(cls, "__test__", True): return skipped = getattr(cls, "__unittest_skip__", False) if not skipped: self._inject_setup_teardown_fixtures(cls) self._inject_setup_class_fixture() self.session._fixturemanager.parsefactories(self, unittest=True) loader = TestLoader() foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue funcobj = getimfunc(x) yield TestCaseFunction(name, parent=self, callobj=funcobj) foundsomething = True if not foundsomething: runtest = getattr(self.obj, "runTest", None) if runtest is not None: ut = sys.modules.get("twisted.trial.unittest", None) if ut is None or runtest != ut.TestCase.runTest: yield TestCaseFunction("runTest", parent=self) def _inject_setup_teardown_fixtures(self, cls): """Injects a hidden auto-use fixture to invoke setUpClass/setup_method and corresponding teardown functions (#517)""" class_fixture = _make_xunit_fixture( cls, "setUpClass", "tearDownClass", scope="class", pass_self=False ) if class_fixture: cls.__pytest_class_setup = class_fixture method_fixture = _make_xunit_fixture( cls, "setup_method", "teardown_method", scope="function", pass_self=True ) if method_fixture: cls.__pytest_method_setup = method_fixture def _make_xunit_fixture(obj, setup_name, teardown_name, scope, pass_self): setup = getattr(obj, setup_name, None) teardown = getattr(obj, teardown_name, None) if setup is None and teardown is None: return None @pytest.fixture(scope=scope, autouse=True) def fixture(self, request): if getattr(self, "__unittest_skip__", None): reason = self.__unittest_skip_why__ pytest.skip(reason) if setup is not None: if pass_self: setup(self, request.function) else: setup() yield if teardown is not None: if pass_self: teardown(self, request.function) else: teardown() return fixture class TestCaseFunction(Function): nofuncargs = True _excinfo = None _testcase = None def setup(self): self._testcase = self.parent.obj(self.name) self._fix_unittest_skip_decorator() self._obj = getattr(self._testcase, self.name) if hasattr(self, "_request"): self._request._fillfixtures() def _fix_unittest_skip_decorator(self): """ The @unittest.skip decorator calls functools.wraps(self._testcase) The call to functools.wraps() fails unless self._testcase has a __name__ attribute. This is usually automatically supplied if the test is a function or method, but we need to add manually here. See issue #1169 """ if sys.version_info[0] == 2: setattr(self._testcase, "__name__", self.name) def teardown(self): self._testcase = None self._obj = None def startTest(self, testcase): pass def _addexcinfo(self, rawexcinfo): # unwrap potential exception info (see twisted trial support below) rawexcinfo = getattr(rawexcinfo, "_rawexcinfo", rawexcinfo) try: excinfo = _pytest._code.ExceptionInfo(rawexcinfo) # invoke the attributes to trigger storing the traceback # trial causes some issue there excinfo.value excinfo.traceback except TypeError: try: try: values = traceback.format_exception(*rawexcinfo) values.insert( 0, "NOTE: Incompatible Exception Representation, " "displaying natively:\n\n", ) fail("".join(values), pytrace=False) except (fail.Exception, KeyboardInterrupt): raise except: # noqa fail( "ERROR: Unknown Incompatible Exception " "representation:\n%r" % (rawexcinfo,), pytrace=False, ) except KeyboardInterrupt: raise except fail.Exception: excinfo = _pytest._code.ExceptionInfo.from_current() self.__dict__.setdefault("_excinfo", []).append(excinfo) def addError(self, testcase, rawexcinfo): self._addexcinfo(rawexcinfo) def addFailure(self, testcase, rawexcinfo): self._addexcinfo(rawexcinfo) def addSkip(self, testcase, reason): try: skip(reason) except skip.Exception: self._skipped_by_mark = True self._addexcinfo(sys.exc_info()) def addExpectedFailure(self, testcase, rawexcinfo, reason=""): try: xfail(str(reason)) except xfail.Exception: self._addexcinfo(sys.exc_info()) def addUnexpectedSuccess(self, testcase, reason=""): self._unexpectedsuccess = reason def addSuccess(self, testcase): pass def stopTest(self, testcase): pass def _handle_skip(self): # implements the skipping machinery (see #2137) # analog to pythons Lib/unittest/case.py:run testMethod = getattr(self._testcase, self._testcase._testMethodName) if getattr(self._testcase.__class__, "__unittest_skip__", False) or getattr( testMethod, "__unittest_skip__", False ): # If the class or method was skipped. skip_why = getattr( self._testcase.__class__, "__unittest_skip_why__", "" ) or getattr(testMethod, "__unittest_skip_why__", "") try: # PY3, unittest2 on PY2 self._testcase._addSkip(self, self._testcase, skip_why) except TypeError: # PY2 if sys.version_info[0] != 2: raise self._testcase._addSkip(self, skip_why) return True return False def runtest(self): if self.config.pluginmanager.get_plugin("pdbinvoke") is None: self._testcase(result=self) else: # disables tearDown and cleanups for post mortem debugging (see #1890) if self._handle_skip(): return self._testcase.debug() def _prunetraceback(self, excinfo): Function._prunetraceback(self, excinfo) traceback = excinfo.traceback.filter( lambda x: not x.frame.f_globals.get("__unittest") ) if traceback: excinfo.traceback = traceback @hookimpl(tryfirst=True) def pytest_runtest_makereport(item, call): if isinstance(item, TestCaseFunction): if item._excinfo: call.excinfo = item._excinfo.pop(0) try: del call.result except AttributeError: pass # twisted trial support @hookimpl(hookwrapper=True) def pytest_runtest_protocol(item): if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules: ut = sys.modules["twisted.python.failure"] Failure__init__ = ut.Failure.__init__ check_testcase_implements_trial_reporter() def excstore( self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None ): if exc_value is None: self._rawexcinfo = sys.exc_info() else: if exc_type is None: exc_type = type(exc_value) self._rawexcinfo = (exc_type, exc_value, exc_tb) try: Failure__init__( self, exc_value, exc_type, exc_tb, captureVars=captureVars ) except TypeError: Failure__init__(self, exc_value, exc_type, exc_tb) ut.Failure.__init__ = excstore yield ut.Failure.__init__ = Failure__init__ else: yield def check_testcase_implements_trial_reporter(done=[]): if done: return from zope.interface import classImplements from twisted.trial.itrial import IReporter classImplements(TestCaseFunction, IReporter) done.append(1) pytest-4.6.9/src/_pytest/warnings.py0000664000372000037200000001360713604173400020411 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import warnings from contextlib import contextmanager import pytest from _pytest import compat SHOW_PYTEST_WARNINGS_ARG = "-Walways::pytest.RemovedInPytest4Warning" def _setoption(wmod, arg): """ Copy of the warning._setoption function but does not escape arguments. """ parts = arg.split(":") if len(parts) > 5: raise wmod._OptionError("too many fields (max 5): %r" % (arg,)) while len(parts) < 5: parts.append("") action, message, category, module, lineno = [s.strip() for s in parts] action = wmod._getaction(action) category = wmod._getcategory(category) if lineno: try: lineno = int(lineno) if lineno < 0: raise ValueError except (ValueError, OverflowError): raise wmod._OptionError("invalid lineno %r" % (lineno,)) else: lineno = 0 wmod.filterwarnings(action, message, category, module, lineno) def pytest_addoption(parser): group = parser.getgroup("pytest-warnings") group.addoption( "-W", "--pythonwarnings", action="append", help="set which warnings to report, see -W option of python itself.", ) parser.addini( "filterwarnings", type="linelist", help="Each line specifies a pattern for " "warnings.filterwarnings. " "Processed after -W and --pythonwarnings.", ) def pytest_configure(config): config.addinivalue_line( "markers", "filterwarnings(warning): add a warning filter to the given test. " "see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings ", ) @contextmanager def catch_warnings_for_item(config, ihook, when, item): """ Context manager that catches warnings generated in the contained execution block. ``item`` can be None if we are not in the context of an item execution. Each warning captured triggers the ``pytest_warning_captured`` hook. """ cmdline_filters = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: if not sys.warnoptions: # if user is not explicitly configuring warning filters, show deprecation warnings by default (#2908) warnings.filterwarnings("always", category=DeprecationWarning) warnings.filterwarnings("always", category=PendingDeprecationWarning) warnings.filterwarnings("error", category=pytest.RemovedInPytest4Warning) # filters should have this precedence: mark, cmdline options, ini # filters should be applied in the inverse order of precedence for arg in inifilters: _setoption(warnings, arg) for arg in cmdline_filters: warnings._setoption(arg) if item is not None: for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: _setoption(warnings, arg) yield for warning_message in log: ihook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=warning_message, when=when, item=item) ) def warning_record_to_str(warning_message): """Convert a warnings.WarningMessage to a string. This takes lot of unicode shenaningans into account for Python 2. When Python 2 support is dropped this function can be greatly simplified. """ warn_msg = warning_message.message unicode_warning = False if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args): new_args = [] for m in warn_msg.args: new_args.append( compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES) else m ) unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args msg = warnings.formatwarning( warn_msg, warning_message.category, warning_message.filename, warning_message.lineno, warning_message.line, ) if unicode_warning: warnings.warn( "Warning is using unicode non convertible to ascii, " "converting to a safe representation:\n {!r}".format(compat.safe_str(msg)), UnicodeWarning, ) return msg @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_protocol(item): with catch_warnings_for_item( config=item.config, ihook=item.ihook, when="runtest", item=item ): yield @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_collection(session): config = session.config with catch_warnings_for_item( config=config, ihook=config.hook, when="collect", item=None ): yield @pytest.hookimpl(hookwrapper=True) def pytest_terminal_summary(terminalreporter): config = terminalreporter.config with catch_warnings_for_item( config=config, ihook=config.hook, when="config", item=None ): yield def _issue_warning_captured(warning, hook, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: at this point the actual options might not have been set, so we manually trigger the pytest_warning_captured hook so we can display this warnings in the terminal. This is a hack until we can sort out #2891. :param warning: the warning instance. :param hook: the hook caller :param stacklevel: stacklevel forwarded to warnings.warn """ with warnings.catch_warnings(record=True) as records: warnings.simplefilter("always", type(warning)) warnings.warn(warning, stacklevel=stacklevel) hook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=records[0], when="config", item=None) ) pytest-4.6.9/src/_pytest/hookspec.py0000664000372000037200000005146113604173400020374 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ hook specifications for pytest plugins, invoked from main.py and builtin plugins. """ from pluggy import HookspecMarker from _pytest.deprecated import PYTEST_LOGWARNING hookspec = HookspecMarker("pytest") # ------------------------------------------------------------------------- # Initialization hooks called for every plugin # ------------------------------------------------------------------------- @hookspec(historic=True) def pytest_addhooks(pluginmanager): """called at plugin registration time to allow adding new hooks via a call to ``pluginmanager.add_hookspecs(module_or_class, prefix)``. :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager .. note:: This hook is incompatible with ``hookwrapper=True``. """ @hookspec(historic=True) def pytest_plugin_registered(plugin, manager): """ a new pytest plugin got registered. :param plugin: the plugin module or instance :param _pytest.config.PytestPluginManager manager: pytest plugin manager .. note:: This hook is incompatible with ``hookwrapper=True``. """ @hookspec(historic=True) def pytest_addoption(parser): """register argparse-style options and ini-style config values, called once at the beginning of a test run. .. note:: This function should be implemented only in plugins or ``conftest.py`` files situated at the tests root directory due to how pytest :ref:`discovers plugins during startup `. :arg _pytest.config.Parser parser: To add command line options, call :py:func:`parser.addoption(...) <_pytest.config.Parser.addoption>`. To add ini-file values call :py:func:`parser.addini(...) <_pytest.config.Parser.addini>`. Options can later be accessed through the :py:class:`config <_pytest.config.Config>` object, respectively: - :py:func:`config.getoption(name) <_pytest.config.Config.getoption>` to retrieve the value of a command line option. - :py:func:`config.getini(name) <_pytest.config.Config.getini>` to retrieve a value read from an ini-style file. The config object is passed around on many internal objects via the ``.config`` attribute or can be retrieved as the ``pytestconfig`` fixture. .. note:: This hook is incompatible with ``hookwrapper=True``. """ @hookspec(historic=True) def pytest_configure(config): """ Allows plugins and conftest files to perform initial configuration. This hook is called for every plugin and initial conftest file after command line options have been parsed. After that, the hook is called for other conftest files as they are imported. .. note:: This hook is incompatible with ``hookwrapper=True``. :arg _pytest.config.Config config: pytest config object """ # ------------------------------------------------------------------------- # Bootstrapping hooks called for plugins registered early enough: # internal and 3rd party plugins. # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_cmdline_parse(pluginmanager, args): """return initialized config object, parsing the specified args. Stops at first non-None result, see :ref:`firstresult` .. note:: This hook will only be called for plugin classes passed to the ``plugins`` arg when using `pytest.main`_ to perform an in-process test run. :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager :param list[str] args: list of arguments passed on the command line """ def pytest_cmdline_preparse(config, args): """(**Deprecated**) modify command line arguments before option parsing. This hook is considered deprecated and will be removed in a future pytest version. Consider using :func:`pytest_load_initial_conftests` instead. .. note:: This hook will not be called for ``conftest.py`` files, only for setuptools plugins. :param _pytest.config.Config config: pytest config object :param list[str] args: list of arguments passed on the command line """ @hookspec(firstresult=True) def pytest_cmdline_main(config): """ called for performing the main command line action. The default implementation will invoke the configure hooks and runtest_mainloop. .. note:: This hook will not be called for ``conftest.py`` files, only for setuptools plugins. Stops at first non-None result, see :ref:`firstresult` :param _pytest.config.Config config: pytest config object """ def pytest_load_initial_conftests(early_config, parser, args): """ implements the loading of initial conftest files ahead of command line option parsing. .. note:: This hook will not be called for ``conftest.py`` files, only for setuptools plugins. :param _pytest.config.Config early_config: pytest config object :param list[str] args: list of arguments passed on the command line :param _pytest.config.Parser parser: to add command line options """ # ------------------------------------------------------------------------- # collection hooks # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_collection(session): """Perform the collection protocol for the given session. Stops at first non-None result, see :ref:`firstresult`. :param _pytest.main.Session session: the pytest session object """ def pytest_collection_modifyitems(session, config, items): """ called after collection has been performed, may filter or re-order the items in-place. :param _pytest.main.Session session: the pytest session object :param _pytest.config.Config config: pytest config object :param List[_pytest.nodes.Item] items: list of item objects """ def pytest_collection_finish(session): """ called after collection has been performed and modified. :param _pytest.main.Session session: the pytest session object """ @hookspec(firstresult=True) def pytest_ignore_collect(path, config): """ return True to prevent considering this path for collection. This hook is consulted for all files and directories prior to calling more specific hooks. Stops at first non-None result, see :ref:`firstresult` :param path: a :py:class:`py.path.local` - the path to analyze :param _pytest.config.Config config: pytest config object """ @hookspec(firstresult=True) def pytest_collect_directory(path, parent): """ called before traversing a directory for collection files. Stops at first non-None result, see :ref:`firstresult` :param path: a :py:class:`py.path.local` - the path to analyze """ def pytest_collect_file(path, parent): """ return collection Node or None for the given path. Any new node needs to have the specified ``parent`` as a parent. :param path: a :py:class:`py.path.local` - the path to collect """ # logging hooks for collection def pytest_collectstart(collector): """ collector starts collecting. """ def pytest_itemcollected(item): """ we just collected a test item. """ def pytest_collectreport(report): """ collector finished collecting. """ def pytest_deselected(items): """ called for test items deselected, e.g. by keyword. """ @hookspec(firstresult=True) def pytest_make_collect_report(collector): """ perform ``collector.collect()`` and return a CollectReport. Stops at first non-None result, see :ref:`firstresult` """ # ------------------------------------------------------------------------- # Python test function related hooks # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_pycollect_makemodule(path, parent): """ return a Module collector or None for the given path. This hook will be called for each matching test module path. The pytest_collect_file hook needs to be used if you want to create test modules for files that do not match as a test module. Stops at first non-None result, see :ref:`firstresult` :param path: a :py:class:`py.path.local` - the path of module to collect """ @hookspec(firstresult=True) def pytest_pycollect_makeitem(collector, name, obj): """ return custom item/collector for a python object in a module, or None. Stops at first non-None result, see :ref:`firstresult` """ @hookspec(firstresult=True) def pytest_pyfunc_call(pyfuncitem): """ call underlying test function. Stops at first non-None result, see :ref:`firstresult` """ def pytest_generate_tests(metafunc): """ generate (multiple) parametrized calls to a test function.""" @hookspec(firstresult=True) def pytest_make_parametrize_id(config, val, argname): """Return a user-friendly string representation of the given ``val`` that will be used by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``. The parameter name is available as ``argname``, if required. Stops at first non-None result, see :ref:`firstresult` :param _pytest.config.Config config: pytest config object :param val: the parametrized value :param str argname: the automatic parameter name produced by pytest """ # ------------------------------------------------------------------------- # generic runtest related hooks # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_runtestloop(session): """ called for performing the main runtest loop (after collection finished). Stops at first non-None result, see :ref:`firstresult` :param _pytest.main.Session session: the pytest session object """ def pytest_itemstart(item, node): """(**Deprecated**) use pytest_runtest_logstart. """ @hookspec(firstresult=True) def pytest_runtest_protocol(item, nextitem): """ implements the runtest_setup/call/teardown protocol for the given test item, including capturing exceptions and calling reporting hooks. :arg item: test item for which the runtest protocol is performed. :arg nextitem: the scheduled-to-be-next test item (or None if this is the end my friend). This argument is passed on to :py:func:`pytest_runtest_teardown`. :return boolean: True if no further hook implementations should be invoked. Stops at first non-None result, see :ref:`firstresult` """ def pytest_runtest_logstart(nodeid, location): """ signal the start of running a single test item. This hook will be called **before** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and :func:`pytest_runtest_teardown` hooks. :param str nodeid: full id of the item :param location: a triple of ``(filename, linenum, testname)`` """ def pytest_runtest_logfinish(nodeid, location): """ signal the complete finish of running a single test item. This hook will be called **after** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and :func:`pytest_runtest_teardown` hooks. :param str nodeid: full id of the item :param location: a triple of ``(filename, linenum, testname)`` """ def pytest_runtest_setup(item): """ called before ``pytest_runtest_call(item)``. """ def pytest_runtest_call(item): """ called to execute the test ``item``. """ def pytest_runtest_teardown(item, nextitem): """ called after ``pytest_runtest_call``. :arg nextitem: the scheduled-to-be-next test item (None if no further test item is scheduled). This argument can be used to perform exact teardowns, i.e. calling just enough finalizers so that nextitem only needs to call setup-functions. """ @hookspec(firstresult=True) def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`pytest.Item <_pytest.main.Item>` and :py:class:`_pytest.runner.CallInfo`. Stops at first non-None result, see :ref:`firstresult` """ def pytest_runtest_logreport(report): """ process a test setup/call/teardown report relating to the respective phase of executing a test. """ @hookspec(firstresult=True) def pytest_report_to_serializable(config, report): """ .. warning:: This hook is experimental and subject to change between pytest releases, even bug fixes. The intent is for this to be used by plugins maintained by the core-devs, such as ``pytest-xdist``, ``pytest-subtests``, and as a replacement for the internal 'resultlog' plugin. In the future it might become part of the public hook API. Serializes the given report object into a data structure suitable for sending over the wire, e.g. converted to JSON. """ @hookspec(firstresult=True) def pytest_report_from_serializable(config, data): """ .. warning:: This hook is experimental and subject to change between pytest releases, even bug fixes. The intent is for this to be used by plugins maintained by the core-devs, such as ``pytest-xdist``, ``pytest-subtests``, and as a replacement for the internal 'resultlog' plugin. In the future it might become part of the public hook API. Restores a report object previously serialized with pytest_report_to_serializable(). """ # ------------------------------------------------------------------------- # Fixture related hooks # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_fixture_setup(fixturedef, request): """ performs fixture setup execution. :return: The return value of the call to the fixture function Stops at first non-None result, see :ref:`firstresult` .. note:: If the fixture function returns None, other implementations of this hook function will continue to be called, according to the behavior of the :ref:`firstresult` option. """ def pytest_fixture_post_finalizer(fixturedef, request): """ called after fixture teardown, but before the cache is cleared so the fixture result cache ``fixturedef.cached_result`` can still be accessed.""" # ------------------------------------------------------------------------- # test session related hooks # ------------------------------------------------------------------------- def pytest_sessionstart(session): """ called after the ``Session`` object has been created and before performing collection and entering the run test loop. :param _pytest.main.Session session: the pytest session object """ def pytest_sessionfinish(session, exitstatus): """ called after whole test run finished, right before returning the exit status to the system. :param _pytest.main.Session session: the pytest session object :param int exitstatus: the status which pytest will return to the system """ def pytest_unconfigure(config): """ called before test process is exited. :param _pytest.config.Config config: pytest config object """ # ------------------------------------------------------------------------- # hooks for customizing the assert methods # ------------------------------------------------------------------------- def pytest_assertrepr_compare(config, op, left, right): """return explanation for comparisons in failing assert expressions. Return None for no custom explanation, otherwise return a list of strings. The strings will be joined by newlines but any newlines *in* a string will be escaped. Note that all but the first line will be indented slightly, the intention is for the first line to be a summary. :param _pytest.config.Config config: pytest config object """ # ------------------------------------------------------------------------- # hooks for influencing reporting (invoked from _pytest_terminal) # ------------------------------------------------------------------------- def pytest_report_header(config, startdir): """ return a string or list of strings to be displayed as header info for terminal reporting. :param _pytest.config.Config config: pytest config object :param startdir: py.path object with the starting dir .. note:: This function should be implemented only in plugins or ``conftest.py`` files situated at the tests root directory due to how pytest :ref:`discovers plugins during startup `. """ def pytest_report_collectionfinish(config, startdir, items): """ .. versionadded:: 3.2 return a string or list of strings to be displayed after collection has finished successfully. This strings will be displayed after the standard "collected X items" message. :param _pytest.config.Config config: pytest config object :param startdir: py.path object with the starting dir :param items: list of pytest items that are going to be executed; this list should not be modified. """ @hookspec(firstresult=True) def pytest_report_teststatus(report, config): """ return result-category, shortletter and verbose word for reporting. :param _pytest.config.Config config: pytest config object Stops at first non-None result, see :ref:`firstresult` """ def pytest_terminal_summary(terminalreporter, exitstatus, config): """Add a section to terminal summary reporting. :param _pytest.terminal.TerminalReporter terminalreporter: the internal terminal reporter object :param int exitstatus: the exit status that will be reported back to the OS :param _pytest.config.Config config: pytest config object .. versionadded:: 4.2 The ``config`` parameter. """ @hookspec(historic=True, warn_on_impl=PYTEST_LOGWARNING) def pytest_logwarning(message, code, nodeid, fslocation): """ .. deprecated:: 3.8 This hook is will stop working in a future release. pytest no longer triggers this hook, but the terminal writer still implements it to display warnings issued by :meth:`_pytest.config.Config.warn` and :meth:`_pytest.nodes.Node.warn`. Calling those functions will be an error in future releases. process a warning specified by a message, a code string, a nodeid and fslocation (both of which may be None if the warning is not tied to a particular node/location). .. note:: This hook is incompatible with ``hookwrapper=True``. """ @hookspec(historic=True) def pytest_warning_captured(warning_message, when, item): """ Process a warning captured by the internal pytest warnings plugin. :param warnings.WarningMessage warning_message: The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains the same attributes as the parameters of :py:func:`warnings.showwarning`. :param str when: Indicates when the warning was captured. Possible values: * ``"config"``: during pytest configuration/initialization stage. * ``"collect"``: during test collection. * ``"runtest"``: during test execution. :param pytest.Item|None item: **DEPRECATED**: This parameter is incompatible with ``pytest-xdist``, and will always receive ``None`` in a future release. The item being executed if ``when`` is ``"runtest"``, otherwise ``None``. """ # ------------------------------------------------------------------------- # doctest hooks # ------------------------------------------------------------------------- @hookspec(firstresult=True) def pytest_doctest_prepare_content(content): """ return processed content for a given doctest Stops at first non-None result, see :ref:`firstresult` """ # ------------------------------------------------------------------------- # error handling and internal debugging hooks # ------------------------------------------------------------------------- def pytest_internalerror(excrepr, excinfo): """ called for internal errors. """ def pytest_keyboard_interrupt(excinfo): """ called for keyboard interrupt. """ def pytest_exception_interact(node, call, report): """called when an exception was raised which can potentially be interactively handled. This hook is only called if an exception was raised that is not an internal exception like ``skip.Exception``. """ def pytest_enter_pdb(config, pdb): """ called upon pdb.set_trace(), can be used by plugins to take special action just before the python debugger enters in interactive mode. :param _pytest.config.Config config: pytest config object :param pdb.Pdb pdb: Pdb instance """ def pytest_leave_pdb(config, pdb): """ called when leaving pdb (e.g. with continue after pdb.set_trace()). Can be used by plugins to take special action just after the python debugger leaves interactive mode. :param _pytest.config.Config config: pytest config object :param pdb.Pdb pdb: Pdb instance """ pytest-4.6.9/src/_pytest/_version.py0000664000372000037200000000016413604173421020402 0ustar travistravis00000000000000# coding: utf-8 # file generated by setuptools_scm # don't change, don't track in version control version = '4.6.9' pytest-4.6.9/src/_pytest/pastebin.py0000664000372000037200000000705113604173400020362 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ submit failure or test session information to a pastebin service. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import tempfile import six import pytest def pytest_addoption(parser): group = parser.getgroup("terminal reporting") group._addoption( "--pastebin", metavar="mode", action="store", dest="pastebin", default=None, choices=["failed", "all"], help="send failed|all info to bpaste.net pastebin service.", ) @pytest.hookimpl(trylast=True) def pytest_configure(config): if config.option.pastebin == "all": tr = config.pluginmanager.getplugin("terminalreporter") # if no terminal reporter plugin is present, nothing we can do here; # this can happen when this function executes in a slave node # when using pytest-xdist, for example if tr is not None: # pastebin file will be utf-8 encoded binary file config._pastebinfile = tempfile.TemporaryFile("w+b") oldwrite = tr._tw.write def tee_write(s, **kwargs): oldwrite(s, **kwargs) if isinstance(s, six.text_type): s = s.encode("utf-8") config._pastebinfile.write(s) tr._tw.write = tee_write def pytest_unconfigure(config): if hasattr(config, "_pastebinfile"): # get terminal contents and delete file config._pastebinfile.seek(0) sessionlog = config._pastebinfile.read() config._pastebinfile.close() del config._pastebinfile # undo our patching in the terminal reporter tr = config.pluginmanager.getplugin("terminalreporter") del tr._tw.__dict__["write"] # write summary tr.write_sep("=", "Sending information to Paste Service") pastebinurl = create_new_paste(sessionlog) tr.write_line("pastebin session-log: %s\n" % pastebinurl) def create_new_paste(contents): """ Creates a new paste using bpaste.net service. :contents: paste contents as utf-8 encoded bytes :returns: url to the pasted contents """ import re if sys.version_info < (3, 0): from urllib import urlopen, urlencode else: from urllib.request import urlopen from urllib.parse import urlencode params = {"code": contents, "lexer": "text", "expiry": "1week"} url = "https://bpaste.net" response = urlopen(url, data=urlencode(params).encode("ascii")).read() m = re.search(r'href="/raw/(\w+)"', response.decode("utf-8")) if m: return "%s/show/%s" % (url, m.group(1)) else: return "bad response: " + response def pytest_terminal_summary(terminalreporter): import _pytest.config if terminalreporter.config.option.pastebin != "failed": return tr = terminalreporter if "failed" in tr.stats: terminalreporter.write_sep("=", "Sending information to Paste Service") for rep in terminalreporter.stats.get("failed"): try: msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc except AttributeError: msg = tr._getfailureheadline(rep) tw = _pytest.config.create_terminal_writer( terminalreporter.config, stringio=True ) rep.toterminal(tw) s = tw.stringio.getvalue() assert len(s) pastebinurl = create_new_paste(s) tr.write_line("%s --> %s" % (msg, pastebinurl)) pytest-4.6.9/src/_pytest/setupplan.py0000664000372000037200000000157313604173400020573 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import pytest def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption( "--setupplan", "--setup-plan", action="store_true", help="show what fixtures and tests would be executed but " "don't execute anything.", ) @pytest.hookimpl(tryfirst=True) def pytest_fixture_setup(fixturedef, request): # Will return a dummy fixture if the setuponly option is provided. if request.config.option.setupplan: fixturedef.cached_result = (None, None, None) return fixturedef.cached_result @pytest.hookimpl(tryfirst=True) def pytest_cmdline_main(config): if config.option.setupplan: config.option.setuponly = True config.option.setupshow = True pytest-4.6.9/src/_pytest/resultlog.py0000664000372000037200000000641413604173400020577 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ log machine-parseable test session result information in a plain text file. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import py def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "resultlog plugin options") group.addoption( "--resultlog", "--result-log", action="store", metavar="path", default=None, help="DEPRECATED path for machine-readable result log.", ) def pytest_configure(config): resultlog = config.option.resultlog # prevent opening resultlog on slave nodes (xdist) if resultlog and not hasattr(config, "slaveinput"): dirname = os.path.dirname(os.path.abspath(resultlog)) if not os.path.isdir(dirname): os.makedirs(dirname) logfile = open(resultlog, "w", 1) # line buffered config._resultlog = ResultLog(config, logfile) config.pluginmanager.register(config._resultlog) from _pytest.deprecated import RESULT_LOG from _pytest.warnings import _issue_warning_captured _issue_warning_captured(RESULT_LOG, config.hook, stacklevel=2) def pytest_unconfigure(config): resultlog = getattr(config, "_resultlog", None) if resultlog: resultlog.logfile.close() del config._resultlog config.pluginmanager.unregister(resultlog) class ResultLog(object): def __init__(self, config, logfile): self.config = config self.logfile = logfile # preferably line buffered def write_log_entry(self, testpath, lettercode, longrepr): print("%s %s" % (lettercode, testpath), file=self.logfile) for line in longrepr.splitlines(): print(" %s" % line, file=self.logfile) def log_outcome(self, report, lettercode, longrepr): testpath = getattr(report, "nodeid", None) if testpath is None: testpath = report.fspath self.write_log_entry(testpath, lettercode, longrepr) def pytest_runtest_logreport(self, report): if report.when != "call" and report.passed: return res = self.config.hook.pytest_report_teststatus( report=report, config=self.config ) code = res[1] if code == "x": longrepr = str(report.longrepr) elif code == "X": longrepr = "" elif report.passed: longrepr = "" elif report.failed: longrepr = str(report.longrepr) elif report.skipped: longrepr = str(report.longrepr[2]) self.log_outcome(report, code, longrepr) def pytest_collectreport(self, report): if not report.passed: if report.failed: code = "F" longrepr = str(report.longrepr) else: assert report.skipped code = "S" longrepr = "%s:%d: %s" % report.longrepr self.log_outcome(report, code, longrepr) def pytest_internalerror(self, excrepr): reprcrash = getattr(excrepr, "reprcrash", None) path = getattr(reprcrash, "path", None) if path is None: path = "cwd:%s" % py.path.local() self.write_log_entry(path, "!", str(excrepr)) pytest-4.6.9/src/_pytest/tmpdir.py0000664000372000037200000001442713604173400020061 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ support for providing temporary directories to test functions. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import re import tempfile import warnings import attr import py import six import pytest from .pathlib import ensure_reset_dir from .pathlib import LOCK_TIMEOUT from .pathlib import make_numbered_dir from .pathlib import make_numbered_dir_with_cleanup from .pathlib import Path from _pytest.monkeypatch import MonkeyPatch @attr.s class TempPathFactory(object): """Factory for temporary directories under the common base temp directory. The base directory can be configured using the ``--basetemp`` option.""" _given_basetemp = attr.ib( # using os.path.abspath() to get absolute path instead of resolve() as it # does not work the same in all platforms (see #4427) # Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012) converter=attr.converters.optional( lambda p: Path(os.path.abspath(six.text_type(p))) ) ) _trace = attr.ib() _basetemp = attr.ib(default=None) @classmethod def from_config(cls, config): """ :param config: a pytest configuration """ return cls( given_basetemp=config.option.basetemp, trace=config.trace.get("tmpdir") ) def mktemp(self, basename, numbered=True): """makes a temporary directory managed by the factory""" if not numbered: p = self.getbasetemp().joinpath(basename) p.mkdir() else: p = make_numbered_dir(root=self.getbasetemp(), prefix=basename) self._trace("mktemp", p) return p def getbasetemp(self): """ return base temporary directory. """ if self._basetemp is not None: return self._basetemp if self._given_basetemp is not None: basetemp = self._given_basetemp ensure_reset_dir(basetemp) basetemp = basetemp.resolve() else: from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT") temproot = Path(from_env or tempfile.gettempdir()).resolve() user = get_user() or "unknown" # use a sub-directory in the temproot to speed-up # make_numbered_dir() call rootdir = temproot.joinpath("pytest-of-{}".format(user)) rootdir.mkdir(exist_ok=True) basetemp = make_numbered_dir_with_cleanup( prefix="pytest-", root=rootdir, keep=3, lock_timeout=LOCK_TIMEOUT ) assert basetemp is not None, basetemp self._basetemp = t = basetemp self._trace("new basetemp", t) return t @attr.s class TempdirFactory(object): """ backward comptibility wrapper that implements :class:``py.path.local`` for :class:``TempPathFactory`` """ _tmppath_factory = attr.ib() def ensuretemp(self, string, dir=1): """ (deprecated) return temporary directory path with the given string as the trailing part. It is usually better to use the 'tmpdir' function argument which provides an empty unique-per-test-invocation directory and is guaranteed to be empty. """ # py.log._apiwarn(">1.1", "use tmpdir function argument") from .deprecated import PYTEST_ENSURETEMP warnings.warn(PYTEST_ENSURETEMP, stacklevel=2) return self.getbasetemp().ensure(string, dir=dir) def mktemp(self, basename, numbered=True): """Create a subdirectory of the base temporary directory and return it. If ``numbered``, ensure the directory is unique by adding a number prefix greater than any existing one. """ return py.path.local(self._tmppath_factory.mktemp(basename, numbered).resolve()) def getbasetemp(self): """backward compat wrapper for ``_tmppath_factory.getbasetemp``""" return py.path.local(self._tmppath_factory.getbasetemp().resolve()) def get_user(): """Return the current user name, or None if getuser() does not work in the current environment (see #1010). """ import getpass try: return getpass.getuser() except (ImportError, KeyError): return None def pytest_configure(config): """Create a TempdirFactory and attach it to the config object. This is to comply with existing plugins which expect the handler to be available at pytest_configure time, but ideally should be moved entirely to the tmpdir_factory session fixture. """ mp = MonkeyPatch() tmppath_handler = TempPathFactory.from_config(config) t = TempdirFactory(tmppath_handler) config._cleanup.append(mp.undo) mp.setattr(config, "_tmp_path_factory", tmppath_handler, raising=False) mp.setattr(config, "_tmpdirhandler", t, raising=False) mp.setattr(pytest, "ensuretemp", t.ensuretemp, raising=False) @pytest.fixture(scope="session") def tmpdir_factory(request): """Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session. """ return request.config._tmpdirhandler @pytest.fixture(scope="session") def tmp_path_factory(request): """Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session. """ return request.config._tmp_path_factory def _mk_tmp(request, factory): name = request.node.name name = re.sub(r"[\W]", "_", name) MAXVAL = 30 name = name[:MAXVAL] return factory.mktemp(name, numbered=True) @pytest.fixture def tmpdir(tmp_path): """Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html """ return py.path.local(tmp_path) @pytest.fixture def tmp_path(request, tmp_path_factory): """Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a :class:`pathlib.Path` object. .. note:: in python < 3.6 this is a pathlib2.Path """ return _mk_tmp(request, tmp_path_factory) pytest-4.6.9/src/_pytest/nose.py0000664000372000037200000000435313604173400017523 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ run test suites written for nose. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import six import pytest from _pytest import python from _pytest import runner from _pytest import unittest from _pytest.config import hookimpl def get_skip_exceptions(): skip_classes = set() for module_name in ("unittest", "unittest2", "nose"): mod = sys.modules.get(module_name) if hasattr(mod, "SkipTest"): skip_classes.add(mod.SkipTest) return tuple(skip_classes) def pytest_runtest_makereport(item, call): if call.excinfo and call.excinfo.errisinstance(get_skip_exceptions()): # let's substitute the excinfo with a pytest.skip one call2 = runner.CallInfo.from_call( lambda: pytest.skip(six.text_type(call.excinfo.value)), call.when ) call.excinfo = call2.excinfo @hookimpl(trylast=True) def pytest_runtest_setup(item): if is_potential_nosetest(item): if not call_optional(item.obj, "setup"): # call module level setup if there is no object level one call_optional(item.parent.obj, "setup") # XXX this implies we only call teardown when setup worked item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item) def teardown_nose(item): if is_potential_nosetest(item): if not call_optional(item.obj, "teardown"): call_optional(item.parent.obj, "teardown") # if hasattr(item.parent, '_nosegensetup'): # #call_optional(item._nosegensetup, 'teardown') # del item.parent._nosegensetup def is_potential_nosetest(item): # extra check needed since we do not do nose style setup/teardown # on direct unittest style classes return isinstance(item, python.Function) and not isinstance( item, unittest.TestCaseFunction ) def call_optional(obj, name): method = getattr(obj, name, None) isfixture = hasattr(method, "_pytestfixturefunction") if method is not None and not isfixture and callable(method): # If there's any problems allow the exception to raise rather than # silently ignoring them method() return True pytest-4.6.9/src/_pytest/outcomes.py0000664000372000037200000001330313604173400020410 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ exception classes and constants handling test outcomes as well as functions creating them """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys from packaging.version import Version class OutcomeException(BaseException): """ OutcomeException and its subclass instances indicate and contain info about test and collection outcomes. """ def __init__(self, msg=None, pytrace=True): BaseException.__init__(self, msg) self.msg = msg self.pytrace = pytrace def __repr__(self): if self.msg: val = self.msg if isinstance(val, bytes): val = val.decode("UTF-8", errors="replace") return val return "<%s instance>" % (self.__class__.__name__,) __str__ = __repr__ TEST_OUTCOME = (OutcomeException, Exception) class Skipped(OutcomeException): # XXX hackish: on 3k we fake to live in the builtins # in order to have Skipped exception printing shorter/nicer __module__ = "builtins" def __init__(self, msg=None, pytrace=True, allow_module_level=False): OutcomeException.__init__(self, msg=msg, pytrace=pytrace) self.allow_module_level = allow_module_level class Failed(OutcomeException): """ raised from an explicit call to pytest.fail() """ __module__ = "builtins" class Exit(Exception): """ raised for immediate program exits (no tracebacks/summaries)""" def __init__(self, msg="unknown reason", returncode=None): self.msg = msg self.returncode = returncode super(Exit, self).__init__(msg) # exposed helper methods def exit(msg, returncode=None): """ Exit testing process. :param str msg: message to display upon exit. :param int returncode: return code to be used when exiting pytest. """ __tracebackhide__ = True raise Exit(msg, returncode) exit.Exception = Exit def skip(msg="", **kwargs): """ Skip an executing test with the given message. This function should be called only during testing (setup, call or teardown) or during collection by using the ``allow_module_level`` flag. This function can be called in doctests as well. :kwarg bool allow_module_level: allows this function to be called at module level, skipping the rest of the module. Default to False. .. note:: It is better to use the :ref:`pytest.mark.skipif ref` marker when possible to declare a test to be skipped under certain conditions like mismatching platforms or dependencies. Similarly, use the ``# doctest: +SKIP`` directive (see `doctest.SKIP `_) to skip a doctest statically. """ __tracebackhide__ = True allow_module_level = kwargs.pop("allow_module_level", False) if kwargs: raise TypeError("unexpected keyword arguments: {}".format(sorted(kwargs))) raise Skipped(msg=msg, allow_module_level=allow_module_level) skip.Exception = Skipped def fail(msg="", pytrace=True): """ Explicitly fail an executing test with the given message. :param str msg: the message to show the user as reason for the failure. :param bool pytrace: if false the msg represents the full failure information and no python traceback will be reported. """ __tracebackhide__ = True raise Failed(msg=msg, pytrace=pytrace) fail.Exception = Failed class XFailed(fail.Exception): """ raised from an explicit call to pytest.xfail() """ def xfail(reason=""): """ Imperatively xfail an executing test or setup functions with the given reason. This function should be called only during testing (setup, call or teardown). .. note:: It is better to use the :ref:`pytest.mark.xfail ref` marker when possible to declare a test to be xfailed under certain conditions like known bugs or missing features. """ __tracebackhide__ = True raise XFailed(reason) xfail.Exception = XFailed def importorskip(modname, minversion=None, reason=None): """Imports and returns the requested module ``modname``, or skip the current test if the module cannot be imported. :param str modname: the name of the module to import :param str minversion: if given, the imported module ``__version__`` attribute must be at least this minimal version, otherwise the test is still skipped. :param str reason: if given, this reason is shown as the message when the module cannot be imported. """ import warnings __tracebackhide__ = True compile(modname, "", "eval") # to catch syntaxerrors import_exc = None with warnings.catch_warnings(): # make sure to ignore ImportWarnings that might happen because # of existing directories with the same name we're trying to # import but without a __init__.py file warnings.simplefilter("ignore") try: __import__(modname) except ImportError as exc: # Do not raise chained exception here(#1485) import_exc = exc if import_exc: if reason is None: reason = "could not import %r: %s" % (modname, import_exc) raise Skipped(reason, allow_module_level=True) mod = sys.modules[modname] if minversion is None: return mod verattr = getattr(mod, "__version__", None) if minversion is not None: if verattr is None or Version(verattr) < Version(minversion): raise Skipped( "module %r has __version__ %r, required is: %r" % (modname, verattr, minversion), allow_module_level=True, ) return mod pytest-4.6.9/src/_pytest/mark/0000775000372000037200000000000013604173421017135 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/mark/evaluate.py0000664000372000037200000000751113604173400021316 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import os import platform import sys import traceback import six from ..outcomes import fail from ..outcomes import TEST_OUTCOME def cached_eval(config, expr, d): if not hasattr(config, "_evalcache"): config._evalcache = {} try: return config._evalcache[expr] except KeyError: import _pytest._code exprcode = _pytest._code.compile(expr, mode="eval") config._evalcache[expr] = x = eval(exprcode, d) return x class MarkEvaluator(object): def __init__(self, item, name): self.item = item self._marks = None self._mark = None self._mark_name = name def __bool__(self): # dont cache here to prevent staleness return bool(self._get_marks()) __nonzero__ = __bool__ def wasvalid(self): return not hasattr(self, "exc") def _get_marks(self): return list(self.item.iter_markers(name=self._mark_name)) def invalidraise(self, exc): raises = self.get("raises") if not raises: return return not isinstance(exc, raises) def istrue(self): try: return self._istrue() except TEST_OUTCOME: self.exc = sys.exc_info() if isinstance(self.exc[1], SyntaxError): msg = [" " * (self.exc[1].offset + 4) + "^"] msg.append("SyntaxError: invalid syntax") else: msg = traceback.format_exception_only(*self.exc[:2]) fail( "Error evaluating %r expression\n" " %s\n" "%s" % (self._mark_name, self.expr, "\n".join(msg)), pytrace=False, ) def _getglobals(self): d = {"os": os, "sys": sys, "platform": platform, "config": self.item.config} if hasattr(self.item, "obj"): d.update(self.item.obj.__globals__) return d def _istrue(self): if hasattr(self, "result"): return self.result self._marks = self._get_marks() if self._marks: self.result = False for mark in self._marks: self._mark = mark if "condition" in mark.kwargs: args = (mark.kwargs["condition"],) else: args = mark.args for expr in args: self.expr = expr if isinstance(expr, six.string_types): d = self._getglobals() result = cached_eval(self.item.config, expr, d) else: if "reason" not in mark.kwargs: # XXX better be checked at collection time msg = ( "you need to specify reason=STRING " "when using booleans as conditions." ) fail(msg) result = bool(expr) if result: self.result = True self.reason = mark.kwargs.get("reason", None) self.expr = expr return self.result if not args: self.result = True self.reason = mark.kwargs.get("reason", None) return self.result return False def get(self, attr, default=None): if self._mark is None: return default return self._mark.kwargs.get(attr, default) def getexplanation(self): expl = getattr(self, "reason", None) or self.get("reason", None) if not expl: if not hasattr(self, "expr"): return "" else: return "condition: " + str(self.expr) return expl pytest-4.6.9/src/_pytest/mark/legacy.py0000664000372000037200000000632713604173400020760 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ this is a place where we put datastructures used by legacy apis we hope ot remove """ import keyword import attr from _pytest.config import UsageError @attr.s class MarkMapping(object): """Provides a local mapping for markers where item access resolves to True if the marker is present. """ own_mark_names = attr.ib() @classmethod def from_item(cls, item): mark_names = {mark.name for mark in item.iter_markers()} return cls(mark_names) def __getitem__(self, name): return name in self.own_mark_names class KeywordMapping(object): """Provides a local mapping for keywords. Given a list of names, map any substring of one of these names to True. """ def __init__(self, names): self._names = names @classmethod def from_item(cls, item): mapped_names = set() # Add the names of the current item and any parent items import pytest for item in item.listchain(): if not isinstance(item, pytest.Instance): mapped_names.add(item.name) # Add the names added as extra keywords to current or parent items mapped_names.update(item.listextrakeywords()) # Add the names attached to the current function through direct assignment if hasattr(item, "function"): mapped_names.update(item.function.__dict__) # add the markers to the keywords as we no longer handle them correctly mapped_names.update(mark.name for mark in item.iter_markers()) return cls(mapped_names) def __getitem__(self, subname): for name in self._names: if subname in name: return True return False python_keywords_allowed_list = ["or", "and", "not"] def matchmark(colitem, markexpr): """Tries to match on any marker names, attached to the given colitem.""" try: return eval(markexpr, {}, MarkMapping.from_item(colitem)) except SyntaxError as e: raise SyntaxError(str(e) + "\nMarker expression must be valid Python!") def matchkeyword(colitem, keywordexpr): """Tries to match given keyword expression to given collector item. Will match on the name of colitem, including the names of its parents. Only matches names of items which are either a :class:`Class` or a :class:`Function`. Additionally, matches on names in the 'extra_keyword_matches' set of any item, as well as names directly assigned to test functions. """ mapping = KeywordMapping.from_item(colitem) if " " not in keywordexpr: # special case to allow for simple "-k pass" and "-k 1.3" return mapping[keywordexpr] elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: return not mapping[keywordexpr[4:]] for kwd in keywordexpr.split(): if keyword.iskeyword(kwd) and kwd not in python_keywords_allowed_list: raise UsageError( "Python keyword '{}' not accepted in expressions passed to '-k'".format( kwd ) ) try: return eval(keywordexpr, {}, mapping) except SyntaxError: raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr)) pytest-4.6.9/src/_pytest/mark/structures.py0000664000372000037200000003255213604173400021736 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import inspect import warnings from collections import namedtuple from operator import attrgetter import attr import six from ..compat import ascii_escaped from ..compat import ATTRS_EQ_FIELD from ..compat import getfslineno from ..compat import MappingMixin from ..compat import NOTSET from _pytest.deprecated import PYTEST_PARAM_UNKNOWN_KWARGS from _pytest.outcomes import fail from _pytest.warning_types import PytestUnknownMarkWarning EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" def alias(name, warning=None): getter = attrgetter(name) def warned(self): warnings.warn(warning, stacklevel=2) return getter(self) return property(getter if warning is None else warned, doc="alias for " + name) def istestfunc(func): return ( hasattr(func, "__call__") and getattr(func, "__name__", "") != "" ) def get_empty_parameterset_mark(config, argnames, func): from ..nodes import Collector requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION) if requested_mark in ("", None, "skip"): mark = MARK_GEN.skip elif requested_mark == "xfail": mark = MARK_GEN.xfail(run=False) elif requested_mark == "fail_at_collect": f_name = func.__name__ _, lineno = getfslineno(func) raise Collector.CollectError( "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1) ) else: raise LookupError(requested_mark) fs, lineno = getfslineno(func) reason = "got empty parameter set %r, function %s at %s:%d" % ( argnames, func.__name__, fs, lineno, ) return mark(reason=reason) class ParameterSet(namedtuple("ParameterSet", "values, marks, id")): @classmethod def param(cls, *values, **kwargs): marks = kwargs.pop("marks", ()) if isinstance(marks, MarkDecorator): marks = (marks,) else: assert isinstance(marks, (tuple, list, set)) id_ = kwargs.pop("id", None) if id_ is not None: if not isinstance(id_, six.string_types): raise TypeError( "Expected id to be a string, got {}: {!r}".format(type(id_), id_) ) id_ = ascii_escaped(id_) if kwargs: warnings.warn( PYTEST_PARAM_UNKNOWN_KWARGS.format(args=sorted(kwargs)), stacklevel=3 ) return cls(values, marks, id_) @classmethod def extract_from(cls, parameterset, force_tuple=False): """ :param parameterset: a legacy style parameterset that may or may not be a tuple, and may or may not be wrapped into a mess of mark objects :param force_tuple: enforce tuple wrapping so single argument tuple values don't get decomposed and break tests """ if isinstance(parameterset, cls): return parameterset if force_tuple: return cls.param(parameterset) else: return cls(parameterset, marks=[], id=None) @staticmethod def _parse_parametrize_args(argnames, argvalues, *args, **kwargs): if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] force_tuple = len(argnames) == 1 else: force_tuple = False return argnames, force_tuple @staticmethod def _parse_parametrize_parameters(argvalues, force_tuple): return [ ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues ] @classmethod def _for_parametrize(cls, argnames, argvalues, func, config, function_definition): argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues) parameters = cls._parse_parametrize_parameters(argvalues, force_tuple) del argvalues if parameters: # check all parameter sets have the correct number of values for param in parameters: if len(param.values) != len(argnames): msg = ( '{nodeid}: in "parametrize" the number of names ({names_len}):\n' " {names}\n" "must be equal to the number of values ({values_len}):\n" " {values}" ) fail( msg.format( nodeid=function_definition.nodeid, values=param.values, names=argnames, names_len=len(argnames), values_len=len(param.values), ), pytrace=False, ) else: # empty parameter set (likely computed at runtime): create a single # parameter set with NOTSET values, with the "empty parameter set" mark applied to it mark = get_empty_parameterset_mark(config, argnames, func) parameters.append( ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None) ) return argnames, parameters @attr.s(frozen=True) class Mark(object): #: name of the mark name = attr.ib(type=str) #: positional arguments of the mark decorator args = attr.ib() # List[object] #: keyword arguments of the mark decorator kwargs = attr.ib() # Dict[str, object] def combined_with(self, other): """ :param other: the mark to combine with :type other: Mark :rtype: Mark combines by appending args and merging the mappings """ assert self.name == other.name return Mark( self.name, self.args + other.args, dict(self.kwargs, **other.kwargs) ) @attr.s class MarkDecorator(object): """ A decorator for test functions and test classes. When applied it will create :class:`MarkInfo` objects which may be :ref:`retrieved by hooks as item keywords `. MarkDecorator instances are often created like this:: mark1 = pytest.mark.NAME # simple MarkDecorator mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator and can then be applied as decorators to test functions:: @mark2 def test_function(): pass When a MarkDecorator instance is called it does the following: 1. If called with a single class as its only positional argument and no additional keyword arguments, it attaches itself to the class so it gets applied automatically to all test cases found in that class. 2. If called with a single function as its only positional argument and no additional keyword arguments, it attaches a MarkInfo object to the function, containing all the arguments already stored internally in the MarkDecorator. 3. When called in any other case, it performs a 'fake construction' call, i.e. it returns a new MarkDecorator instance with the original MarkDecorator's content updated with the arguments passed to this call. Note: The rules above prevent MarkDecorator objects from storing only a single function or class reference as their positional argument with no additional keyword or positional arguments. """ mark = attr.ib(validator=attr.validators.instance_of(Mark)) name = alias("mark.name") args = alias("mark.args") kwargs = alias("mark.kwargs") @property def markname(self): return self.name # for backward-compat (2.4.1 had this attr) def __eq__(self, other): return self.mark == other.mark if isinstance(other, MarkDecorator) else False def __repr__(self): return "" % (self.mark,) def with_args(self, *args, **kwargs): """ return a MarkDecorator with extra arguments added unlike call this can be used even if the sole argument is a callable/class :return: MarkDecorator """ mark = Mark(self.name, args, kwargs) return self.__class__(self.mark.combined_with(mark)) def __call__(self, *args, **kwargs): """ if passed a single callable argument: decorate it with mark info. otherwise add *args/**kwargs in-place to mark information. """ if args and not kwargs: func = args[0] is_class = inspect.isclass(func) if len(args) == 1 and (istestfunc(func) or is_class): store_mark(func, self.mark) return func return self.with_args(*args, **kwargs) def get_unpacked_marks(obj): """ obtain the unpacked marks that are stored on an object """ mark_list = getattr(obj, "pytestmark", []) if not isinstance(mark_list, list): mark_list = [mark_list] return normalize_mark_list(mark_list) def normalize_mark_list(mark_list): """ normalizes marker decorating helpers to mark objects :type mark_list: List[Union[Mark, Markdecorator]] :rtype: List[Mark] """ extracted = [ getattr(mark, "mark", mark) for mark in mark_list ] # unpack MarkDecorator for mark in extracted: if not isinstance(mark, Mark): raise TypeError("got {!r} instead of Mark".format(mark)) return [x for x in extracted if isinstance(x, Mark)] def store_mark(obj, mark): """store a Mark on an object this is used to implement the Mark declarations/decorators correctly """ assert isinstance(mark, Mark), mark # always reassign name to avoid updating pytestmark # in a reference that was only borrowed obj.pytestmark = get_unpacked_marks(obj) + [mark] class MarkGenerator(object): """ Factory for :class:`MarkDecorator` objects - exposed as a ``pytest.mark`` singleton instance. Example:: import pytest @pytest.mark.slowtest def test_function(): pass will set a 'slowtest' :class:`MarkInfo` object on the ``test_function`` object. """ _config = None _markers = set() def __getattr__(self, name): if name[0] == "_": raise AttributeError("Marker name must NOT start with underscore") if self._config is not None: # We store a set of markers as a performance optimisation - if a mark # name is in the set we definitely know it, but a mark may be known and # not in the set. We therefore start by updating the set! if name not in self._markers: for line in self._config.getini("markers"): # example lines: "skipif(condition): skip the given test if..." # or "hypothesis: tests which use Hypothesis", so to get the # marker name we split on both `:` and `(`. marker = line.split(":")[0].split("(")[0].strip() self._markers.add(marker) # If the name is not in the set of known marks after updating, # then it really is time to issue a warning or an error. if name not in self._markers: if self._config.option.strict_markers: fail( "{!r} not found in `markers` configuration option".format(name), pytrace=False, ) else: warnings.warn( "Unknown pytest.mark.%s - is this a typo? You can register " "custom marks to avoid this warning - for details, see " "https://docs.pytest.org/en/latest/mark.html" % name, PytestUnknownMarkWarning, ) return MarkDecorator(Mark(name, (), {})) MARK_GEN = MarkGenerator() class NodeKeywords(MappingMixin): def __init__(self, node): self.node = node self.parent = node.parent self._markers = {node.name: True} def __getitem__(self, key): try: return self._markers[key] except KeyError: if self.parent is None: raise return self.parent.keywords[key] def __setitem__(self, key, value): self._markers[key] = value def __delitem__(self, key): raise ValueError("cannot delete key in keywords dict") def __iter__(self): seen = self._seen() return iter(seen) def _seen(self): seen = set(self._markers) if self.parent is not None: seen.update(self.parent.keywords) return seen def __len__(self): return len(self._seen()) def __repr__(self): return "" % (self.node,) # mypy cannot find this overload, remove when on attrs>=19.2 @attr.s(hash=False, **{ATTRS_EQ_FIELD: False}) # type: ignore class NodeMarkers(object): """ internal structure for storing marks belonging to a node ..warning:: unstable api """ own_markers = attr.ib(default=attr.Factory(list)) def update(self, add_markers): """update the own markers """ self.own_markers.extend(add_markers) def find(self, name): """ find markers in own nodes or parent nodes needs a better place """ for mark in self.own_markers: if mark.name == name: yield mark def __iter__(self): return iter(self.own_markers) pytest-4.6.9/src/_pytest/mark/__init__.py0000664000372000037200000001226113604173400021245 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ generic mechanism for marking and selecting python functions. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from .legacy import matchkeyword from .legacy import matchmark from .structures import EMPTY_PARAMETERSET_OPTION from .structures import get_empty_parameterset_mark from .structures import Mark from .structures import MARK_GEN from .structures import MarkDecorator from .structures import MarkGenerator from .structures import ParameterSet from _pytest.config import UsageError __all__ = ["Mark", "MarkDecorator", "MarkGenerator", "get_empty_parameterset_mark"] def param(*values, **kw): """Specify a parameter in `pytest.mark.parametrize`_ calls or :ref:`parametrized fixtures `. .. code-block:: python @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): assert eval(test_input) == expected :param values: variable args of the values of the parameter set, in order. :keyword marks: a single mark or a list of marks to be applied to this parameter set. :keyword str id: the id to attribute to this parameter set. """ return ParameterSet.param(*values, **kw) def pytest_addoption(parser): group = parser.getgroup("general") group._addoption( "-k", action="store", dest="keyword", default="", metavar="EXPRESSION", help="only run tests which match the given substring expression. " "An expression is a python evaluatable expression " "where all names are substring-matched against test names " "and their parent classes. Example: -k 'test_method or test_" "other' matches all test functions and classes whose name " "contains 'test_method' or 'test_other', while -k 'not test_method' " "matches those that don't contain 'test_method' in their names. " "-k 'not test_method and not test_other' will eliminate the matches. " "Additionally keywords are matched to classes and functions " "containing extra names in their 'extra_keyword_matches' set, " "as well as functions which have names assigned directly to them.", ) group._addoption( "-m", action="store", dest="markexpr", default="", metavar="MARKEXPR", help="only run tests matching given mark expression. " "example: -m 'mark1 and not mark2'.", ) group.addoption( "--markers", action="store_true", help="show markers (builtin, plugin and per-project ones).", ) parser.addini("markers", "markers for test functions", "linelist") parser.addini(EMPTY_PARAMETERSET_OPTION, "default marker for empty parametersets") def pytest_cmdline_main(config): import _pytest.config if config.option.markers: config._do_configure() tw = _pytest.config.create_terminal_writer(config) for line in config.getini("markers"): parts = line.split(":", 1) name = parts[0] rest = parts[1] if len(parts) == 2 else "" tw.write("@pytest.mark.%s:" % name, bold=True) tw.line(rest) tw.line() config._ensure_unconfigure() return 0 pytest_cmdline_main.tryfirst = True def deselect_by_keyword(items, config): keywordexpr = config.option.keyword.lstrip() if not keywordexpr: return if keywordexpr.startswith("-"): keywordexpr = "not " + keywordexpr[1:] selectuntil = False if keywordexpr[-1:] == ":": selectuntil = True keywordexpr = keywordexpr[:-1] remaining = [] deselected = [] for colitem in items: if keywordexpr and not matchkeyword(colitem, keywordexpr): deselected.append(colitem) else: if selectuntil: keywordexpr = None remaining.append(colitem) if deselected: config.hook.pytest_deselected(items=deselected) items[:] = remaining def deselect_by_mark(items, config): matchexpr = config.option.markexpr if not matchexpr: return remaining = [] deselected = [] for item in items: if matchmark(item, matchexpr): remaining.append(item) else: deselected.append(item) if deselected: config.hook.pytest_deselected(items=deselected) items[:] = remaining def pytest_collection_modifyitems(items, config): deselect_by_keyword(items, config) deselect_by_mark(items, config) def pytest_configure(config): config._old_mark_config = MARK_GEN._config MARK_GEN._config = config empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION) if empty_parameterset not in ("skip", "xfail", "fail_at_collect", None, ""): raise UsageError( "{!s} must be one of skip, xfail or fail_at_collect" " but it is {!r}".format(EMPTY_PARAMETERSET_OPTION, empty_parameterset) ) def pytest_unconfigure(config): MARK_GEN._config = getattr(config, "_old_mark_config", None) pytest-4.6.9/src/_pytest/reports.py0000664000372000037200000003325113604173400020254 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from pprint import pprint import py import six from _pytest._code.code import ExceptionInfo from _pytest._code.code import ReprEntry from _pytest._code.code import ReprEntryNative from _pytest._code.code import ReprExceptionInfo from _pytest._code.code import ReprFileLocation from _pytest._code.code import ReprFuncArgs from _pytest._code.code import ReprLocals from _pytest._code.code import ReprTraceback from _pytest._code.code import TerminalRepr from _pytest.outcomes import skip from _pytest.pathlib import Path def getslaveinfoline(node): try: return node._slaveinfocache except AttributeError: d = node.slaveinfo ver = "%s.%s.%s" % d["version_info"][:3] node._slaveinfocache = s = "[%s] %s -- Python %s %s" % ( d["id"], d["sysplatform"], ver, d["executable"], ) return s class BaseReport(object): when = None location = None def __init__(self, **kw): self.__dict__.update(kw) def toterminal(self, out): if hasattr(self, "node"): out.line(getslaveinfoline(self.node)) longrepr = self.longrepr if longrepr is None: return if hasattr(longrepr, "toterminal"): longrepr.toterminal(out) else: try: out.line(longrepr) except UnicodeEncodeError: out.line("") def get_sections(self, prefix): for name, content in self.sections: if name.startswith(prefix): yield prefix, content @property def longreprtext(self): """ Read-only property that returns the full string representation of ``longrepr``. .. versionadded:: 3.0 """ tw = py.io.TerminalWriter(stringio=True) tw.hasmarkup = False self.toterminal(tw) exc = tw.stringio.getvalue() return exc.strip() @property def caplog(self): """Return captured log lines, if log capturing is enabled .. versionadded:: 3.5 """ return "\n".join( content for (prefix, content) in self.get_sections("Captured log") ) @property def capstdout(self): """Return captured text from stdout, if capturing is enabled .. versionadded:: 3.0 """ return "".join( content for (prefix, content) in self.get_sections("Captured stdout") ) @property def capstderr(self): """Return captured text from stderr, if capturing is enabled .. versionadded:: 3.0 """ return "".join( content for (prefix, content) in self.get_sections("Captured stderr") ) passed = property(lambda x: x.outcome == "passed") failed = property(lambda x: x.outcome == "failed") skipped = property(lambda x: x.outcome == "skipped") @property def fspath(self): return self.nodeid.split("::")[0] @property def count_towards_summary(self): """ **Experimental** Returns True if this report should be counted towards the totals shown at the end of the test session: "1 passed, 1 failure, etc". .. note:: This function is considered **experimental**, so beware that it is subject to changes even in patch releases. """ return True @property def head_line(self): """ **Experimental** Returns the head line shown with longrepr output for this report, more commonly during traceback representation during failures:: ________ Test.foo ________ In the example above, the head_line is "Test.foo". .. note:: This function is considered **experimental**, so beware that it is subject to changes even in patch releases. """ if self.location is not None: fspath, lineno, domain = self.location return domain def _get_verbose_word(self, config): _category, _short, verbose = config.hook.pytest_report_teststatus( report=self, config=config ) return verbose def _to_json(self): """ This was originally the serialize_report() function from xdist (ca03269). Returns the contents of this report as a dict of builtin entries, suitable for serialization. Experimental method. """ def disassembled_report(rep): reprtraceback = rep.longrepr.reprtraceback.__dict__.copy() reprcrash = rep.longrepr.reprcrash.__dict__.copy() new_entries = [] for entry in reprtraceback["reprentries"]: entry_data = { "type": type(entry).__name__, "data": entry.__dict__.copy(), } for key, value in entry_data["data"].items(): if hasattr(value, "__dict__"): entry_data["data"][key] = value.__dict__.copy() new_entries.append(entry_data) reprtraceback["reprentries"] = new_entries return { "reprcrash": reprcrash, "reprtraceback": reprtraceback, "sections": rep.longrepr.sections, } d = self.__dict__.copy() if hasattr(self.longrepr, "toterminal"): if hasattr(self.longrepr, "reprtraceback") and hasattr( self.longrepr, "reprcrash" ): d["longrepr"] = disassembled_report(self) else: d["longrepr"] = six.text_type(self.longrepr) else: d["longrepr"] = self.longrepr for name in d: if isinstance(d[name], (py.path.local, Path)): d[name] = str(d[name]) elif name == "result": d[name] = None # for now return d @classmethod def _from_json(cls, reportdict): """ This was originally the serialize_report() function from xdist (ca03269). Factory method that returns either a TestReport or CollectReport, depending on the calling class. It's the callers responsibility to know which class to pass here. Experimental method. """ if reportdict["longrepr"]: if ( "reprcrash" in reportdict["longrepr"] and "reprtraceback" in reportdict["longrepr"] ): reprtraceback = reportdict["longrepr"]["reprtraceback"] reprcrash = reportdict["longrepr"]["reprcrash"] unserialized_entries = [] reprentry = None for entry_data in reprtraceback["reprentries"]: data = entry_data["data"] entry_type = entry_data["type"] if entry_type == "ReprEntry": reprfuncargs = None reprfileloc = None reprlocals = None if data["reprfuncargs"]: reprfuncargs = ReprFuncArgs(**data["reprfuncargs"]) if data["reprfileloc"]: reprfileloc = ReprFileLocation(**data["reprfileloc"]) if data["reprlocals"]: reprlocals = ReprLocals(data["reprlocals"]["lines"]) reprentry = ReprEntry( lines=data["lines"], reprfuncargs=reprfuncargs, reprlocals=reprlocals, filelocrepr=reprfileloc, style=data["style"], ) elif entry_type == "ReprEntryNative": reprentry = ReprEntryNative(data["lines"]) else: _report_unserialization_failure(entry_type, cls, reportdict) unserialized_entries.append(reprentry) reprtraceback["reprentries"] = unserialized_entries exception_info = ReprExceptionInfo( reprtraceback=ReprTraceback(**reprtraceback), reprcrash=ReprFileLocation(**reprcrash), ) for section in reportdict["longrepr"]["sections"]: exception_info.addsection(*section) reportdict["longrepr"] = exception_info return cls(**reportdict) def _report_unserialization_failure(type_name, report_class, reportdict): url = "https://github.com/pytest-dev/pytest/issues" stream = py.io.TextIO() pprint("-" * 100, stream=stream) pprint("INTERNALERROR: Unknown entry type returned: %s" % type_name, stream=stream) pprint("report_name: %s" % report_class, stream=stream) pprint(reportdict, stream=stream) pprint("Please report this bug at %s" % url, stream=stream) pprint("-" * 100, stream=stream) raise RuntimeError(stream.getvalue()) class TestReport(BaseReport): """ Basic test report object (also used for setup and teardown calls if they fail). """ __test__ = False def __init__( self, nodeid, location, keywords, outcome, longrepr, when, sections=(), duration=0, user_properties=None, **extra ): #: normalized collection node id self.nodeid = nodeid #: a (filesystempath, lineno, domaininfo) tuple indicating the #: actual location of a test item - it might be different from the #: collected one e.g. if a method is inherited from a different module. self.location = location #: a name -> value dictionary containing all keywords and #: markers associated with a test invocation. self.keywords = keywords #: test outcome, always one of "passed", "failed", "skipped". self.outcome = outcome #: None or a failure representation. self.longrepr = longrepr #: one of 'setup', 'call', 'teardown' to indicate runtest phase. self.when = when #: user properties is a list of tuples (name, value) that holds user #: defined properties of the test self.user_properties = list(user_properties or []) #: list of pairs ``(str, str)`` of extra information which needs to #: marshallable. Used by pytest to add captured text #: from ``stdout`` and ``stderr``, but may be used by other plugins #: to add arbitrary information to reports. self.sections = list(sections) #: time it took to run just the test self.duration = duration self.__dict__.update(extra) def __repr__(self): return "<%s %r when=%r outcome=%r>" % ( self.__class__.__name__, self.nodeid, self.when, self.outcome, ) @classmethod def from_item_and_call(cls, item, call): """ Factory method to create and fill a TestReport with standard item and call info. """ when = call.when duration = call.stop - call.start keywords = {x: 1 for x in item.keywords} excinfo = call.excinfo sections = [] if not call.excinfo: outcome = "passed" longrepr = None else: if not isinstance(excinfo, ExceptionInfo): outcome = "failed" longrepr = excinfo elif excinfo.errisinstance(skip.Exception): outcome = "skipped" r = excinfo._getreprcrash() longrepr = (str(r.path), r.lineno, r.message) else: outcome = "failed" if call.when == "call": longrepr = item.repr_failure(excinfo) else: # exception in setup or teardown longrepr = item._repr_failure_py( excinfo, style=item.config.getoption("tbstyle", "auto") ) for rwhen, key, content in item._report_sections: sections.append(("Captured %s %s" % (key, rwhen), content)) return cls( item.nodeid, item.location, keywords, outcome, longrepr, when, sections, duration, user_properties=item.user_properties, ) class CollectReport(BaseReport): when = "collect" def __init__(self, nodeid, outcome, longrepr, result, sections=(), **extra): self.nodeid = nodeid self.outcome = outcome self.longrepr = longrepr self.result = result or [] self.sections = list(sections) self.__dict__.update(extra) @property def location(self): return (self.fspath, None, self.fspath) def __repr__(self): return "" % ( self.nodeid, len(self.result), self.outcome, ) class CollectErrorRepr(TerminalRepr): def __init__(self, msg): self.longrepr = msg def toterminal(self, out): out.line(self.longrepr, red=True) def pytest_report_to_serializable(report): if isinstance(report, (TestReport, CollectReport)): data = report._to_json() data["_report_type"] = report.__class__.__name__ return data def pytest_report_from_serializable(data): if "_report_type" in data: if data["_report_type"] == "TestReport": return TestReport._from_json(data) elif data["_report_type"] == "CollectReport": return CollectReport._from_json(data) assert False, "Unknown report_type unserialize data: {}".format( data["_report_type"] ) pytest-4.6.9/src/_pytest/capture.py0000664000372000037200000006523013604173400020223 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ per-test stdout/stderr capturing mechanism. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import contextlib import io import os import sys from io import UnsupportedOperation from tempfile import TemporaryFile import six import pytest from _pytest.compat import _PY3 from _pytest.compat import CaptureIO patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"} def pytest_addoption(parser): group = parser.getgroup("general") group._addoption( "--capture", action="store", default="fd" if hasattr(os, "dup") else "sys", metavar="method", choices=["fd", "sys", "no"], help="per-test capturing method: one of fd|sys|no.", ) group._addoption( "-s", action="store_const", const="no", dest="capture", help="shortcut for --capture=no.", ) @pytest.hookimpl(hookwrapper=True) def pytest_load_initial_conftests(early_config, parser, args): ns = early_config.known_args_namespace if ns.capture == "fd": _py36_windowsconsoleio_workaround(sys.stdout) _colorama_workaround() _readline_workaround() pluginmanager = early_config.pluginmanager capman = CaptureManager(ns.capture) pluginmanager.register(capman, "capturemanager") # make sure that capturemanager is properly reset at final shutdown early_config.add_cleanup(capman.stop_global_capturing) # finally trigger conftest loading but while capturing (issue93) capman.start_global_capturing() outcome = yield capman.suspend_global_capture() if outcome.excinfo is not None: out, err = capman.read_global_capture() sys.stdout.write(out) sys.stderr.write(err) class CaptureManager(object): """ Capture plugin, manages that the appropriate capture method is enabled/disabled during collection and each test phase (setup, call, teardown). After each of those points, the captured output is obtained and attached to the collection/runtest report. There are two levels of capture: * global: which is enabled by default and can be suppressed by the ``-s`` option. This is always enabled/disabled during collection and each test phase. * fixture: when a test function or one of its fixture depend on the ``capsys`` or ``capfd`` fixtures. In this case special handling is needed to ensure the fixtures take precedence over the global capture. """ def __init__(self, method): self._method = method self._global_capturing = None self._current_item = None def __repr__(self): return "" % ( self._method, self._global_capturing, self._current_item, ) def _getcapture(self, method): if method == "fd": return MultiCapture(out=True, err=True, Capture=FDCapture) elif method == "sys": return MultiCapture(out=True, err=True, Capture=SysCapture) elif method == "no": return MultiCapture(out=False, err=False, in_=False) raise ValueError("unknown capturing method: %r" % method) # pragma: no cover def is_capturing(self): if self.is_globally_capturing(): return "global" capture_fixture = getattr(self._current_item, "_capture_fixture", None) if capture_fixture is not None: return ( "fixture %s" % self._current_item._capture_fixture.request.fixturename ) return False # Global capturing control def is_globally_capturing(self): return self._method != "no" def start_global_capturing(self): assert self._global_capturing is None self._global_capturing = self._getcapture(self._method) self._global_capturing.start_capturing() def stop_global_capturing(self): if self._global_capturing is not None: self._global_capturing.pop_outerr_to_orig() self._global_capturing.stop_capturing() self._global_capturing = None def resume_global_capture(self): # During teardown of the python process, and on rare occasions, capture # attributes can be `None` while trying to resume global capture. if self._global_capturing is not None: self._global_capturing.resume_capturing() def suspend_global_capture(self, in_=False): cap = getattr(self, "_global_capturing", None) if cap is not None: cap.suspend_capturing(in_=in_) def suspend(self, in_=False): # Need to undo local capsys-et-al if it exists before disabling global capture. self.suspend_fixture(self._current_item) self.suspend_global_capture(in_) def resume(self): self.resume_global_capture() self.resume_fixture(self._current_item) def read_global_capture(self): return self._global_capturing.readouterr() # Fixture Control (it's just forwarding, think about removing this later) def activate_fixture(self, item): """If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over the global capture. """ fixture = getattr(item, "_capture_fixture", None) if fixture is not None: fixture._start() def deactivate_fixture(self, item): """Deactivates the ``capsys`` or ``capfd`` fixture of this item, if any.""" fixture = getattr(item, "_capture_fixture", None) if fixture is not None: fixture.close() def suspend_fixture(self, item): fixture = getattr(item, "_capture_fixture", None) if fixture is not None: fixture._suspend() def resume_fixture(self, item): fixture = getattr(item, "_capture_fixture", None) if fixture is not None: fixture._resume() # Helper context managers @contextlib.contextmanager def global_and_fixture_disabled(self): """Context manager to temporarily disable global and current fixture capturing.""" self.suspend() try: yield finally: self.resume() @contextlib.contextmanager def item_capture(self, when, item): self.resume_global_capture() self.activate_fixture(item) try: yield finally: self.deactivate_fixture(item) self.suspend_global_capture(in_=False) out, err = self.read_global_capture() item.add_report_section(when, "stdout", out) item.add_report_section(when, "stderr", err) # Hooks @pytest.hookimpl(hookwrapper=True) def pytest_make_collect_report(self, collector): if isinstance(collector, pytest.File): self.resume_global_capture() outcome = yield self.suspend_global_capture() out, err = self.read_global_capture() rep = outcome.get_result() if out: rep.sections.append(("Captured stdout", out)) if err: rep.sections.append(("Captured stderr", err)) else: yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_protocol(self, item): self._current_item = item yield self._current_item = None @pytest.hookimpl(hookwrapper=True) def pytest_runtest_setup(self, item): with self.item_capture("setup", item): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(self, item): with self.item_capture("call", item): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_teardown(self, item): with self.item_capture("teardown", item): yield @pytest.hookimpl(tryfirst=True) def pytest_keyboard_interrupt(self, excinfo): self.stop_global_capturing() @pytest.hookimpl(tryfirst=True) def pytest_internalerror(self, excinfo): self.stop_global_capturing() capture_fixtures = {"capfd", "capfdbinary", "capsys", "capsysbinary"} def _ensure_only_one_capture_fixture(request, name): fixtures = set(request.fixturenames) & capture_fixtures - {name} if fixtures: fixtures = sorted(fixtures) fixtures = fixtures[0] if len(fixtures) == 1 else fixtures raise request.raiseerror( "cannot use {} and {} at the same time".format(fixtures, name) ) @pytest.fixture def capsys(request): """Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsys.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. """ _ensure_only_one_capture_fixture(request, "capsys") with _install_capture_fixture_on_item(request, SysCapture) as fixture: yield fixture @pytest.fixture def capsysbinary(request): """Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsysbinary.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``bytes`` objects. """ _ensure_only_one_capture_fixture(request, "capsysbinary") # Currently, the implementation uses the python3 specific `.buffer` # property of CaptureIO. if sys.version_info < (3,): raise request.raiseerror("capsysbinary is only supported on Python 3") with _install_capture_fixture_on_item(request, SysCaptureBinary) as fixture: yield fixture @pytest.fixture def capfd(request): """Enable text capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. """ _ensure_only_one_capture_fixture(request, "capfd") if not hasattr(os, "dup"): pytest.skip( "capfd fixture needs os.dup function which is not available in this system" ) with _install_capture_fixture_on_item(request, FDCapture) as fixture: yield fixture @pytest.fixture def capfdbinary(request): """Enable bytes capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``byte`` objects. """ _ensure_only_one_capture_fixture(request, "capfdbinary") if not hasattr(os, "dup"): pytest.skip( "capfdbinary fixture needs os.dup function which is not available in this system" ) with _install_capture_fixture_on_item(request, FDCaptureBinary) as fixture: yield fixture @contextlib.contextmanager def _install_capture_fixture_on_item(request, capture_class): """ Context manager which creates a ``CaptureFixture`` instance and "installs" it on the item/node of the given request. Used by ``capsys`` and ``capfd``. The CaptureFixture is added as attribute of the item because it needs to accessed by ``CaptureManager`` during its ``pytest_runtest_*`` hooks. """ request.node._capture_fixture = fixture = CaptureFixture(capture_class, request) capmanager = request.config.pluginmanager.getplugin("capturemanager") # Need to active this fixture right away in case it is being used by another fixture (setup phase). # If this fixture is being used only by a test function (call phase), then we wouldn't need this # activation, but it doesn't hurt. capmanager.activate_fixture(request.node) yield fixture fixture.close() del request.node._capture_fixture class CaptureFixture(object): """ Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary` fixtures. """ def __init__(self, captureclass, request): self.captureclass = captureclass self.request = request self._capture = None self._captured_out = self.captureclass.EMPTY_BUFFER self._captured_err = self.captureclass.EMPTY_BUFFER def _start(self): if self._capture is None: self._capture = MultiCapture( out=True, err=True, in_=False, Capture=self.captureclass ) self._capture.start_capturing() def close(self): if self._capture is not None: out, err = self._capture.pop_outerr_to_orig() self._captured_out += out self._captured_err += err self._capture.stop_capturing() self._capture = None def readouterr(self): """Read and return the captured output so far, resetting the internal buffer. :return: captured content as a namedtuple with ``out`` and ``err`` string attributes """ captured_out, captured_err = self._captured_out, self._captured_err if self._capture is not None: out, err = self._capture.readouterr() captured_out += out captured_err += err self._captured_out = self.captureclass.EMPTY_BUFFER self._captured_err = self.captureclass.EMPTY_BUFFER return CaptureResult(captured_out, captured_err) def _suspend(self): """Suspends this fixture's own capturing temporarily.""" if self._capture is not None: self._capture.suspend_capturing() def _resume(self): """Resumes this fixture's own capturing temporarily.""" if self._capture is not None: self._capture.resume_capturing() @contextlib.contextmanager def disabled(self): """Temporarily disables capture while inside the 'with' block.""" capmanager = self.request.config.pluginmanager.getplugin("capturemanager") with capmanager.global_and_fixture_disabled(): yield def safe_text_dupfile(f, mode, default_encoding="UTF8"): """ return an open text file object that's a duplicate of f on the FD-level if possible. """ encoding = getattr(f, "encoding", None) try: fd = f.fileno() except Exception: if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"): # we seem to have a text stream, let's just use it return f else: newfd = os.dup(fd) if "b" not in mode: mode += "b" f = os.fdopen(newfd, mode, 0) # no buffering return EncodedFile(f, encoding or default_encoding) class EncodedFile(object): errors = "strict" # possibly needed by py3 code (issue555) def __init__(self, buffer, encoding): self.buffer = buffer self.encoding = encoding def write(self, obj): if isinstance(obj, six.text_type): obj = obj.encode(self.encoding, "replace") elif _PY3: raise TypeError( "write() argument must be str, not {}".format(type(obj).__name__) ) self.buffer.write(obj) def writelines(self, linelist): data = "".join(linelist) self.write(data) @property def name(self): """Ensure that file.name is a string.""" return repr(self.buffer) @property def mode(self): return self.buffer.mode.replace("b", "") def __getattr__(self, name): return getattr(object.__getattribute__(self, "buffer"), name) CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"]) class MultiCapture(object): out = err = in_ = None _state = None def __init__(self, out=True, err=True, in_=True, Capture=None): if in_: self.in_ = Capture(0) if out: self.out = Capture(1) if err: self.err = Capture(2) def __repr__(self): return "" % ( self.out, self.err, self.in_, self._state, getattr(self, "_in_suspended", ""), ) def start_capturing(self): self._state = "started" if self.in_: self.in_.start() if self.out: self.out.start() if self.err: self.err.start() def pop_outerr_to_orig(self): """ pop current snapshot out/err capture and flush to orig streams. """ out, err = self.readouterr() if out: self.out.writeorg(out) if err: self.err.writeorg(err) return out, err def suspend_capturing(self, in_=False): self._state = "suspended" if self.out: self.out.suspend() if self.err: self.err.suspend() if in_ and self.in_: self.in_.suspend() self._in_suspended = True def resume_capturing(self): self._state = "resumed" if self.out: self.out.resume() if self.err: self.err.resume() if hasattr(self, "_in_suspended"): self.in_.resume() del self._in_suspended def stop_capturing(self): """ stop capturing and reset capturing streams """ if self._state == "stopped": raise ValueError("was already stopped") self._state = "stopped" if self.out: self.out.done() if self.err: self.err.done() if self.in_: self.in_.done() def readouterr(self): """ return snapshot unicode value of stdout/stderr capturings. """ return CaptureResult( self.out.snap() if self.out is not None else "", self.err.snap() if self.err is not None else "", ) class NoCapture(object): EMPTY_BUFFER = None __init__ = start = done = suspend = resume = lambda *args: None class FDCaptureBinary(object): """Capture IO to/from a given os-level filedescriptor. snap() produces `bytes` """ EMPTY_BUFFER = b"" _state = None def __init__(self, targetfd, tmpfile=None): self.targetfd = targetfd try: self.targetfd_save = os.dup(self.targetfd) except OSError: self.start = lambda: None self.done = lambda: None else: if targetfd == 0: assert not tmpfile, "cannot set tmpfile with stdin" tmpfile = open(os.devnull, "r") self.syscapture = SysCapture(targetfd) else: if tmpfile is None: f = TemporaryFile() with f: tmpfile = safe_text_dupfile(f, mode="wb+") if targetfd in patchsysdict: self.syscapture = SysCapture(targetfd, tmpfile) else: self.syscapture = NoCapture() self.tmpfile = tmpfile self.tmpfile_fd = tmpfile.fileno() def __repr__(self): return "" % ( self.targetfd, getattr(self, "targetfd_save", None), self._state, ) def start(self): """ Start capturing on targetfd using memorized tmpfile. """ try: os.fstat(self.targetfd_save) except (AttributeError, OSError): raise ValueError("saved filedescriptor not valid anymore") os.dup2(self.tmpfile_fd, self.targetfd) self.syscapture.start() self._state = "started" def snap(self): self.tmpfile.seek(0) res = self.tmpfile.read() self.tmpfile.seek(0) self.tmpfile.truncate() return res def done(self): """ stop capturing, restore streams, return original capture file, seeked to position zero. """ targetfd_save = self.__dict__.pop("targetfd_save") os.dup2(targetfd_save, self.targetfd) os.close(targetfd_save) self.syscapture.done() _attempt_to_close_capture_file(self.tmpfile) self._state = "done" def suspend(self): self.syscapture.suspend() os.dup2(self.targetfd_save, self.targetfd) self._state = "suspended" def resume(self): self.syscapture.resume() os.dup2(self.tmpfile_fd, self.targetfd) self._state = "resumed" def writeorg(self, data): """ write to original file descriptor. """ if isinstance(data, six.text_type): data = data.encode("utf8") # XXX use encoding of original stream os.write(self.targetfd_save, data) class FDCapture(FDCaptureBinary): """Capture IO to/from a given os-level filedescriptor. snap() produces text """ EMPTY_BUFFER = str() def snap(self): res = super(FDCapture, self).snap() enc = getattr(self.tmpfile, "encoding", None) if enc and isinstance(res, bytes): res = six.text_type(res, enc, "replace") return res class SysCapture(object): EMPTY_BUFFER = str() _state = None def __init__(self, fd, tmpfile=None): name = patchsysdict[fd] self._old = getattr(sys, name) self.name = name if tmpfile is None: if name == "stdin": tmpfile = DontReadFromInput() else: tmpfile = CaptureIO() self.tmpfile = tmpfile def __repr__(self): return "" % ( self.name, self._old, self.tmpfile, self._state, ) def start(self): setattr(sys, self.name, self.tmpfile) self._state = "started" def snap(self): res = self.tmpfile.getvalue() self.tmpfile.seek(0) self.tmpfile.truncate() return res def done(self): setattr(sys, self.name, self._old) del self._old _attempt_to_close_capture_file(self.tmpfile) self._state = "done" def suspend(self): setattr(sys, self.name, self._old) self._state = "suspended" def resume(self): setattr(sys, self.name, self.tmpfile) self._state = "resumed" def writeorg(self, data): self._old.write(data) self._old.flush() class SysCaptureBinary(SysCapture): EMPTY_BUFFER = b"" def snap(self): res = self.tmpfile.buffer.getvalue() self.tmpfile.seek(0) self.tmpfile.truncate() return res class DontReadFromInput(six.Iterator): """Temporary stub class. Ideally when stdin is accessed, the capturing should be turned off, with possibly all data captured so far sent to the screen. This should be configurable, though, because in automated test runs it is better to crash than hang indefinitely. """ encoding = None def read(self, *args): raise IOError("reading from stdin while output is captured") readline = read readlines = read __next__ = read def __iter__(self): return self def fileno(self): raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()") def isatty(self): return False def close(self): pass @property def buffer(self): if sys.version_info >= (3, 0): return self else: raise AttributeError("redirected stdin has no attribute buffer") def _colorama_workaround(): """ Ensure colorama is imported so that it attaches to the correct stdio handles on Windows. colorama uses the terminal on import time. So if something does the first import of colorama while I/O capture is active, colorama will fail in various ways. """ if sys.platform.startswith("win32"): try: import colorama # noqa: F401 except ImportError: pass def _readline_workaround(): """ Ensure readline is imported so that it attaches to the correct stdio handles on Windows. Pdb uses readline support where available--when not running from the Python prompt, the readline module is not imported until running the pdb REPL. If running pytest with the --pdb option this means the readline module is not imported until after I/O capture has been started. This is a problem for pyreadline, which is often used to implement readline support on Windows, as it does not attach to the correct handles for stdout and/or stdin if they have been redirected by the FDCapture mechanism. This workaround ensures that readline is imported before I/O capture is setup so that it can attach to the actual stdin/out for the console. See https://github.com/pytest-dev/pytest/pull/1281 """ if sys.platform.startswith("win32"): try: import readline # noqa: F401 except ImportError: pass def _py36_windowsconsoleio_workaround(stream): """ Python 3.6 implemented unicode console handling for Windows. This works by reading/writing to the raw console handle using ``{Read,Write}ConsoleW``. The problem is that we are going to ``dup2`` over the stdio file descriptors when doing ``FDCapture`` and this will ``CloseHandle`` the handles used by Python to write to the console. Though there is still some weirdness and the console handle seems to only be closed randomly and not on the first call to ``CloseHandle``, or maybe it gets reopened with the same handle value when we suspend capturing. The workaround in this case will reopen stdio with a different fd which also means a different handle by replicating the logic in "Py_lifecycle.c:initstdio/create_stdio". :param stream: in practice ``sys.stdout`` or ``sys.stderr``, but given here as parameter for unittesting purposes. See https://github.com/pytest-dev/py/issues/103 """ if not sys.platform.startswith("win32") or sys.version_info[:2] < (3, 6): return # bail out if ``stream`` doesn't seem like a proper ``io`` stream (#2666) if not hasattr(stream, "buffer"): return buffered = hasattr(stream.buffer, "raw") raw_stdout = stream.buffer.raw if buffered else stream.buffer if not isinstance(raw_stdout, io._WindowsConsoleIO): return def _reopen_stdio(f, mode): if not buffered and mode[0] == "w": buffering = 0 else: buffering = -1 return io.TextIOWrapper( open(os.dup(f.fileno()), mode, buffering), f.encoding, f.errors, f.newlines, f.line_buffering, ) sys.stdin = _reopen_stdio(sys.stdin, "rb") sys.stdout = _reopen_stdio(sys.stdout, "wb") sys.stderr = _reopen_stdio(sys.stderr, "wb") def _attempt_to_close_capture_file(f): """Suppress IOError when closing the temporary file used for capturing streams in py27 (#2370)""" if six.PY2: try: f.close() except IOError: pass else: f.close() pytest-4.6.9/src/_pytest/terminal.py0000664000372000037200000011376713604173400020404 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ terminal reporting of the full testing process. This is a good source for looking at the various reporting hooks. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import collections import platform import sys import time from functools import partial import attr import pluggy import py import six from more_itertools import collapse import pytest from _pytest import nodes from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_OK from _pytest.main import EXIT_TESTSFAILED from _pytest.main import EXIT_USAGEERROR REPORT_COLLECTING_RESOLUTION = 0.5 class MoreQuietAction(argparse.Action): """ a modified copy of the argparse count action which counts down and updates the legacy quiet attribute at the same time used to unify verbosity handling """ def __init__(self, option_strings, dest, default=None, required=False, help=None): super(MoreQuietAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, default=default, required=required, help=help, ) def __call__(self, parser, namespace, values, option_string=None): new_count = getattr(namespace, self.dest, 0) - 1 setattr(namespace, self.dest, new_count) # todo Deprecate config.quiet namespace.quiet = getattr(namespace, "quiet", 0) + 1 def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") group._addoption( "-v", "--verbose", action="count", default=0, dest="verbose", help="increase verbosity.", ), group._addoption( "-q", "--quiet", action=MoreQuietAction, default=0, dest="verbose", help="decrease verbosity.", ), group._addoption( "--verbosity", dest="verbose", type=int, default=0, help="set verbosity" ) group._addoption( "-r", action="store", dest="reportchars", default="", metavar="chars", help="show extra test summary info as specified by chars: (f)ailed, " "(E)rror, (s)kipped, (x)failed, (X)passed, " "(p)assed, (P)assed with output, (a)ll except passed (p/P), or (A)ll. " "Warnings are displayed at all times except when " "--disable-warnings is set.", ) group._addoption( "--disable-warnings", "--disable-pytest-warnings", default=False, dest="disable_warnings", action="store_true", help="disable warnings summary", ) group._addoption( "-l", "--showlocals", action="store_true", dest="showlocals", default=False, help="show locals in tracebacks (disabled by default).", ) group._addoption( "--tb", metavar="style", action="store", dest="tbstyle", default="auto", choices=["auto", "long", "short", "no", "line", "native"], help="traceback print mode (auto/long/short/line/native/no).", ) group._addoption( "--show-capture", action="store", dest="showcapture", choices=["no", "stdout", "stderr", "log", "all"], default="all", help="Controls how captured stdout/stderr/log is shown on failed tests. " "Default is 'all'.", ) group._addoption( "--fulltrace", "--full-trace", action="store_true", default=False, help="don't cut any tracebacks (default is to cut).", ) group._addoption( "--color", metavar="color", action="store", dest="color", default="auto", choices=["yes", "no", "auto"], help="color terminal output (yes/no/auto).", ) parser.addini( "console_output_style", help='console output: "classic", or with additional progress information ("progress" (percentage) | "count").', default="progress", ) def pytest_configure(config): reporter = TerminalReporter(config, sys.stdout) config.pluginmanager.register(reporter, "terminalreporter") if config.option.debug or config.option.traceconfig: def mywriter(tags, args): msg = " ".join(map(str, args)) reporter.write_line("[traceconfig] " + msg) config.trace.root.setprocessor("pytest:config", mywriter) def getreportopt(config): reportopts = "" reportchars = config.option.reportchars if not config.option.disable_warnings and "w" not in reportchars: reportchars += "w" elif config.option.disable_warnings and "w" in reportchars: reportchars = reportchars.replace("w", "") for char in reportchars: if char == "a": reportopts = "sxXwEf" elif char == "A": reportopts = "PpsxXwEf" break elif char not in reportopts: reportopts += char return reportopts @pytest.hookimpl(trylast=True) # after _pytest.runner def pytest_report_teststatus(report): if report.passed: letter = "." elif report.skipped: letter = "s" elif report.failed: letter = "F" if report.when != "call": letter = "f" return report.outcome, letter, report.outcome.upper() @attr.s class WarningReport(object): """ Simple structure to hold warnings information captured by ``pytest_warning_captured``. :ivar str message: user friendly message about the warning :ivar str|None nodeid: node id that generated the warning (see ``get_location``). :ivar tuple|py.path.local fslocation: file system location of the source of the warning (see ``get_location``). """ message = attr.ib() nodeid = attr.ib(default=None) fslocation = attr.ib(default=None) count_towards_summary = True def get_location(self, config): """ Returns the more user-friendly information about the location of a warning, or None. """ if self.nodeid: return self.nodeid if self.fslocation: if isinstance(self.fslocation, tuple) and len(self.fslocation) >= 2: filename, linenum = self.fslocation[:2] relpath = py.path.local(filename).relto(config.invocation_dir) if not relpath: relpath = str(filename) return "%s:%s" % (relpath, linenum) else: return str(self.fslocation) return None class TerminalReporter(object): def __init__(self, config, file=None): import _pytest.config self.config = config self._numcollected = 0 self._session = None self._showfspath = None self.stats = {} self.startdir = config.invocation_dir if file is None: file = sys.stdout self._tw = _pytest.config.create_terminal_writer(config, file) # self.writer will be deprecated in pytest-3.4 self.writer = self._tw self._screen_width = self._tw.fullwidth self.currentfspath = None self.reportchars = getreportopt(config) self.hasmarkup = self._tw.hasmarkup self.isatty = file.isatty() self._progress_nodeids_reported = set() self._show_progress_info = self._determine_show_progress_info() self._collect_report_last_write = None def _determine_show_progress_info(self): """Return True if we should display progress information based on the current config""" # do not show progress if we are not capturing output (#3038) if self.config.getoption("capture", "no") == "no": return False # do not show progress if we are showing fixture setup/teardown if self.config.getoption("setupshow", False): return False cfg = self.config.getini("console_output_style") if cfg in ("progress", "count"): return cfg return False @property def verbosity(self): return self.config.option.verbose @property def showheader(self): return self.verbosity >= 0 @property def showfspath(self): if self._showfspath is None: return self.verbosity >= 0 return self._showfspath @showfspath.setter def showfspath(self, value): self._showfspath = value @property def showlongtestinfo(self): return self.verbosity > 0 def hasopt(self, char): char = {"xfailed": "x", "skipped": "s"}.get(char, char) return char in self.reportchars def write_fspath_result(self, nodeid, res, **markup): fspath = self.config.rootdir.join(nodeid.split("::")[0]) # NOTE: explicitly check for None to work around py bug, and for less # overhead in general (https://github.com/pytest-dev/py/pull/207). if self.currentfspath is None or fspath != self.currentfspath: if self.currentfspath is not None and self._show_progress_info: self._write_progress_information_filling_space() self.currentfspath = fspath fspath = self.startdir.bestrelpath(fspath) self._tw.line() self._tw.write(fspath + " ") self._tw.write(res, **markup) def write_ensure_prefix(self, prefix, extra="", **kwargs): if self.currentfspath != prefix: self._tw.line() self.currentfspath = prefix self._tw.write(prefix) if extra: self._tw.write(extra, **kwargs) self.currentfspath = -2 def ensure_newline(self): if self.currentfspath: self._tw.line() self.currentfspath = None def write(self, content, **markup): self._tw.write(content, **markup) def write_line(self, line, **markup): if not isinstance(line, six.text_type): line = six.text_type(line, errors="replace") self.ensure_newline() self._tw.line(line, **markup) def rewrite(self, line, **markup): """ Rewinds the terminal cursor to the beginning and writes the given line. :kwarg erase: if True, will also add spaces until the full terminal width to ensure previous lines are properly erased. The rest of the keyword arguments are markup instructions. """ erase = markup.pop("erase", False) if erase: fill_count = self._tw.fullwidth - len(line) - 1 fill = " " * fill_count else: fill = "" line = str(line) self._tw.write("\r" + line + fill, **markup) def write_sep(self, sep, title=None, **markup): self.ensure_newline() self._tw.sep(sep, title, **markup) def section(self, title, sep="=", **kw): self._tw.sep(sep, title, **kw) def line(self, msg, **kw): self._tw.line(msg, **kw) def pytest_internalerror(self, excrepr): for line in six.text_type(excrepr).split("\n"): self.write_line("INTERNALERROR> " + line) return 1 def pytest_warning_captured(self, warning_message, item): # from _pytest.nodes import get_fslocation_from_item from _pytest.warnings import warning_record_to_str warnings = self.stats.setdefault("warnings", []) fslocation = warning_message.filename, warning_message.lineno message = warning_record_to_str(warning_message) nodeid = item.nodeid if item is not None else "" warning_report = WarningReport( fslocation=fslocation, message=message, nodeid=nodeid ) warnings.append(warning_report) def pytest_plugin_registered(self, plugin): if self.config.option.traceconfig: msg = "PLUGIN registered: %s" % (plugin,) # XXX this event may happen during setup/teardown time # which unfortunately captures our output here # which garbles our output if we use self.write_line self.write_line(msg) def pytest_deselected(self, items): self.stats.setdefault("deselected", []).extend(items) def pytest_runtest_logstart(self, nodeid, location): # ensure that the path is printed before the # 1st test of a module starts running if self.showlongtestinfo: line = self._locationline(nodeid, *location) self.write_ensure_prefix(line, "") elif self.showfspath: fsid = nodeid.split("::")[0] self.write_fspath_result(fsid, "") def pytest_runtest_logreport(self, report): self._tests_ran = True rep = report res = self.config.hook.pytest_report_teststatus(report=rep, config=self.config) category, letter, word = res if isinstance(word, tuple): word, markup = word else: markup = None self.stats.setdefault(category, []).append(rep) if not letter and not word: # probably passed setup/teardown return running_xdist = hasattr(rep, "node") if markup is None: was_xfail = hasattr(report, "wasxfail") if rep.passed and not was_xfail: markup = {"green": True} elif rep.passed and was_xfail: markup = {"yellow": True} elif rep.failed: markup = {"red": True} elif rep.skipped: markup = {"yellow": True} else: markup = {} if self.verbosity <= 0: if not running_xdist and self.showfspath: self.write_fspath_result(rep.nodeid, letter, **markup) else: self._tw.write(letter, **markup) else: self._progress_nodeids_reported.add(rep.nodeid) line = self._locationline(rep.nodeid, *rep.location) if not running_xdist: self.write_ensure_prefix(line, word, **markup) if self._show_progress_info: self._write_progress_information_filling_space() else: self.ensure_newline() self._tw.write("[%s]" % rep.node.gateway.id) if self._show_progress_info: self._tw.write( self._get_progress_information_message() + " ", cyan=True ) else: self._tw.write(" ") self._tw.write(word, **markup) self._tw.write(" " + line) self.currentfspath = -2 def pytest_runtest_logfinish(self, nodeid): if self.verbosity <= 0 and self._show_progress_info: if self._show_progress_info == "count": num_tests = self._session.testscollected progress_length = len(" [{}/{}]".format(str(num_tests), str(num_tests))) else: progress_length = len(" [100%]") self._progress_nodeids_reported.add(nodeid) is_last_item = ( len(self._progress_nodeids_reported) == self._session.testscollected ) if is_last_item: self._write_progress_information_filling_space() else: w = self._width_of_current_line past_edge = w + progress_length + 1 >= self._screen_width if past_edge: msg = self._get_progress_information_message() self._tw.write(msg + "\n", cyan=True) def _get_progress_information_message(self): collected = self._session.testscollected if self._show_progress_info == "count": if collected: progress = self._progress_nodeids_reported counter_format = "{{:{}d}}".format(len(str(collected))) format_string = " [{}/{{}}]".format(counter_format) return format_string.format(len(progress), collected) return " [ {} / {} ]".format(collected, collected) else: if collected: progress = len(self._progress_nodeids_reported) * 100 // collected return " [{:3d}%]".format(progress) return " [100%]" def _write_progress_information_filling_space(self): msg = self._get_progress_information_message() w = self._width_of_current_line fill = self._tw.fullwidth - w - 1 self.write(msg.rjust(fill), cyan=True) @property def _width_of_current_line(self): """Return the width of current line, using the superior implementation of py-1.6 when available""" try: return self._tw.width_of_current_line except AttributeError: # py < 1.6.0 return self._tw.chars_on_current_line def pytest_collection(self): if self.isatty: if self.config.option.verbose >= 0: self.write("collecting ... ", bold=True) self._collect_report_last_write = time.time() elif self.config.option.verbose >= 1: self.write("collecting ... ", bold=True) def pytest_collectreport(self, report): if report.failed: self.stats.setdefault("error", []).append(report) elif report.skipped: self.stats.setdefault("skipped", []).append(report) items = [x for x in report.result if isinstance(x, pytest.Item)] self._numcollected += len(items) if self.isatty: self.report_collect() def report_collect(self, final=False): if self.config.option.verbose < 0: return if not final: # Only write "collecting" report every 0.5s. t = time.time() if ( self._collect_report_last_write is not None and self._collect_report_last_write > t - REPORT_COLLECTING_RESOLUTION ): return self._collect_report_last_write = t errors = len(self.stats.get("error", [])) skipped = len(self.stats.get("skipped", [])) deselected = len(self.stats.get("deselected", [])) selected = self._numcollected - errors - skipped - deselected if final: line = "collected " else: line = "collecting " line += ( str(self._numcollected) + " item" + ("" if self._numcollected == 1 else "s") ) if errors: line += " / %d errors" % errors if deselected: line += " / %d deselected" % deselected if skipped: line += " / %d skipped" % skipped if self._numcollected > selected > 0: line += " / %d selected" % selected if self.isatty: self.rewrite(line, bold=True, erase=True) if final: self.write("\n") else: self.write_line(line) @pytest.hookimpl(trylast=True) def pytest_sessionstart(self, session): self._session = session self._sessionstarttime = time.time() if not self.showheader: return self.write_sep("=", "test session starts", bold=True) verinfo = platform.python_version() msg = "platform %s -- Python %s" % (sys.platform, verinfo) if hasattr(sys, "pypy_version_info"): verinfo = ".".join(map(str, sys.pypy_version_info[:3])) msg += "[pypy-%s-%s]" % (verinfo, sys.pypy_version_info[3]) msg += ", pytest-%s, py-%s, pluggy-%s" % ( pytest.__version__, py.__version__, pluggy.__version__, ) if ( self.verbosity > 0 or self.config.option.debug or getattr(self.config.option, "pastebin", None) ): msg += " -- " + str(sys.executable) self.write_line(msg) lines = self.config.hook.pytest_report_header( config=self.config, startdir=self.startdir ) self._write_report_lines_from_hooks(lines) def _write_report_lines_from_hooks(self, lines): lines.reverse() for line in collapse(lines): self.write_line(line) def pytest_report_header(self, config): line = "rootdir: %s" % config.rootdir if config.inifile: line += ", inifile: " + config.rootdir.bestrelpath(config.inifile) testpaths = config.getini("testpaths") if testpaths and config.args == testpaths: rel_paths = [config.rootdir.bestrelpath(x) for x in testpaths] line += ", testpaths: {}".format(", ".join(rel_paths)) result = [line] plugininfo = config.pluginmanager.list_plugin_distinfo() if plugininfo: result.append("plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) return result def pytest_collection_finish(self, session): self.report_collect(True) if self.config.getoption("collectonly"): self._printcollecteditems(session.items) lines = self.config.hook.pytest_report_collectionfinish( config=self.config, startdir=self.startdir, items=session.items ) self._write_report_lines_from_hooks(lines) if self.config.getoption("collectonly"): if self.stats.get("failed"): self._tw.sep("!", "collection failures") for rep in self.stats.get("failed"): rep.toterminal(self._tw) def _printcollecteditems(self, items): # to print out items and their parent collectors # we take care to leave out Instances aka () # because later versions are going to get rid of them anyway if self.config.option.verbose < 0: if self.config.option.verbose < -1: counts = {} for item in items: name = item.nodeid.split("::", 1)[0] counts[name] = counts.get(name, 0) + 1 for name, count in sorted(counts.items()): self._tw.line("%s: %d" % (name, count)) else: for item in items: self._tw.line(item.nodeid) return stack = [] indent = "" for item in items: needed_collectors = item.listchain()[1:] # strip root node while stack: if stack == needed_collectors[: len(stack)]: break stack.pop() for col in needed_collectors[len(stack) :]: stack.append(col) if col.name == "()": # Skip Instances. continue indent = (len(stack) - 1) * " " self._tw.line("%s%s" % (indent, col)) if self.config.option.verbose >= 1: if hasattr(col, "_obj") and col._obj.__doc__: for line in col._obj.__doc__.strip().splitlines(): self._tw.line("%s%s" % (indent + " ", line.strip())) @pytest.hookimpl(hookwrapper=True) def pytest_sessionfinish(self, exitstatus): outcome = yield outcome.get_result() self._tw.line("") summary_exit_codes = ( EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED, ) if exitstatus in summary_exit_codes: self.config.hook.pytest_terminal_summary( terminalreporter=self, exitstatus=exitstatus, config=self.config ) if exitstatus == EXIT_INTERRUPTED: self._report_keyboardinterrupt() del self._keyboardinterrupt_memo self.summary_stats() @pytest.hookimpl(hookwrapper=True) def pytest_terminal_summary(self): self.summary_errors() self.summary_failures() self.summary_warnings() self.summary_passes() yield self.short_test_summary() # Display any extra warnings from teardown here (if any). self.summary_warnings() def pytest_keyboard_interrupt(self, excinfo): self._keyboardinterrupt_memo = excinfo.getrepr(funcargs=True) def pytest_unconfigure(self): if hasattr(self, "_keyboardinterrupt_memo"): self._report_keyboardinterrupt() def _report_keyboardinterrupt(self): excrepr = self._keyboardinterrupt_memo msg = excrepr.reprcrash.message self.write_sep("!", msg) if "KeyboardInterrupt" in msg: if self.config.option.fulltrace: excrepr.toterminal(self._tw) else: excrepr.reprcrash.toterminal(self._tw) self._tw.line( "(to show a full traceback on KeyboardInterrupt use --fulltrace)", yellow=True, ) def _locationline(self, nodeid, fspath, lineno, domain): def mkrel(nodeid): line = self.config.cwd_relative_nodeid(nodeid) if domain and line.endswith(domain): line = line[: -len(domain)] values = domain.split("[") values[0] = values[0].replace(".", "::") # don't replace '.' in params line += "[".join(values) return line # collect_fspath comes from testid which has a "/"-normalized path if fspath: res = mkrel(nodeid) if self.verbosity >= 2 and nodeid.split("::")[0] != fspath.replace( "\\", nodes.SEP ): res += " <- " + self.startdir.bestrelpath(fspath) else: res = "[location]" return res + " " def _getfailureheadline(self, rep): head_line = rep.head_line if head_line: return head_line return "test session" # XXX? def _getcrashline(self, rep): try: return str(rep.longrepr.reprcrash) except AttributeError: try: return str(rep.longrepr)[:50] except AttributeError: return "" # # summaries for sessionfinish # def getreports(self, name): values = [] for x in self.stats.get(name, []): if not hasattr(x, "_pdbshown"): values.append(x) return values def summary_warnings(self): if self.hasopt("w"): all_warnings = self.stats.get("warnings") if not all_warnings: return final = hasattr(self, "_already_displayed_warnings") if final: warning_reports = all_warnings[self._already_displayed_warnings :] else: warning_reports = all_warnings self._already_displayed_warnings = len(warning_reports) if not warning_reports: return reports_grouped_by_message = collections.OrderedDict() for wr in warning_reports: reports_grouped_by_message.setdefault(wr.message, []).append(wr) title = "warnings summary (final)" if final else "warnings summary" self.write_sep("=", title, yellow=True, bold=False) for message, warning_reports in reports_grouped_by_message.items(): has_any_location = False for w in warning_reports: location = w.get_location(self.config) if location: self._tw.line(str(location)) has_any_location = True if has_any_location: lines = message.splitlines() indented = "\n".join(" " + x for x in lines) message = indented.rstrip() else: message = message.rstrip() self._tw.line(message) self._tw.line() self._tw.line("-- Docs: https://docs.pytest.org/en/latest/warnings.html") def summary_passes(self): if self.config.option.tbstyle != "no": if self.hasopt("P"): reports = self.getreports("passed") if not reports: return self.write_sep("=", "PASSES") for rep in reports: if rep.sections: msg = self._getfailureheadline(rep) self.write_sep("_", msg, green=True, bold=True) self._outrep_summary(rep) def print_teardown_sections(self, rep): showcapture = self.config.option.showcapture if showcapture == "no": return for secname, content in rep.sections: if showcapture != "all" and showcapture not in secname: continue if "teardown" in secname: self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] self._tw.line(content) def summary_failures(self): if self.config.option.tbstyle != "no": reports = self.getreports("failed") if not reports: return self.write_sep("=", "FAILURES") if self.config.option.tbstyle == "line": for rep in reports: line = self._getcrashline(rep) self.write_line(line) else: teardown_sections = {} for report in self.getreports(""): if report.when == "teardown": teardown_sections.setdefault(report.nodeid, []).append(report) for rep in reports: msg = self._getfailureheadline(rep) self.write_sep("_", msg, red=True, bold=True) self._outrep_summary(rep) for report in teardown_sections.get(rep.nodeid, []): self.print_teardown_sections(report) def summary_errors(self): if self.config.option.tbstyle != "no": reports = self.getreports("error") if not reports: return self.write_sep("=", "ERRORS") for rep in self.stats["error"]: msg = self._getfailureheadline(rep) if rep.when == "collect": msg = "ERROR collecting " + msg else: msg = "ERROR at %s of %s" % (rep.when, msg) self.write_sep("_", msg, red=True, bold=True) self._outrep_summary(rep) def _outrep_summary(self, rep): rep.toterminal(self._tw) showcapture = self.config.option.showcapture if showcapture == "no": return for secname, content in rep.sections: if showcapture != "all" and showcapture not in secname: continue self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] self._tw.line(content) def summary_stats(self): session_duration = time.time() - self._sessionstarttime (line, color) = build_summary_stats_line(self.stats) msg = "%s in %.2f seconds" % (line, session_duration) markup = {color: True, "bold": True} if self.verbosity >= 0: self.write_sep("=", msg, **markup) if self.verbosity == -1: self.write_line(msg, **markup) def short_test_summary(self): if not self.reportchars: return def show_simple(stat, lines): failed = self.stats.get(stat, []) if not failed: return termwidth = self.writer.fullwidth config = self.config for rep in failed: line = _get_line_with_reprcrash_message(config, rep, termwidth) lines.append(line) def show_xfailed(lines): xfailed = self.stats.get("xfailed", []) for rep in xfailed: verbose_word = rep._get_verbose_word(self.config) pos = _get_pos(self.config, rep) lines.append("%s %s" % (verbose_word, pos)) reason = rep.wasxfail if reason: lines.append(" " + str(reason)) def show_xpassed(lines): xpassed = self.stats.get("xpassed", []) for rep in xpassed: verbose_word = rep._get_verbose_word(self.config) pos = _get_pos(self.config, rep) reason = rep.wasxfail lines.append("%s %s %s" % (verbose_word, pos, reason)) def show_skipped(lines): skipped = self.stats.get("skipped", []) fskips = _folded_skips(skipped) if skipped else [] if not fskips: return verbose_word = skipped[0]._get_verbose_word(self.config) for num, fspath, lineno, reason in fskips: if reason.startswith("Skipped: "): reason = reason[9:] if lineno is not None: lines.append( "%s [%d] %s:%d: %s" % (verbose_word, num, fspath, lineno + 1, reason) ) else: lines.append("%s [%d] %s: %s" % (verbose_word, num, fspath, reason)) REPORTCHAR_ACTIONS = { "x": show_xfailed, "X": show_xpassed, "f": partial(show_simple, "failed"), "F": partial(show_simple, "failed"), "s": show_skipped, "S": show_skipped, "p": partial(show_simple, "passed"), "E": partial(show_simple, "error"), } lines = [] for char in self.reportchars: action = REPORTCHAR_ACTIONS.get(char) if action: # skipping e.g. "P" (passed with output) here. action(lines) if lines: self.write_sep("=", "short test summary info") for line in lines: self.write_line(line) def _get_pos(config, rep): nodeid = config.cwd_relative_nodeid(rep.nodeid) return nodeid def _get_line_with_reprcrash_message(config, rep, termwidth): """Get summary line for a report, trying to add reprcrash message.""" from wcwidth import wcswidth verbose_word = rep._get_verbose_word(config) pos = _get_pos(config, rep) line = "%s %s" % (verbose_word, pos) len_line = wcswidth(line) ellipsis, len_ellipsis = "...", 3 if len_line > termwidth - len_ellipsis: # No space for an additional message. return line try: msg = rep.longrepr.reprcrash.message except AttributeError: pass else: # Only use the first line. i = msg.find("\n") if i != -1: msg = msg[:i] len_msg = wcswidth(msg) sep, len_sep = " - ", 3 max_len_msg = termwidth - len_line - len_sep if max_len_msg >= len_ellipsis: if len_msg > max_len_msg: max_len_msg -= len_ellipsis msg = msg[:max_len_msg] while wcswidth(msg) > max_len_msg: msg = msg[:-1] if six.PY2: # on python 2 systems with narrow unicode compilation, trying to # get a single character out of a multi-byte unicode character such as # u'😄' will result in a High Surrogate (U+D83D) character, which is # rendered as u'�'; in this case we just strip that character out as it # serves no purpose being rendered try: surrogate = six.unichr(0xD83D) msg = msg.rstrip(surrogate) except ValueError: # pragma: no cover # Jython cannot represent this lone surrogate at all (#5256): # ValueError: unichr() arg is a lone surrogate in range # (0xD800, 0xDFFF) (Jython UTF-16 encoding) # ignore this case as it shouldn't appear in the string anyway pass msg += ellipsis line += sep + msg return line def _folded_skips(skipped): d = {} for event in skipped: key = event.longrepr assert len(key) == 3, (event, key) keywords = getattr(event, "keywords", {}) # folding reports with global pytestmark variable # this is workaround, because for now we cannot identify the scope of a skip marker # TODO: revisit after marks scope would be fixed if ( event.when == "setup" and "skip" in keywords and "pytestmark" not in keywords ): key = (key[0], None, key[2]) d.setdefault(key, []).append(event) values = [] for key, events in d.items(): values.append((len(events),) + key) return values def build_summary_stats_line(stats): known_types = ( "failed passed skipped deselected xfailed xpassed warnings error".split() ) unknown_type_seen = False for found_type in stats: if found_type not in known_types: if found_type: # setup/teardown reports have an empty key, ignore them known_types.append(found_type) unknown_type_seen = True parts = [] for key in known_types: reports = stats.get(key, None) if reports: count = sum( 1 for rep in reports if getattr(rep, "count_towards_summary", True) ) parts.append("%d %s" % (count, key)) if parts: line = ", ".join(parts) else: line = "no tests ran" if "failed" in stats or "error" in stats: color = "red" elif "warnings" in stats or unknown_type_seen: color = "yellow" elif "passed" in stats: color = "green" else: color = "yellow" return line, color def _plugin_nameversions(plugininfo): values = [] for plugin, dist in plugininfo: # gets us name and version! name = "{dist.project_name}-{dist.version}".format(dist=dist) # questionable convenience, but it keeps things short if name.startswith("pytest-"): name = name[7:] # we decided to print python package names # they can have more than one plugin if name not in values: values.append(name) return values pytest-4.6.9/src/_pytest/logging.py0000664000372000037200000005770613604173400020217 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Access and control log capturing. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import logging import re from contextlib import contextmanager import py import six import pytest from _pytest.compat import dummy_context_manager from _pytest.config import create_terminal_writer from _pytest.pathlib import Path DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s" DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S" _ANSI_ESCAPE_SEQ = re.compile(r"\x1b\[[\d;]+m") def _remove_ansi_escape_sequences(text): return _ANSI_ESCAPE_SEQ.sub("", text) class ColoredLevelFormatter(logging.Formatter): """ Colorize the %(levelname)..s part of the log format passed to __init__. """ LOGLEVEL_COLOROPTS = { logging.CRITICAL: {"red"}, logging.ERROR: {"red", "bold"}, logging.WARNING: {"yellow"}, logging.WARN: {"yellow"}, logging.INFO: {"green"}, logging.DEBUG: {"purple"}, logging.NOTSET: set(), } LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-]?\d*s)") def __init__(self, terminalwriter, *args, **kwargs): super(ColoredLevelFormatter, self).__init__(*args, **kwargs) if six.PY2: self._original_fmt = self._fmt else: self._original_fmt = self._style._fmt self._level_to_fmt_mapping = {} levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt) if not levelname_fmt_match: return levelname_fmt = levelname_fmt_match.group() for level, color_opts in self.LOGLEVEL_COLOROPTS.items(): formatted_levelname = levelname_fmt % { "levelname": logging.getLevelName(level) } # add ANSI escape sequences around the formatted levelname color_kwargs = {name: True for name in color_opts} colorized_formatted_levelname = terminalwriter.markup( formatted_levelname, **color_kwargs ) self._level_to_fmt_mapping[level] = self.LEVELNAME_FMT_REGEX.sub( colorized_formatted_levelname, self._fmt ) def format(self, record): fmt = self._level_to_fmt_mapping.get(record.levelno, self._original_fmt) if six.PY2: self._fmt = fmt else: self._style._fmt = fmt return super(ColoredLevelFormatter, self).format(record) if not six.PY2: # Formatter classes don't support format styles in PY2 class PercentStyleMultiline(logging.PercentStyle): """A logging style with special support for multiline messages. If the message of a record consists of multiple lines, this style formats the message as if each line were logged separately. """ @staticmethod def _update_message(record_dict, message): tmp = record_dict.copy() tmp["message"] = message return tmp def format(self, record): if "\n" in record.message: lines = record.message.splitlines() formatted = self._fmt % self._update_message(record.__dict__, lines[0]) # TODO optimize this by introducing an option that tells the # logging framework that the indentation doesn't # change. This allows to compute the indentation only once. indentation = _remove_ansi_escape_sequences(formatted).find(lines[0]) lines[0] = formatted return ("\n" + " " * indentation).join(lines) else: return self._fmt % record.__dict__ def get_option_ini(config, *names): for name in names: ret = config.getoption(name) # 'default' arg won't work as expected if ret is None: ret = config.getini(name) if ret: return ret def pytest_addoption(parser): """Add options to control log capturing.""" group = parser.getgroup("logging") def add_option_ini(option, dest, default=None, type=None, **kwargs): parser.addini( dest, default=default, type=type, help="default value for " + option ) group.addoption(option, dest=dest, **kwargs) add_option_ini( "--no-print-logs", dest="log_print", action="store_const", const=False, default=True, type="bool", help="disable printing caught logs on failed tests.", ) add_option_ini( "--log-level", dest="log_level", default=None, help="logging level used by the logging module", ) add_option_ini( "--log-format", dest="log_format", default=DEFAULT_LOG_FORMAT, help="log format as used by the logging module.", ) add_option_ini( "--log-date-format", dest="log_date_format", default=DEFAULT_LOG_DATE_FORMAT, help="log date format as used by the logging module.", ) parser.addini( "log_cli", default=False, type="bool", help='enable log display during test run (also known as "live logging").', ) add_option_ini( "--log-cli-level", dest="log_cli_level", default=None, help="cli logging level." ) add_option_ini( "--log-cli-format", dest="log_cli_format", default=None, help="log format as used by the logging module.", ) add_option_ini( "--log-cli-date-format", dest="log_cli_date_format", default=None, help="log date format as used by the logging module.", ) add_option_ini( "--log-file", dest="log_file", default=None, help="path to a file when logging will be written to.", ) add_option_ini( "--log-file-level", dest="log_file_level", default=None, help="log file logging level.", ) add_option_ini( "--log-file-format", dest="log_file_format", default=DEFAULT_LOG_FORMAT, help="log format as used by the logging module.", ) add_option_ini( "--log-file-date-format", dest="log_file_date_format", default=DEFAULT_LOG_DATE_FORMAT, help="log date format as used by the logging module.", ) @contextmanager def catching_logs(handler, formatter=None, level=None): """Context manager that prepares the whole logging machinery properly.""" root_logger = logging.getLogger() if formatter is not None: handler.setFormatter(formatter) if level is not None: handler.setLevel(level) # Adding the same handler twice would confuse logging system. # Just don't do that. add_new_handler = handler not in root_logger.handlers if add_new_handler: root_logger.addHandler(handler) if level is not None: orig_level = root_logger.level root_logger.setLevel(min(orig_level, level)) try: yield handler finally: if level is not None: root_logger.setLevel(orig_level) if add_new_handler: root_logger.removeHandler(handler) class LogCaptureHandler(logging.StreamHandler): """A logging handler that stores log records and the log text.""" def __init__(self): """Creates a new log handler.""" logging.StreamHandler.__init__(self, py.io.TextIO()) self.records = [] def emit(self, record): """Keep the log records in a list in addition to the log text.""" self.records.append(record) logging.StreamHandler.emit(self, record) def reset(self): self.records = [] self.stream = py.io.TextIO() class LogCaptureFixture(object): """Provides access and control of log capturing.""" def __init__(self, item): """Creates a new funcarg.""" self._item = item # dict of log name -> log level self._initial_log_levels = {} # Dict[str, int] def _finalize(self): """Finalizes the fixture. This restores the log levels changed by :meth:`set_level`. """ # restore log levels for logger_name, level in self._initial_log_levels.items(): logger = logging.getLogger(logger_name) logger.setLevel(level) @property def handler(self): """ :rtype: LogCaptureHandler """ return self._item.catch_log_handler def get_records(self, when): """ Get the logging records for one of the possible test phases. :param str when: Which test phase to obtain the records from. Valid values are: "setup", "call" and "teardown". :rtype: List[logging.LogRecord] :return: the list of captured records at the given stage .. versionadded:: 3.4 """ handler = self._item.catch_log_handlers.get(when) if handler: return handler.records else: return [] @property def text(self): """Returns the formatted log text.""" return _remove_ansi_escape_sequences(self.handler.stream.getvalue()) @property def records(self): """Returns the list of log records.""" return self.handler.records @property def record_tuples(self): """Returns a list of a stripped down version of log records intended for use in assertion comparison. The format of the tuple is: (logger_name, log_level, message) """ return [(r.name, r.levelno, r.getMessage()) for r in self.records] @property def messages(self): """Returns a list of format-interpolated log messages. Unlike 'records', which contains the format string and parameters for interpolation, log messages in this list are all interpolated. Unlike 'text', which contains the output from the handler, log messages in this list are unadorned with levels, timestamps, etc, making exact comparisons more reliable. Note that traceback or stack info (from :func:`logging.exception` or the `exc_info` or `stack_info` arguments to the logging functions) is not included, as this is added by the formatter in the handler. .. versionadded:: 3.7 """ return [r.getMessage() for r in self.records] def clear(self): """Reset the list of log records and the captured log text.""" self.handler.reset() def set_level(self, level, logger=None): """Sets the level for capturing of logs. The level will be restored to its previous value at the end of the test. :param int level: the logger to level. :param str logger: the logger to update the level. If not given, the root logger level is updated. .. versionchanged:: 3.4 The levels of the loggers changed by this function will be restored to their initial values at the end of the test. """ logger_name = logger logger = logging.getLogger(logger_name) # save the original log-level to restore it during teardown self._initial_log_levels.setdefault(logger_name, logger.level) logger.setLevel(level) @contextmanager def at_level(self, level, logger=None): """Context manager that sets the level for capturing of logs. After the end of the 'with' statement the level is restored to its original value. :param int level: the logger to level. :param str logger: the logger to update the level. If not given, the root logger level is updated. """ logger = logging.getLogger(logger) orig_level = logger.level logger.setLevel(level) try: yield finally: logger.setLevel(orig_level) @pytest.fixture def caplog(request): """Access and control log capturing. Captured logs are available through the following properties/methods:: * caplog.text -> string containing formatted log output * caplog.records -> list of logging.LogRecord instances * caplog.record_tuples -> list of (logger_name, level, message) tuples * caplog.clear() -> clear captured records and formatted log output string """ result = LogCaptureFixture(request.node) yield result result._finalize() def get_actual_log_level(config, *setting_names): """Return the actual logging level.""" for setting_name in setting_names: log_level = config.getoption(setting_name) if log_level is None: log_level = config.getini(setting_name) if log_level: break else: return if isinstance(log_level, six.string_types): log_level = log_level.upper() try: return int(getattr(logging, log_level, log_level)) except ValueError: # Python logging does not recognise this as a logging level raise pytest.UsageError( "'{}' is not recognized as a logging level name for " "'{}'. Please consider passing the " "logging level num instead.".format(log_level, setting_name) ) # run after terminalreporter/capturemanager are configured @pytest.hookimpl(trylast=True) def pytest_configure(config): config.pluginmanager.register(LoggingPlugin(config), "logging-plugin") class LoggingPlugin(object): """Attaches to the logging module and captures log messages for each test. """ def __init__(self, config): """Creates a new plugin to capture log messages. The formatter can be safely shared across all handlers so create a single one for the entire test session here. """ self._config = config self.print_logs = get_option_ini(config, "log_print") self.formatter = self._create_formatter( get_option_ini(config, "log_format"), get_option_ini(config, "log_date_format"), ) self.log_level = get_actual_log_level(config, "log_level") self.log_file_level = get_actual_log_level(config, "log_file_level") self.log_file_format = get_option_ini(config, "log_file_format", "log_format") self.log_file_date_format = get_option_ini( config, "log_file_date_format", "log_date_format" ) self.log_file_formatter = logging.Formatter( self.log_file_format, datefmt=self.log_file_date_format ) log_file = get_option_ini(config, "log_file") if log_file: self.log_file_handler = logging.FileHandler( log_file, mode="w", encoding="UTF-8" ) self.log_file_handler.setFormatter(self.log_file_formatter) else: self.log_file_handler = None self.log_cli_handler = None self.live_logs_context = lambda: dummy_context_manager() # Note that the lambda for the live_logs_context is needed because # live_logs_context can otherwise not be entered multiple times due # to limitations of contextlib.contextmanager. if self._log_cli_enabled(): self._setup_cli_logging() def _create_formatter(self, log_format, log_date_format): # color option doesn't exist if terminal plugin is disabled color = getattr(self._config.option, "color", "no") if color != "no" and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search( log_format ): formatter = ColoredLevelFormatter( create_terminal_writer(self._config), log_format, log_date_format ) else: formatter = logging.Formatter(log_format, log_date_format) if not six.PY2: formatter._style = PercentStyleMultiline(formatter._style._fmt) return formatter def _setup_cli_logging(self): config = self._config terminal_reporter = config.pluginmanager.get_plugin("terminalreporter") if terminal_reporter is None: # terminal reporter is disabled e.g. by pytest-xdist. return capture_manager = config.pluginmanager.get_plugin("capturemanager") # if capturemanager plugin is disabled, live logging still works. log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager) log_cli_formatter = self._create_formatter( get_option_ini(config, "log_cli_format", "log_format"), get_option_ini(config, "log_cli_date_format", "log_date_format"), ) log_cli_level = get_actual_log_level(config, "log_cli_level", "log_level") self.log_cli_handler = log_cli_handler self.live_logs_context = lambda: catching_logs( log_cli_handler, formatter=log_cli_formatter, level=log_cli_level ) def set_log_path(self, fname): """Public method, which can set filename parameter for Logging.FileHandler(). Also creates parent directory if it does not exist. .. warning:: Please considered as an experimental API. """ fname = Path(fname) if not fname.is_absolute(): fname = Path(self._config.rootdir, fname) if not fname.parent.exists(): fname.parent.mkdir(exist_ok=True, parents=True) self.log_file_handler = logging.FileHandler( str(fname), mode="w", encoding="UTF-8" ) self.log_file_handler.setFormatter(self.log_file_formatter) def _log_cli_enabled(self): """Return True if log_cli should be considered enabled, either explicitly or because --log-cli-level was given in the command-line. """ return self._config.getoption( "--log-cli-level" ) is not None or self._config.getini("log_cli") @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_collection(self): with self.live_logs_context(): if self.log_cli_handler: self.log_cli_handler.set_when("collection") if self.log_file_handler is not None: with catching_logs(self.log_file_handler, level=self.log_file_level): yield else: yield @contextmanager def _runtest_for(self, item, when): with self._runtest_for_main(item, when): if self.log_file_handler is not None: with catching_logs(self.log_file_handler, level=self.log_file_level): yield else: yield @contextmanager def _runtest_for_main(self, item, when): """Implements the internals of pytest_runtest_xxx() hook.""" with catching_logs( LogCaptureHandler(), formatter=self.formatter, level=self.log_level ) as log_handler: if self.log_cli_handler: self.log_cli_handler.set_when(when) if item is None: yield # run the test return if not hasattr(item, "catch_log_handlers"): item.catch_log_handlers = {} item.catch_log_handlers[when] = log_handler item.catch_log_handler = log_handler try: yield # run test finally: if when == "teardown": del item.catch_log_handler del item.catch_log_handlers if self.print_logs: # Add a captured log section to the report. log = log_handler.stream.getvalue().strip() item.add_report_section(when, "log", log) @pytest.hookimpl(hookwrapper=True) def pytest_runtest_setup(self, item): with self._runtest_for(item, "setup"): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(self, item): with self._runtest_for(item, "call"): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_teardown(self, item): with self._runtest_for(item, "teardown"): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_logstart(self): if self.log_cli_handler: self.log_cli_handler.reset() with self._runtest_for(None, "start"): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_logfinish(self): with self._runtest_for(None, "finish"): yield @pytest.hookimpl(hookwrapper=True) def pytest_runtest_logreport(self): with self._runtest_for(None, "logreport"): yield @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_sessionfinish(self): with self.live_logs_context(): if self.log_cli_handler: self.log_cli_handler.set_when("sessionfinish") if self.log_file_handler is not None: try: with catching_logs( self.log_file_handler, level=self.log_file_level ): yield finally: # Close the FileHandler explicitly. # (logging.shutdown might have lost the weakref?!) self.log_file_handler.close() else: yield @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_sessionstart(self): with self.live_logs_context(): if self.log_cli_handler: self.log_cli_handler.set_when("sessionstart") if self.log_file_handler is not None: with catching_logs(self.log_file_handler, level=self.log_file_level): yield else: yield @pytest.hookimpl(hookwrapper=True) def pytest_runtestloop(self, session): """Runs all collected test items.""" if session.config.option.collectonly: yield return if self._log_cli_enabled() and self._config.getoption("verbose") < 1: # setting verbose flag is needed to avoid messy test progress output self._config.option.verbose = 1 with self.live_logs_context(): if self.log_file_handler is not None: with catching_logs(self.log_file_handler, level=self.log_file_level): yield # run all the tests else: yield # run all the tests class _LiveLoggingStreamHandler(logging.StreamHandler): """ Custom StreamHandler used by the live logging feature: it will write a newline before the first log message in each test. During live logging we must also explicitly disable stdout/stderr capturing otherwise it will get captured and won't appear in the terminal. """ def __init__(self, terminal_reporter, capture_manager): """ :param _pytest.terminal.TerminalReporter terminal_reporter: :param _pytest.capture.CaptureManager capture_manager: """ logging.StreamHandler.__init__(self, stream=terminal_reporter) self.capture_manager = capture_manager self.reset() self.set_when(None) self._test_outcome_written = False def reset(self): """Reset the handler; should be called before the start of each test""" self._first_record_emitted = False def set_when(self, when): """Prepares for the given test phase (setup/call/teardown)""" self._when = when self._section_name_shown = False if when == "start": self._test_outcome_written = False def emit(self, record): ctx_manager = ( self.capture_manager.global_and_fixture_disabled() if self.capture_manager else dummy_context_manager() ) with ctx_manager: if not self._first_record_emitted: self.stream.write("\n") self._first_record_emitted = True elif self._when in ("teardown", "finish"): if not self._test_outcome_written: self._test_outcome_written = True self.stream.write("\n") if not self._section_name_shown and self._when: self.stream.section("live log " + self._when, sep="-", bold=True) self._section_name_shown = True logging.StreamHandler.emit(self, record) pytest-4.6.9/src/_pytest/debugging.py0000664000372000037200000002556413604173400020521 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ interactive debugging with PDB, the Python Debugger. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import pdb import sys from doctest import UnexpectedException from _pytest import outcomes from _pytest.config import hookimpl from _pytest.config.exceptions import UsageError def _validate_usepdb_cls(value): """Validate syntax of --pdbcls option.""" try: modname, classname = value.split(":") except ValueError: raise argparse.ArgumentTypeError( "{!r} is not in the format 'modname:classname'".format(value) ) return (modname, classname) def pytest_addoption(parser): group = parser.getgroup("general") group._addoption( "--pdb", dest="usepdb", action="store_true", help="start the interactive Python debugger on errors or KeyboardInterrupt.", ) group._addoption( "--pdbcls", dest="usepdb_cls", metavar="modulename:classname", type=_validate_usepdb_cls, help="start a custom interactive Python debugger on errors. " "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb", ) group._addoption( "--trace", dest="trace", action="store_true", help="Immediately break when running each test.", ) def pytest_configure(config): if config.getvalue("trace"): config.pluginmanager.register(PdbTrace(), "pdbtrace") if config.getvalue("usepdb"): config.pluginmanager.register(PdbInvoke(), "pdbinvoke") pytestPDB._saved.append( (pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config) ) pdb.set_trace = pytestPDB.set_trace pytestPDB._pluginmanager = config.pluginmanager pytestPDB._config = config # NOTE: not using pytest_unconfigure, since it might get called although # pytest_configure was not (if another plugin raises UsageError). def fin(): ( pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config, ) = pytestPDB._saved.pop() config._cleanup.append(fin) class pytestPDB(object): """ Pseudo PDB that defers to the real pdb. """ _pluginmanager = None _config = None _saved = [] _recursive_debug = 0 _wrapped_pdb_cls = None @classmethod def _is_capturing(cls, capman): if capman: return capman.is_capturing() return False @classmethod def _import_pdb_cls(cls, capman): if not cls._config: # Happens when using pytest.set_trace outside of a test. return pdb.Pdb usepdb_cls = cls._config.getvalue("usepdb_cls") if cls._wrapped_pdb_cls and cls._wrapped_pdb_cls[0] == usepdb_cls: return cls._wrapped_pdb_cls[1] if usepdb_cls: modname, classname = usepdb_cls try: __import__(modname) mod = sys.modules[modname] # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp). parts = classname.split(".") pdb_cls = getattr(mod, parts[0]) for part in parts[1:]: pdb_cls = getattr(pdb_cls, part) except Exception as exc: value = ":".join((modname, classname)) raise UsageError( "--pdbcls: could not import {!r}: {}".format(value, exc) ) else: pdb_cls = pdb.Pdb wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman) cls._wrapped_pdb_cls = (usepdb_cls, wrapped_cls) return wrapped_cls @classmethod def _get_pdb_wrapper_class(cls, pdb_cls, capman): import _pytest.config class PytestPdbWrapper(pdb_cls, object): _pytest_capman = capman _continued = False def do_debug(self, arg): cls._recursive_debug += 1 ret = super(PytestPdbWrapper, self).do_debug(arg) cls._recursive_debug -= 1 return ret def do_continue(self, arg): ret = super(PytestPdbWrapper, self).do_continue(arg) if cls._recursive_debug == 0: tw = _pytest.config.create_terminal_writer(cls._config) tw.line() capman = self._pytest_capman capturing = pytestPDB._is_capturing(capman) if capturing: if capturing == "global": tw.sep(">", "PDB continue (IO-capturing resumed)") else: tw.sep( ">", "PDB continue (IO-capturing resumed for %s)" % capturing, ) capman.resume() else: tw.sep(">", "PDB continue") cls._pluginmanager.hook.pytest_leave_pdb(config=cls._config, pdb=self) self._continued = True return ret do_c = do_cont = do_continue def do_quit(self, arg): """Raise Exit outcome when quit command is used in pdb. This is a bit of a hack - it would be better if BdbQuit could be handled, but this would require to wrap the whole pytest run, and adjust the report etc. """ ret = super(PytestPdbWrapper, self).do_quit(arg) if cls._recursive_debug == 0: outcomes.exit("Quitting debugger") return ret do_q = do_quit do_exit = do_quit def setup(self, f, tb): """Suspend on setup(). Needed after do_continue resumed, and entering another breakpoint again. """ ret = super(PytestPdbWrapper, self).setup(f, tb) if not ret and self._continued: # pdb.setup() returns True if the command wants to exit # from the interaction: do not suspend capturing then. if self._pytest_capman: self._pytest_capman.suspend_global_capture(in_=True) return ret def get_stack(self, f, t): stack, i = super(PytestPdbWrapper, self).get_stack(f, t) if f is None: # Find last non-hidden frame. i = max(0, len(stack) - 1) while i and stack[i][0].f_locals.get("__tracebackhide__", False): i -= 1 return stack, i return PytestPdbWrapper @classmethod def _init_pdb(cls, method, *args, **kwargs): """ Initialize PDB debugging, dropping any IO capturing. """ import _pytest.config if cls._pluginmanager is not None: capman = cls._pluginmanager.getplugin("capturemanager") else: capman = None if capman: capman.suspend(in_=True) if cls._config: tw = _pytest.config.create_terminal_writer(cls._config) tw.line() if cls._recursive_debug == 0: # Handle header similar to pdb.set_trace in py37+. header = kwargs.pop("header", None) if header is not None: tw.sep(">", header) else: capturing = cls._is_capturing(capman) if capturing == "global": tw.sep(">", "PDB %s (IO-capturing turned off)" % (method,)) elif capturing: tw.sep( ">", "PDB %s (IO-capturing turned off for %s)" % (method, capturing), ) else: tw.sep(">", "PDB %s" % (method,)) _pdb = cls._import_pdb_cls(capman)(**kwargs) if cls._pluginmanager: cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb) return _pdb @classmethod def set_trace(cls, *args, **kwargs): """Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing.""" frame = sys._getframe().f_back _pdb = cls._init_pdb("set_trace", *args, **kwargs) _pdb.set_trace(frame) class PdbInvoke(object): def pytest_exception_interact(self, node, call, report): capman = node.config.pluginmanager.getplugin("capturemanager") if capman: capman.suspend_global_capture(in_=True) out, err = capman.read_global_capture() sys.stdout.write(out) sys.stdout.write(err) _enter_pdb(node, call.excinfo, report) def pytest_internalerror(self, excrepr, excinfo): tb = _postmortem_traceback(excinfo) post_mortem(tb) class PdbTrace(object): @hookimpl(hookwrapper=True) def pytest_pyfunc_call(self, pyfuncitem): _test_pytest_function(pyfuncitem) yield def _test_pytest_function(pyfuncitem): _pdb = pytestPDB._init_pdb("runcall") testfunction = pyfuncitem.obj pyfuncitem.obj = _pdb.runcall if "func" in pyfuncitem._fixtureinfo.argnames: # pragma: no branch raise ValueError("--trace can't be used with a fixture named func!") pyfuncitem.funcargs["func"] = testfunction new_list = list(pyfuncitem._fixtureinfo.argnames) new_list.append("func") pyfuncitem._fixtureinfo.argnames = tuple(new_list) def _enter_pdb(node, excinfo, rep): # XXX we re-use the TerminalReporter's terminalwriter # because this seems to avoid some encoding related troubles # for not completely clear reasons. tw = node.config.pluginmanager.getplugin("terminalreporter")._tw tw.line() showcapture = node.config.option.showcapture for sectionname, content in ( ("stdout", rep.capstdout), ("stderr", rep.capstderr), ("log", rep.caplog), ): if showcapture in (sectionname, "all") and content: tw.sep(">", "captured " + sectionname) if content[-1:] == "\n": content = content[:-1] tw.line(content) tw.sep(">", "traceback") rep.toterminal(tw) tw.sep(">", "entering PDB") tb = _postmortem_traceback(excinfo) rep._pdbshown = True post_mortem(tb) return rep def _postmortem_traceback(excinfo): if isinstance(excinfo.value, UnexpectedException): # A doctest.UnexpectedException is not useful for post_mortem. # Use the underlying exception instead: return excinfo.value.exc_info[2] else: return excinfo._excinfo[2] def post_mortem(t): p = pytestPDB._init_pdb("post_mortem") p.reset() p.interaction(None, t) if p.quitting: outcomes.exit("Quitting debugger") pytest-4.6.9/src/_pytest/_argcomplete.py0000664000372000037200000000724413604173400021222 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """allow bash-completion for argparse with argcomplete if installed needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail to find the magic string, so _ARGCOMPLETE env. var is never set, and this does not need special code. Function try_argcomplete(parser) should be called directly before the call to ArgumentParser.parse_args(). The filescompleter is what you normally would use on the positional arguments specification, in order to get "dirname/" after "dirn" instead of the default "dirname ": optparser.add_argument(Config._file_or_dir, nargs='*' ).completer=filescompleter Other, application specific, completers should go in the file doing the add_argument calls as they need to be specified as .completer attributes as well. (If argcomplete is not installed, the function the attribute points to will not be used). SPEEDUP ======= The generic argcomplete script for bash-completion (/etc/bash_completion.d/python-argcomplete.sh ) uses a python program to determine startup script generated by pip. You can speed up completion somewhat by changing this script to include # PYTHON_ARGCOMPLETE_OK so the the python-argcomplete-check-easy-install-script does not need to be called to find the entry point of the code and see if that is marked with PYTHON_ARGCOMPLETE_OK INSTALL/DEBUGGING ================= To include this support in another application that has setup.py generated scripts: - add the line: # PYTHON_ARGCOMPLETE_OK near the top of the main python entry point - include in the file calling parse_args(): from _argcomplete import try_argcomplete, filescompleter , call try_argcomplete just before parse_args(), and optionally add filescompleter to the positional arguments' add_argument() If things do not work right away: - switch on argcomplete debugging with (also helpful when doing custom completers): export _ARC_DEBUG=1 - run: python-argcomplete-check-easy-install-script $(which appname) echo $? will echo 0 if the magic line has been found, 1 if not - sometimes it helps to find early on errors using: _ARGCOMPLETE=1 _ARC_DEBUG=1 appname which should throw a KeyError: 'COMPLINE' (which is properly set by the global argcomplete script). """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys from glob import glob class FastFilesCompleter(object): "Fast file completer class" def __init__(self, directories=True): self.directories = directories def __call__(self, prefix, **kwargs): """only called on non option completions""" if os.path.sep in prefix[1:]: prefix_dir = len(os.path.dirname(prefix) + os.path.sep) else: prefix_dir = 0 completion = [] globbed = [] if "*" not in prefix and "?" not in prefix: # we are on unix, otherwise no bash if not prefix or prefix[-1] == os.path.sep: globbed.extend(glob(prefix + ".*")) prefix += "*" globbed.extend(glob(prefix)) for x in sorted(globbed): if os.path.isdir(x): x += "/" # append stripping the prefix (like bash, not like compgen) completion.append(x[prefix_dir:]) return completion if os.environ.get("_ARGCOMPLETE"): try: import argcomplete.completers except ImportError: sys.exit(-1) filescompleter = FastFilesCompleter() def try_argcomplete(parser): argcomplete.autocomplete(parser, always_complete_options=False) else: def try_argcomplete(parser): pass filescompleter = None pytest-4.6.9/src/_pytest/_code/0000775000372000037200000000000013604173421017254 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/_code/code.py0000664000372000037200000010652013604173400020541 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import inspect import re import sys import traceback from inspect import CO_VARARGS from inspect import CO_VARKEYWORDS from weakref import ref import attr import pluggy import py from six import text_type import _pytest from _pytest._io.saferepr import safeformat from _pytest._io.saferepr import saferepr from _pytest.compat import _PY2 from _pytest.compat import _PY3 from _pytest.compat import PY35 from _pytest.compat import safe_str if _PY3: from traceback import format_exception_only else: from ._py2traceback import format_exception_only class Code(object): """ wrapper around Python code objects """ def __init__(self, rawcode): if not hasattr(rawcode, "co_filename"): rawcode = getrawcode(rawcode) try: self.filename = rawcode.co_filename self.firstlineno = rawcode.co_firstlineno - 1 self.name = rawcode.co_name except AttributeError: raise TypeError("not a code object: %r" % (rawcode,)) self.raw = rawcode def __eq__(self, other): return self.raw == other.raw __hash__ = None def __ne__(self, other): return not self == other @property def path(self): """ return a path object pointing to source code (note that it might not point to an actually existing file). """ try: p = py.path.local(self.raw.co_filename) # maybe don't try this checking if not p.check(): raise OSError("py.path check failed.") except OSError: # XXX maybe try harder like the weird logic # in the standard lib [linecache.updatecache] does? p = self.raw.co_filename return p @property def fullsource(self): """ return a _pytest._code.Source object for the full source file of the code """ from _pytest._code import source full, _ = source.findsource(self.raw) return full def source(self): """ return a _pytest._code.Source object for the code object's source only """ # return source only for that part of code import _pytest._code return _pytest._code.Source(self.raw) def getargs(self, var=False): """ return a tuple with the argument names for the code object if 'var' is set True also return the names of the variable and keyword arguments when present """ # handfull shortcut for getting args raw = self.raw argcount = raw.co_argcount if var: argcount += raw.co_flags & CO_VARARGS argcount += raw.co_flags & CO_VARKEYWORDS return raw.co_varnames[:argcount] class Frame(object): """Wrapper around a Python frame holding f_locals and f_globals in which expressions can be evaluated.""" def __init__(self, frame): self.lineno = frame.f_lineno - 1 self.f_globals = frame.f_globals self.f_locals = frame.f_locals self.raw = frame self.code = Code(frame.f_code) @property def statement(self): """ statement this frame is at """ import _pytest._code if self.code.fullsource is None: return _pytest._code.Source("") return self.code.fullsource.getstatement(self.lineno) def eval(self, code, **vars): """ evaluate 'code' in the frame 'vars' are optional additional local variables returns the result of the evaluation """ f_locals = self.f_locals.copy() f_locals.update(vars) return eval(code, self.f_globals, f_locals) def exec_(self, code, **vars): """ exec 'code' in the frame 'vars' are optiona; additional local variables """ f_locals = self.f_locals.copy() f_locals.update(vars) exec(code, self.f_globals, f_locals) def repr(self, object): """ return a 'safe' (non-recursive, one-line) string repr for 'object' """ return saferepr(object) def is_true(self, object): return object def getargs(self, var=False): """ return a list of tuples (name, value) for all arguments if 'var' is set True also include the variable and keyword arguments when present """ retval = [] for arg in self.code.getargs(var): try: retval.append((arg, self.f_locals[arg])) except KeyError: pass # this can occur when using Psyco return retval class TracebackEntry(object): """ a single entry in a traceback """ _repr_style = None exprinfo = None def __init__(self, rawentry, excinfo=None): self._excinfo = excinfo self._rawentry = rawentry self.lineno = rawentry.tb_lineno - 1 def set_repr_style(self, mode): assert mode in ("short", "long") self._repr_style = mode @property def frame(self): import _pytest._code return _pytest._code.Frame(self._rawentry.tb_frame) @property def relline(self): return self.lineno - self.frame.code.firstlineno def __repr__(self): return "" % (self.frame.code.path, self.lineno + 1) @property def statement(self): """ _pytest._code.Source object for the current statement """ source = self.frame.code.fullsource return source.getstatement(self.lineno) @property def path(self): """ path to the source code """ return self.frame.code.path def getlocals(self): return self.frame.f_locals locals = property(getlocals, None, None, "locals of underlaying frame") def getfirstlinesource(self): # on Jython this firstlineno can be -1 apparently return max(self.frame.code.firstlineno, 0) def getsource(self, astcache=None): """ return failing source code. """ # we use the passed in astcache to not reparse asttrees # within exception info printing from _pytest._code.source import getstatementrange_ast source = self.frame.code.fullsource if source is None: return None key = astnode = None if astcache is not None: key = self.frame.code.path if key is not None: astnode = astcache.get(key, None) start = self.getfirstlinesource() try: astnode, _, end = getstatementrange_ast( self.lineno, source, astnode=astnode ) except SyntaxError: end = self.lineno + 1 else: if key is not None: astcache[key] = astnode return source[start:end] source = property(getsource) def ishidden(self): """ return True if the current frame has a var __tracebackhide__ resolving to True. If __tracebackhide__ is a callable, it gets called with the ExceptionInfo instance and can decide whether to hide the traceback. mostly for internal use """ f = self.frame tbh = f.f_locals.get( "__tracebackhide__", f.f_globals.get("__tracebackhide__", False) ) if tbh and callable(tbh): return tbh(None if self._excinfo is None else self._excinfo()) return tbh def __str__(self): try: fn = str(self.path) except py.error.Error: fn = "???" name = self.frame.code.name try: line = str(self.statement).lstrip() except KeyboardInterrupt: raise except: # noqa line = "???" return " File %r:%d in %s\n %s\n" % (fn, self.lineno + 1, name, line) def name(self): return self.frame.code.raw.co_name name = property(name, None, None, "co_name of underlaying code") class Traceback(list): """ Traceback objects encapsulate and offer higher level access to Traceback entries. """ Entry = TracebackEntry def __init__(self, tb, excinfo=None): """ initialize from given python traceback object and ExceptionInfo """ self._excinfo = excinfo if hasattr(tb, "tb_next"): def f(cur): while cur is not None: yield self.Entry(cur, excinfo=excinfo) cur = cur.tb_next list.__init__(self, f(tb)) else: list.__init__(self, tb) def cut(self, path=None, lineno=None, firstlineno=None, excludepath=None): """ return a Traceback instance wrapping part of this Traceback by provding any combination of path, lineno and firstlineno, the first frame to start the to-be-returned traceback is determined this allows cutting the first part of a Traceback instance e.g. for formatting reasons (removing some uninteresting bits that deal with handling of the exception/traceback) """ for x in self: code = x.frame.code codepath = code.path if ( (path is None or codepath == path) and ( excludepath is None or not hasattr(codepath, "relto") or not codepath.relto(excludepath) ) and (lineno is None or x.lineno == lineno) and (firstlineno is None or x.frame.code.firstlineno == firstlineno) ): return Traceback(x._rawentry, self._excinfo) return self def __getitem__(self, key): val = super(Traceback, self).__getitem__(key) if isinstance(key, type(slice(0))): val = self.__class__(val) return val def filter(self, fn=lambda x: not x.ishidden()): """ return a Traceback instance with certain items removed fn is a function that gets a single argument, a TracebackEntry instance, and should return True when the item should be added to the Traceback, False when not by default this removes all the TracebackEntries which are hidden (see ishidden() above) """ return Traceback(filter(fn, self), self._excinfo) def getcrashentry(self): """ return last non-hidden traceback entry that lead to the exception of a traceback. """ for i in range(-1, -len(self) - 1, -1): entry = self[i] if not entry.ishidden(): return entry return self[-1] def recursionindex(self): """ return the index of the frame/TracebackEntry where recursion originates if appropriate, None if no recursion occurred """ cache = {} for i, entry in enumerate(self): # id for the code.raw is needed to work around # the strange metaprogramming in the decorator lib from pypi # which generates code objects that have hash/value equality # XXX needs a test key = entry.frame.code.path, id(entry.frame.code.raw), entry.lineno # print "checking for recursion at", key values = cache.setdefault(key, []) if values: f = entry.frame loc = f.f_locals for otherloc in values: if f.is_true( f.eval( co_equal, __recursioncache_locals_1=loc, __recursioncache_locals_2=otherloc, ) ): return i values.append(entry.frame.f_locals) return None co_equal = compile( "__recursioncache_locals_1 == __recursioncache_locals_2", "?", "eval" ) @attr.s(repr=False) class ExceptionInfo(object): """ wraps sys.exc_info() objects and offers help for navigating the traceback. """ _assert_start_repr = ( "AssertionError(u'assert " if _PY2 else "AssertionError('assert " ) _excinfo = attr.ib() _striptext = attr.ib(default="") _traceback = attr.ib(default=None) @classmethod def from_current(cls, exprinfo=None): """returns an ExceptionInfo matching the current traceback .. warning:: Experimental API :param exprinfo: a text string helping to determine if we should strip ``AssertionError`` from the output, defaults to the exception message/``__str__()`` """ tup = sys.exc_info() assert tup[0] is not None, "no current exception" _striptext = "" if exprinfo is None and isinstance(tup[1], AssertionError): exprinfo = getattr(tup[1], "msg", None) if exprinfo is None: exprinfo = saferepr(tup[1]) if exprinfo and exprinfo.startswith(cls._assert_start_repr): _striptext = "AssertionError: " return cls(tup, _striptext) @classmethod def for_later(cls): """return an unfilled ExceptionInfo """ return cls(None) @property def type(self): """the exception class""" return self._excinfo[0] @property def value(self): """the exception value""" return self._excinfo[1] @property def tb(self): """the exception raw traceback""" return self._excinfo[2] @property def typename(self): """the type name of the exception""" return self.type.__name__ @property def traceback(self): """the traceback""" if self._traceback is None: self._traceback = Traceback(self.tb, excinfo=ref(self)) return self._traceback @traceback.setter def traceback(self, value): self._traceback = value def __repr__(self): if self._excinfo is None: return "" return "" % (self.typename, len(self.traceback)) def exconly(self, tryshort=False): """ return the exception as a string when 'tryshort' resolves to True, and the exception is a _pytest._code._AssertionError, only the actual exception part of the exception representation is returned (so 'AssertionError: ' is removed from the beginning) """ lines = format_exception_only(self.type, self.value) text = "".join(lines) text = text.rstrip() if tryshort: if text.startswith(self._striptext): text = text[len(self._striptext) :] return text def errisinstance(self, exc): """ return True if the exception is an instance of exc """ return isinstance(self.value, exc) def _getreprcrash(self): exconly = self.exconly(tryshort=True) entry = self.traceback.getcrashentry() path, lineno = entry.frame.code.raw.co_filename, entry.lineno return ReprFileLocation(path, lineno + 1, exconly) def getrepr( self, showlocals=False, style="long", abspath=False, tbfilter=True, funcargs=False, truncate_locals=True, chain=True, ): """ Return str()able representation of this exception info. :param bool showlocals: Show locals per traceback entry. Ignored if ``style=="native"``. :param str style: long|short|no|native traceback style :param bool abspath: If paths should be changed to absolute or left unchanged. :param bool tbfilter: Hide entries that contain a local variable ``__tracebackhide__==True``. Ignored if ``style=="native"``. :param bool funcargs: Show fixtures ("funcargs" for legacy purposes) per traceback entry. :param bool truncate_locals: With ``showlocals==True``, make sure locals can be safely represented as strings. :param bool chain: if chained exceptions in Python 3 should be shown. .. versionchanged:: 3.9 Added the ``chain`` parameter. """ if style == "native": return ReprExceptionInfo( ReprTracebackNative( traceback.format_exception( self.type, self.value, self.traceback[0]._rawentry ) ), self._getreprcrash(), ) fmt = FormattedExcinfo( showlocals=showlocals, style=style, abspath=abspath, tbfilter=tbfilter, funcargs=funcargs, truncate_locals=truncate_locals, chain=chain, ) return fmt.repr_excinfo(self) def __str__(self): if self._excinfo is None: return repr(self) entry = self.traceback[-1] loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly()) return str(loc) def __unicode__(self): entry = self.traceback[-1] loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly()) return text_type(loc) def match(self, regexp): """ Check whether the regular expression 'regexp' is found in the string representation of the exception using ``re.search``. If it matches then True is returned (so that it is possible to write ``assert excinfo.match()``). If it doesn't match an AssertionError is raised. """ __tracebackhide__ = True value = ( text_type(self.value) if isinstance(regexp, text_type) else str(self.value) ) if not re.search(regexp, value): raise AssertionError( u"Pattern {!r} not found in {!r}".format(regexp, value) ) return True @attr.s class FormattedExcinfo(object): """ presenting information about failing Functions and Generators. """ # for traceback entries flow_marker = ">" fail_marker = "E" showlocals = attr.ib(default=False) style = attr.ib(default="long") abspath = attr.ib(default=True) tbfilter = attr.ib(default=True) funcargs = attr.ib(default=False) truncate_locals = attr.ib(default=True) chain = attr.ib(default=True) astcache = attr.ib(default=attr.Factory(dict), init=False, repr=False) def _getindent(self, source): # figure out indent for given source try: s = str(source.getstatement(len(source) - 1)) except KeyboardInterrupt: raise except: # noqa try: s = str(source[-1]) except KeyboardInterrupt: raise except: # noqa return 0 return 4 + (len(s) - len(s.lstrip())) def _getentrysource(self, entry): source = entry.getsource(self.astcache) if source is not None: source = source.deindent() return source def repr_args(self, entry): if self.funcargs: args = [] for argname, argvalue in entry.frame.getargs(var=True): args.append((argname, saferepr(argvalue))) return ReprFuncArgs(args) def get_source(self, source, line_index=-1, excinfo=None, short=False): """ return formatted and marked up source lines. """ import _pytest._code lines = [] if source is None or line_index >= len(source.lines): source = _pytest._code.Source("???") line_index = 0 if line_index < 0: line_index += len(source) space_prefix = " " if short: lines.append(space_prefix + source.lines[line_index].strip()) else: for line in source.lines[:line_index]: lines.append(space_prefix + line) lines.append(self.flow_marker + " " + source.lines[line_index]) for line in source.lines[line_index + 1 :]: lines.append(space_prefix + line) if excinfo is not None: indent = 4 if short else self._getindent(source) lines.extend(self.get_exconly(excinfo, indent=indent, markall=True)) return lines def get_exconly(self, excinfo, indent=4, markall=False): lines = [] indent = " " * indent # get the real exception information out exlines = excinfo.exconly(tryshort=True).split("\n") failindent = self.fail_marker + indent[1:] for line in exlines: lines.append(failindent + line) if not markall: failindent = indent return lines def repr_locals(self, locals): if self.showlocals: lines = [] keys = [loc for loc in locals if loc[0] != "@"] keys.sort() for name in keys: value = locals[name] if name == "__builtins__": lines.append("__builtins__ = ") else: # This formatting could all be handled by the # _repr() function, which is only reprlib.Repr in # disguise, so is very configurable. if self.truncate_locals: str_repr = saferepr(value) else: str_repr = safeformat(value) # if len(str_repr) < 70 or not isinstance(value, # (list, tuple, dict)): lines.append("%-10s = %s" % (name, str_repr)) # else: # self._line("%-10s =\\" % (name,)) # # XXX # pprint.pprint(value, stream=self.excinfowriter) return ReprLocals(lines) def repr_traceback_entry(self, entry, excinfo=None): import _pytest._code source = self._getentrysource(entry) if source is None: source = _pytest._code.Source("???") line_index = 0 else: # entry.getfirstlinesource() can be -1, should be 0 on jython line_index = entry.lineno - max(entry.getfirstlinesource(), 0) lines = [] style = entry._repr_style if style is None: style = self.style if style in ("short", "long"): short = style == "short" reprargs = self.repr_args(entry) if not short else None s = self.get_source(source, line_index, excinfo, short=short) lines.extend(s) if short: message = "in %s" % (entry.name) else: message = excinfo and excinfo.typename or "" path = self._makepath(entry.path) filelocrepr = ReprFileLocation(path, entry.lineno + 1, message) localsrepr = None if not short: localsrepr = self.repr_locals(entry.locals) return ReprEntry(lines, reprargs, localsrepr, filelocrepr, style) if excinfo: lines.extend(self.get_exconly(excinfo, indent=4)) return ReprEntry(lines, None, None, None, style) def _makepath(self, path): if not self.abspath: try: np = py.path.local().bestrelpath(path) except OSError: return path if len(np) < len(str(path)): path = np return path def repr_traceback(self, excinfo): traceback = excinfo.traceback if self.tbfilter: traceback = traceback.filter() if is_recursion_error(excinfo): traceback, extraline = self._truncate_recursive_traceback(traceback) else: extraline = None last = traceback[-1] entries = [] for index, entry in enumerate(traceback): einfo = (last == entry) and excinfo or None reprentry = self.repr_traceback_entry(entry, einfo) entries.append(reprentry) return ReprTraceback(entries, extraline, style=self.style) def _truncate_recursive_traceback(self, traceback): """ Truncate the given recursive traceback trying to find the starting point of the recursion. The detection is done by going through each traceback entry and finding the point in which the locals of the frame are equal to the locals of a previous frame (see ``recursionindex()``. Handle the situation where the recursion process might raise an exception (for example comparing numpy arrays using equality raises a TypeError), in which case we do our best to warn the user of the error and show a limited traceback. """ try: recursionindex = traceback.recursionindex() except Exception as e: max_frames = 10 extraline = ( "!!! Recursion error detected, but an error occurred locating the origin of recursion.\n" " The following exception happened when comparing locals in the stack frame:\n" " {exc_type}: {exc_msg}\n" " Displaying first and last {max_frames} stack frames out of {total}." ).format( exc_type=type(e).__name__, exc_msg=safe_str(e), max_frames=max_frames, total=len(traceback), ) traceback = traceback[:max_frames] + traceback[-max_frames:] else: if recursionindex is not None: extraline = "!!! Recursion detected (same locals & position)" traceback = traceback[: recursionindex + 1] else: extraline = None return traceback, extraline def repr_excinfo(self, excinfo): if _PY2: reprtraceback = self.repr_traceback(excinfo) reprcrash = excinfo._getreprcrash() return ReprExceptionInfo(reprtraceback, reprcrash) else: repr_chain = [] e = excinfo.value descr = None seen = set() while e is not None and id(e) not in seen: seen.add(id(e)) if excinfo: reprtraceback = self.repr_traceback(excinfo) reprcrash = excinfo._getreprcrash() else: # fallback to native repr if the exception doesn't have a traceback: # ExceptionInfo objects require a full traceback to work reprtraceback = ReprTracebackNative( traceback.format_exception(type(e), e, None) ) reprcrash = None repr_chain += [(reprtraceback, reprcrash, descr)] if e.__cause__ is not None and self.chain: e = e.__cause__ excinfo = ( ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None ) descr = "The above exception was the direct cause of the following exception:" elif ( e.__context__ is not None and not e.__suppress_context__ and self.chain ): e = e.__context__ excinfo = ( ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None ) descr = "During handling of the above exception, another exception occurred:" else: e = None repr_chain.reverse() return ExceptionChainRepr(repr_chain) class TerminalRepr(object): def __str__(self): s = self.__unicode__() if _PY2: s = s.encode("utf-8") return s def __unicode__(self): # FYI this is called from pytest-xdist's serialization of exception # information. io = py.io.TextIO() tw = py.io.TerminalWriter(file=io) self.toterminal(tw) return io.getvalue().strip() def __repr__(self): return "<%s instance at %0x>" % (self.__class__, id(self)) class ExceptionRepr(TerminalRepr): def __init__(self): self.sections = [] def addsection(self, name, content, sep="-"): self.sections.append((name, content, sep)) def toterminal(self, tw): for name, content, sep in self.sections: tw.sep(sep, name) tw.line(content) class ExceptionChainRepr(ExceptionRepr): def __init__(self, chain): super(ExceptionChainRepr, self).__init__() self.chain = chain # reprcrash and reprtraceback of the outermost (the newest) exception # in the chain self.reprtraceback = chain[-1][0] self.reprcrash = chain[-1][1] def toterminal(self, tw): for element in self.chain: element[0].toterminal(tw) if element[2] is not None: tw.line("") tw.line(element[2], yellow=True) super(ExceptionChainRepr, self).toterminal(tw) class ReprExceptionInfo(ExceptionRepr): def __init__(self, reprtraceback, reprcrash): super(ReprExceptionInfo, self).__init__() self.reprtraceback = reprtraceback self.reprcrash = reprcrash def toterminal(self, tw): self.reprtraceback.toterminal(tw) super(ReprExceptionInfo, self).toterminal(tw) class ReprTraceback(TerminalRepr): entrysep = "_ " def __init__(self, reprentries, extraline, style): self.reprentries = reprentries self.extraline = extraline self.style = style def toterminal(self, tw): # the entries might have different styles for i, entry in enumerate(self.reprentries): if entry.style == "long": tw.line("") entry.toterminal(tw) if i < len(self.reprentries) - 1: next_entry = self.reprentries[i + 1] if ( entry.style == "long" or entry.style == "short" and next_entry.style == "long" ): tw.sep(self.entrysep) if self.extraline: tw.line(self.extraline) class ReprTracebackNative(ReprTraceback): def __init__(self, tblines): self.style = "native" self.reprentries = [ReprEntryNative(tblines)] self.extraline = None class ReprEntryNative(TerminalRepr): style = "native" def __init__(self, tblines): self.lines = tblines def toterminal(self, tw): tw.write("".join(self.lines)) class ReprEntry(TerminalRepr): def __init__(self, lines, reprfuncargs, reprlocals, filelocrepr, style): self.lines = lines self.reprfuncargs = reprfuncargs self.reprlocals = reprlocals self.reprfileloc = filelocrepr self.style = style def toterminal(self, tw): if self.style == "short": self.reprfileloc.toterminal(tw) for line in self.lines: red = line.startswith("E ") tw.line(line, bold=True, red=red) # tw.line("") return if self.reprfuncargs: self.reprfuncargs.toterminal(tw) for line in self.lines: red = line.startswith("E ") tw.line(line, bold=True, red=red) if self.reprlocals: tw.line("") self.reprlocals.toterminal(tw) if self.reprfileloc: if self.lines: tw.line("") self.reprfileloc.toterminal(tw) def __str__(self): return "%s\n%s\n%s" % ("\n".join(self.lines), self.reprlocals, self.reprfileloc) class ReprFileLocation(TerminalRepr): def __init__(self, path, lineno, message): self.path = str(path) self.lineno = lineno self.message = message def toterminal(self, tw): # filename and lineno output for each entry, # using an output format that most editors unterstand msg = self.message i = msg.find("\n") if i != -1: msg = msg[:i] tw.write(self.path, bold=True, red=True) tw.line(":%s: %s" % (self.lineno, msg)) class ReprLocals(TerminalRepr): def __init__(self, lines): self.lines = lines def toterminal(self, tw): for line in self.lines: tw.line(line) class ReprFuncArgs(TerminalRepr): def __init__(self, args): self.args = args def toterminal(self, tw): if self.args: linesofar = "" for name, value in self.args: ns = "%s = %s" % (safe_str(name), safe_str(value)) if len(ns) + len(linesofar) + 2 > tw.fullwidth: if linesofar: tw.line(linesofar) linesofar = ns else: if linesofar: linesofar += ", " + ns else: linesofar = ns if linesofar: tw.line(linesofar) tw.line("") def getrawcode(obj, trycall=True): """ return code object for given function. """ try: return obj.__code__ except AttributeError: obj = getattr(obj, "im_func", obj) obj = getattr(obj, "func_code", obj) obj = getattr(obj, "f_code", obj) obj = getattr(obj, "__code__", obj) if trycall and not hasattr(obj, "co_firstlineno"): if hasattr(obj, "__call__") and not inspect.isclass(obj): x = getrawcode(obj.__call__, trycall=False) if hasattr(x, "co_firstlineno"): return x return obj if PY35: # RecursionError introduced in 3.5 def is_recursion_error(excinfo): return excinfo.errisinstance(RecursionError) # noqa else: def is_recursion_error(excinfo): if not excinfo.errisinstance(RuntimeError): return False try: return "maximum recursion depth exceeded" in str(excinfo.value) except UnicodeError: return False # relative paths that we use to filter traceback entries from appearing to the user; # see filter_traceback # note: if we need to add more paths than what we have now we should probably use a list # for better maintenance _PLUGGY_DIR = py.path.local(pluggy.__file__.rstrip("oc")) # pluggy is either a package or a single module depending on the version if _PLUGGY_DIR.basename == "__init__.py": _PLUGGY_DIR = _PLUGGY_DIR.dirpath() _PYTEST_DIR = py.path.local(_pytest.__file__).dirpath() _PY_DIR = py.path.local(py.__file__).dirpath() 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 a non-existing file, in which case it will # also return a str object. see #1133 p = py.path.local(entry.path) return ( not p.relto(_PLUGGY_DIR) and not p.relto(_PYTEST_DIR) and not p.relto(_PY_DIR) ) pytest-4.6.9/src/_pytest/_code/source.py0000664000372000037200000002431613604173400021131 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import ast import inspect import linecache import sys import textwrap import tokenize import warnings from ast import PyCF_ONLY_AST as _AST_FLAG from bisect import bisect_right import py import six class Source(object): """ an immutable object holding a source code fragment, possibly deindenting it. """ _compilecounter = 0 def __init__(self, *parts, **kwargs): self.lines = lines = [] de = kwargs.get("deindent", True) for part in parts: if not part: partlines = [] elif isinstance(part, Source): partlines = part.lines elif isinstance(part, (tuple, list)): partlines = [x.rstrip("\n") for x in part] elif isinstance(part, six.string_types): partlines = part.split("\n") else: partlines = getsource(part, deindent=de).lines if de: partlines = deindent(partlines) lines.extend(partlines) def __eq__(self, other): try: return self.lines == other.lines except AttributeError: if isinstance(other, str): return str(self) == other return False __hash__ = None def __getitem__(self, key): if isinstance(key, int): return self.lines[key] else: if key.step not in (None, 1): raise IndexError("cannot slice a Source with a step") newsource = Source() newsource.lines = self.lines[key.start : key.stop] return newsource def __len__(self): return len(self.lines) def strip(self): """ return new source object with trailing and leading blank lines removed. """ start, end = 0, len(self) while start < end and not self.lines[start].strip(): start += 1 while end > start and not self.lines[end - 1].strip(): end -= 1 source = Source() source.lines[:] = self.lines[start:end] return source def putaround(self, before="", after="", indent=" " * 4): """ return a copy of the source object with 'before' and 'after' wrapped around it. """ before = Source(before) after = Source(after) newsource = Source() lines = [(indent + line) for line in self.lines] newsource.lines = before.lines + lines + after.lines return newsource def indent(self, indent=" " * 4): """ return a copy of the source object with all lines indented by the given indent-string. """ newsource = Source() newsource.lines = [(indent + line) for line in self.lines] return newsource def getstatement(self, lineno): """ return Source statement which contains the given linenumber (counted from 0). """ start, end = self.getstatementrange(lineno) return self[start:end] def getstatementrange(self, lineno): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ if not (0 <= lineno < len(self)): raise IndexError("lineno out of range") ast, start, end = getstatementrange_ast(lineno, self) return start, end def deindent(self): """return a new source object deindented.""" newsource = Source() newsource.lines[:] = deindent(self.lines) return newsource def isparseable(self, deindent=True): """ return True if source is parseable, heuristically deindenting it by default. """ from parser import suite as syntax_checker if deindent: source = str(self.deindent()) else: source = str(self) try: # compile(source+'\n', "x", "exec") syntax_checker(source + "\n") except KeyboardInterrupt: raise except Exception: return False else: return True def __str__(self): return "\n".join(self.lines) def compile( self, filename=None, mode="exec", flag=0, dont_inherit=0, _genframe=None ): """ return compiled code object. if filename is None invent an artificial filename which displays the source/line position of the caller frame. """ if not filename or py.path.local(filename).check(file=0): if _genframe is None: _genframe = sys._getframe(1) # the caller fn, lineno = _genframe.f_code.co_filename, _genframe.f_lineno base = "<%d-codegen " % self._compilecounter self.__class__._compilecounter += 1 if not filename: filename = base + "%s:%d>" % (fn, lineno) else: filename = base + "%r %s:%d>" % (filename, fn, lineno) source = "\n".join(self.lines) + "\n" try: co = compile(source, filename, mode, flag) except SyntaxError: ex = sys.exc_info()[1] # re-represent syntax errors from parsing python strings msglines = self.lines[: ex.lineno] if ex.offset: msglines.append(" " * ex.offset + "^") msglines.append("(code was compiled probably from here: %s)" % filename) newex = SyntaxError("\n".join(msglines)) newex.offset = ex.offset newex.lineno = ex.lineno newex.text = ex.text raise newex else: if flag & _AST_FLAG: return co lines = [(x + "\n") for x in self.lines] linecache.cache[filename] = (1, None, lines, filename) return co # # public API shortcut functions # def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0): """ compile the given source to a raw code object, and maintain an internal cache which allows later retrieval of the source code for the code object and any recursively created code objects. """ if isinstance(source, ast.AST): # XXX should Source support having AST? return compile(source, filename, mode, flags, dont_inherit) _genframe = sys._getframe(1) # the caller s = Source(source) co = s.compile(filename, mode, flags, _genframe=_genframe) return co def getfslineno(obj): """ Return source location (path, lineno) for the given object. If the source cannot be determined return ("", -1). The line number is 0-based. """ from .code import Code try: code = Code(obj) except TypeError: try: fn = inspect.getsourcefile(obj) or inspect.getfile(obj) except TypeError: return "", -1 fspath = fn and py.path.local(fn) or None lineno = -1 if fspath: try: _, lineno = findsource(obj) except IOError: pass else: fspath = code.path lineno = code.firstlineno assert isinstance(lineno, int) return fspath, lineno # # helper functions # def findsource(obj): try: sourcelines, lineno = inspect.findsource(obj) except Exception: return None, -1 source = Source() source.lines = [line.rstrip() for line in sourcelines] return source, lineno def getsource(obj, **kwargs): from .code import getrawcode obj = getrawcode(obj) try: strsrc = inspect.getsource(obj) except IndentationError: strsrc = '"Buggy python version consider upgrading, cannot get source"' assert isinstance(strsrc, str) return Source(strsrc, **kwargs) def deindent(lines): return textwrap.dedent("\n".join(lines)).splitlines() def get_statement_startend2(lineno, node): import ast # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1 values = [] for x in ast.walk(node): if isinstance(x, (ast.stmt, ast.ExceptHandler)): values.append(x.lineno - 1) for name in ("finalbody", "orelse"): val = getattr(x, name, None) if val: # treat the finally/orelse part as its own statement values.append(val[0].lineno - 1 - 1) values.sort() insert_index = bisect_right(values, lineno) start = values[insert_index - 1] if insert_index >= len(values): end = None else: end = values[insert_index] return start, end def getstatementrange_ast(lineno, source, assertion=False, astnode=None): if astnode is None: content = str(source) # See #4260: # don't produce duplicate warnings when compiling source to find ast with warnings.catch_warnings(): warnings.simplefilter("ignore") astnode = compile(content, "source", "exec", _AST_FLAG) start, end = get_statement_startend2(lineno, astnode) # we need to correct the end: # - ast-parsing strips comments # - there might be empty lines # - we might have lesser indented code blocks at the end if end is None: end = len(source.lines) if end > start + 1: # make sure we don't span differently indented code blocks # by using the BlockFinder helper used which inspect.getsource() uses itself block_finder = inspect.BlockFinder() # if we start with an indented line, put blockfinder to "started" mode block_finder.started = source.lines[start][0].isspace() it = ((x + "\n") for x in source.lines[start:end]) try: for tok in tokenize.generate_tokens(lambda: next(it)): block_finder.tokeneater(*tok) except (inspect.EndOfBlock, IndentationError): end = block_finder.last + start except Exception: pass # the end might still point to a comment or empty line, correct it while end: line = source.lines[end - 1].lstrip() if line.startswith("#") or not line: end -= 1 else: break return astnode, start, end pytest-4.6.9/src/_pytest/_code/_py2traceback.py0000664000372000037200000000614713604173400022344 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # copied from python-2.7.3's traceback.py # CHANGES: # - some_str is replaced, trying to create unicode strings # from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import types from six import text_type def format_exception_only(etype, value): """Format the exception part of a traceback. The arguments are the exception type and value such as given by sys.last_type and sys.last_value. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the list. """ # An instance should not have a meaningful value parameter, but # sometimes does, particularly for string exceptions, such as # >>> raise string1, string2 # deprecated # # Clear these out first because issubtype(string1, SyntaxError) # would throw another exception and mask the original problem. if ( isinstance(etype, BaseException) or isinstance(etype, types.InstanceType) or etype is None or type(etype) is str ): return [_format_final_exc_line(etype, value)] stype = etype.__name__ if not issubclass(etype, SyntaxError): return [_format_final_exc_line(stype, value)] # It was a syntax error; show exactly where the problem was found. lines = [] try: msg, (filename, lineno, offset, badline) = value.args except Exception: pass else: filename = filename or "" lines.append(' File "{}", line {}\n'.format(filename, lineno)) if badline is not None: if isinstance(badline, bytes): # python 2 only badline = badline.decode("utf-8", "replace") lines.append(" {}\n".format(badline.strip())) if offset is not None: caretspace = badline.rstrip("\n")[:offset].lstrip() # non-space whitespace (likes tabs) must be kept for alignment caretspace = ((c.isspace() and c or " ") for c in caretspace) # only three spaces to account for offset1 == pos 0 lines.append(" {}^\n".format("".join(caretspace))) value = msg lines.append(_format_final_exc_line(stype, value)) return lines def _format_final_exc_line(etype, value): """Return a list of a single line -- normal case for format_exception_only""" valuestr = _some_str(value) if value is None or not valuestr: line = "{}\n".format(etype) else: line = "{}: {}\n".format(etype, valuestr) return line def _some_str(value): try: return text_type(value) except Exception: try: return bytes(value).decode("UTF-8", "replace") except Exception: pass return "".format(type(value).__name__) pytest-4.6.9/src/_pytest/_code/__init__.py0000664000372000037200000000101213604173400021354 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ python inspection/code generation API """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from .code import Code # noqa from .code import ExceptionInfo # noqa from .code import filter_traceback # noqa from .code import Frame # noqa from .code import getrawcode # noqa from .code import Traceback # noqa from .source import compile_ as compile # noqa from .source import getfslineno # noqa from .source import Source # noqa pytest-4.6.9/src/_pytest/warning_types.py0000664000372000037200000000543713604173400021454 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import attr class PytestWarning(UserWarning): """ Bases: :class:`UserWarning`. Base class for all warnings emitted by pytest. """ class PytestAssertRewriteWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted by the pytest assert rewrite module. """ class PytestCacheWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted by the cache plugin in various situations. """ class PytestConfigWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted for configuration issues. """ class PytestCollectionWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted when pytest is not able to collect a file or symbol in a module. """ class PytestDeprecationWarning(PytestWarning, DeprecationWarning): """ Bases: :class:`pytest.PytestWarning`, :class:`DeprecationWarning`. Warning class for features that will be removed in a future version. """ class PytestExperimentalApiWarning(PytestWarning, FutureWarning): """ Bases: :class:`pytest.PytestWarning`, :class:`FutureWarning`. Warning category used to denote experiments in pytest. Use sparingly as the API might change or even be removed completely in future version """ @classmethod def simple(cls, apiname): return cls( "{apiname} is an experimental api that may change over time".format( apiname=apiname ) ) class PytestUnhandledCoroutineWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted when pytest encounters a test function which is a coroutine, but it was not handled by any async-aware plugin. Coroutine test functions are not natively supported. """ class PytestUnknownMarkWarning(PytestWarning): """ Bases: :class:`PytestWarning`. Warning emitted on use of unknown markers. See https://docs.pytest.org/en/latest/mark.html for details. """ class RemovedInPytest4Warning(PytestDeprecationWarning): """ Bases: :class:`pytest.PytestDeprecationWarning`. Warning class for features scheduled to be removed in pytest 4.0. """ @attr.s class UnformattedWarning(object): """Used to hold warnings that need to format their message at runtime, as opposed to a direct message. Using this class avoids to keep all the warning types and messages in this module, avoiding misuse. """ category = attr.ib() template = attr.ib() def format(self, **kwargs): """Returns an instance of the warning category, formatted with given kwargs""" return self.category(self.template.format(**kwargs)) PYTESTER_COPY_EXAMPLE = PytestExperimentalApiWarning.simple("testdir.copy_example") pytest-4.6.9/src/_pytest/stepwise.py0000664000372000037200000000702413604173400020420 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest def pytest_addoption(parser): group = parser.getgroup("general") group.addoption( "--sw", "--stepwise", action="store_true", dest="stepwise", help="exit on test failure and continue from last failing test next time", ) group.addoption( "--stepwise-skip", action="store_true", dest="stepwise_skip", help="ignore the first failing test but stop on the next failing test", ) @pytest.hookimpl def pytest_configure(config): config.pluginmanager.register(StepwisePlugin(config), "stepwiseplugin") class StepwisePlugin: def __init__(self, config): self.config = config self.active = config.getvalue("stepwise") self.session = None self.report_status = "" if self.active: self.lastfailed = config.cache.get("cache/stepwise", None) self.skip = config.getvalue("stepwise_skip") def pytest_sessionstart(self, session): self.session = session def pytest_collection_modifyitems(self, session, config, items): if not self.active: return if not self.lastfailed: self.report_status = "no previously failed tests, not skipping." return already_passed = [] found = False # Make a list of all tests that have been run before the last failing one. for item in items: if item.nodeid == self.lastfailed: found = True break else: already_passed.append(item) # If the previously failed test was not found among the test items, # do not skip any tests. if not found: self.report_status = "previously failed test not found, not skipping." already_passed = [] else: self.report_status = "skipping {} already passed items.".format( len(already_passed) ) for item in already_passed: items.remove(item) config.hook.pytest_deselected(items=already_passed) def pytest_runtest_logreport(self, report): if not self.active: return if report.failed: if self.skip: # Remove test from the failed ones (if it exists) and unset the skip option # to make sure the following tests will not be skipped. if report.nodeid == self.lastfailed: self.lastfailed = None self.skip = False else: # Mark test as the last failing and interrupt the test session. self.lastfailed = report.nodeid self.session.shouldstop = ( "Test failed, continuing from this test next run." ) else: # If the test was actually run and did pass. if report.when == "call": # Remove test from the failed ones, if exists. if report.nodeid == self.lastfailed: self.lastfailed = None def pytest_report_collectionfinish(self): if self.active and self.config.getoption("verbose") >= 0 and self.report_status: return "stepwise: %s" % self.report_status def pytest_sessionfinish(self, session): if self.active: self.config.cache.set("cache/stepwise", self.lastfailed) else: # Clear the list of failing tests if the plugin is not active. self.config.cache.set("cache/stepwise", []) pytest-4.6.9/src/_pytest/skipping.py0000664000372000037200000001471513604173400020406 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ support for skip/xfail functions and markers. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from _pytest.config import hookimpl from _pytest.mark.evaluate import MarkEvaluator from _pytest.outcomes import fail from _pytest.outcomes import skip from _pytest.outcomes import xfail def pytest_addoption(parser): group = parser.getgroup("general") group.addoption( "--runxfail", action="store_true", dest="runxfail", default=False, help="report the results of xfail tests as if they were not marked", ) parser.addini( "xfail_strict", "default for the strict parameter of xfail " "markers when not given explicitly (default: False)", default=False, type="bool", ) def pytest_configure(config): if config.option.runxfail: # yay a hack import pytest old = pytest.xfail config._cleanup.append(lambda: setattr(pytest, "xfail", old)) def nop(*args, **kwargs): pass nop.Exception = xfail.Exception 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 " "https://docs.pytest.org/en/latest/skipping.html", ) config.addinivalue_line( "markers", "xfail(condition, reason=None, run=True, raises=None, strict=False): " "mark 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 https://docs.pytest.org/en/latest/skipping.html", ) @hookimpl(tryfirst=True) def pytest_runtest_setup(item): # Check if skip or skipif are specified as pytest marks item._skipped_by_mark = False eval_skipif = MarkEvaluator(item, "skipif") if eval_skipif.istrue(): item._skipped_by_mark = True skip(eval_skipif.getexplanation()) for skip_info in item.iter_markers(name="skip"): item._skipped_by_mark = True if "reason" in skip_info.kwargs: skip(skip_info.kwargs["reason"]) elif skip_info.args: skip(skip_info.args[0]) else: skip("unconditional skip") item._evalxfail = MarkEvaluator(item, "xfail") check_xfail_no_run(item) @hookimpl(hookwrapper=True) def pytest_pyfunc_call(pyfuncitem): check_xfail_no_run(pyfuncitem) outcome = yield passed = outcome.excinfo is None if passed: check_strict_xfail(pyfuncitem) def check_xfail_no_run(item): """check xfail(run=False)""" if not item.config.option.runxfail: evalxfail = item._evalxfail if evalxfail.istrue(): if not evalxfail.get("run", True): xfail("[NOTRUN] " + evalxfail.getexplanation()) def check_strict_xfail(pyfuncitem): """check xfail(strict=True) for the given PASSING test""" evalxfail = pyfuncitem._evalxfail if evalxfail.istrue(): strict_default = pyfuncitem.config.getini("xfail_strict") is_strict_xfail = evalxfail.get("strict", strict_default) if is_strict_xfail: del pyfuncitem._evalxfail explanation = evalxfail.getexplanation() fail("[XPASS(strict)] " + explanation, pytrace=False) @hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() evalxfail = getattr(item, "_evalxfail", None) # unitttest special case, see setting of _unexpectedsuccess if hasattr(item, "_unexpectedsuccess") and rep.when == "call": from _pytest.compat import _is_unittest_unexpected_success_a_failure if item._unexpectedsuccess: rep.longrepr = "Unexpected success: {}".format(item._unexpectedsuccess) else: rep.longrepr = "Unexpected success" if _is_unittest_unexpected_success_a_failure(): rep.outcome = "failed" else: rep.outcome = "passed" rep.wasxfail = rep.longrepr elif item.config.option.runxfail: pass # don't interefere elif call.excinfo and call.excinfo.errisinstance(xfail.Exception): rep.wasxfail = "reason: " + call.excinfo.value.msg rep.outcome = "skipped" elif evalxfail and not rep.skipped and evalxfail.wasvalid() and evalxfail.istrue(): if call.excinfo: if evalxfail.invalidraise(call.excinfo.value): rep.outcome = "failed" else: rep.outcome = "skipped" rep.wasxfail = evalxfail.getexplanation() elif call.when == "call": strict_default = item.config.getini("xfail_strict") is_strict_xfail = evalxfail.get("strict", strict_default) explanation = evalxfail.getexplanation() if is_strict_xfail: rep.outcome = "failed" rep.longrepr = "[XPASS(strict)] {}".format(explanation) else: rep.outcome = "passed" rep.wasxfail = explanation elif ( getattr(item, "_skipped_by_mark", False) and rep.skipped and type(rep.longrepr) is tuple ): # skipped by mark.skipif; change the location of the failure # to point to the item definition, otherwise it will display # the location of where the skip exception was raised within pytest filename, line, reason = rep.longrepr filename, line = item.location[:2] rep.longrepr = filename, line, reason # called by terminalreporter progress reporting def pytest_report_teststatus(report): if hasattr(report, "wasxfail"): if report.skipped: return "xfailed", "x", "XFAIL" elif report.passed: return "xpassed", "X", "XPASS" pytest-4.6.9/src/_pytest/monkeypatch.py0000664000372000037200000002731013604173400021077 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ monkeypatching and mocking functionality. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import re import sys import warnings from contextlib import contextmanager import six import pytest from _pytest.fixtures import fixture from _pytest.pathlib import Path RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$") @fixture def monkeypatch(): """The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.delattr(obj, name, raising=True) monkeypatch.setitem(mapping, name, value) monkeypatch.delitem(obj, name, raising=True) monkeypatch.setenv(name, value, prepend=False) monkeypatch.delenv(name, raising=True) monkeypatch.syspath_prepend(path) monkeypatch.chdir(path) All modifications will be undone after the requesting test function or fixture has finished. The ``raising`` parameter determines if a KeyError or AttributeError will be raised if the set/deletion operation has no target. """ mpatch = MonkeyPatch() yield mpatch mpatch.undo() def resolve(name): # simplified from zope.dottedname parts = name.split(".") used = parts.pop(0) found = __import__(used) for part in parts: used += "." + part try: found = getattr(found, part) except AttributeError: pass else: continue # we use explicit un-nesting of the handling block in order # to avoid nested exceptions on python 3 try: __import__(used) except ImportError as ex: # str is used for py2 vs py3 expected = str(ex).split()[-1] if expected == used: raise else: raise ImportError("import error in %s: %s" % (used, ex)) found = annotated_getattr(found, part, used) return found def annotated_getattr(obj, name, ann): try: obj = getattr(obj, name) except AttributeError: raise AttributeError( "%r object at %s has no attribute %r" % (type(obj).__name__, ann, name) ) return obj def derive_importpath(import_path, raising): if not isinstance(import_path, six.string_types) or "." not in import_path: raise TypeError("must be absolute import path string, not %r" % (import_path,)) module, attr = import_path.rsplit(".", 1) target = resolve(module) if raising: annotated_getattr(target, attr, ann=module) return attr, target class Notset(object): def __repr__(self): return "" notset = Notset() class MonkeyPatch(object): """ Object returned by the ``monkeypatch`` fixture keeping a record of setattr/item/env/syspath changes. """ def __init__(self): self._setattr = [] self._setitem = [] self._cwd = None self._savesyspath = None @contextmanager def context(self): """ Context manager that returns a new :class:`MonkeyPatch` object which undoes any patching done inside the ``with`` block upon exit: .. code-block:: python import functools def test_partial(monkeypatch): with monkeypatch.context() as m: m.setattr(functools, "partial", 3) Useful in situations where it is desired to undo some patches before the test ends, such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples of this see `#3290 `_. """ m = MonkeyPatch() try: yield m finally: m.undo() def setattr(self, target, name, value=notset, raising=True): """ Set attribute value on target, memorizing the old value. By default raise AttributeError if the attribute did not exist. For convenience you can specify a string as ``target`` which will be interpreted as a dotted import path, with the last part being the attribute name. Example: ``monkeypatch.setattr("os.getcwd", lambda: "/")`` would set the ``getcwd`` function of the ``os`` module. The ``raising`` value determines if the setattr should fail if the attribute is not already present (defaults to True which means it will raise). """ __tracebackhide__ = True import inspect if value is notset: if not isinstance(target, six.string_types): raise TypeError( "use setattr(target, name, value) or " "setattr(target, value) with target being a dotted " "import string" ) value = name name, target = derive_importpath(target, raising) oldval = getattr(target, name, notset) if raising and oldval is notset: raise AttributeError("%r has no attribute %r" % (target, name)) # avoid class descriptors like staticmethod/classmethod if inspect.isclass(target): oldval = target.__dict__.get(name, notset) self._setattr.append((target, name, oldval)) setattr(target, name, value) def delattr(self, target, name=notset, raising=True): """ Delete attribute ``name`` from ``target``, by default raise AttributeError it the attribute did not previously exist. If no ``name`` is specified and ``target`` is a string it will be interpreted as a dotted import path with the last part being the attribute name. If ``raising`` is set to False, no exception will be raised if the attribute is missing. """ __tracebackhide__ = True import inspect if name is notset: if not isinstance(target, six.string_types): raise TypeError( "use delattr(target, name) or " "delattr(target) with target being a dotted " "import string" ) name, target = derive_importpath(target, raising) if not hasattr(target, name): if raising: raise AttributeError(name) else: oldval = getattr(target, name, notset) # Avoid class descriptors like staticmethod/classmethod. if inspect.isclass(target): oldval = target.__dict__.get(name, notset) self._setattr.append((target, name, oldval)) delattr(target, name) def setitem(self, dic, name, value): """ Set dictionary entry ``name`` to value. """ self._setitem.append((dic, name, dic.get(name, notset))) dic[name] = value def delitem(self, dic, name, raising=True): """ Delete ``name`` from dict. Raise KeyError if it doesn't exist. If ``raising`` is set to False, no exception will be raised if the key is missing. """ if name not in dic: if raising: raise KeyError(name) else: self._setitem.append((dic, name, dic.get(name, notset))) del dic[name] def _warn_if_env_name_is_not_str(self, name): """On Python 2, warn if the given environment variable name is not a native str (#4056)""" if six.PY2 and not isinstance(name, str): warnings.warn( pytest.PytestWarning( "Environment variable name {!r} should be str".format(name) ) ) def setenv(self, name, value, prepend=None): """ Set environment variable ``name`` to ``value``. If ``prepend`` is a character, read the current environment variable value and prepend the ``value`` adjoined with the ``prepend`` character.""" if not isinstance(value, str): warnings.warn( pytest.PytestWarning( "Value of environment variable {name} type should be str, but got " "{value!r} (type: {type}); converted to str implicitly".format( name=name, value=value, type=type(value).__name__ ) ), stacklevel=2, ) value = str(value) if prepend and name in os.environ: value = value + prepend + os.environ[name] self._warn_if_env_name_is_not_str(name) self.setitem(os.environ, name, value) def delenv(self, name, raising=True): """ Delete ``name`` from the environment. Raise KeyError if it does not exist. If ``raising`` is set to False, no exception will be raised if the environment variable is missing. """ self._warn_if_env_name_is_not_str(name) self.delitem(os.environ, name, raising=raising) def syspath_prepend(self, path): """ Prepend ``path`` to ``sys.path`` list of import locations. """ from pkg_resources import fixup_namespace_packages if self._savesyspath is None: self._savesyspath = sys.path[:] sys.path.insert(0, str(path)) # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171 fixup_namespace_packages(str(path)) # A call to syspathinsert() usually means that the caller wants to # import some dynamically created files, thus with python3 we # invalidate its import caches. # This is especially important when any namespace package is in used, # since then the mtime based FileFinder cache (that gets created in # this case already) gets not invalidated when writing the new files # quickly afterwards. if sys.version_info >= (3, 3): from importlib import invalidate_caches invalidate_caches() def chdir(self, path): """ Change the current working directory to the specified path. Path can be a string or a py.path.local object. """ if self._cwd is None: self._cwd = os.getcwd() if hasattr(path, "chdir"): path.chdir() elif isinstance(path, Path): # modern python uses the fspath protocol here LEGACY os.chdir(str(path)) else: os.chdir(path) def undo(self): """ Undo previous changes. This call consumes the undo stack. Calling it a second time has no effect unless you do more monkeypatching after the undo call. There is generally no need to call `undo()`, since it is called automatically during tear-down. Note that the same `monkeypatch` fixture is used across a single test function invocation. If `monkeypatch` is used both by the test function itself and one of the test fixtures, calling `undo()` will undo all of the changes made in both functions. """ for obj, name, value in reversed(self._setattr): if value is not notset: setattr(obj, name, value) else: delattr(obj, name) self._setattr[:] = [] for dictionary, name, value in reversed(self._setitem): if value is notset: try: del dictionary[name] except KeyError: pass # was already deleted, so we have the desired state else: dictionary[name] = value self._setitem[:] = [] if self._savesyspath is not None: sys.path[:] = self._savesyspath self._savesyspath = None if self._cwd is not None: os.chdir(self._cwd) self._cwd = None pytest-4.6.9/src/_pytest/compat.py0000664000372000037200000003316013604173400020040 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ python version compatibility code """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import codecs import functools import inspect import re import sys from contextlib import contextmanager import attr import py import six from six import text_type import _pytest from _pytest._io.saferepr import saferepr from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME try: import enum except ImportError: # pragma: no cover # Only available in Python 3.4+ or as a backport enum = None _PY3 = sys.version_info > (3, 0) _PY2 = not _PY3 if _PY3: from inspect import signature, Parameter as Parameter else: from funcsigs import signature, Parameter as Parameter NOTSET = object() PY35 = sys.version_info[:2] >= (3, 5) PY36 = sys.version_info[:2] >= (3, 6) MODULE_NOT_FOUND_ERROR = "ModuleNotFoundError" if PY36 else "ImportError" if _PY3: from collections.abc import MutableMapping as MappingMixin from collections.abc import Iterable, Mapping, Sequence, Sized else: # those raise DeprecationWarnings in Python >=3.7 from collections import MutableMapping as MappingMixin # noqa from collections import Iterable, Mapping, Sequence, Sized # noqa if sys.version_info >= (3, 4): from importlib.util import spec_from_file_location else: def spec_from_file_location(*_, **__): return None if sys.version_info >= (3, 8): from importlib import metadata as importlib_metadata # noqa else: import importlib_metadata # noqa def _format_args(func): return str(signature(func)) isfunction = inspect.isfunction isclass = inspect.isclass # used to work around a python2 exception info leak exc_clear = getattr(sys, "exc_clear", lambda: None) # The type of re.compile objects is not exposed in Python. REGEX_TYPE = type(re.compile("")) def is_generator(func): genfunc = inspect.isgeneratorfunction(func) return genfunc and not iscoroutinefunction(func) def iscoroutinefunction(func): """Return True if func is a decorated coroutine function. Note: copied and modified from Python 3.5's builtin couroutines.py to avoid import asyncio directly, which in turns also initializes the "logging" module as side-effect (see issue #8). """ return getattr(func, "_is_coroutine", False) or ( hasattr(inspect, "iscoroutinefunction") and inspect.iscoroutinefunction(func) ) def getlocation(function, curdir): function = get_real_func(function) fn = py.path.local(inspect.getfile(function)) lineno = function.__code__.co_firstlineno if fn.relto(curdir): fn = fn.relto(curdir) return "%s:%d" % (fn, lineno + 1) def num_mock_patch_args(function): """ return number of arguments used up by mock arguments (if any) """ patchings = getattr(function, "patchings", None) if not patchings: return 0 mock_modules = [sys.modules.get("mock"), sys.modules.get("unittest.mock")] if any(mock_modules): sentinels = [m.DEFAULT for m in mock_modules if m is not None] return len( [p for p in patchings if not p.attribute_name and p.new in sentinels] ) return len(patchings) def getfuncargnames(function, is_method=False, cls=None): """Returns the names of a function's mandatory arguments. This should return the names of all function arguments that: * Aren't bound to an instance or type as in instance or class methods. * Don't have default values. * Aren't bound with functools.partial. * Aren't replaced with mocks. The is_method and cls arguments indicate that the function should be treated as a bound method even though it's not unless, only in the case of cls, the function is a static method. @RonnyPfannschmidt: This function should be refactored when we revisit fixtures. The fixture mechanism should ask the node for the fixture names, and not try to obtain directly from the function object well after collection has occurred. """ # The parameters attribute of a Signature object contains an # ordered mapping of parameter names to Parameter instances. This # creates a tuple of the names of the parameters that don't have # defaults. try: parameters = signature(function).parameters except (ValueError, TypeError) as e: fail( "Could not determine arguments of {!r}: {}".format(function, e), pytrace=False, ) arg_names = tuple( p.name for p in parameters.values() if ( p.kind is Parameter.POSITIONAL_OR_KEYWORD or p.kind is Parameter.KEYWORD_ONLY ) and p.default is Parameter.empty ) # If this function should be treated as a bound method even though # it's passed as an unbound method or function, remove the first # parameter name. if is_method or ( cls and not isinstance(cls.__dict__.get(function.__name__, None), staticmethod) ): arg_names = arg_names[1:] # Remove any names that will be replaced with mocks. if hasattr(function, "__wrapped__"): arg_names = arg_names[num_mock_patch_args(function) :] return arg_names @contextmanager def dummy_context_manager(): """Context manager that does nothing, useful in situations where you might need an actual context manager or not depending on some condition. Using this allow to keep the same code""" yield def get_default_arg_names(function): # Note: this code intentionally mirrors the code at the beginning of getfuncargnames, # to get the arguments which were excluded from its result because they had default values return tuple( p.name for p in signature(function).parameters.values() if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY) and p.default is not Parameter.empty ) _non_printable_ascii_translate_table = { i: u"\\x{:02x}".format(i) for i in range(128) if i not in range(32, 127) } _non_printable_ascii_translate_table.update( {ord("\t"): u"\\t", ord("\r"): u"\\r", ord("\n"): u"\\n"} ) def _translate_non_printable(s): return s.translate(_non_printable_ascii_translate_table) if _PY3: STRING_TYPES = bytes, str UNICODE_TYPES = six.text_type if PY35: def _bytes_to_ascii(val): return val.decode("ascii", "backslashreplace") else: def _bytes_to_ascii(val): if val: # source: http://goo.gl/bGsnwC encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode("ascii") else: # empty bytes crashes codecs.escape_encode (#1087) return "" def ascii_escaped(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6' and escapes unicode objects into a sequence of escaped unicode ids, e.g.: '4\\nV\\U00043efa\\x0eMXWB\\x1e\\u3028\\u15fd\\xcd\\U0007d944' note: the obvious "v.decode('unicode-escape')" will return valid utf-8 unicode if it finds them in bytes, but we want to return escaped bytes for any byte, even if they match a utf-8 string. """ if isinstance(val, bytes): ret = _bytes_to_ascii(val) else: ret = val.encode("unicode_escape").decode("ascii") return _translate_non_printable(ret) else: STRING_TYPES = six.string_types UNICODE_TYPES = six.text_type def ascii_escaped(val): """In py2 bytes and str are the same type, so return if it's a bytes object, return it unchanged if it is a full ascii string, otherwise escape it into its binary form. If it's a unicode string, change the unicode characters into unicode escapes. """ if isinstance(val, bytes): try: ret = val.decode("ascii") except UnicodeDecodeError: ret = val.encode("string-escape").decode("ascii") else: ret = val.encode("unicode-escape").decode("ascii") return _translate_non_printable(ret) class _PytestWrapper(object): """Dummy wrapper around a function object for internal use only. Used to correctly unwrap the underlying function object when we are creating fixtures, because we wrap the function object ourselves with a decorator to issue warnings when the fixture function is called directly. """ def __init__(self, obj): self.obj = obj def get_real_func(obj): """ gets the real function object of the (possibly) wrapped object by functools.wraps or functools.partial. """ start_obj = obj for i in range(100): # __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function # to trigger a warning if it gets called directly instead of by pytest: we don't # want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774) new_obj = getattr(obj, "__pytest_wrapped__", None) if isinstance(new_obj, _PytestWrapper): obj = new_obj.obj break new_obj = getattr(obj, "__wrapped__", None) if new_obj is None: break obj = new_obj else: raise ValueError( ("could not find real function of {start}\nstopped at {current}").format( start=saferepr(start_obj), current=saferepr(obj) ) ) if isinstance(obj, functools.partial): obj = obj.func return obj def get_real_method(obj, holder): """ Attempts to obtain the real function object that might be wrapping ``obj``, while at the same time returning a bound method to ``holder`` if the original object was a bound method. """ try: is_method = hasattr(obj, "__func__") obj = get_real_func(obj) except Exception: return obj if is_method and hasattr(obj, "__get__") and callable(obj.__get__): obj = obj.__get__(holder) return obj def getfslineno(obj): # xxx let decorators etc specify a sane ordering obj = get_real_func(obj) if hasattr(obj, "place_as"): obj = obj.place_as fslineno = _pytest._code.getfslineno(obj) assert isinstance(fslineno[1], int), obj return fslineno def getimfunc(func): try: return func.__func__ except AttributeError: return func def safe_getattr(object, name, default): """ Like getattr but return default upon any Exception or any OutcomeException. Attribute access can potentially fail for 'evil' Python objects. See issue #214. It catches OutcomeException because of #2490 (issue #580), new outcomes are derived from BaseException instead of Exception (for more details check #2707) """ try: return getattr(object, name, default) except TEST_OUTCOME: return default def safe_isclass(obj): """Ignore any exception via isinstance on Python 3.""" try: return isclass(obj) except Exception: return False def _is_unittest_unexpected_success_a_failure(): """Return if the test suite should fail if an @expectedFailure unittest test PASSES. From https://docs.python.org/3/library/unittest.html?highlight=unittest#unittest.TestResult.wasSuccessful: Changed in version 3.4: Returns False if there were any unexpectedSuccesses from tests marked with the expectedFailure() decorator. """ return sys.version_info >= (3, 4) if _PY3: def safe_str(v): """returns v as string""" return str(v) else: def safe_str(v): """returns v as string, converting to utf-8 if necessary""" try: return str(v) except UnicodeError: if not isinstance(v, text_type): v = text_type(v) errors = "replace" return v.encode("utf-8", errors) COLLECT_FAKEMODULE_ATTRIBUTES = ( "Collector", "Module", "Function", "Instance", "Session", "Item", "Class", "File", "_fillfuncargs", ) def _setup_collect_fakemodule(): from types import ModuleType import pytest pytest.collect = ModuleType("pytest.collect") pytest.collect.__all__ = [] # used for setns for attribute in COLLECT_FAKEMODULE_ATTRIBUTES: setattr(pytest.collect, attribute, getattr(pytest, attribute)) if _PY2: # Without this the test_dupfile_on_textio will fail, otherwise CaptureIO could directly inherit from StringIO. from py.io import TextIO class CaptureIO(TextIO): @property def encoding(self): return getattr(self, "_encoding", "UTF-8") else: import io class CaptureIO(io.TextIOWrapper): def __init__(self): super(CaptureIO, self).__init__( io.BytesIO(), encoding="UTF-8", newline="", write_through=True ) def getvalue(self): return self.buffer.getvalue().decode("UTF-8") class FuncargnamesCompatAttr(object): """ helper class so that Metafunc, Function and FixtureRequest don't need to each define the "funcargnames" compatibility attribute. """ @property def funcargnames(self): """ alias attribute for ``fixturenames`` for pre-2.3 compatibility""" return self.fixturenames if six.PY2: def lru_cache(*_, **__): def dec(fn): return fn return dec else: from functools import lru_cache # noqa: F401 if getattr(attr, "__version_info__", ()) >= (19, 2): ATTRS_EQ_FIELD = "eq" else: ATTRS_EQ_FIELD = "cmp" pytest-4.6.9/src/_pytest/nodes.py0000664000372000037200000003324613604173400017672 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import warnings import py import six import _pytest._code from _pytest.compat import getfslineno from _pytest.mark.structures import NodeKeywords from _pytest.outcomes import fail SEP = "/" tracebackcutdir = py.path.local(_pytest.__file__).dirpath() def _splitnode(nodeid): """Split a nodeid into constituent 'parts'. Node IDs are strings, and can be things like: '' 'testing/code' 'testing/code/test_excinfo.py' 'testing/code/test_excinfo.py::TestFormattedExcinfo' Return values are lists e.g. [] ['testing', 'code'] ['testing', 'code', 'test_excinfo.py'] ['testing', 'code', 'test_excinfo.py', 'TestFormattedExcinfo', '()'] """ if nodeid == "": # If there is no root node at all, return an empty list so the caller's logic can remain sane return [] parts = nodeid.split(SEP) # Replace single last element 'test_foo.py::Bar' with multiple elements 'test_foo.py', 'Bar' parts[-1:] = parts[-1].split("::") return parts def ischildnode(baseid, nodeid): """Return True if the nodeid is a child node of the baseid. E.g. 'foo/bar::Baz' is a child of 'foo', 'foo/bar' and 'foo/bar::Baz', but not of 'foo/blorp' """ base_parts = _splitnode(baseid) node_parts = _splitnode(nodeid) if len(node_parts) < len(base_parts): return False return node_parts[: len(base_parts)] == base_parts class Node(object): """ base class for Collector and Item the test collection tree. Collector subclasses have children, Items are terminal nodes.""" def __init__( self, name, parent=None, config=None, session=None, fspath=None, nodeid=None ): #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object self.config = config or parent.config #: the session this node is part of self.session = session or parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, "fspath", None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: the marker objects belonging to this node self.own_markers = [] #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name @property def ihook(self): """ fspath sensitive hook proxy used to call pytest hooks""" return self.session.gethookproxy(self.fspath) def __repr__(self): return "<%s %s>" % (self.__class__.__name__, getattr(self, "name", None)) def warn(self, warning): """Issue a warning for this item. Warnings will be displayed after the test session, unless explicitly suppressed :param Warning warning: the warning instance to issue. Must be a subclass of PytestWarning. :raise ValueError: if ``warning`` instance is not a subclass of PytestWarning. Example usage: .. code-block:: python node.warn(PytestWarning("some message")) """ from _pytest.warning_types import PytestWarning if not isinstance(warning, PytestWarning): raise ValueError( "warning must be an instance of PytestWarning or subclass, got {!r}".format( warning ) ) path, lineno = get_fslocation_from_item(self) warnings.warn_explicit( warning, category=None, filename=str(path), lineno=lineno + 1 if lineno is not None else None, ) # methods for ordering nodes @property def nodeid(self): """ a ::-separated string denoting its collection tree address. """ return self._nodeid def __hash__(self): return hash(self.nodeid) def setup(self): pass def teardown(self): pass def listchain(self): """ return list of all parent collectors up to self, starting from root of collection tree. """ chain = [] item = self while item is not None: chain.append(item) item = item.parent chain.reverse() return chain def add_marker(self, marker, append=True): """dynamically add a marker object to the node. :type marker: ``str`` or ``pytest.mark.*`` object :param marker: ``append=True`` whether to append the marker, if ``False`` insert at position ``0``. """ from _pytest.mark import MarkDecorator, MARK_GEN if isinstance(marker, six.string_types): marker = getattr(MARK_GEN, marker) elif not isinstance(marker, MarkDecorator): raise ValueError("is not a string or pytest.mark.* Marker") self.keywords[marker.name] = marker if append: self.own_markers.append(marker.mark) else: self.own_markers.insert(0, marker.mark) def iter_markers(self, name=None): """ :param name: if given, filter the results by the name attribute iterate over all markers of the node """ return (x[1] for x in self.iter_markers_with_node(name=name)) def iter_markers_with_node(self, name=None): """ :param name: if given, filter the results by the name attribute iterate over all markers of the node returns sequence of tuples (node, mark) """ for node in reversed(self.listchain()): for mark in node.own_markers: if name is None or getattr(mark, "name", None) == name: yield node, mark def get_closest_marker(self, name, default=None): """return the first marker matching the name, from closest (for example function) to farther level (for example module level). :param default: fallback return value of no marker was found :param name: name to filter by """ return next(self.iter_markers(name=name), default) def listextrakeywords(self): """ Return a set of all extra keywords in self and any parents.""" extra_keywords = set() for item in self.listchain(): extra_keywords.update(item.extra_keyword_matches) return extra_keywords def listnames(self): return [x.name for x in self.listchain()] def addfinalizer(self, fin): """ register a function to be called when this node is finalized. This method can only be called when this node is active in a setup chain, for example during self.setup(). """ self.session._setupstate.addfinalizer(fin, self) def getparent(self, cls): """ get the next parent node (including ourself) which is an instance of the given class""" current = self while current and not isinstance(current, cls): current = current.parent return current def _prunetraceback(self, excinfo): pass def _repr_failure_py(self, excinfo, style=None): if excinfo.errisinstance(fail.Exception): if not excinfo.value.pytrace: return six.text_type(excinfo.value) fm = self.session._fixturemanager if excinfo.errisinstance(fm.FixtureLookupError): return excinfo.value.formatrepr() tbfilter = True if self.config.getoption("fulltrace", False): style = "long" else: tb = _pytest._code.Traceback([excinfo.traceback[-1]]) self._prunetraceback(excinfo) if len(excinfo.traceback) == 0: excinfo.traceback = tb tbfilter = False # prunetraceback already does it if style == "auto": style = "long" # XXX should excinfo.getrepr record all data and toterminal() process it? if style is None: if self.config.getoption("tbstyle", "auto") == "short": style = "short" else: style = "long" if self.config.getoption("verbose", 0) > 1: truncate_locals = False else: truncate_locals = True try: os.getcwd() abspath = False except OSError: abspath = True return excinfo.getrepr( funcargs=True, abspath=abspath, showlocals=self.config.getoption("showlocals", False), style=style, tbfilter=tbfilter, truncate_locals=truncate_locals, ) repr_failure = _repr_failure_py def get_fslocation_from_item(item): """Tries to extract the actual location from an item, depending on available attributes: * "fslocation": a pair (path, lineno) * "obj": a Python object that the item wraps. * "fspath": just a path :rtype: a tuple of (str|LocalPath, int) with filename and line number. """ result = getattr(item, "location", None) if result is not None: return result[:2] obj = getattr(item, "obj", None) if obj is not None: return getfslineno(obj) return getattr(item, "fspath", "unknown location"), -1 class Collector(Node): """ Collector instances create children through collect() and thus iteratively build a tree. """ class CollectError(Exception): """ an error during collection, contains a custom message. """ def collect(self): """ returns a list of children (items and collectors) for this collection node. """ raise NotImplementedError("abstract") def repr_failure(self, excinfo): """ represent a collection failure. """ if excinfo.errisinstance(self.CollectError): exc = excinfo.value return str(exc.args[0]) # Respect explicit tbstyle option, but default to "short" # (None._repr_failure_py defaults to "long" without "fulltrace" option). tbstyle = self.config.getoption("tbstyle", "auto") if tbstyle == "auto": tbstyle = "short" return self._repr_failure_py(excinfo, style=tbstyle) def _prunetraceback(self, excinfo): if hasattr(self, "fspath"): traceback = excinfo.traceback ntraceback = traceback.cut(path=self.fspath) if ntraceback == traceback: ntraceback = ntraceback.cut(excludepath=tracebackcutdir) excinfo.traceback = ntraceback.filter() def _check_initialpaths_for_relpath(session, fspath): for initial_path in session._initialpaths: if fspath.common(initial_path) == initial_path: return fspath.relto(initial_path) class FSCollector(Collector): def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None): fspath = py.path.local(fspath) # xxx only for test_resultlog.py? name = fspath.basename if parent is not None: rel = fspath.relto(parent.fspath) if rel: name = rel name = name.replace(os.sep, SEP) self.fspath = fspath session = session or parent.session if nodeid is None: nodeid = self.fspath.relto(session.config.rootdir) if not nodeid: nodeid = _check_initialpaths_for_relpath(session, fspath) if nodeid and os.sep != SEP: nodeid = nodeid.replace(os.sep, SEP) super(FSCollector, self).__init__( name, parent, config, session, nodeid=nodeid, fspath=fspath ) class File(FSCollector): """ base class for collecting tests from a file. """ class Item(Node): """ a basic test invocation item. Note that for a single function there might be multiple test invocation items. """ nextitem = None def __init__(self, name, parent=None, config=None, session=None, nodeid=None): super(Item, self).__init__(name, parent, config, session, nodeid=nodeid) self._report_sections = [] #: user properties is a list of tuples (name, value) that holds user #: defined properties for this test. self.user_properties = [] def add_report_section(self, when, key, content): """ Adds a new report section, similar to what's done internally to add stdout and stderr captured output:: item.add_report_section("call", "stdout", "report section contents") :param str when: One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``. :param str key: Name of the section, can be customized at will. Pytest uses ``"stdout"`` and ``"stderr"`` internally. :param str content: The full contents as a string. """ if content: self._report_sections.append((when, key, content)) def reportinfo(self): return self.fspath, None, "" @property def location(self): try: return self._location except AttributeError: location = self.reportinfo() fspath = self.session._node_location_to_relpath(location[0]) location = (fspath, location[1], str(location[2])) self._location = location return location pytest-4.6.9/src/_pytest/cacheprovider.py0000775000372000037200000003622113604173400021377 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ merged implementation of the cache provider the name cache was not chosen to ensure pluggy automatically ignores the external pytest-cache """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import json import os from collections import OrderedDict import attr import py import six import pytest from .compat import _PY2 as PY2 from .pathlib import Path from .pathlib import resolve_from_str from .pathlib import rm_rf README_CONTENT = u"""\ # pytest cache directory # This directory contains data from the pytest's cache plugin, which provides the `--lf` and `--ff` options, as well as the `cache` fixture. **Do not** commit this to version control. See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. """ CACHEDIR_TAG_CONTENT = b"""\ Signature: 8a477f597d28d172789f06886806bc55 # This file is a cache directory tag created by pytest. # For information about cache directory tags, see: # http://www.bford.info/cachedir/spec.html """ @attr.s class Cache(object): _cachedir = attr.ib(repr=False) _config = attr.ib(repr=False) @classmethod def for_config(cls, config): cachedir = cls.cache_dir_from_config(config) if config.getoption("cacheclear") and cachedir.exists(): rm_rf(cachedir) cachedir.mkdir() return cls(cachedir, config) @staticmethod def cache_dir_from_config(config): return resolve_from_str(config.getini("cache_dir"), config.rootdir) def warn(self, fmt, **args): from _pytest.warnings import _issue_warning_captured from _pytest.warning_types import PytestCacheWarning _issue_warning_captured( PytestCacheWarning(fmt.format(**args) if args else fmt), self._config.hook, stacklevel=3, ) def makedir(self, name): """ return a directory path object with the given name. If the directory does not yet exist, it will be created. You can use it to manage files likes e. g. store/retrieve database dumps across test sessions. :param name: must be a string not containing a ``/`` separator. Make sure the name contains your plugin or application identifiers to prevent clashes with other cache users. """ name = Path(name) if len(name.parts) > 1: raise ValueError("name is not allowed to contain path separators") res = self._cachedir.joinpath("d", name) res.mkdir(exist_ok=True, parents=True) return py.path.local(res) def _getvaluepath(self, key): return self._cachedir.joinpath("v", Path(key)) def get(self, key, default): """ return cached value for the given key. If no value was yet cached or the value cannot be read, the specified default is returned. :param key: must be a ``/`` separated value. Usually the first name is the name of your plugin or your application. :param default: must be provided in case of a cache-miss or invalid cache values. """ path = self._getvaluepath(key) try: with path.open("r") as f: return json.load(f) except (ValueError, IOError, OSError): return default def set(self, key, value): """ save value for the given key. :param key: must be a ``/`` separated value. Usually the first name is the name of your plugin or your application. :param value: must be of any combination of basic python types, including nested types like e. g. lists of dictionaries. """ path = self._getvaluepath(key) try: if path.parent.is_dir(): cache_dir_exists_already = True else: cache_dir_exists_already = self._cachedir.exists() path.parent.mkdir(exist_ok=True, parents=True) except (IOError, OSError): self.warn("could not create cache path {path}", path=path) return if not cache_dir_exists_already: self._ensure_supporting_files() try: f = path.open("wb" if PY2 else "w") except (IOError, OSError): self.warn("cache could not write path {path}", path=path) else: with f: json.dump(value, f, indent=2, sort_keys=True) def _ensure_supporting_files(self): """Create supporting files in the cache dir that are not really part of the cache.""" readme_path = self._cachedir / "README.md" readme_path.write_text(README_CONTENT) gitignore_path = self._cachedir.joinpath(".gitignore") msg = u"# Created by pytest automatically.\n*" gitignore_path.write_text(msg, encoding="UTF-8") cachedir_tag_path = self._cachedir.joinpath("CACHEDIR.TAG") cachedir_tag_path.write_bytes(CACHEDIR_TAG_CONTENT) class LFPlugin(object): """ Plugin which implements the --lf (run last-failing) option """ def __init__(self, config): self.config = config active_keys = "lf", "failedfirst" self.active = any(config.getoption(key) for key in active_keys) self.lastfailed = config.cache.get("cache/lastfailed", {}) self._previously_failed_count = None self._report_status = None self._skipped_files = 0 # count skipped files during collection due to --lf def last_failed_paths(self): """Returns a set with all Paths()s of the previously failed nodeids (cached). """ try: return self._last_failed_paths except AttributeError: rootpath = Path(self.config.rootdir) result = {rootpath / nodeid.split("::")[0] for nodeid in self.lastfailed} result = {x for x in result if x.exists()} self._last_failed_paths = result return result def pytest_ignore_collect(self, path): """ Ignore this file path if we are in --lf mode and it is not in the list of previously failed files. """ if self.active and self.config.getoption("lf") and path.isfile(): last_failed_paths = self.last_failed_paths() if last_failed_paths: skip_it = Path(path) not in self.last_failed_paths() if skip_it: self._skipped_files += 1 return skip_it def pytest_report_collectionfinish(self): if self.active and self.config.getoption("verbose") >= 0: return "run-last-failure: %s" % self._report_status def pytest_runtest_logreport(self, report): if (report.when == "call" and report.passed) or report.skipped: self.lastfailed.pop(report.nodeid, None) elif report.failed: self.lastfailed[report.nodeid] = True def pytest_collectreport(self, report): passed = report.outcome in ("passed", "skipped") if passed: if report.nodeid in self.lastfailed: self.lastfailed.pop(report.nodeid) self.lastfailed.update((item.nodeid, True) for item in report.result) else: self.lastfailed[report.nodeid] = True def pytest_collection_modifyitems(self, session, config, items): if not self.active: return if self.lastfailed: previously_failed = [] previously_passed = [] for item in items: if item.nodeid in self.lastfailed: previously_failed.append(item) else: previously_passed.append(item) self._previously_failed_count = len(previously_failed) if not previously_failed: # Running a subset of all tests with recorded failures # only outside of it. self._report_status = "%d known failures not in selected tests" % ( len(self.lastfailed), ) else: if self.config.getoption("lf"): items[:] = previously_failed config.hook.pytest_deselected(items=previously_passed) else: # --failedfirst items[:] = previously_failed + previously_passed noun = "failure" if self._previously_failed_count == 1 else "failures" suffix = " first" if self.config.getoption("failedfirst") else "" self._report_status = "rerun previous {count} {noun}{suffix}".format( count=self._previously_failed_count, suffix=suffix, noun=noun ) if self._skipped_files > 0: files_noun = "file" if self._skipped_files == 1 else "files" self._report_status += " (skipped {files} {files_noun})".format( files=self._skipped_files, files_noun=files_noun ) else: self._report_status = "no previously failed tests, " if self.config.getoption("last_failed_no_failures") == "none": self._report_status += "deselecting all items." config.hook.pytest_deselected(items=items) items[:] = [] else: self._report_status += "not deselecting items." def pytest_sessionfinish(self, session): config = self.config if config.getoption("cacheshow") or hasattr(config, "slaveinput"): return saved_lastfailed = config.cache.get("cache/lastfailed", {}) if saved_lastfailed != self.lastfailed: config.cache.set("cache/lastfailed", self.lastfailed) class NFPlugin(object): """ Plugin which implements the --nf (run new-first) option """ def __init__(self, config): self.config = config self.active = config.option.newfirst self.cached_nodeids = config.cache.get("cache/nodeids", []) def pytest_collection_modifyitems(self, session, config, items): if self.active: new_items = OrderedDict() other_items = OrderedDict() for item in items: if item.nodeid not in self.cached_nodeids: new_items[item.nodeid] = item else: other_items[item.nodeid] = item items[:] = self._get_increasing_order( six.itervalues(new_items) ) + self._get_increasing_order(six.itervalues(other_items)) self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)] def _get_increasing_order(self, items): return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True) def pytest_sessionfinish(self, session): config = self.config if config.getoption("cacheshow") or hasattr(config, "slaveinput"): return config.cache.set("cache/nodeids", self.cached_nodeids) def pytest_addoption(parser): group = parser.getgroup("general") group.addoption( "--lf", "--last-failed", action="store_true", dest="lf", help="rerun only the tests that failed " "at the last run (or all if none failed)", ) group.addoption( "--ff", "--failed-first", action="store_true", dest="failedfirst", help="run all tests but run the last failures first. " "This may re-order tests and thus lead to " "repeated fixture setup/teardown", ) group.addoption( "--nf", "--new-first", action="store_true", dest="newfirst", help="run tests from new files first, then the rest of the tests " "sorted by file mtime", ) group.addoption( "--cache-show", action="append", nargs="?", dest="cacheshow", help=( "show cache contents, don't perform collection or tests. " "Optional argument: glob (default: '*')." ), ) group.addoption( "--cache-clear", action="store_true", dest="cacheclear", help="remove all cache contents at start of test run.", ) cache_dir_default = ".pytest_cache" if "TOX_ENV_DIR" in os.environ: cache_dir_default = os.path.join(os.environ["TOX_ENV_DIR"], cache_dir_default) parser.addini("cache_dir", default=cache_dir_default, help="cache directory path.") group.addoption( "--lfnf", "--last-failed-no-failures", action="store", dest="last_failed_no_failures", choices=("all", "none"), default="all", help="which tests to run with no previously (known) failures.", ) def pytest_cmdline_main(config): if config.option.cacheshow: from _pytest.main import wrap_session return wrap_session(config, cacheshow) @pytest.hookimpl(tryfirst=True) def pytest_configure(config): config.cache = Cache.for_config(config) config.pluginmanager.register(LFPlugin(config), "lfplugin") config.pluginmanager.register(NFPlugin(config), "nfplugin") @pytest.fixture def cache(request): """ Return a cache object that can persist state between testing sessions. cache.get(key, default) cache.set(key, value) Keys must be a ``/`` separated value, where the first part is usually the name of your plugin or application to avoid clashes with other cache users. Values can be any object handled by the json stdlib module. """ return request.config.cache def pytest_report_header(config): """Display cachedir with --cache-show and if non-default.""" if config.option.verbose > 0 or config.getini("cache_dir") != ".pytest_cache": cachedir = config.cache._cachedir # TODO: evaluate generating upward relative paths # starting with .., ../.. if sensible try: displaypath = cachedir.relative_to(config.rootdir) except ValueError: displaypath = cachedir return "cachedir: {}".format(displaypath) def cacheshow(config, session): from pprint import pformat tw = py.io.TerminalWriter() tw.line("cachedir: " + str(config.cache._cachedir)) if not config.cache._cachedir.is_dir(): tw.line("cache is empty") return 0 glob = config.option.cacheshow[0] if glob is None: glob = "*" dummy = object() basedir = config.cache._cachedir vdir = basedir / "v" tw.sep("-", "cache values for %r" % glob) for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()): key = valpath.relative_to(vdir) val = config.cache.get(key, dummy) if val is dummy: tw.line("%s contains unreadable content, will be ignored" % key) else: tw.line("%s contains:" % key) for line in pformat(val).splitlines(): tw.line(" " + line) ddir = basedir / "d" if ddir.is_dir(): contents = sorted(ddir.rglob(glob)) tw.sep("-", "cache directories for %r" % glob) for p in contents: # if p.check(dir=1): # print("%s/" % p.relto(basedir)) if p.is_file(): key = p.relative_to(basedir) tw.line("{} is a file of length {:d}".format(key, p.stat().st_size)) return 0 pytest-4.6.9/src/_pytest/pathlib.py0000664000372000037200000002600013604173400020173 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import import atexit import errno import fnmatch import itertools import operator import os import shutil import sys import uuid import warnings from functools import partial from functools import reduce from os.path import expanduser from os.path import expandvars from os.path import isabs from os.path import sep from posixpath import sep as posix_sep import six from six.moves import map from .compat import PY36 from _pytest.warning_types import PytestWarning if PY36: from pathlib import Path, PurePath else: from pathlib2 import Path, PurePath __all__ = ["Path", "PurePath"] LOCK_TIMEOUT = 60 * 60 * 3 get_lock_path = operator.methodcaller("joinpath", ".lock") def ensure_reset_dir(path): """ ensures the given path is an empty directory """ if path.exists(): rm_rf(path) path.mkdir() def on_rm_rf_error(func, path, exc, **kwargs): """Handles known read-only errors during rmtree. The returned value is used only by our own tests. """ start_path = kwargs["start_path"] exctype, excvalue = exc[:2] # another process removed the file in the middle of the "rm_rf" (xdist for example) # more context: https://github.com/pytest-dev/pytest/issues/5974#issuecomment-543799018 if isinstance(excvalue, OSError) and excvalue.errno == errno.ENOENT: return False if not isinstance(excvalue, OSError) or excvalue.errno not in ( errno.EACCES, errno.EPERM, ): warnings.warn( PytestWarning( "(rm_rf) error removing {}\n{}: {}".format(path, exctype, excvalue) ) ) return False if func not in (os.rmdir, os.remove, os.unlink): warnings.warn( PytestWarning( "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( path, func, exctype, excvalue ) ) ) return False # Chmod + retry. import stat def chmod_rw(p): mode = os.stat(p).st_mode os.chmod(p, mode | stat.S_IRUSR | stat.S_IWUSR) # For files, we need to recursively go upwards in the directories to # ensure they all are also writable. p = Path(path) if p.is_file(): for parent in p.parents: chmod_rw(str(parent)) # stop when we reach the original path passed to rm_rf if parent == start_path: break chmod_rw(str(path)) func(path) return True def rm_rf(path): """Remove the path contents recursively, even if some elements are read-only. """ onerror = partial(on_rm_rf_error, start_path=path) shutil.rmtree(str(path), onerror=onerror) def find_prefixed(root, prefix): """finds all elements in root that begin with the prefix, case insensitive""" l_prefix = prefix.lower() for x in root.iterdir(): if x.name.lower().startswith(l_prefix): yield x def extract_suffixes(iter, prefix): """ :param iter: iterator over path names :param prefix: expected prefix of the path names :returns: the parts of the paths following the prefix """ p_len = len(prefix) for p in iter: yield p.name[p_len:] def find_suffixes(root, prefix): """combines find_prefixes and extract_suffixes """ return extract_suffixes(find_prefixed(root, prefix), prefix) def parse_num(maybe_num): """parses number path suffixes, returns -1 on error""" try: return int(maybe_num) except ValueError: return -1 if six.PY2: def _max(iterable, default): """needed due to python2.7 lacking the default argument for max""" return reduce(max, iterable, default) else: _max = max def _force_symlink(root, target, link_to): """helper to create the current symlink it's full of race conditions that are reasonably ok to ignore for the context of best effort linking to the latest testrun the presumption being thatin case of much parallelism the inaccuracy is going to be acceptable """ current_symlink = root.joinpath(target) try: current_symlink.unlink() except OSError: pass try: current_symlink.symlink_to(link_to) except Exception: pass def make_numbered_dir(root, prefix): """create a directory with an increased number as suffix for the given prefix""" for i in range(10): # try up to 10 times to create the folder max_existing = _max(map(parse_num, find_suffixes(root, prefix)), default=-1) new_number = max_existing + 1 new_path = root.joinpath("{}{}".format(prefix, new_number)) try: new_path.mkdir() except Exception: pass else: _force_symlink(root, prefix + "current", new_path) return new_path else: raise EnvironmentError( "could not create numbered dir with prefix " "{prefix} in {root} after 10 tries".format(prefix=prefix, root=root) ) def create_cleanup_lock(p): """crates a lock to prevent premature folder cleanup""" lock_path = get_lock_path(p) try: fd = os.open(str(lock_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644) except OSError as e: if e.errno == errno.EEXIST: six.raise_from( EnvironmentError("cannot create lockfile in {path}".format(path=p)), e ) else: raise else: pid = os.getpid() spid = str(pid) if not isinstance(spid, bytes): spid = spid.encode("ascii") os.write(fd, spid) os.close(fd) if not lock_path.is_file(): raise EnvironmentError("lock path got renamed after successful creation") return lock_path def register_cleanup_lock_removal(lock_path, register=atexit.register): """registers a cleanup function for removing a lock, by default on atexit""" pid = os.getpid() def cleanup_on_exit(lock_path=lock_path, original_pid=pid): current_pid = os.getpid() if current_pid != original_pid: # fork return try: lock_path.unlink() except (OSError, IOError): pass return register(cleanup_on_exit) def maybe_delete_a_numbered_dir(path): """removes a numbered directory if its lock can be obtained and it does not seem to be in use""" lock_path = None try: lock_path = create_cleanup_lock(path) parent = path.parent garbage = parent.joinpath("garbage-{}".format(uuid.uuid4())) path.rename(garbage) rm_rf(garbage) except (OSError, EnvironmentError): # known races: # * other process did a cleanup at the same time # * deletable folder was found # * process cwd (Windows) return finally: # if we created the lock, ensure we remove it even if we failed # to properly remove the numbered dir if lock_path is not None: try: lock_path.unlink() except (OSError, IOError): pass def ensure_deletable(path, consider_lock_dead_if_created_before): """checks if a lock exists and breaks it if its considered dead""" if path.is_symlink(): return False lock = get_lock_path(path) if not lock.exists(): return True try: lock_time = lock.stat().st_mtime except Exception: return False else: if lock_time < consider_lock_dead_if_created_before: lock.unlink() return True else: return False def try_cleanup(path, consider_lock_dead_if_created_before): """tries to cleanup a folder if we can ensure it's deletable""" if ensure_deletable(path, consider_lock_dead_if_created_before): maybe_delete_a_numbered_dir(path) def cleanup_candidates(root, prefix, keep): """lists candidates for numbered directories to be removed - follows py.path""" max_existing = _max(map(parse_num, find_suffixes(root, prefix)), default=-1) max_delete = max_existing - keep paths = find_prefixed(root, prefix) paths, paths2 = itertools.tee(paths) numbers = map(parse_num, extract_suffixes(paths2, prefix)) for path, number in zip(paths, numbers): if number <= max_delete: yield path def cleanup_numbered_dir(root, prefix, keep, consider_lock_dead_if_created_before): """cleanup for lock driven numbered directories""" for path in cleanup_candidates(root, prefix, keep): try_cleanup(path, consider_lock_dead_if_created_before) for path in root.glob("garbage-*"): try_cleanup(path, consider_lock_dead_if_created_before) def make_numbered_dir_with_cleanup(root, prefix, keep, lock_timeout): """creates a numbered dir with a cleanup lock and removes old ones""" e = None for i in range(10): try: p = make_numbered_dir(root, prefix) lock_path = create_cleanup_lock(p) register_cleanup_lock_removal(lock_path) except Exception as exc: e = exc else: consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout cleanup_numbered_dir( root=root, prefix=prefix, keep=keep, consider_lock_dead_if_created_before=consider_lock_dead_if_created_before, ) return p assert e is not None raise e def resolve_from_str(input, root): assert not isinstance(input, Path), "would break on py2" root = Path(root) input = expanduser(input) input = expandvars(input) if isabs(input): return Path(input) else: return root.joinpath(input) def fnmatch_ex(pattern, path): """FNMatcher port from py.path.common which works with PurePath() instances. The difference between this algorithm and PurePath.match() is that the latter matches "**" glob expressions for each part of the path, while this algorithm uses the whole path instead. For example: "tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py" with this algorithm, but not with PurePath.match(). This algorithm was ported to keep backward-compatibility with existing settings which assume paths match according this logic. References: * https://bugs.python.org/issue29249 * https://bugs.python.org/issue34731 """ path = PurePath(path) iswin32 = sys.platform.startswith("win") if iswin32 and sep not in pattern and posix_sep in pattern: # Running on Windows, the pattern has no Windows path separators, # and the pattern has one or more Posix path separators. Replace # the Posix path separators with the Windows path separator. pattern = pattern.replace(posix_sep, sep) if sep not in pattern: name = path.name else: name = six.text_type(path) return fnmatch.fnmatch(name, pattern) def parts(s): parts = s.split(sep) return {sep.join(parts[: i + 1]) or sep for i in range(len(parts))} pytest-4.6.9/src/_pytest/main.py0000664000372000037200000006442713604173400017513 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ core implementation of testing process: init, session, runtest loop. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import contextlib import fnmatch import functools import os import pkgutil import sys import warnings import attr import py import six import _pytest._code from _pytest import nodes from _pytest.config import directory_arg from _pytest.config import hookimpl from _pytest.config import UsageError from _pytest.deprecated import PYTEST_CONFIG_GLOBAL from _pytest.outcomes import exit from _pytest.runner import collect_one_node # exitcodes for the command line EXIT_OK = 0 EXIT_TESTSFAILED = 1 EXIT_INTERRUPTED = 2 EXIT_INTERNALERROR = 3 EXIT_USAGEERROR = 4 EXIT_NOTESTSCOLLECTED = 5 def pytest_addoption(parser): parser.addini( "norecursedirs", "directory patterns to avoid for recursion", type="args", default=[".*", "build", "dist", "CVS", "_darcs", "{arch}", "*.egg", "venv"], ) parser.addini( "testpaths", "directories to search for tests when no files or directories are given in the " "command line.", type="args", default=[], ) group = parser.getgroup("general", "running and selection options") group._addoption( "-x", "--exitfirst", action="store_const", dest="maxfail", const=1, help="exit instantly on first error or failed test.", ), group._addoption( "--maxfail", metavar="num", action="store", type=int, dest="maxfail", default=0, help="exit after first num failures or errors.", ) group._addoption( "--strict-markers", "--strict", action="store_true", help="markers not registered in the `markers` section of the configuration file raise errors.", ) group._addoption( "-c", metavar="file", type=str, dest="inifilename", help="load configuration from `file` instead of trying to locate one of the implicit " "configuration files.", ) group._addoption( "--continue-on-collection-errors", action="store_true", default=False, dest="continue_on_collection_errors", help="Force test execution even if collection errors occur.", ) group._addoption( "--rootdir", action="store", dest="rootdir", help="Define root directory for tests. Can be relative path: 'root_dir', './root_dir', " "'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables: " "'$HOME/root_dir'.", ) group = parser.getgroup("collect", "collection") group.addoption( "--collectonly", "--collect-only", action="store_true", help="only collect tests, don't execute them.", ), group.addoption( "--pyargs", action="store_true", help="try to interpret all arguments as python packages.", ) group.addoption( "--ignore", action="append", metavar="path", help="ignore path during collection (multi-allowed).", ) group.addoption( "--ignore-glob", action="append", metavar="path", help="ignore path pattern during collection (multi-allowed).", ) group.addoption( "--deselect", action="append", metavar="nodeid_prefix", help="deselect item during collection (multi-allowed).", ) # when changing this to --conf-cut-dir, config.py Conftest.setinitial # needs upgrading as well group.addoption( "--confcutdir", dest="confcutdir", default=None, metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"), help="only load conftest.py's relative to specified dir.", ) group.addoption( "--noconftest", action="store_true", dest="noconftest", default=False, help="Don't load any conftest.py files.", ) group.addoption( "--keepduplicates", "--keep-duplicates", action="store_true", dest="keepduplicates", default=False, help="Keep duplicate tests.", ) group.addoption( "--collect-in-virtualenv", action="store_true", dest="collect_in_virtualenv", default=False, help="Don't ignore tests in a local virtualenv directory", ) group = parser.getgroup("debugconfig", "test session debugging and configuration") group.addoption( "--basetemp", dest="basetemp", default=None, metavar="dir", help=( "base temporary directory for this test run." "(warning: this directory is removed if it exists)" ), ) class _ConfigDeprecated(object): def __init__(self, config): self.__dict__["_config"] = config def __getattr__(self, attr): warnings.warn(PYTEST_CONFIG_GLOBAL, stacklevel=2) return getattr(self._config, attr) def __setattr__(self, attr, val): warnings.warn(PYTEST_CONFIG_GLOBAL, stacklevel=2) return setattr(self._config, attr, val) def __repr__(self): return "{}({!r})".format(type(self).__name__, self._config) def pytest_configure(config): __import__("pytest").config = _ConfigDeprecated(config) # compatibility def wrap_session(config, doit): """Skeleton command line program""" session = Session(config) session.exitstatus = EXIT_OK initstate = 0 try: try: config._do_configure() initstate = 1 config.hook.pytest_sessionstart(session=session) initstate = 2 session.exitstatus = doit(config, session) or 0 except UsageError: session.exitstatus = EXIT_USAGEERROR raise except Failed: session.exitstatus = EXIT_TESTSFAILED except (KeyboardInterrupt, exit.Exception): excinfo = _pytest._code.ExceptionInfo.from_current() exitstatus = EXIT_INTERRUPTED if isinstance(excinfo.value, exit.Exception): if excinfo.value.returncode is not None: exitstatus = excinfo.value.returncode if initstate < 2: sys.stderr.write( "{}: {}\n".format(excinfo.typename, excinfo.value.msg) ) config.hook.pytest_keyboard_interrupt(excinfo=excinfo) session.exitstatus = exitstatus except: # noqa excinfo = _pytest._code.ExceptionInfo.from_current() config.notify_exception(excinfo, config.option) session.exitstatus = EXIT_INTERNALERROR if excinfo.errisinstance(SystemExit): sys.stderr.write("mainloop: caught unexpected SystemExit!\n") finally: excinfo = None # Explicitly break reference cycle. session.startdir.chdir() if initstate >= 2: config.hook.pytest_sessionfinish( session=session, exitstatus=session.exitstatus ) config._ensure_unconfigure() return session.exitstatus def pytest_cmdline_main(config): return wrap_session(config, _main) def _main(config, session): """ default command line protocol for initialization, session, running tests and reporting. """ config.hook.pytest_collection(session=session) config.hook.pytest_runtestloop(session=session) if session.testsfailed: return EXIT_TESTSFAILED elif session.testscollected == 0: return EXIT_NOTESTSCOLLECTED def pytest_collection(session): return session.perform_collect() def pytest_runtestloop(session): if session.testsfailed and not session.config.option.continue_on_collection_errors: raise session.Interrupted("%d errors during collection" % session.testsfailed) if session.config.option.collectonly: return True for i, item in enumerate(session.items): nextitem = session.items[i + 1] if i + 1 < len(session.items) else None item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) if session.shouldfail: raise session.Failed(session.shouldfail) if session.shouldstop: raise session.Interrupted(session.shouldstop) return True def _in_venv(path): """Attempts to detect if ``path`` is the root of a Virtual Environment by checking for the existence of the appropriate activate script""" bindir = path.join("Scripts" if sys.platform.startswith("win") else "bin") if not bindir.isdir(): return False activates = ( "activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1", ) return any([fname.basename in activates for fname in bindir.listdir()]) def pytest_ignore_collect(path, config): ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath()) ignore_paths = ignore_paths or [] excludeopt = config.getoption("ignore") if excludeopt: ignore_paths.extend([py.path.local(x) for x in excludeopt]) if py.path.local(path) in ignore_paths: return True ignore_globs = config._getconftest_pathlist( "collect_ignore_glob", path=path.dirpath() ) ignore_globs = ignore_globs or [] excludeglobopt = config.getoption("ignore_glob") if excludeglobopt: ignore_globs.extend([py.path.local(x) for x in excludeglobopt]) if any( fnmatch.fnmatch(six.text_type(path), six.text_type(glob)) for glob in ignore_globs ): return True allow_in_venv = config.getoption("collect_in_virtualenv") if not allow_in_venv and _in_venv(path): return True return False def pytest_collection_modifyitems(items, config): deselect_prefixes = tuple(config.getoption("deselect") or []) if not deselect_prefixes: return remaining = [] deselected = [] for colitem in items: if colitem.nodeid.startswith(deselect_prefixes): deselected.append(colitem) else: remaining.append(colitem) if deselected: config.hook.pytest_deselected(items=deselected) items[:] = remaining @contextlib.contextmanager def _patched_find_module(): """Patch bug in pkgutil.ImpImporter.find_module When using pkgutil.find_loader on python<3.4 it removes symlinks from the path due to a call to os.path.realpath. This is not consistent with actually doing the import (in these versions, pkgutil and __import__ did not share the same underlying code). This can break conftest discovery for pytest where symlinks are involved. The only supported python<3.4 by pytest is python 2.7. """ if six.PY2: # python 3.4+ uses importlib instead def find_module_patched(self, fullname, path=None): # Note: we ignore 'path' argument since it is only used via meta_path subname = fullname.split(".")[-1] if subname != fullname and self.path is None: return None if self.path is None: path = None else: # original: path = [os.path.realpath(self.path)] path = [self.path] try: file, filename, etc = pkgutil.imp.find_module(subname, path) except ImportError: return None return pkgutil.ImpLoader(fullname, file, filename, etc) old_find_module = pkgutil.ImpImporter.find_module pkgutil.ImpImporter.find_module = find_module_patched try: yield finally: pkgutil.ImpImporter.find_module = old_find_module else: yield class FSHookProxy(object): def __init__(self, fspath, pm, remove_mods): self.fspath = fspath self.pm = pm self.remove_mods = remove_mods def __getattr__(self, name): x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods) self.__dict__[name] = x return x class NoMatch(Exception): """ raised if matching cannot locate a matching names. """ class Interrupted(KeyboardInterrupt): """ signals an interrupted test run. """ __module__ = "builtins" # for py3 class Failed(Exception): """ signals a stop as failed test run. """ @attr.s class _bestrelpath_cache(dict): path = attr.ib() def __missing__(self, path): r = self.path.bestrelpath(path) self[path] = r return r class Session(nodes.FSCollector): Interrupted = Interrupted Failed = Failed def __init__(self, config): nodes.FSCollector.__init__( self, config.rootdir, parent=None, config=config, session=self, nodeid="" ) self.testsfailed = 0 self.testscollected = 0 self.shouldstop = False self.shouldfail = False self.trace = config.trace.root.get("collection") self._norecursepatterns = config.getini("norecursedirs") self.startdir = config.invocation_dir self._initialpaths = frozenset() # Keep track of any collected nodes in here, so we don't duplicate fixtures self._node_cache = {} self._bestrelpathcache = _bestrelpath_cache(config.rootdir) # Dirnames of pkgs with dunder-init files. self._pkg_roots = {} self.config.pluginmanager.register(self, name="session") def __repr__(self): return "<%s %s exitstatus=%r testsfailed=%d testscollected=%d>" % ( self.__class__.__name__, self.name, getattr(self, "exitstatus", ""), self.testsfailed, self.testscollected, ) def _node_location_to_relpath(self, node_path): # bestrelpath is a quite slow function return self._bestrelpathcache[node_path] @hookimpl(tryfirst=True) def pytest_collectstart(self): if self.shouldfail: raise self.Failed(self.shouldfail) if self.shouldstop: raise self.Interrupted(self.shouldstop) @hookimpl(tryfirst=True) def pytest_runtest_logreport(self, report): if report.failed and not hasattr(report, "wasxfail"): self.testsfailed += 1 maxfail = self.config.getvalue("maxfail") if maxfail and self.testsfailed >= maxfail: self.shouldfail = "stopping after %d failures" % (self.testsfailed) pytest_collectreport = pytest_runtest_logreport def isinitpath(self, path): return path in self._initialpaths def gethookproxy(self, fspath): # check if we have the common case of running # hooks with all conftest.py files pm = self.config.pluginmanager my_conftestmodules = pm._getconftestmodules(fspath) remove_mods = pm._conftest_plugins.difference(my_conftestmodules) if remove_mods: # one or more conftests are not in use at this fspath proxy = FSHookProxy(fspath, pm, remove_mods) else: # all plugis are active for this fspath proxy = self.config.hook return proxy def perform_collect(self, args=None, genitems=True): hook = self.config.hook try: items = self._perform_collect(args, genitems) self.config.pluginmanager.check_pending() hook.pytest_collection_modifyitems( session=self, config=self.config, items=items ) finally: hook.pytest_collection_finish(session=self) self.testscollected = len(items) return items def _perform_collect(self, args, genitems): if args is None: args = self.config.args self.trace("perform_collect", self, args) self.trace.root.indent += 1 self._notfound = [] initialpaths = [] self._initialparts = [] self.items = items = [] for arg in args: parts = self._parsearg(arg) self._initialparts.append(parts) initialpaths.append(parts[0]) self._initialpaths = frozenset(initialpaths) rep = collect_one_node(self) self.ihook.pytest_collectreport(report=rep) self.trace.root.indent -= 1 if self._notfound: errors = [] for arg, exc in self._notfound: line = "(no name %r in any of %r)" % (arg, exc.args[0]) errors.append("not found: %s\n%s" % (arg, line)) # XXX: test this raise UsageError(*errors) if not genitems: return rep.result else: if rep.passed: for node in rep.result: self.items.extend(self.genitems(node)) return items def collect(self): for initialpart in self._initialparts: arg = "::".join(map(str, initialpart)) self.trace("processing argument", arg) self.trace.root.indent += 1 try: for x in self._collect(arg): yield x except NoMatch: # we are inside a make_report hook so # we cannot directly pass through the exception self._notfound.append((arg, sys.exc_info()[1])) self.trace.root.indent -= 1 def _collect(self, arg): from _pytest.python import Package names = self._parsearg(arg) argpath = names.pop(0) # Start with a Session root, and delve to argpath item (dir or file) # and stack all Packages found on the way. # No point in finding packages when collecting doctests if not self.config.getoption("doctestmodules", False): pm = self.config.pluginmanager for parent in reversed(argpath.parts()): if pm._confcutdir and pm._confcutdir.relto(parent): break if parent.isdir(): pkginit = parent.join("__init__.py") if pkginit.isfile(): if pkginit not in self._node_cache: col = self._collectfile(pkginit, handle_dupes=False) if col: if isinstance(col[0], Package): self._pkg_roots[parent] = col[0] # always store a list in the cache, matchnodes expects it self._node_cache[col[0].fspath] = [col[0]] # If it's a directory argument, recurse and look for any Subpackages. # Let the Package collector deal with subnodes, don't collect here. if argpath.check(dir=1): assert not names, "invalid arg %r" % (arg,) seen_dirs = set() for path in argpath.visit( fil=self._visit_filter, rec=self._recurse, bf=True, sort=True ): dirpath = path.dirpath() if dirpath not in seen_dirs: # Collect packages first. seen_dirs.add(dirpath) pkginit = dirpath.join("__init__.py") if pkginit.exists(): for x in self._collectfile(pkginit): yield x if isinstance(x, Package): self._pkg_roots[dirpath] = x if dirpath in self._pkg_roots: # Do not collect packages here. continue for x in self._collectfile(path): key = (type(x), x.fspath) if key in self._node_cache: yield self._node_cache[key] else: self._node_cache[key] = x yield x else: assert argpath.check(file=1) if argpath in self._node_cache: col = self._node_cache[argpath] else: collect_root = self._pkg_roots.get(argpath.dirname, self) col = collect_root._collectfile(argpath, handle_dupes=False) if col: self._node_cache[argpath] = col m = self.matchnodes(col, names) # If __init__.py was the only file requested, then the matched node will be # the corresponding Package, and the first yielded item will be the __init__ # Module itself, so just use that. If this special case isn't taken, then all # the files in the package will be yielded. if argpath.basename == "__init__.py": try: yield next(m[0].collect()) except StopIteration: # The package collects nothing with only an __init__.py # file in it, which gets ignored by the default # "python_files" option. pass return for y in m: yield y def _collectfile(self, path, handle_dupes=True): assert path.isfile(), "%r is not a file (isdir=%r, exists=%r, islink=%r)" % ( path, path.isdir(), path.exists(), path.islink(), ) ihook = self.gethookproxy(path) if not self.isinitpath(path): if ihook.pytest_ignore_collect(path=path, config=self.config): return () if handle_dupes: keepduplicates = self.config.getoption("keepduplicates") if not keepduplicates: duplicate_paths = self.config.pluginmanager._duplicatepaths if path in duplicate_paths: return () else: duplicate_paths.add(path) return ihook.pytest_collect_file(path=path, parent=self) def _recurse(self, dirpath): if dirpath.basename == "__pycache__": return False ihook = self.gethookproxy(dirpath.dirpath()) if ihook.pytest_ignore_collect(path=dirpath, config=self.config): return False for pat in self._norecursepatterns: if dirpath.check(fnmatch=pat): return False ihook = self.gethookproxy(dirpath) ihook.pytest_collect_directory(path=dirpath, parent=self) return True if six.PY2: @staticmethod def _visit_filter(f): return f.check(file=1) and not f.strpath.endswith("*.pyc") else: @staticmethod def _visit_filter(f): return f.check(file=1) def _tryconvertpyarg(self, x): """Convert a dotted module name to path.""" try: with _patched_find_module(): loader = pkgutil.find_loader(x) except ImportError: return x if loader is None: return x # This method is sometimes invoked when AssertionRewritingHook, which # does not define a get_filename method, is already in place: try: with _patched_find_module(): path = loader.get_filename(x) except AttributeError: # Retrieve path from AssertionRewritingHook: path = loader.modules[x][0].co_filename if loader.is_package(x): path = os.path.dirname(path) return path def _parsearg(self, arg): """ return (fspath, names) tuple after checking the file exists. """ parts = str(arg).split("::") if self.config.option.pyargs: parts[0] = self._tryconvertpyarg(parts[0]) relpath = parts[0].replace("/", os.sep) path = self.config.invocation_dir.join(relpath, abs=True) if not path.check(): if self.config.option.pyargs: raise UsageError( "file or package not found: " + arg + " (missing __init__.py?)" ) raise UsageError("file not found: " + arg) parts[0] = path.realpath() return parts def matchnodes(self, matching, names): self.trace("matchnodes", matching, names) self.trace.root.indent += 1 nodes = self._matchnodes(matching, names) num = len(nodes) self.trace("matchnodes finished -> ", num, "nodes") self.trace.root.indent -= 1 if num == 0: raise NoMatch(matching, names[:1]) return nodes def _matchnodes(self, matching, names): if not matching or not names: return matching name = names[0] assert name nextnames = names[1:] resultnodes = [] for node in matching: if isinstance(node, nodes.Item): if not names: resultnodes.append(node) continue assert isinstance(node, nodes.Collector) key = (type(node), node.nodeid) if key in self._node_cache: rep = self._node_cache[key] else: rep = collect_one_node(node) self._node_cache[key] = rep if rep.passed: has_matched = False for x in rep.result: # TODO: remove parametrized workaround once collection structure contains parametrization if x.name == name or x.name.split("[")[0] == name: resultnodes.extend(self.matchnodes([x], nextnames)) has_matched = True # XXX accept IDs that don't have "()" for class instances if not has_matched and len(rep.result) == 1 and x.name == "()": nextnames.insert(0, name) resultnodes.extend(self.matchnodes([x], nextnames)) else: # report collection failures here to avoid failing to run some test # specified in the command line because the module could not be # imported (#134) node.ihook.pytest_collectreport(report=rep) return resultnodes def genitems(self, node): self.trace("genitems", node) if isinstance(node, nodes.Item): node.ihook.pytest_itemcollected(item=node) yield node else: assert isinstance(node, nodes.Collector) rep = collect_one_node(node) if rep.passed: for subnode in rep.result: for x in self.genitems(subnode): yield x node.ihook.pytest_collectreport(report=rep) pytest-4.6.9/src/_pytest/__init__.py0000664000372000037200000000040713604173400020312 0ustar travistravis00000000000000# -*- coding: utf-8 -*- __all__ = ["__version__"] try: from ._version import version as __version__ except ImportError: # broken installation, we don't even try # unknown only works because we do poor mans version compare __version__ = "unknown" pytest-4.6.9/src/_pytest/setuponly.py0000664000372000037200000000525113604173400020617 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import pytest def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption( "--setuponly", "--setup-only", action="store_true", help="only setup fixtures, do not execute tests.", ) group.addoption( "--setupshow", "--setup-show", action="store_true", help="show setup of fixtures while executing tests.", ) @pytest.hookimpl(hookwrapper=True) def pytest_fixture_setup(fixturedef, request): yield config = request.config if config.option.setupshow: if hasattr(request, "param"): # Save the fixture parameter so ._show_fixture_action() can # display it now and during the teardown (in .finish()). if fixturedef.ids: if callable(fixturedef.ids): fixturedef.cached_param = fixturedef.ids(request.param) else: fixturedef.cached_param = fixturedef.ids[request.param_index] else: fixturedef.cached_param = request.param _show_fixture_action(fixturedef, "SETUP") def pytest_fixture_post_finalizer(fixturedef): if hasattr(fixturedef, "cached_result"): config = fixturedef._fixturemanager.config if config.option.setupshow: _show_fixture_action(fixturedef, "TEARDOWN") if hasattr(fixturedef, "cached_param"): del fixturedef.cached_param def _show_fixture_action(fixturedef, msg): config = fixturedef._fixturemanager.config capman = config.pluginmanager.getplugin("capturemanager") if capman: capman.suspend_global_capture() out, err = capman.read_global_capture() tw = config.get_terminal_writer() tw.line() tw.write(" " * 2 * fixturedef.scopenum) tw.write( "{step} {scope} {fixture}".format( step=msg.ljust(8), # align the output to TEARDOWN scope=fixturedef.scope[0].upper(), fixture=fixturedef.argname, ) ) if msg == "SETUP": deps = sorted(arg for arg in fixturedef.argnames if arg != "request") if deps: tw.write(" (fixtures used: {})".format(", ".join(deps))) if hasattr(fixturedef, "cached_param"): tw.write("[{}]".format(fixturedef.cached_param)) if capman: capman.resume_global_capture() sys.stdout.write(out) sys.stderr.write(err) @pytest.hookimpl(tryfirst=True) def pytest_cmdline_main(config): if config.option.setuponly: config.option.setupshow = True pytest-4.6.9/src/_pytest/assertion/0000775000372000037200000000000013604173421020212 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/assertion/util.py0000664000372000037200000003314413604173400021543 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """Utilities for assertion debugging""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import pprint import six import _pytest._code from ..compat import Sequence from _pytest import outcomes from _pytest._io.saferepr import saferepr from _pytest.compat import ATTRS_EQ_FIELD # The _reprcompare attribute on the util module is used by the new assertion # interpretation code and assertion rewriter to detect this plugin was # loaded and in turn call the hooks defined here as part of the # DebugInterpreter. _reprcompare = None # the re-encoding is needed for python2 repr # with non-ascii characters (see issue 877 and 1379) def ecu(s): if isinstance(s, bytes): return s.decode("UTF-8", "replace") else: return s def format_explanation(explanation): """This formats an explanation Normally all embedded newlines are escaped, however there are three exceptions: \n{, \n} and \n~. The first two are intended cover nested explanations, see function and attribute explanations for examples (.visit_Call(), visit_Attribute()). The last one is for when one explanation needs to span multiple lines, e.g. when displaying diffs. """ explanation = ecu(explanation) lines = _split_explanation(explanation) result = _format_lines(lines) return u"\n".join(result) def _split_explanation(explanation): """Return a list of individual lines in the explanation This will return a list of lines split on '\n{', '\n}' and '\n~'. Any other newlines will be escaped and appear in the line as the literal '\n' characters. """ raw_lines = (explanation or u"").split("\n") lines = [raw_lines[0]] for values in raw_lines[1:]: if values and values[0] in ["{", "}", "~", ">"]: lines.append(values) else: lines[-1] += "\\n" + values return lines def _format_lines(lines): """Format the individual lines This will replace the '{', '}' and '~' characters of our mini formatting language with the proper 'where ...', 'and ...' and ' + ...' text, taking care of indentation along the way. Return a list of formatted lines. """ result = lines[:1] stack = [0] stackcnt = [0] for line in lines[1:]: if line.startswith("{"): if stackcnt[-1]: s = u"and " else: s = u"where " stack.append(len(result)) stackcnt[-1] += 1 stackcnt.append(0) result.append(u" +" + u" " * (len(stack) - 1) + s + line[1:]) elif line.startswith("}"): stack.pop() stackcnt.pop() result[stack[-1]] += line[1:] else: assert line[0] in ["~", ">"] stack[-1] += 1 indent = len(stack) if line.startswith("~") else len(stack) - 1 result.append(u" " * indent + line[1:]) assert len(stack) == 1 return result # Provide basestring in python3 try: basestring = basestring except NameError: basestring = str def issequence(x): return isinstance(x, Sequence) and not isinstance(x, basestring) def istext(x): return isinstance(x, basestring) def isdict(x): return isinstance(x, dict) def isset(x): return isinstance(x, (set, frozenset)) def isdatacls(obj): return getattr(obj, "__dataclass_fields__", None) is not None def isattrs(obj): return getattr(obj, "__attrs_attrs__", None) is not None def isiterable(obj): try: iter(obj) return not istext(obj) except TypeError: return False def assertrepr_compare(config, op, left, right): """Return specialised explanations for some operators/operands""" width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op left_repr = saferepr(left, maxsize=int(width // 2)) right_repr = saferepr(right, maxsize=width - len(left_repr)) summary = u"%s %s %s" % (ecu(left_repr), op, ecu(right_repr)) verbose = config.getoption("verbose") explanation = None try: if op == "==": if istext(left) and istext(right): explanation = _diff_text(left, right, verbose) else: if issequence(left) and issequence(right): explanation = _compare_eq_sequence(left, right, verbose) elif isset(left) and isset(right): explanation = _compare_eq_set(left, right, verbose) elif isdict(left) and isdict(right): explanation = _compare_eq_dict(left, right, verbose) elif type(left) == type(right) and (isdatacls(left) or isattrs(left)): type_fn = (isdatacls, isattrs) explanation = _compare_eq_cls(left, right, verbose, type_fn) elif verbose > 0: explanation = _compare_eq_verbose(left, right) if isiterable(left) and isiterable(right): expl = _compare_eq_iterable(left, right, verbose) if explanation is not None: explanation.extend(expl) else: explanation = expl elif op == "not in": if istext(left) and istext(right): explanation = _notin_text(left, right, verbose) except outcomes.Exit: raise except Exception: explanation = [ u"(pytest_assertion plugin: representation of details failed. " u"Probably an object has a faulty __repr__.)", six.text_type(_pytest._code.ExceptionInfo.from_current()), ] if not explanation: return None return [summary] + explanation def _diff_text(left, right, verbose=0): """Return the explanation for the diff between text or bytes. Unless --verbose is used this will skip leading and trailing characters which are identical to keep the diff minimal. If the input are bytes they will be safely converted to text. """ from difflib import ndiff explanation = [] def escape_for_readable_diff(binary_text): """ Ensures that the internal string is always valid unicode, converting any bytes safely to valid unicode. This is done using repr() which then needs post-processing to fix the encompassing quotes and un-escape newlines and carriage returns (#429). """ r = six.text_type(repr(binary_text)[1:-1]) r = r.replace(r"\n", "\n") r = r.replace(r"\r", "\r") return r if isinstance(left, bytes): left = escape_for_readable_diff(left) if isinstance(right, bytes): right = escape_for_readable_diff(right) if verbose < 1: i = 0 # just in case left or right has zero length for i in range(min(len(left), len(right))): if left[i] != right[i]: break if i > 42: i -= 10 # Provide some context explanation = [ u"Skipping %s identical leading characters in diff, use -v to show" % i ] left = left[i:] right = right[i:] if len(left) == len(right): for i in range(len(left)): if left[-i] != right[-i]: break if i > 42: i -= 10 # Provide some context explanation += [ u"Skipping {} identical trailing " u"characters in diff, use -v to show".format(i) ] left = left[:-i] right = right[:-i] keepends = True if left.isspace() or right.isspace(): left = repr(str(left)) right = repr(str(right)) explanation += [u"Strings contain only whitespace, escaping them using repr()"] explanation += [ line.strip("\n") for line in ndiff(left.splitlines(keepends), right.splitlines(keepends)) ] return explanation def _compare_eq_verbose(left, right): keepends = True left_lines = repr(left).splitlines(keepends) right_lines = repr(right).splitlines(keepends) explanation = [] explanation += [u"-" + line for line in left_lines] explanation += [u"+" + line for line in right_lines] return explanation def _compare_eq_iterable(left, right, verbose=0): if not verbose: return [u"Use -v to get the full diff"] # dynamic import to speedup pytest import difflib try: left_formatting = pprint.pformat(left).splitlines() right_formatting = pprint.pformat(right).splitlines() explanation = [u"Full diff:"] except Exception: # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling # sorted() on a list would raise. See issue #718. # As a workaround, the full diff is generated by using the repr() string of each item of each container. left_formatting = sorted(repr(x) for x in left) right_formatting = sorted(repr(x) for x in right) explanation = [u"Full diff (fallback to calling repr on each item):"] explanation.extend( line.strip() for line in difflib.ndiff(left_formatting, right_formatting) ) return explanation def _compare_eq_sequence(left, right, verbose=0): explanation = [] len_left = len(left) len_right = len(right) for i in range(min(len_left, len_right)): if left[i] != right[i]: explanation += [u"At index %s diff: %r != %r" % (i, left[i], right[i])] break len_diff = len_left - len_right if len_diff: if len_diff > 0: dir_with_more = "Left" extra = saferepr(left[len_right]) else: len_diff = 0 - len_diff dir_with_more = "Right" extra = saferepr(right[len_left]) if len_diff == 1: explanation += [u"%s contains one more item: %s" % (dir_with_more, extra)] else: explanation += [ u"%s contains %d more items, first extra item: %s" % (dir_with_more, len_diff, extra) ] return explanation def _compare_eq_set(left, right, verbose=0): explanation = [] diff_left = left - right diff_right = right - left if diff_left: explanation.append(u"Extra items in the left set:") for item in diff_left: explanation.append(saferepr(item)) if diff_right: explanation.append(u"Extra items in the right set:") for item in diff_right: explanation.append(saferepr(item)) return explanation def _compare_eq_dict(left, right, verbose=0): explanation = [] set_left = set(left) set_right = set(right) common = set_left.intersection(set_right) same = {k: left[k] for k in common if left[k] == right[k]} if same and verbose < 2: explanation += [u"Omitting %s identical items, use -vv to show" % len(same)] elif same: explanation += [u"Common items:"] explanation += pprint.pformat(same).splitlines() diff = {k for k in common if left[k] != right[k]} if diff: explanation += [u"Differing items:"] for k in diff: explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})] extra_left = set_left - set_right len_extra_left = len(extra_left) if len_extra_left: explanation.append( u"Left contains %d more item%s:" % (len_extra_left, "" if len_extra_left == 1 else "s") ) explanation.extend( pprint.pformat({k: left[k] for k in extra_left}).splitlines() ) extra_right = set_right - set_left len_extra_right = len(extra_right) if len_extra_right: explanation.append( u"Right contains %d more item%s:" % (len_extra_right, "" if len_extra_right == 1 else "s") ) explanation.extend( pprint.pformat({k: right[k] for k in extra_right}).splitlines() ) return explanation def _compare_eq_cls(left, right, verbose, type_fns): isdatacls, isattrs = type_fns if isdatacls(left): all_fields = left.__dataclass_fields__ fields_to_check = [field for field, info in all_fields.items() if info.compare] elif isattrs(left): all_fields = left.__attrs_attrs__ fields_to_check = [ field.name for field in all_fields if getattr(field, ATTRS_EQ_FIELD) ] same = [] diff = [] for field in fields_to_check: if getattr(left, field) == getattr(right, field): same.append(field) else: diff.append(field) explanation = [] if same and verbose < 2: explanation.append(u"Omitting %s identical items, use -vv to show" % len(same)) elif same: explanation += [u"Matching attributes:"] explanation += pprint.pformat(same).splitlines() if diff: explanation += [u"Differing attributes:"] for field in diff: explanation += [ (u"%s: %r != %r") % (field, getattr(left, field), getattr(right, field)) ] return explanation def _notin_text(term, text, verbose=0): index = text.find(term) head = text[:index] tail = text[index + len(term) :] correct_text = head + tail diff = _diff_text(correct_text, text, verbose) newdiff = [u"%s is contained here:" % saferepr(term, maxsize=42)] for line in diff: if line.startswith(u"Skipping"): continue if line.startswith(u"- "): continue if line.startswith(u"+ "): newdiff.append(u" " + line[2:]) else: newdiff.append(line) return newdiff pytest-4.6.9/src/_pytest/assertion/truncate.py0000664000372000037200000000650713604173400022416 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Utilities for truncating assertion output. Current default behaviour is to truncate assertion explanations at ~8 terminal lines, unless running in "-vv" mode or running on CI. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import six DEFAULT_MAX_LINES = 8 DEFAULT_MAX_CHARS = 8 * 80 USAGE_MSG = "use '-vv' to show" def truncate_if_required(explanation, item, max_length=None): """ Truncate this assertion explanation if the given test item is eligible. """ if _should_truncate_item(item): return _truncate_explanation(explanation) return explanation def _should_truncate_item(item): """ Whether or not this test item is eligible for truncation. """ verbose = item.config.option.verbose return verbose < 2 and not _running_on_ci() def _running_on_ci(): """Check if we're currently running on a CI system.""" env_vars = ["CI", "BUILD_NUMBER"] return any(var in os.environ for var in env_vars) def _truncate_explanation(input_lines, max_lines=None, max_chars=None): """ Truncate given list of strings that makes up the assertion explanation. Truncates to either 8 lines, or 640 characters - whichever the input reaches first. The remaining lines will be replaced by a usage message. """ if max_lines is None: max_lines = DEFAULT_MAX_LINES if max_chars is None: max_chars = DEFAULT_MAX_CHARS # Check if truncation required input_char_count = len("".join(input_lines)) if len(input_lines) <= max_lines and input_char_count <= max_chars: return input_lines # Truncate first to max_lines, and then truncate to max_chars if max_chars # is exceeded. truncated_explanation = input_lines[:max_lines] truncated_explanation = _truncate_by_char_count(truncated_explanation, max_chars) # Add ellipsis to final line truncated_explanation[-1] = truncated_explanation[-1] + "..." # Append useful message to explanation truncated_line_count = len(input_lines) - len(truncated_explanation) truncated_line_count += 1 # Account for the part-truncated final line msg = "...Full output truncated" if truncated_line_count == 1: msg += " ({} line hidden)".format(truncated_line_count) else: msg += " ({} lines hidden)".format(truncated_line_count) msg += ", {}".format(USAGE_MSG) truncated_explanation.extend([six.text_type(""), six.text_type(msg)]) return truncated_explanation def _truncate_by_char_count(input_lines, max_chars): # Check if truncation required if len("".join(input_lines)) <= max_chars: return input_lines # Find point at which input length exceeds total allowed length iterated_char_count = 0 for iterated_index, input_line in enumerate(input_lines): if iterated_char_count + len(input_line) > max_chars: break iterated_char_count += len(input_line) # Create truncated explanation with modified final line truncated_result = input_lines[:iterated_index] final_line = input_lines[iterated_index] if final_line: final_line_truncate_point = max_chars - iterated_char_count final_line = final_line[:final_line_truncate_point] truncated_result.append(final_line) return truncated_result pytest-4.6.9/src/_pytest/assertion/rewrite.py0000664000372000037200000011743413604173400022254 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """Rewrite assertion AST to produce nice error messages""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import ast import errno import imp import itertools import marshal import os import re import string import struct import sys import types import atomicwrites import py import six from _pytest._io.saferepr import saferepr from _pytest.assertion import util from _pytest.assertion.util import ( # noqa: F401 format_explanation as _format_explanation, ) from _pytest.compat import spec_from_file_location from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import PurePath # pytest caches rewritten pycs in __pycache__. if hasattr(imp, "get_tag"): PYTEST_TAG = imp.get_tag() + "-PYTEST" else: if hasattr(sys, "pypy_version_info"): impl = "pypy" elif sys.platform == "java": impl = "jython" else: impl = "cpython" ver = sys.version_info PYTEST_TAG = "%s-%s%s-PYTEST" % (impl, ver[0], ver[1]) del ver, impl PYC_EXT = ".py" + (__debug__ and "c" or "o") PYC_TAIL = "." + PYTEST_TAG + PYC_EXT ASCII_IS_DEFAULT_ENCODING = sys.version_info[0] < 3 if sys.version_info >= (3, 5): ast_Call = ast.Call else: def ast_Call(a, b, c): return ast.Call(a, b, c, None, None) class AssertionRewritingHook(object): """PEP302 Import hook which rewrites asserts.""" def __init__(self, config): self.config = config try: self.fnpats = config.getini("python_files") except ValueError: self.fnpats = ["test_*.py", "*_test.py"] self.session = None self.modules = {} self._rewritten_names = set() self._must_rewrite = set() # flag to guard against trying to rewrite a pyc file while we are already writing another pyc file, # which might result in infinite recursion (#3506) self._writing_pyc = False self._basenames_to_check_rewrite = {"conftest"} self._marked_for_rewrite_cache = {} self._session_paths_checked = False def set_session(self, session): self.session = session self._session_paths_checked = False def _imp_find_module(self, name, path=None): """Indirection so we can mock calls to find_module originated from the hook during testing""" return imp.find_module(name, path) def find_module(self, name, path=None): if self._writing_pyc: return None state = self.config._assertstate if self._early_rewrite_bailout(name, state): return None state.trace("find_module called for: %s" % name) names = name.rsplit(".", 1) lastname = names[-1] pth = None if path is not None: # Starting with Python 3.3, path is a _NamespacePath(), which # causes problems if not converted to list. path = list(path) if len(path) == 1: pth = path[0] if pth is None: try: fd, fn, desc = self._imp_find_module(lastname, path) except ImportError: return None if fd is not None: fd.close() tp = desc[2] if tp == imp.PY_COMPILED: if hasattr(imp, "source_from_cache"): try: fn = imp.source_from_cache(fn) except ValueError: # Python 3 doesn't like orphaned but still-importable # .pyc files. fn = fn[:-1] else: fn = fn[:-1] elif tp != imp.PY_SOURCE: # Don't know what this is. return None else: fn = os.path.join(pth, name.rpartition(".")[2] + ".py") fn_pypath = py.path.local(fn) if not self._should_rewrite(name, fn_pypath, state): return None self._rewritten_names.add(name) # The requested module looks like a test file, so rewrite it. This is # the most magical part of the process: load the source, rewrite the # asserts, and load the rewritten source. We also cache the rewritten # module code in a special pyc. We must be aware of the possibility of # concurrent pytest processes rewriting and loading pycs. To avoid # tricky race conditions, we maintain the following invariant: The # cached pyc is always a complete, valid pyc. Operations on it must be # atomic. POSIX's atomic rename comes in handy. write = not sys.dont_write_bytecode cache_dir = os.path.join(fn_pypath.dirname, "__pycache__") if write: try: os.mkdir(cache_dir) except OSError: e = sys.exc_info()[1].errno if e == errno.EEXIST: # Either the __pycache__ directory already exists (the # common case) or it's blocked by a non-dir node. In the # latter case, we'll ignore it in _write_pyc. pass elif e in [errno.ENOENT, errno.ENOTDIR]: # One of the path components was not a directory, likely # because we're in a zip file. write = False elif e in [errno.EACCES, errno.EROFS, errno.EPERM]: state.trace("read only directory: %r" % fn_pypath.dirname) write = False else: raise cache_name = fn_pypath.basename[:-3] + PYC_TAIL pyc = os.path.join(cache_dir, cache_name) # Notice that even if we're in a read-only directory, I'm going # to check for a cached pyc. This may not be optimal... co = _read_pyc(fn_pypath, pyc, state.trace) if co is None: state.trace("rewriting %r" % (fn,)) source_stat, co = _rewrite_test(self.config, fn_pypath) if co is None: # Probably a SyntaxError in the test. return None if write: self._writing_pyc = True try: _write_pyc(state, co, source_stat, pyc) finally: self._writing_pyc = False else: state.trace("found cached rewritten pyc for %r" % (fn,)) self.modules[name] = co, pyc return self def _early_rewrite_bailout(self, name, state): """ This is a fast way to get out of rewriting modules. Profiling has shown that the call to imp.find_module (inside of the find_module from this class) is a major slowdown, so, this method tries to filter what we're sure won't be rewritten before getting to it. """ if self.session is not None and not self._session_paths_checked: self._session_paths_checked = True for path in self.session._initialpaths: # Make something as c:/projects/my_project/path.py -> # ['c:', 'projects', 'my_project', 'path.py'] parts = str(path).split(os.path.sep) # add 'path' to basenames to be checked. self._basenames_to_check_rewrite.add(os.path.splitext(parts[-1])[0]) # Note: conftest already by default in _basenames_to_check_rewrite. parts = name.split(".") if parts[-1] in self._basenames_to_check_rewrite: return False # For matching the name it must be as if it was a filename. path = PurePath(os.path.sep.join(parts) + ".py") for pat in self.fnpats: # if the pattern contains subdirectories ("tests/**.py" for example) we can't bail out based # on the name alone because we need to match against the full path if os.path.dirname(pat): return False if fnmatch_ex(pat, path): return False if self._is_marked_for_rewrite(name, state): return False state.trace("early skip of rewriting module: %s" % (name,)) return True def _should_rewrite(self, name, fn_pypath, state): # always rewrite conftest files fn = str(fn_pypath) if fn_pypath.basename == "conftest.py": state.trace("rewriting conftest file: %r" % (fn,)) return True if self.session is not None: if self.session.isinitpath(fn): state.trace("matched test file (was specified on cmdline): %r" % (fn,)) return True # modules not passed explicitly on the command line are only # rewritten if they match the naming convention for test files for pat in self.fnpats: if fn_pypath.fnmatch(pat): state.trace("matched test file %r" % (fn,)) return True return self._is_marked_for_rewrite(name, state) def _is_marked_for_rewrite(self, name, state): try: return self._marked_for_rewrite_cache[name] except KeyError: for marked in self._must_rewrite: if name == marked or name.startswith(marked + "."): state.trace("matched marked file %r (from %r)" % (name, marked)) self._marked_for_rewrite_cache[name] = True return True self._marked_for_rewrite_cache[name] = False return False def mark_rewrite(self, *names): """Mark import names as needing to be rewritten. The named module or package as well as any nested modules will be rewritten on import. """ already_imported = ( set(names).intersection(sys.modules).difference(self._rewritten_names) ) for name in already_imported: if not AssertionRewriter.is_rewrite_disabled( sys.modules[name].__doc__ or "" ): self._warn_already_imported(name) self._must_rewrite.update(names) self._marked_for_rewrite_cache.clear() def _warn_already_imported(self, name): from _pytest.warning_types import PytestAssertRewriteWarning from _pytest.warnings import _issue_warning_captured _issue_warning_captured( PytestAssertRewriteWarning( "Module already imported so cannot be rewritten: %s" % name ), self.config.hook, stacklevel=5, ) def load_module(self, name): co, pyc = self.modules.pop(name) if name in sys.modules: # If there is an existing module object named 'fullname' in # sys.modules, the loader must use that existing module. (Otherwise, # the reload() builtin will not work correctly.) mod = sys.modules[name] else: # I wish I could just call imp.load_compiled here, but __file__ has to # be set properly. In Python 3.2+, this all would be handled correctly # by load_compiled. mod = sys.modules[name] = imp.new_module(name) try: mod.__file__ = co.co_filename # Normally, this attribute is 3.2+. mod.__cached__ = pyc mod.__loader__ = self # Normally, this attribute is 3.4+ mod.__spec__ = spec_from_file_location(name, co.co_filename, loader=self) exec(co, mod.__dict__) except: # noqa if name in sys.modules: del sys.modules[name] raise return sys.modules[name] def is_package(self, name): try: fd, fn, desc = self._imp_find_module(name) except ImportError: return False if fd is not None: fd.close() tp = desc[2] return tp == imp.PKG_DIRECTORY def get_data(self, pathname): """Optional PEP302 get_data API. """ with open(pathname, "rb") as f: return f.read() def _write_pyc(state, co, source_stat, pyc): # Technically, we don't have to have the same pyc format as # (C)Python, since these "pycs" should never be seen by builtin # import. However, there's little reason deviate, and I hope # sometime to be able to use imp.load_compiled to load them. (See # the comment in load_module above.) try: with atomicwrites.atomic_write(pyc, mode="wb", overwrite=True) as fp: fp.write(imp.get_magic()) # as of now, bytecode header expects 32-bit numbers for size and mtime (#4903) mtime = int(source_stat.mtime) & 0xFFFFFFFF size = source_stat.size & 0xFFFFFFFF # ">", ast.Add: "+", ast.Sub: "-", ast.Mult: "*", ast.Div: "/", ast.FloorDiv: "//", ast.Mod: "%%", # escaped for string formatting ast.Eq: "==", ast.NotEq: "!=", ast.Lt: "<", ast.LtE: "<=", ast.Gt: ">", ast.GtE: ">=", ast.Pow: "**", ast.Is: "is", ast.IsNot: "is not", ast.In: "in", ast.NotIn: "not in", } # Python 3.5+ compatibility try: binop_map[ast.MatMult] = "@" except AttributeError: pass # Python 3.4+ compatibility if hasattr(ast, "NameConstant"): _NameConstant = ast.NameConstant else: def _NameConstant(c): return ast.Name(str(c), ast.Load()) def set_location(node, lineno, col_offset): """Set node location information recursively.""" def _fix(node, lineno, col_offset): if "lineno" in node._attributes: node.lineno = lineno if "col_offset" in node._attributes: node.col_offset = col_offset for child in ast.iter_child_nodes(node): _fix(child, lineno, col_offset) _fix(node, lineno, col_offset) return node class AssertionRewriter(ast.NodeVisitor): """Assertion rewriting implementation. The main entrypoint is to call .run() with an ast.Module instance, this will then find all the assert statements and rewrite them to provide intermediate values and a detailed assertion error. See http://pybites.blogspot.be/2011/07/behind-scenes-of-pytests-new-assertion.html for an overview of how this works. The entry point here is .run() which will iterate over all the statements in an ast.Module and for each ast.Assert statement it finds call .visit() with it. Then .visit_Assert() takes over and is responsible for creating new ast statements to replace the original assert statement: it rewrites the test of an assertion to provide intermediate values and replace it with an if statement which raises an assertion error with a detailed explanation in case the expression is false. For this .visit_Assert() uses the visitor pattern to visit all the AST nodes of the ast.Assert.test field, each visit call returning an AST node and the corresponding explanation string. During this state is kept in several instance attributes: :statements: All the AST statements which will replace the assert statement. :variables: This is populated by .variable() with each variable used by the statements so that they can all be set to None at the end of the statements. :variable_counter: Counter to create new unique variables needed by statements. Variables are created using .variable() and have the form of "@py_assert0". :on_failure: The AST statements which will be executed if the assertion test fails. This is the code which will construct the failure message and raises the AssertionError. :explanation_specifiers: A dict filled by .explanation_param() with %-formatting placeholders and their corresponding expressions to use in the building of an assertion message. This is used by .pop_format_context() to build a message. :stack: A stack of the explanation_specifiers dicts maintained by .push_format_context() and .pop_format_context() which allows to build another %-formatted string while already building one. This state is reset on every new assert statement visited and used by the other visitors. """ def __init__(self, module_path, config): super(AssertionRewriter, self).__init__() self.module_path = module_path self.config = config def run(self, mod): """Find all assert statements in *mod* and rewrite them.""" if not mod.body: # Nothing to do. return # Insert some special imports at the top of the module but after any # docstrings and __future__ imports. aliases = [ ast.alias(six.moves.builtins.__name__, "@py_builtins"), ast.alias("_pytest.assertion.rewrite", "@pytest_ar"), ] doc = getattr(mod, "docstring", None) expect_docstring = doc is None if doc is not None and self.is_rewrite_disabled(doc): return pos = 0 lineno = 1 for item in mod.body: if ( expect_docstring and isinstance(item, ast.Expr) and isinstance(item.value, ast.Str) ): doc = item.value.s if self.is_rewrite_disabled(doc): return expect_docstring = False elif ( not isinstance(item, ast.ImportFrom) or item.level > 0 or item.module != "__future__" ): lineno = item.lineno break pos += 1 else: lineno = item.lineno imports = [ ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases ] mod.body[pos:pos] = imports # Collect asserts. nodes = [mod] while nodes: node = nodes.pop() for name, field in ast.iter_fields(node): if isinstance(field, list): new = [] for i, child in enumerate(field): if isinstance(child, ast.Assert): # Transform assert. new.extend(self.visit(child)) else: new.append(child) if isinstance(child, ast.AST): nodes.append(child) setattr(node, name, new) elif ( isinstance(field, ast.AST) # Don't recurse into expressions as they can't contain # asserts. and not isinstance(field, ast.expr) ): nodes.append(field) @staticmethod def is_rewrite_disabled(docstring): return "PYTEST_DONT_REWRITE" in docstring def variable(self): """Get a new variable.""" # Use a character invalid in python identifiers to avoid clashing. name = "@py_assert" + str(next(self.variable_counter)) self.variables.append(name) return name def assign(self, expr): """Give *expr* a name.""" name = self.variable() self.statements.append(ast.Assign([ast.Name(name, ast.Store())], expr)) return ast.Name(name, ast.Load()) def display(self, expr): """Call saferepr on the expression.""" return self.helper("_saferepr", expr) def helper(self, name, *args): """Call a helper in this module.""" py_name = ast.Name("@pytest_ar", ast.Load()) attr = ast.Attribute(py_name, name, ast.Load()) return ast_Call(attr, list(args), []) def builtin(self, name): """Return the builtin called *name*.""" builtin_name = ast.Name("@py_builtins", ast.Load()) return ast.Attribute(builtin_name, name, ast.Load()) def explanation_param(self, expr): """Return a new named %-formatting placeholder for expr. This creates a %-formatting placeholder for expr in the current formatting context, e.g. ``%(py0)s``. The placeholder and expr are placed in the current format context so that it can be used on the next call to .pop_format_context(). """ specifier = "py" + str(next(self.variable_counter)) self.explanation_specifiers[specifier] = expr return "%(" + specifier + ")s" def push_format_context(self): """Create a new formatting context. The format context is used for when an explanation wants to have a variable value formatted in the assertion message. In this case the value required can be added using .explanation_param(). Finally .pop_format_context() is used to format a string of %-formatted values as added by .explanation_param(). """ self.explanation_specifiers = {} self.stack.append(self.explanation_specifiers) def pop_format_context(self, expl_expr): """Format the %-formatted string with current format context. The expl_expr should be an ast.Str instance constructed from the %-placeholders created by .explanation_param(). This will add the required code to format said string to .on_failure and return the ast.Name instance of the formatted string. """ current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] keys = [ast.Str(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) self.on_failure.append(ast.Assign([ast.Name(name, ast.Store())], form)) return ast.Name(name, ast.Load()) def generic_visit(self, node): """Handle expressions we don't have custom code for.""" assert isinstance(node, ast.expr) res = self.assign(node) return res, self.explanation_param(self.display(res)) def visit_Assert(self, assert_): """Return the AST statements to replace the ast.Assert instance. This rewrites the test of an assertion to provide intermediate values and replace it with an if statement which raises an assertion error with a detailed explanation in case the expression is false. """ if isinstance(assert_.test, ast.Tuple) and len(assert_.test.elts) >= 1: from _pytest.warning_types import PytestAssertRewriteWarning import warnings warnings.warn_explicit( PytestAssertRewriteWarning( "assertion is always true, perhaps remove parentheses?" ), category=None, filename=str(self.module_path), lineno=assert_.lineno, ) self.statements = [] self.variables = [] self.variable_counter = itertools.count() self.stack = [] self.on_failure = [] self.push_format_context() # Rewrite assert into a bunch of statements. top_condition, explanation = self.visit(assert_.test) # If in a test module, check if directly asserting None, in order to warn [Issue #3191] if self.module_path is not None: self.statements.append( self.warn_about_none_ast( top_condition, module_path=self.module_path, lineno=assert_.lineno ) ) # Create failure message. body = self.on_failure negation = ast.UnaryOp(ast.Not(), top_condition) self.statements.append(ast.If(negation, body, [])) if assert_.msg: assertmsg = self.helper("_format_assertmsg", assert_.msg) explanation = "\n>assert " + explanation else: assertmsg = ast.Str("") explanation = "assert " + explanation template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation)) msg = self.pop_format_context(template) fmt = self.helper("_format_explanation", msg) err_name = ast.Name("AssertionError", ast.Load()) exc = ast_Call(err_name, [fmt], []) if sys.version_info[0] >= 3: raise_ = ast.Raise(exc, None) else: raise_ = ast.Raise(exc, None, None) body.append(raise_) # Clear temporary variables by setting them to None. if self.variables: variables = [ast.Name(name, ast.Store()) for name in self.variables] clear = ast.Assign(variables, _NameConstant(None)) self.statements.append(clear) # Fix line numbers. for stmt in self.statements: set_location(stmt, assert_.lineno, assert_.col_offset) return self.statements def warn_about_none_ast(self, node, module_path, lineno): """ Returns an AST issuing a warning if the value of node is `None`. This is used to warn the user when asserting a function that asserts internally already. See issue #3191 for more details. """ # Using parse because it is different between py2 and py3. AST_NONE = ast.parse("None").body[0].value val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE]) send_warning = ast.parse( """ from _pytest.warning_types import PytestAssertRewriteWarning from warnings import warn_explicit warn_explicit( PytestAssertRewriteWarning('asserting the value None, please use "assert is None"'), category=None, filename={filename!r}, lineno={lineno}, ) """.format( filename=module_path.strpath, lineno=lineno ) ).body return ast.If(val_is_none, send_warning, []) def visit_Name(self, name): # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable. locs = ast_Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) dorepr = self.helper("_should_repr_global_name", name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr) def visit_BoolOp(self, boolop): res_var = self.variable() expl_list = self.assign(ast.List([], ast.Load())) app = ast.Attribute(expl_list, "append", ast.Load()) is_or = int(isinstance(boolop.op, ast.Or)) body = save = self.statements fail_save = self.on_failure levels = len(boolop.values) - 1 self.push_format_context() # Process each operand, short-circuting if needed. for i, v in enumerate(boolop.values): if i: fail_inner = [] # cond is set in a prior loop iteration below self.on_failure.append(ast.If(cond, fail_inner, [])) # noqa self.on_failure = fail_inner self.push_format_context() res, expl = self.visit(v) body.append(ast.Assign([ast.Name(res_var, ast.Store())], res)) expl_format = self.pop_format_context(ast.Str(expl)) call = ast_Call(app, [expl_format], []) self.on_failure.append(ast.Expr(call)) if i < levels: cond = res if is_or: cond = ast.UnaryOp(ast.Not(), cond) inner = [] self.statements.append(ast.If(cond, inner, [])) self.statements = body = inner self.statements = save self.on_failure = fail_save expl_template = self.helper("_format_boolop", expl_list, ast.Num(is_or)) expl = self.pop_format_context(expl_template) return ast.Name(res_var, ast.Load()), self.explanation_param(expl) def visit_UnaryOp(self, unary): pattern = unary_map[unary.op.__class__] operand_res, operand_expl = self.visit(unary.operand) res = self.assign(ast.UnaryOp(unary.op, operand_res)) return res, pattern % (operand_expl,) def visit_BinOp(self, binop): symbol = binop_map[binop.op.__class__] left_expr, left_expl = self.visit(binop.left) right_expr, right_expl = self.visit(binop.right) explanation = "(%s %s %s)" % (left_expl, symbol, right_expl) res = self.assign(ast.BinOp(left_expr, binop.op, right_expr)) return res, explanation def visit_Call_35(self, call): """ visit `ast.Call` nodes on Python3.5 and after """ new_func, func_expl = self.visit(call.func) arg_expls = [] new_args = [] new_kwargs = [] for arg in call.args: res, expl = self.visit(arg) arg_expls.append(expl) new_args.append(res) for keyword in call.keywords: res, expl = self.visit(keyword.value) new_kwargs.append(ast.keyword(keyword.arg, res)) if keyword.arg: arg_expls.append(keyword.arg + "=" + expl) else: # **args have `arg` keywords with an .arg of None arg_expls.append("**" + expl) expl = "%s(%s)" % (func_expl, ", ".join(arg_expls)) new_call = ast.Call(new_func, new_args, new_kwargs) res = self.assign(new_call) res_expl = self.explanation_param(self.display(res)) outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl) return res, outer_expl def visit_Starred(self, starred): # From Python 3.5, a Starred node can appear in a function call res, expl = self.visit(starred.value) new_starred = ast.Starred(res, starred.ctx) return new_starred, "*" + expl def visit_Call_legacy(self, call): """ visit `ast.Call nodes on 3.4 and below` """ new_func, func_expl = self.visit(call.func) arg_expls = [] new_args = [] new_kwargs = [] new_star = new_kwarg = None for arg in call.args: res, expl = self.visit(arg) new_args.append(res) arg_expls.append(expl) for keyword in call.keywords: res, expl = self.visit(keyword.value) new_kwargs.append(ast.keyword(keyword.arg, res)) arg_expls.append(keyword.arg + "=" + expl) if call.starargs: new_star, expl = self.visit(call.starargs) arg_expls.append("*" + expl) if call.kwargs: new_kwarg, expl = self.visit(call.kwargs) arg_expls.append("**" + expl) expl = "%s(%s)" % (func_expl, ", ".join(arg_expls)) new_call = ast.Call(new_func, new_args, new_kwargs, new_star, new_kwarg) res = self.assign(new_call) res_expl = self.explanation_param(self.display(res)) outer_expl = "%s\n{%s = %s\n}" % (res_expl, res_expl, expl) return res, outer_expl # ast.Call signature changed on 3.5, # conditionally change which methods is named # visit_Call depending on Python version if sys.version_info >= (3, 5): visit_Call = visit_Call_35 else: visit_Call = visit_Call_legacy def visit_Attribute(self, attr): if not isinstance(attr.ctx, ast.Load): return self.generic_visit(attr) value, value_expl = self.visit(attr.value) res = self.assign(ast.Attribute(value, attr.attr, ast.Load())) res_expl = self.explanation_param(self.display(res)) pat = "%s\n{%s = %s.%s\n}" expl = pat % (res_expl, res_expl, value_expl, attr.attr) return res, expl def visit_Compare(self, comp): self.push_format_context() left_res, left_expl = self.visit(comp.left) if isinstance(comp.left, (ast.Compare, ast.BoolOp)): left_expl = "({})".format(left_expl) res_variables = [self.variable() for i in range(len(comp.ops))] load_names = [ast.Name(v, ast.Load()) for v in res_variables] store_names = [ast.Name(v, ast.Store()) for v in res_variables] it = zip(range(len(comp.ops)), comp.ops, comp.comparators) expls = [] syms = [] results = [left_res] for i, op, next_operand in it: next_res, next_expl = self.visit(next_operand) if isinstance(next_operand, (ast.Compare, ast.BoolOp)): next_expl = "({})".format(next_expl) results.append(next_res) sym = binop_map[op.__class__] syms.append(ast.Str(sym)) expl = "%s %s %s" % (left_expl, sym, next_expl) expls.append(ast.Str(expl)) res_expr = ast.Compare(left_res, [op], [next_res]) self.statements.append(ast.Assign([store_names[i]], res_expr)) left_res, left_expl = next_res, next_expl # Use pytest.assertion.util._reprcompare if that's available. expl_call = self.helper( "_call_reprcompare", ast.Tuple(syms, ast.Load()), ast.Tuple(load_names, ast.Load()), ast.Tuple(expls, ast.Load()), ast.Tuple(results, ast.Load()), ) if len(comp.ops) > 1: res = ast.BoolOp(ast.And(), load_names) else: res = load_names[0] return res, self.explanation_param(self.pop_format_context(expl_call)) pytest-4.6.9/src/_pytest/assertion/__init__.py0000664000372000037200000001211313604173400022316 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ support for presenting detailed information in failing assertions. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import six from _pytest.assertion import rewrite from _pytest.assertion import truncate from _pytest.assertion import util def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption( "--assert", action="store", dest="assertmode", choices=("rewrite", "plain"), default="rewrite", metavar="MODE", help="""Control assertion debugging tools. 'plain' performs no assertion debugging. 'rewrite' (the default) rewrites assert statements in test modules on import to provide assert expression information.""", ) def register_assert_rewrite(*names): """Register one or more module names to be rewritten on import. This function will make sure that this module or all modules inside the package will get their assert statements rewritten. Thus you should make sure to call this before the module is actually imported, usually in your __init__.py if you are a plugin using a package. :raise TypeError: if the given module names are not strings. """ for name in names: if not isinstance(name, str): msg = "expected module names as *args, got {0} instead" raise TypeError(msg.format(repr(names))) for hook in sys.meta_path: if isinstance(hook, rewrite.AssertionRewritingHook): importhook = hook break else: importhook = DummyRewriteHook() importhook.mark_rewrite(*names) class DummyRewriteHook(object): """A no-op import hook for when rewriting is disabled.""" def mark_rewrite(self, *names): pass class AssertionState(object): """State for the assertion plugin.""" def __init__(self, config, mode): self.mode = mode self.trace = config.trace.root.get("assertion") self.hook = None def install_importhook(config): """Try to install the rewrite hook, raise SystemError if it fails.""" # Jython has an AST bug that make the assertion rewriting hook malfunction. if sys.platform.startswith("java"): raise SystemError("rewrite not supported") config._assertstate = AssertionState(config, "rewrite") config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config) sys.meta_path.insert(0, hook) config._assertstate.trace("installed rewrite import hook") def undo(): hook = config._assertstate.hook if hook is not None and hook in sys.meta_path: sys.meta_path.remove(hook) config.add_cleanup(undo) return hook def pytest_collection(session): # this hook is only called when test modules are collected # so for example not in the master process of pytest-xdist # (which does not collect test modules) assertstate = getattr(session.config, "_assertstate", None) if assertstate: if assertstate.hook is not None: assertstate.hook.set_session(session) def pytest_runtest_setup(item): """Setup the pytest_assertrepr_compare hook The newinterpret and rewrite modules will use util._reprcompare if it exists to use custom reporting via the pytest_assertrepr_compare hook. This sets up this custom comparison for the test. """ def callbinrepr(op, left, right): """Call the pytest_assertrepr_compare hook and prepare the result This uses the first result from the hook and then ensures the following: * Overly verbose explanations are truncated unless configured otherwise (eg. if running in verbose mode). * Embedded newlines are escaped to help util.format_explanation() later. * If the rewrite mode is used embedded %-characters are replaced to protect later % formatting. The result can be formatted by util.format_explanation() for pretty printing. """ hook_result = item.ihook.pytest_assertrepr_compare( config=item.config, op=op, left=left, right=right ) for new_expl in hook_result: if new_expl: new_expl = truncate.truncate_if_required(new_expl, item) new_expl = [line.replace("\n", "\\n") for line in new_expl] res = six.text_type("\n~").join(new_expl) if item.config.getvalue("assertmode") == "rewrite": res = res.replace("%", "%%") return res util._reprcompare = callbinrepr def pytest_runtest_teardown(item): util._reprcompare = None def pytest_sessionfinish(session): assertstate = getattr(session.config, "_assertstate", None) if assertstate: if assertstate.hook is not None: assertstate.hook.set_session(None) # Expose this plugin's implementation for the pytest_assertrepr_compare hook pytest_assertrepr_compare = util.assertrepr_compare pytest-4.6.9/src/_pytest/helpconfig.py0000664000372000037200000001712313604173400020674 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ version info, help messages, tracing configuration. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys from argparse import Action import py import pytest from _pytest.config import PrintHelp class HelpAction(Action): """This is an argparse Action that will raise an exception in order to skip the rest of the argument parsing when --help is passed. This prevents argparse from quitting due to missing required arguments when any are defined, for example by ``pytest_addoption``. This is similar to the way that the builtin argparse --help option is implemented by raising SystemExit. """ def __init__(self, option_strings, dest=None, default=False, help=None): super(HelpAction, self).__init__( option_strings=option_strings, dest=dest, const=True, default=default, nargs=0, help=help, ) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, self.const) # We should only skip the rest of the parsing after preparse is done if getattr(parser._parser, "after_preparse", False): raise PrintHelp def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption( "--version", action="store_true", help="display pytest lib version and import information.", ) group._addoption( "-h", "--help", action=HelpAction, dest="help", help="show help message and configuration info", ) group._addoption( "-p", action="append", dest="plugins", default=[], metavar="name", help="early-load given plugin module name or entry point (multi-allowed). " "To avoid loading of plugins, use the `no:` prefix, e.g. " "`no:doctest`.", ) group.addoption( "--traceconfig", "--trace-config", action="store_true", default=False, help="trace considerations of conftest.py files.", ), group.addoption( "--debug", action="store_true", dest="debug", default=False, help="store internal tracing debug information in 'pytestdebug.log'.", ) group._addoption( "-o", "--override-ini", dest="override_ini", action="append", help='override ini option with "option=value" style, e.g. `-o xfail_strict=True -o cache_dir=cache`.', ) @pytest.hookimpl(hookwrapper=True) def pytest_cmdline_parse(): outcome = yield config = outcome.get_result() if config.option.debug: path = os.path.abspath("pytestdebug.log") debugfile = open(path, "w") debugfile.write( "versions pytest-%s, py-%s, " "python-%s\ncwd=%s\nargs=%s\n\n" % ( pytest.__version__, py.__version__, ".".join(map(str, sys.version_info)), os.getcwd(), config._origargs, ) ) config.trace.root.setwriter(debugfile.write) undo_tracing = config.pluginmanager.enable_tracing() sys.stderr.write("writing pytestdebug information to %s\n" % path) def unset_tracing(): debugfile.close() sys.stderr.write("wrote pytestdebug information to %s\n" % debugfile.name) config.trace.root.setwriter(None) undo_tracing() config.add_cleanup(unset_tracing) def showversion(config): p = py.path.local(pytest.__file__) sys.stderr.write( "This is pytest version %s, imported from %s\n" % (pytest.__version__, p) ) plugininfo = getpluginversioninfo(config) if plugininfo: for line in plugininfo: sys.stderr.write(line + "\n") def pytest_cmdline_main(config): if config.option.version: showversion(config) return 0 elif config.option.help: config._do_configure() showhelp(config) config._ensure_unconfigure() return 0 def showhelp(config): import textwrap reporter = config.pluginmanager.get_plugin("terminalreporter") tw = reporter._tw tw.write(config._parser.optparser.format_help()) tw.line() tw.line( "[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:" ) tw.line() columns = tw.fullwidth # costly call indent_len = 24 # based on argparse's max_help_position=24 indent = " " * indent_len for name in config._parser._ininames: help, type, default = config._parser._inidict[name] if type is None: type = "string" spec = "%s (%s):" % (name, type) tw.write(" %s" % spec) spec_len = len(spec) if spec_len > (indent_len - 3): # Display help starting at a new line. tw.line() helplines = textwrap.wrap( help, columns, initial_indent=indent, subsequent_indent=indent, break_on_hyphens=False, ) for line in helplines: tw.line(line) else: # Display help starting after the spec, following lines indented. tw.write(" " * (indent_len - spec_len - 2)) wrapped = textwrap.wrap(help, columns - indent_len, break_on_hyphens=False) tw.line(wrapped[0]) for line in wrapped[1:]: tw.line(indent + line) tw.line() tw.line("environment variables:") vars = [ ("PYTEST_ADDOPTS", "extra command line options"), ("PYTEST_PLUGINS", "comma-separated plugins to load during startup"), ("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "set to disable plugin auto-loading"), ("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals"), ] for name, help in vars: tw.line(" %-24s %s" % (name, help)) tw.line() tw.line() tw.line("to see available markers type: pytest --markers") tw.line("to see available fixtures type: pytest --fixtures") tw.line( "(shown according to specified file_or_dir or current dir " "if not specified; fixtures with leading '_' are only shown " "with the '-v' option" ) for warningreport in reporter.stats.get("warnings", []): tw.line("warning : " + warningreport.message, red=True) return conftest_options = [("pytest_plugins", "list of plugin names to load")] def getpluginversioninfo(config): lines = [] plugininfo = config.pluginmanager.list_plugin_distinfo() if plugininfo: lines.append("setuptools registered plugins:") for plugin, dist in plugininfo: loc = getattr(plugin, "__file__", repr(plugin)) content = "%s-%s at %s" % (dist.project_name, dist.version, loc) lines.append(" " + content) return lines def pytest_report_header(config): lines = [] if config.option.debug or config.option.traceconfig: lines.append("using: pytest-%s pylib-%s" % (pytest.__version__, py.__version__)) verinfo = getpluginversioninfo(config) if verinfo: lines.extend(verinfo) if config.option.traceconfig: lines.append("active plugins:") items = config.pluginmanager.list_name_plugin() for name, plugin in items: if hasattr(plugin, "__file__"): r = plugin.__file__ else: r = repr(plugin) lines.append(" %-20s: %s" % (name, r)) return lines pytest-4.6.9/src/_pytest/junitxml.py0000664000372000037200000005655013604173400020437 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ report test results in JUnit-XML format, for use with Jenkins and build integration servers. Based on initial code from Ross Lawley. Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/ src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import os import platform import re import sys import time from datetime import datetime import py import six import pytest from _pytest import nodes from _pytest.config import filename_arg # Python 2.X and 3.X compatibility if sys.version_info[0] < 3: from codecs import open class Junit(py.xml.Namespace): pass # We need to get the subset of the invalid unicode ranges according to # XML 1.0 which are valid in this python build. Hence we calculate # this dynamically instead of hardcoding it. The spec range of valid # chars is: Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] # | [#x10000-#x10FFFF] _legal_chars = (0x09, 0x0A, 0x0D) _legal_ranges = ((0x20, 0x7E), (0x80, 0xD7FF), (0xE000, 0xFFFD), (0x10000, 0x10FFFF)) _legal_xml_re = [ u"%s-%s" % (six.unichr(low), six.unichr(high)) for (low, high) in _legal_ranges if low < sys.maxunicode ] _legal_xml_re = [six.unichr(x) for x in _legal_chars] + _legal_xml_re illegal_xml_re = re.compile(u"[^%s]" % u"".join(_legal_xml_re)) del _legal_chars del _legal_ranges del _legal_xml_re _py_ext_re = re.compile(r"\.py$") def bin_xml_escape(arg): def repl(matchobj): i = ord(matchobj.group()) if i <= 0xFF: return u"#x%02X" % i else: return u"#x%04X" % i return py.xml.raw(illegal_xml_re.sub(repl, py.xml.escape(arg))) def merge_family(left, right): result = {} for kl, vl in left.items(): for kr, vr in right.items(): if not isinstance(vl, list): raise TypeError(type(vl)) result[kl] = vl + vr left.update(result) families = {} families["_base"] = {"testcase": ["classname", "name"]} families["_base_legacy"] = {"testcase": ["file", "line", "url"]} # xUnit 1.x inherits legacy attributes families["xunit1"] = families["_base"].copy() merge_family(families["xunit1"], families["_base_legacy"]) # xUnit 2.x uses strict base attributes families["xunit2"] = families["_base"] class _NodeReporter(object): def __init__(self, nodeid, xml): self.id = nodeid self.xml = xml self.add_stats = self.xml.add_stats self.family = self.xml.family self.duration = 0 self.properties = [] self.nodes = [] self.testcase = None self.attrs = {} def append(self, node): self.xml.add_stats(type(node).__name__) self.nodes.append(node) def add_property(self, name, value): self.properties.append((str(name), bin_xml_escape(value))) def add_attribute(self, name, value): self.attrs[str(name)] = bin_xml_escape(value) def make_properties_node(self): """Return a Junit node containing custom properties, if any. """ if self.properties: return Junit.properties( [ Junit.property(name=name, value=value) for name, value in self.properties ] ) return "" def record_testreport(self, testreport): assert not self.testcase names = mangle_test_address(testreport.nodeid) existing_attrs = self.attrs classnames = names[:-1] if self.xml.prefix: classnames.insert(0, self.xml.prefix) attrs = { "classname": ".".join(classnames), "name": bin_xml_escape(names[-1]), "file": testreport.location[0], } if testreport.location[1] is not None: attrs["line"] = testreport.location[1] if hasattr(testreport, "url"): attrs["url"] = testreport.url self.attrs = attrs self.attrs.update(existing_attrs) # restore any user-defined attributes # Preserve legacy testcase behavior if self.family == "xunit1": return # Filter out attributes not permitted by this test family. # Including custom attributes because they are not valid here. temp_attrs = {} for key in self.attrs.keys(): if key in families[self.family]["testcase"]: temp_attrs[key] = self.attrs[key] self.attrs = temp_attrs def to_xml(self): testcase = Junit.testcase(time="%.3f" % self.duration, **self.attrs) testcase.append(self.make_properties_node()) for node in self.nodes: testcase.append(node) return testcase def _add_simple(self, kind, message, data=None): data = bin_xml_escape(data) node = kind(data, message=message) self.append(node) def write_captured_output(self, report): if not self.xml.log_passing_tests and report.passed: return content_out = report.capstdout content_log = report.caplog content_err = report.capstderr if content_log or content_out: if content_log and self.xml.logging == "system-out": if content_out: # syncing stdout and the log-output is not done yet. It's # probably not worth the effort. Therefore, first the captured # stdout is shown and then the captured logs. content = "\n".join( [ " Captured Stdout ".center(80, "-"), content_out, "", " Captured Log ".center(80, "-"), content_log, ] ) else: content = content_log else: content = content_out if content: tag = getattr(Junit, "system-out") self.append(tag(bin_xml_escape(content))) if content_log or content_err: if content_log and self.xml.logging == "system-err": if content_err: content = "\n".join( [ " Captured Stderr ".center(80, "-"), content_err, "", " Captured Log ".center(80, "-"), content_log, ] ) else: content = content_log else: content = content_err if content: tag = getattr(Junit, "system-err") self.append(tag(bin_xml_escape(content))) def append_pass(self, report): self.add_stats("passed") def append_failure(self, report): # msg = str(report.longrepr.reprtraceback.extraline) if hasattr(report, "wasxfail"): self._add_simple(Junit.skipped, "xfail-marked test passes unexpectedly") else: if hasattr(report.longrepr, "reprcrash"): message = report.longrepr.reprcrash.message elif isinstance(report.longrepr, six.string_types): message = report.longrepr else: message = str(report.longrepr) message = bin_xml_escape(message) fail = Junit.failure(message=message) fail.append(bin_xml_escape(report.longrepr)) self.append(fail) def append_collect_error(self, report): # msg = str(report.longrepr.reprtraceback.extraline) self.append( Junit.error(bin_xml_escape(report.longrepr), message="collection failure") ) def append_collect_skipped(self, report): self._add_simple(Junit.skipped, "collection skipped", report.longrepr) def append_error(self, report): if report.when == "teardown": msg = "test teardown failure" else: msg = "test setup failure" self._add_simple(Junit.error, msg, report.longrepr) def append_skipped(self, report): if hasattr(report, "wasxfail"): xfailreason = report.wasxfail if xfailreason.startswith("reason: "): xfailreason = xfailreason[8:] self.append( Junit.skipped( "", type="pytest.xfail", message=bin_xml_escape(xfailreason) ) ) else: filename, lineno, skipreason = report.longrepr if skipreason.startswith("Skipped: "): skipreason = skipreason[9:] details = "%s:%s: %s" % (filename, lineno, skipreason) self.append( Junit.skipped( bin_xml_escape(details), type="pytest.skip", message=bin_xml_escape(skipreason), ) ) self.write_captured_output(report) def finalize(self): data = self.to_xml().unicode(indent=0) self.__dict__.clear() self.to_xml = lambda: py.xml.raw(data) def _warn_incompatibility_with_xunit2(request, fixture_name): """Emits a PytestWarning about the given fixture being incompatible with newer xunit revisions""" from _pytest.warning_types import PytestWarning xml = getattr(request.config, "_xml", None) if xml is not None and xml.family not in ("xunit1", "legacy"): request.node.warn( PytestWarning( "{fixture_name} is incompatible with junit_family '{family}' (use 'legacy' or 'xunit1')".format( fixture_name=fixture_name, family=xml.family ) ) ) @pytest.fixture def record_property(request): """Add an extra properties the calling test. User properties become part of the test report and are available to the configured reporters, like JUnit XML. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded. Example:: def test_function(record_property): record_property("example_key", 1) """ _warn_incompatibility_with_xunit2(request, "record_property") def append_property(name, value): request.node.user_properties.append((name, value)) return append_property @pytest.fixture def record_xml_attribute(request): """Add extra xml attributes to the tag for the calling test. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded """ from _pytest.warning_types import PytestExperimentalApiWarning request.node.warn( PytestExperimentalApiWarning("record_xml_attribute is an experimental feature") ) _warn_incompatibility_with_xunit2(request, "record_xml_attribute") # Declare noop def add_attr_noop(name, value): pass attr_func = add_attr_noop xml = getattr(request.config, "_xml", None) if xml is not None: node_reporter = xml.node_reporter(request.node.nodeid) attr_func = node_reporter.add_attribute return attr_func def _check_record_param_type(param, v): """Used by record_testsuite_property to check that the given parameter name is of the proper type""" __tracebackhide__ = True if not isinstance(v, six.string_types): msg = "{param} parameter needs to be a string, but {g} given" raise TypeError(msg.format(param=param, g=type(v).__name__)) @pytest.fixture(scope="session") def record_testsuite_property(request): """ Records a new ```` tag as child of the root ````. This is suitable to writing global information regarding the entire test suite, and is compatible with ``xunit2`` JUnit family. This is a ``session``-scoped fixture which is called with ``(name, value)``. Example: .. code-block:: python def test_foo(record_testsuite_property): record_testsuite_property("ARCH", "PPC") record_testsuite_property("STORAGE_TYPE", "CEPH") ``name`` must be a string, ``value`` will be converted to a string and properly xml-escaped. """ __tracebackhide__ = True def record_func(name, value): """noop function in case --junitxml was not passed in the command-line""" __tracebackhide__ = True _check_record_param_type("name", name) xml = getattr(request.config, "_xml", None) if xml is not None: record_func = xml.add_global_property # noqa return record_func def pytest_addoption(parser): group = parser.getgroup("terminal reporting") group.addoption( "--junitxml", "--junit-xml", action="store", dest="xmlpath", metavar="path", type=functools.partial(filename_arg, optname="--junitxml"), default=None, help="create junit-xml style report file at given path.", ) group.addoption( "--junitprefix", "--junit-prefix", action="store", metavar="str", default=None, help="prepend prefix to classnames in junit-xml output", ) parser.addini( "junit_suite_name", "Test suite name for JUnit report", default="pytest" ) parser.addini( "junit_logging", "Write captured log messages to JUnit report: " "one of no|system-out|system-err", default="no", ) # choices=['no', 'stdout', 'stderr']) parser.addini( "junit_log_passing_tests", "Capture log information for passing tests to JUnit report: ", type="bool", default=True, ) parser.addini( "junit_duration_report", "Duration time to report: one of total|call", default="total", ) # choices=['total', 'call']) parser.addini( "junit_family", "Emit XML for schema: one of legacy|xunit1|xunit2", default="xunit1", ) def pytest_configure(config): xmlpath = config.option.xmlpath # prevent opening xmllog on slave nodes (xdist) if xmlpath and not hasattr(config, "slaveinput"): config._xml = LogXML( xmlpath, config.option.junitprefix, config.getini("junit_suite_name"), config.getini("junit_logging"), config.getini("junit_duration_report"), config.getini("junit_family"), config.getini("junit_log_passing_tests"), ) config.pluginmanager.register(config._xml) def pytest_unconfigure(config): xml = getattr(config, "_xml", None) if xml: del config._xml config.pluginmanager.unregister(xml) def mangle_test_address(address): path, possible_open_bracket, params = address.partition("[") names = path.split("::") try: names.remove("()") except ValueError: pass # convert file path to dotted path names[0] = names[0].replace(nodes.SEP, ".") names[0] = _py_ext_re.sub("", names[0]) # put any params back names[-1] += possible_open_bracket + params return names class LogXML(object): def __init__( self, logfile, prefix, suite_name="pytest", logging="no", report_duration="total", family="xunit1", log_passing_tests=True, ): logfile = os.path.expanduser(os.path.expandvars(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix self.suite_name = suite_name self.logging = logging self.log_passing_tests = log_passing_tests self.report_duration = report_duration self.family = family self.stats = dict.fromkeys(["error", "passed", "failure", "skipped"], 0) self.node_reporters = {} # nodeid -> _NodeReporter self.node_reporters_ordered = [] self.global_properties = [] # List of reports that failed on call but teardown is pending. self.open_reports = [] self.cnt_double_fail_tests = 0 # Replaces convenience family with real family if self.family == "legacy": self.family = "xunit1" def finalize(self, report): nodeid = getattr(report, "nodeid", report) # local hack to handle xdist report order slavenode = getattr(report, "node", None) reporter = self.node_reporters.pop((nodeid, slavenode)) if reporter is not None: reporter.finalize() def node_reporter(self, report): nodeid = getattr(report, "nodeid", report) # local hack to handle xdist report order slavenode = getattr(report, "node", None) key = nodeid, slavenode if key in self.node_reporters: # TODO: breasks for --dist=each return self.node_reporters[key] reporter = _NodeReporter(nodeid, self) self.node_reporters[key] = reporter self.node_reporters_ordered.append(reporter) return reporter def add_stats(self, key): if key in self.stats: self.stats[key] += 1 def _opentestcase(self, report): reporter = self.node_reporter(report) reporter.record_testreport(report) return reporter def pytest_runtest_logreport(self, report): """handle a setup/call/teardown report, generating the appropriate xml tags as necessary. note: due to plugins like xdist, this hook may be called in interlaced order with reports from other nodes. for example: usual call order: -> setup node1 -> call node1 -> teardown node1 -> setup node2 -> call node2 -> teardown node2 possible call order in xdist: -> setup node1 -> call node1 -> setup node2 -> call node2 -> teardown node2 -> teardown node1 """ close_report = None if report.passed: if report.when == "call": # ignore setup/teardown reporter = self._opentestcase(report) reporter.append_pass(report) elif report.failed: if report.when == "teardown": # The following vars are needed when xdist plugin is used report_wid = getattr(report, "worker_id", None) report_ii = getattr(report, "item_index", None) close_report = next( ( rep for rep in self.open_reports if ( rep.nodeid == report.nodeid and getattr(rep, "item_index", None) == report_ii and getattr(rep, "worker_id", None) == report_wid ) ), None, ) if close_report: # We need to open new testcase in case we have failure in # call and error in teardown in order to follow junit # schema self.finalize(close_report) self.cnt_double_fail_tests += 1 reporter = self._opentestcase(report) if report.when == "call": reporter.append_failure(report) self.open_reports.append(report) if not self.log_passing_tests: reporter.write_captured_output(report) else: reporter.append_error(report) elif report.skipped: reporter = self._opentestcase(report) reporter.append_skipped(report) self.update_testcase_duration(report) if report.when == "teardown": reporter = self._opentestcase(report) reporter.write_captured_output(report) for propname, propvalue in report.user_properties: reporter.add_property(propname, propvalue) self.finalize(report) report_wid = getattr(report, "worker_id", None) report_ii = getattr(report, "item_index", None) close_report = next( ( rep for rep in self.open_reports if ( rep.nodeid == report.nodeid and getattr(rep, "item_index", None) == report_ii and getattr(rep, "worker_id", None) == report_wid ) ), None, ) if close_report: self.open_reports.remove(close_report) def update_testcase_duration(self, report): """accumulates total duration for nodeid from given report and updates the Junit.testcase with the new total if already created. """ if self.report_duration == "total" or report.when == self.report_duration: reporter = self.node_reporter(report) reporter.duration += getattr(report, "duration", 0.0) def pytest_collectreport(self, report): if not report.passed: reporter = self._opentestcase(report) if report.failed: reporter.append_collect_error(report) else: reporter.append_collect_skipped(report) def pytest_internalerror(self, excrepr): reporter = self.node_reporter("internal") reporter.attrs.update(classname="pytest", name="internal") reporter._add_simple(Junit.error, "internal error", excrepr) def pytest_sessionstart(self): self.suite_start_time = time.time() def pytest_sessionfinish(self): dirname = os.path.dirname(os.path.abspath(self.logfile)) if not os.path.isdir(dirname): os.makedirs(dirname) logfile = open(self.logfile, "w", encoding="utf-8") suite_stop_time = time.time() suite_time_delta = suite_stop_time - self.suite_start_time numtests = ( self.stats["passed"] + self.stats["failure"] + self.stats["skipped"] + self.stats["error"] - self.cnt_double_fail_tests ) logfile.write('') suite_node = Junit.testsuite( self._get_global_properties_node(), [x.to_xml() for x in self.node_reporters_ordered], name=self.suite_name, errors=self.stats["error"], failures=self.stats["failure"], skipped=self.stats["skipped"], tests=numtests, time="%.3f" % suite_time_delta, timestamp=datetime.fromtimestamp(self.suite_start_time).isoformat(), hostname=platform.node(), ) logfile.write(Junit.testsuites([suite_node]).unicode(indent=0)) logfile.close() def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) def add_global_property(self, name, value): __tracebackhide__ = True _check_record_param_type("name", name) self.global_properties.append((name, bin_xml_escape(value))) def _get_global_properties_node(self): """Return a Junit node containing custom properties, if any. """ if self.global_properties: return Junit.properties( [ Junit.property(name=name, value=value) for name, value in self.global_properties ] ) return "" pytest-4.6.9/src/_pytest/config/0000775000372000037200000000000013604173421017450 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/config/exceptions.py0000664000372000037200000000037513604173400022205 0ustar travistravis00000000000000# -*- coding: utf-8 -*- class UsageError(Exception): """ error in pytest usage or invocation""" class PrintHelp(Exception): """Raised when pytest should print it's help to skip the rest of the argument parsing and validation.""" pass pytest-4.6.9/src/_pytest/config/__init__.py0000664000372000037200000011722313604173400021564 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ command line options, ini-file and conftest.py processing. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import copy import inspect import os import shlex import sys import types import warnings import py import six from packaging.version import Version from pluggy import HookimplMarker from pluggy import HookspecMarker from pluggy import PluginManager import _pytest._code import _pytest.assertion import _pytest.hookspec # the extension point definitions from .exceptions import PrintHelp from .exceptions import UsageError from .findpaths import determine_setup from .findpaths import exists from _pytest import deprecated from _pytest._code import ExceptionInfo from _pytest._code import filter_traceback from _pytest.compat import importlib_metadata from _pytest.compat import lru_cache from _pytest.compat import safe_str from _pytest.outcomes import fail from _pytest.outcomes import Skipped from _pytest.warning_types import PytestConfigWarning hookimpl = HookimplMarker("pytest") hookspec = HookspecMarker("pytest") class ConftestImportFailure(Exception): def __init__(self, path, excinfo): Exception.__init__(self, path, excinfo) self.path = path self.excinfo = excinfo def main(args=None, plugins=None): """ return exit code, after performing an in-process test run. :arg args: list of command line arguments. :arg plugins: list of plugin objects to be auto-registered during initialization. """ from _pytest.main import EXIT_USAGEERROR try: try: config = _prepareconfig(args, plugins) except ConftestImportFailure as e: exc_info = ExceptionInfo(e.excinfo) tw = py.io.TerminalWriter(sys.stderr) tw.line( "ImportError while loading conftest '{e.path}'.".format(e=e), red=True ) exc_info.traceback = exc_info.traceback.filter(filter_traceback) exc_repr = ( exc_info.getrepr(style="short", chain=False) if exc_info.traceback else exc_info.exconly() ) formatted_tb = safe_str(exc_repr) for line in formatted_tb.splitlines(): tw.line(line.rstrip(), red=True) return 4 else: try: return config.hook.pytest_cmdline_main(config=config) finally: config._ensure_unconfigure() except UsageError as e: tw = py.io.TerminalWriter(sys.stderr) for msg in e.args: tw.line("ERROR: {}\n".format(msg), red=True) return EXIT_USAGEERROR class cmdline(object): # compatibility namespace main = staticmethod(main) def filename_arg(path, optname): """ Argparse type validator for filename arguments. :path: path of filename :optname: name of the option """ if os.path.isdir(path): raise UsageError("{} must be a filename, given: {}".format(optname, path)) return path def directory_arg(path, optname): """Argparse type validator for directory arguments. :path: path of directory :optname: name of the option """ if not os.path.isdir(path): raise UsageError("{} must be a directory, given: {}".format(optname, path)) return path # Plugins that cannot be disabled via "-p no:X" currently. essential_plugins = ( "mark", "main", "runner", "fixtures", "helpconfig", # Provides -p. ) default_plugins = essential_plugins + ( "python", "terminal", "debugging", "unittest", "capture", "skipping", "tmpdir", "monkeypatch", "recwarn", "pastebin", "nose", "assertion", "junitxml", "resultlog", "doctest", "cacheprovider", "freeze_support", "setuponly", "setupplan", "stepwise", "warnings", "logging", "reports", ) builtin_plugins = set(default_plugins) builtin_plugins.add("pytester") def get_config(args=None): # subsequent calls to main will create a fresh instance pluginmanager = PytestPluginManager() config = Config(pluginmanager) if args is not None: # Handle any "-p no:plugin" args. pluginmanager.consider_preparse(args) for spec in default_plugins: pluginmanager.import_plugin(spec) return config def get_plugin_manager(): """ Obtain a new instance of the :py:class:`_pytest.config.PytestPluginManager`, with default plugins already loaded. This function can be used by integration with other tools, like hooking into pytest to run tests into an IDE. """ return get_config().pluginmanager def _prepareconfig(args=None, plugins=None): warning = None if args is None: args = sys.argv[1:] elif isinstance(args, py.path.local): args = [str(args)] elif not isinstance(args, (tuple, list)): msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})" raise TypeError(msg.format(args, type(args))) config = get_config(args) pluginmanager = config.pluginmanager try: if plugins: for plugin in plugins: if isinstance(plugin, six.string_types): pluginmanager.consider_pluginarg(plugin) else: pluginmanager.register(plugin) if warning: from _pytest.warnings import _issue_warning_captured _issue_warning_captured(warning, hook=config.hook, stacklevel=4) return pluginmanager.hook.pytest_cmdline_parse( pluginmanager=pluginmanager, args=args ) except BaseException: config._ensure_unconfigure() raise class PytestPluginManager(PluginManager): """ Overwrites :py:class:`pluggy.PluginManager ` to add pytest-specific functionality: * loading plugins from the command line, ``PYTEST_PLUGINS`` env variable and ``pytest_plugins`` global variables found in plugins being loaded; * ``conftest.py`` loading during start-up; """ def __init__(self): super(PytestPluginManager, self).__init__("pytest") self._conftest_plugins = set() # state related to local conftest plugins self._dirpath2confmods = {} self._conftestpath2mod = {} self._confcutdir = None self._noconftest = False self._duplicatepaths = set() self.add_hookspecs(_pytest.hookspec) self.register(self) if os.environ.get("PYTEST_DEBUG"): err = sys.stderr encoding = getattr(err, "encoding", "utf8") try: err = py.io.dupfile(err, encoding=encoding) except Exception: pass self.trace.root.setwriter(err.write) self.enable_tracing() # Config._consider_importhook will set a real object if required. self.rewrite_hook = _pytest.assertion.DummyRewriteHook() # Used to know when we are importing conftests after the pytest_configure stage self._configured = False def addhooks(self, module_or_class): """ .. deprecated:: 2.8 Use :py:meth:`pluggy.PluginManager.add_hookspecs ` instead. """ warnings.warn(deprecated.PLUGIN_MANAGER_ADDHOOKS, stacklevel=2) return self.add_hookspecs(module_or_class) def parse_hookimpl_opts(self, plugin, name): # pytest hooks are always prefixed with pytest_ # so we avoid accessing possibly non-readable attributes # (see issue #1073) if not name.startswith("pytest_"): return # ignore names which can not be hooks if name == "pytest_plugins": return method = getattr(plugin, name) opts = super(PytestPluginManager, self).parse_hookimpl_opts(plugin, name) # consider only actual functions for hooks (#3775) if not inspect.isroutine(method): return # collect unmarked hooks as long as they have the `pytest_' prefix if opts is None and name.startswith("pytest_"): opts = {} if opts is not None: # TODO: DeprecationWarning, people should use hookimpl # https://github.com/pytest-dev/pytest/issues/4562 known_marks = {m.name for m in getattr(method, "pytestmark", [])} for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper"): opts.setdefault(name, hasattr(method, name) or name in known_marks) return opts def parse_hookspec_opts(self, module_or_class, name): opts = super(PytestPluginManager, self).parse_hookspec_opts( module_or_class, name ) if opts is None: method = getattr(module_or_class, name) if name.startswith("pytest_"): # todo: deprecate hookspec hacks # https://github.com/pytest-dev/pytest/issues/4562 known_marks = {m.name for m in getattr(method, "pytestmark", [])} opts = { "firstresult": hasattr(method, "firstresult") or "firstresult" in known_marks, "historic": hasattr(method, "historic") or "historic" in known_marks, } return opts def register(self, plugin, name=None): if name in ["pytest_catchlog", "pytest_capturelog"]: warnings.warn( PytestConfigWarning( "{} plugin has been merged into the core, " "please remove it from your requirements.".format( name.replace("_", "-") ) ) ) return ret = super(PytestPluginManager, self).register(plugin, name) if ret: self.hook.pytest_plugin_registered.call_historic( kwargs=dict(plugin=plugin, manager=self) ) if isinstance(plugin, types.ModuleType): self.consider_module(plugin) return ret def getplugin(self, name): # support deprecated naming because plugins (xdist e.g.) use it return self.get_plugin(name) def hasplugin(self, name): """Return True if the plugin with the given name is registered.""" return bool(self.get_plugin(name)) def pytest_configure(self, config): # XXX now that the pluginmanager exposes hookimpl(tryfirst...) # we should remove tryfirst/trylast as markers config.addinivalue_line( "markers", "tryfirst: mark a hook implementation function such that the " "plugin machinery will try to call it first/as early as possible.", ) config.addinivalue_line( "markers", "trylast: mark a hook implementation function such that the " "plugin machinery will try to call it last/as late as possible.", ) self._configured = True # # internal API for local conftest plugin handling # def _set_initial_conftests(self, namespace): """ load initial conftest files given a preparsed "namespace". As conftest files may add their own command line options which have arguments ('--my-opt somepath') we might get some false positives. All builtin and 3rd party plugins will have been loaded, however, so common options will not confuse our logic here. """ current = py.path.local() self._confcutdir = ( current.join(namespace.confcutdir, abs=True) if namespace.confcutdir else None ) self._noconftest = namespace.noconftest self._using_pyargs = namespace.pyargs testpaths = namespace.file_or_dir foundanchor = False for path in testpaths: path = str(path) # remove node-id syntax i = path.find("::") if i != -1: path = path[:i] anchor = current.join(path, abs=1) if exists(anchor): # we found some file object self._try_load_conftest(anchor) foundanchor = True if not foundanchor: self._try_load_conftest(current) def _try_load_conftest(self, anchor): self._getconftestmodules(anchor) # let's also consider test* subdirs if anchor.check(dir=1): for x in anchor.listdir("test*"): if x.check(dir=1): self._getconftestmodules(x) @lru_cache(maxsize=128) def _getconftestmodules(self, path): if self._noconftest: return [] if path.isfile(): directory = path.dirpath() else: directory = path if six.PY2: # py2 is not using lru_cache. try: return self._dirpath2confmods[directory] except KeyError: pass # XXX these days we may rather want to use config.rootdir # and allow users to opt into looking into the rootdir parent # directories instead of requiring to specify confcutdir clist = [] for parent in directory.realpath().parts(): if self._confcutdir and self._confcutdir.relto(parent): continue conftestpath = parent.join("conftest.py") if conftestpath.isfile(): # Use realpath to avoid loading the same conftest twice # with build systems that create build directories containing # symlinks to actual files. mod = self._importconftest(conftestpath.realpath()) clist.append(mod) self._dirpath2confmods[directory] = clist return clist def _rget_with_confmod(self, name, path): modules = self._getconftestmodules(path) for mod in reversed(modules): try: return mod, getattr(mod, name) except AttributeError: continue raise KeyError(name) def _importconftest(self, conftestpath): try: return self._conftestpath2mod[conftestpath] except KeyError: pkgpath = conftestpath.pypkgpath() if pkgpath is None: _ensure_removed_sysmodule(conftestpath.purebasename) try: mod = conftestpath.pyimport() if ( hasattr(mod, "pytest_plugins") and self._configured and not self._using_pyargs ): from _pytest.deprecated import ( PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST, ) fail( PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST.format( conftestpath, self._confcutdir ), pytrace=False, ) except Exception: raise ConftestImportFailure(conftestpath, sys.exc_info()) self._conftest_plugins.add(mod) self._conftestpath2mod[conftestpath] = mod dirpath = conftestpath.dirpath() if dirpath in self._dirpath2confmods: for path, mods in self._dirpath2confmods.items(): if path and path.relto(dirpath) or path == dirpath: assert mod not in mods mods.append(mod) self.trace("loaded conftestmodule %r" % (mod)) self.consider_conftest(mod) return mod # # API for bootstrapping plugin loading # # def consider_preparse(self, args): i = 0 n = len(args) while i < n: opt = args[i] i += 1 if isinstance(opt, six.string_types): if opt == "-p": try: parg = args[i] except IndexError: return i += 1 elif opt.startswith("-p"): parg = opt[2:] else: continue self.consider_pluginarg(parg) def consider_pluginarg(self, arg): if arg.startswith("no:"): name = arg[3:] if name in essential_plugins: raise UsageError("plugin %s cannot be disabled" % name) # PR #4304 : remove stepwise if cacheprovider is blocked if name == "cacheprovider": self.set_blocked("stepwise") self.set_blocked("pytest_stepwise") self.set_blocked(name) if not name.startswith("pytest_"): self.set_blocked("pytest_" + name) else: name = arg # Unblock the plugin. None indicates that it has been blocked. # There is no interface with pluggy for this. if self._name2plugin.get(name, -1) is None: del self._name2plugin[name] if not name.startswith("pytest_"): if self._name2plugin.get("pytest_" + name, -1) is None: del self._name2plugin["pytest_" + name] self.import_plugin(arg, consider_entry_points=True) def consider_conftest(self, conftestmodule): self.register(conftestmodule, name=conftestmodule.__file__) def consider_env(self): self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS")) def consider_module(self, mod): self._import_plugin_specs(getattr(mod, "pytest_plugins", [])) def _import_plugin_specs(self, spec): plugins = _get_plugin_specs_as_list(spec) for import_spec in plugins: self.import_plugin(import_spec) def import_plugin(self, modname, consider_entry_points=False): """ Imports a plugin with ``modname``. If ``consider_entry_points`` is True, entry point names are also considered to find a plugin. """ # most often modname refers to builtin modules, e.g. "pytester", # "terminal" or "capture". Those plugins are registered under their # basename for historic purposes but must be imported with the # _pytest prefix. assert isinstance(modname, six.string_types), ( "module name as text required, got %r" % modname ) modname = str(modname) if self.is_blocked(modname) or self.get_plugin(modname) is not None: return importspec = "_pytest." + modname if modname in builtin_plugins else modname self.rewrite_hook.mark_rewrite(importspec) if consider_entry_points: loaded = self.load_setuptools_entrypoints("pytest11", name=modname) if loaded: return try: __import__(importspec) except ImportError as e: new_exc_message = 'Error importing plugin "%s": %s' % ( modname, safe_str(e.args[0]), ) new_exc = ImportError(new_exc_message) tb = sys.exc_info()[2] six.reraise(ImportError, new_exc, tb) except Skipped as e: from _pytest.warnings import _issue_warning_captured _issue_warning_captured( PytestConfigWarning("skipped plugin %r: %s" % (modname, e.msg)), self.hook, stacklevel=1, ) else: mod = sys.modules[importspec] self.register(mod, modname) def _get_plugin_specs_as_list(specs): """ Parses a list of "plugin specs" and returns a list of plugin names. Plugin specs can be given as a list of strings separated by "," or already as a list/tuple in which case it is returned as a list. Specs can also be `None` in which case an empty list is returned. """ if specs is not None and not isinstance(specs, types.ModuleType): if isinstance(specs, six.string_types): specs = specs.split(",") if specs else [] if not isinstance(specs, (list, tuple)): raise UsageError( "Plugin specs must be a ','-separated string or a " "list/tuple of strings for plugin names. Given: %r" % specs ) return list(specs) return [] def _ensure_removed_sysmodule(modname): try: del sys.modules[modname] except KeyError: pass class Notset(object): def __repr__(self): return "" notset = Notset() def _iter_rewritable_modules(package_files): """ Given an iterable of file names in a source distribution, return the "names" that should be marked for assertion rewrite (for example the package "pytest_mock/__init__.py" should be added as "pytest_mock" in the assertion rewrite mechanism. This function has to deal with dist-info based distributions and egg based distributions (which are still very much in use for "editable" installs). Here are the file names as seen in a dist-info based distribution: pytest_mock/__init__.py pytest_mock/_version.py pytest_mock/plugin.py pytest_mock.egg-info/PKG-INFO Here are the file names as seen in an egg based distribution: src/pytest_mock/__init__.py src/pytest_mock/_version.py src/pytest_mock/plugin.py src/pytest_mock.egg-info/PKG-INFO LICENSE setup.py We have to take in account those two distribution flavors in order to determine which names should be considered for assertion rewriting. More information: https://github.com/pytest-dev/pytest-mock/issues/167 """ package_files = list(package_files) seen_some = False for fn in package_files: is_simple_module = "/" not in fn and fn.endswith(".py") is_package = fn.count("/") == 1 and fn.endswith("__init__.py") if is_simple_module: module_name, _ = os.path.splitext(fn) # we ignore "setup.py" at the root of the distribution if module_name != "setup": seen_some = True yield module_name elif is_package: package_name = os.path.dirname(fn) seen_some = True yield package_name if not seen_some: # at this point we did not find any packages or modules suitable for assertion # rewriting, so we try again by stripping the first path component (to account for # "src" based source trees for example) # this approach lets us have the common case continue to be fast, as egg-distributions # are rarer new_package_files = [] for fn in package_files: parts = fn.split("/") new_fn = "/".join(parts[1:]) if new_fn: new_package_files.append(new_fn) if new_package_files: for _module in _iter_rewritable_modules(new_package_files): yield _module class Config(object): """ access to configuration values, pluginmanager and plugin hooks. """ def __init__(self, pluginmanager): #: access to command line option as attributes. #: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead self.option = argparse.Namespace() from .argparsing import Parser, FILE_OR_DIR _a = FILE_OR_DIR self._parser = Parser( usage="%%(prog)s [options] [%s] [%s] [...]" % (_a, _a), processopt=self._processopt, ) #: a pluginmanager instance self.pluginmanager = pluginmanager self.trace = self.pluginmanager.trace.root.get("config") self.hook = self.pluginmanager.hook self._inicache = {} self._override_ini = () self._opt2dest = {} self._cleanup = [] self.pluginmanager.register(self, "pytestconfig") self._configured = False self.invocation_dir = py.path.local() self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser)) def add_cleanup(self, func): """ Add a function to be called when the config object gets out of use (usually coninciding with pytest_unconfigure).""" self._cleanup.append(func) def _do_configure(self): assert not self._configured self._configured = True self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) def _ensure_unconfigure(self): if self._configured: self._configured = False self.hook.pytest_unconfigure(config=self) self.hook.pytest_configure._call_history = [] while self._cleanup: fin = self._cleanup.pop() fin() def get_terminal_writer(self): return self.pluginmanager.get_plugin("terminalreporter")._tw def pytest_cmdline_parse(self, pluginmanager, args): try: self.parse(args) except UsageError: # Handle --version and --help here in a minimal fashion. # This gets done via helpconfig normally, but its # pytest_cmdline_main is not called in case of errors. if getattr(self.option, "version", False) or "--version" in args: from _pytest.helpconfig import showversion showversion(self) elif ( getattr(self.option, "help", False) or "--help" in args or "-h" in args ): self._parser._getparser().print_help() sys.stdout.write( "\nNOTE: displaying only minimal help due to UsageError.\n\n" ) raise return self def notify_exception(self, excinfo, option=None): if option and getattr(option, "fulltrace", False): style = "long" else: style = "native" excrepr = excinfo.getrepr( funcargs=True, showlocals=getattr(option, "showlocals", False), style=style ) res = self.hook.pytest_internalerror(excrepr=excrepr, excinfo=excinfo) if not any(res): for line in str(excrepr).split("\n"): sys.stderr.write("INTERNALERROR> %s\n" % line) sys.stderr.flush() def cwd_relative_nodeid(self, nodeid): # nodeid's are relative to the rootpath, compute relative to cwd if self.invocation_dir != self.rootdir: fullpath = self.rootdir.join(nodeid) nodeid = self.invocation_dir.bestrelpath(fullpath) return nodeid @classmethod def fromdictargs(cls, option_dict, args): """ constructor useable for subprocesses. """ config = get_config(args) config.option.__dict__.update(option_dict) config.parse(args, addopts=False) for x in config.option.plugins: config.pluginmanager.consider_pluginarg(x) return config def _processopt(self, opt): for name in opt._short_opts + opt._long_opts: self._opt2dest[name] = opt.dest if hasattr(opt, "default") and opt.dest: if not hasattr(self.option, opt.dest): setattr(self.option, opt.dest, opt.default) @hookimpl(trylast=True) def pytest_load_initial_conftests(self, early_config): self.pluginmanager._set_initial_conftests(early_config.known_args_namespace) def _initini(self, args): ns, unknown_args = self._parser.parse_known_and_unknown_args( args, namespace=copy.copy(self.option) ) r = determine_setup( ns.inifilename, ns.file_or_dir + unknown_args, rootdir_cmd_arg=ns.rootdir or None, config=self, ) self.rootdir, self.inifile, self.inicfg = r self._parser.extra_info["rootdir"] = self.rootdir self._parser.extra_info["inifile"] = self.inifile self._parser.addini("addopts", "extra command line options", "args") self._parser.addini("minversion", "minimally required pytest version") self._override_ini = ns.override_ini or () def _consider_importhook(self, args): """Install the PEP 302 import hook if using assertion rewriting. Needs to parse the --assert= option from the commandline and find all the installed plugins to mark them for rewriting by the importhook. """ ns, unknown_args = self._parser.parse_known_and_unknown_args(args) mode = getattr(ns, "assertmode", "plain") if mode == "rewrite": try: hook = _pytest.assertion.install_importhook(self) except SystemError: mode = "plain" else: self._mark_plugins_for_rewrite(hook) _warn_about_missing_assertion(mode) def _mark_plugins_for_rewrite(self, hook): """ Given an importhook, mark for rewrite any top-level modules or packages in the distribution package for all pytest plugins. """ self.pluginmanager.rewrite_hook = hook if os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"): # We don't autoload from setuptools entry points, no need to continue. return package_files = ( str(file) for dist in importlib_metadata.distributions() if any(ep.group == "pytest11" for ep in dist.entry_points) for file in dist.files or [] ) for name in _iter_rewritable_modules(package_files): hook.mark_rewrite(name) def _validate_args(self, args, via): """Validate known args.""" self._parser._config_source_hint = via try: self._parser.parse_known_and_unknown_args( args, namespace=copy.copy(self.option) ) finally: del self._parser._config_source_hint return args def _preparse(self, args, addopts=True): if addopts: env_addopts = os.environ.get("PYTEST_ADDOPTS", "") if len(env_addopts): args[:] = ( self._validate_args(shlex.split(env_addopts), "via PYTEST_ADDOPTS") + args ) self._initini(args) if addopts: args[:] = ( self._validate_args(self.getini("addopts"), "via addopts config") + args ) self._checkversion() self._consider_importhook(args) self.pluginmanager.consider_preparse(args) if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"): # Don't autoload from setuptools entry point. Only explicitly specified # plugins are going to be loaded. self.pluginmanager.load_setuptools_entrypoints("pytest11") self.pluginmanager.consider_env() self.known_args_namespace = ns = self._parser.parse_known_args( args, namespace=copy.copy(self.option) ) if self.known_args_namespace.confcutdir is None and self.inifile: confcutdir = py.path.local(self.inifile).dirname self.known_args_namespace.confcutdir = confcutdir try: self.hook.pytest_load_initial_conftests( early_config=self, args=args, parser=self._parser ) except ConftestImportFailure: e = sys.exc_info()[1] if ns.help or ns.version: # we don't want to prevent --help/--version to work # so just let is pass and print a warning at the end from _pytest.warnings import _issue_warning_captured _issue_warning_captured( PytestConfigWarning( "could not load initial conftests: {}".format(e.path) ), self.hook, stacklevel=2, ) else: raise def _checkversion(self): import pytest minver = self.inicfg.get("minversion", None) if minver: if Version(minver) > Version(pytest.__version__): raise pytest.UsageError( "%s:%d: requires pytest-%s, actual pytest-%s'" % ( self.inicfg.config.path, self.inicfg.lineof("minversion"), minver, pytest.__version__, ) ) def parse(self, args, addopts=True): # parse given cmdline arguments into this config object. assert not hasattr( self, "args" ), "can only parse cmdline args at most once per Config object" self._origargs = args self.hook.pytest_addhooks.call_historic( kwargs=dict(pluginmanager=self.pluginmanager) ) self._preparse(args, addopts=addopts) # XXX deprecated hook: self.hook.pytest_cmdline_preparse(config=self, args=args) self._parser.after_preparse = True try: args = self._parser.parse_setoption( args, self.option, namespace=self.option ) if not args: if self.invocation_dir == self.rootdir: args = self.getini("testpaths") if not args: args = [str(self.invocation_dir)] self.args = args except PrintHelp: pass def addinivalue_line(self, name, line): """ add a line to an ini-file option. The option must have been declared but might not yet be set in which case the line becomes the the first line in its value. """ x = self.getini(name) assert isinstance(x, list) x.append(line) # modifies the cached list inline def getini(self, name): """ return configuration value from an :ref:`ini file `. If the specified name hasn't been registered through a prior :py:func:`parser.addini <_pytest.config.Parser.addini>` call (usually from a plugin), a ValueError is raised. """ try: return self._inicache[name] except KeyError: self._inicache[name] = val = self._getini(name) return val def _getini(self, name): try: description, type, default = self._parser._inidict[name] except KeyError: raise ValueError("unknown configuration value: %r" % (name,)) value = self._get_override_ini_value(name) if value is None: try: value = self.inicfg[name] except KeyError: if default is not None: return default if type is None: return "" return [] if type == "pathlist": dp = py.path.local(self.inicfg.config.path).dirpath() values = [] for relpath in shlex.split(value): values.append(dp.join(relpath, abs=True)) return values elif type == "args": return shlex.split(value) elif type == "linelist": return [t for t in map(lambda x: x.strip(), value.split("\n")) if t] elif type == "bool": return bool(_strtobool(value.strip())) else: assert type is None return value def _getconftest_pathlist(self, name, path): try: mod, relroots = self.pluginmanager._rget_with_confmod(name, path) except KeyError: return None modpath = py.path.local(mod.__file__).dirpath() values = [] for relroot in relroots: if not isinstance(relroot, py.path.local): relroot = relroot.replace("/", py.path.local.sep) relroot = modpath.join(relroot, abs=True) values.append(relroot) return values def _get_override_ini_value(self, name): value = None # override_ini is a list of "ini=value" options # always use the last item if multiple values are set for same ini-name, # e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2 for ini_config in self._override_ini: try: key, user_ini_value = ini_config.split("=", 1) except ValueError: raise UsageError("-o/--override-ini expects option=value style.") else: if key == name: value = user_ini_value return value def getoption(self, name, default=notset, skip=False): """ return command line option value. :arg name: name of the option. You may also specify the literal ``--OPT`` option instead of the "dest" option name. :arg default: default value if no option of that name exists. :arg skip: if True raise pytest.skip if option does not exists or has a None value. """ name = self._opt2dest.get(name, name) try: val = getattr(self.option, name) if val is None and skip: raise AttributeError(name) return val except AttributeError: if default is not notset: return default if skip: import pytest pytest.skip("no %r option found" % (name,)) raise ValueError("no option named %r" % (name,)) def getvalue(self, name, path=None): """ (deprecated, use getoption()) """ return self.getoption(name) def getvalueorskip(self, name, path=None): """ (deprecated, use getoption(skip=True)) """ return self.getoption(name, skip=True) def _assertion_supported(): try: assert False except AssertionError: return True else: return False def _warn_about_missing_assertion(mode): if not _assertion_supported(): if mode == "plain": sys.stderr.write( "WARNING: ASSERTIONS ARE NOT EXECUTED" " and FAILING TESTS WILL PASS. Are you" " using python -O?" ) else: sys.stderr.write( "WARNING: assertions not in test modules or" " plugins will be ignored" " because assert statements are not executed " "by the underlying Python interpreter " "(are you using python -O?)\n" ) def setns(obj, dic): import pytest for name, value in dic.items(): if isinstance(value, dict): mod = getattr(obj, name, None) if mod is None: modname = "pytest.%s" % name mod = types.ModuleType(modname) sys.modules[modname] = mod mod.__all__ = [] setattr(obj, name, mod) obj.__all__.append(name) setns(mod, value) else: setattr(obj, name, value) obj.__all__.append(name) # if obj != pytest: # pytest.__all__.append(name) setattr(pytest, name, value) def create_terminal_writer(config, *args, **kwargs): """Create a TerminalWriter instance configured according to the options in the config object. Every code which requires a TerminalWriter object and has access to a config object should use this function. """ tw = py.io.TerminalWriter(*args, **kwargs) if config.option.color == "yes": tw.hasmarkup = True if config.option.color == "no": tw.hasmarkup = False return tw def _strtobool(val): """Convert a string representation of truth to true (1) or false (0). True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if 'val' is anything else. .. note:: copied from distutils.util """ val = val.lower() if val in ("y", "yes", "t", "true", "on", "1"): return 1 elif val in ("n", "no", "f", "false", "off", "0"): return 0 else: raise ValueError("invalid truth value %r" % (val,)) pytest-4.6.9/src/_pytest/config/findpaths.py0000664000372000037200000001234513604173400022004 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import os import py from .exceptions import UsageError from _pytest.outcomes import fail def exists(path, ignore=EnvironmentError): try: return path.check() except ignore: return False def getcfg(args, config=None): """ Search the list of arguments for a valid ini-file for pytest, and return a tuple of (rootdir, inifile, cfg-dict). note: config is optional and used only to issue warnings explicitly (#2891). """ from _pytest.deprecated import CFG_PYTEST_SECTION inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"] args = [x for x in args if not str(x).startswith("-")] if not args: args = [py.path.local()] for arg in args: arg = py.path.local(arg) for base in arg.parts(reverse=True): for inibasename in inibasenames: p = base.join(inibasename) if exists(p): try: iniconfig = py.iniconfig.IniConfig(p) except py.iniconfig.ParseError as exc: raise UsageError(str(exc)) if ( inibasename == "setup.cfg" and "tool:pytest" in iniconfig.sections ): return base, p, iniconfig["tool:pytest"] elif "pytest" in iniconfig.sections: if inibasename == "setup.cfg" and config is not None: fail( CFG_PYTEST_SECTION.format(filename=inibasename), pytrace=False, ) return base, p, iniconfig["pytest"] elif inibasename == "pytest.ini": # allowed to be empty return base, p, {} return None, None, None def get_common_ancestor(paths): common_ancestor = None for path in paths: if not path.exists(): continue if common_ancestor is None: common_ancestor = path else: if path.relto(common_ancestor) or path == common_ancestor: continue elif common_ancestor.relto(path): common_ancestor = path else: shared = path.common(common_ancestor) if shared is not None: common_ancestor = shared if common_ancestor is None: common_ancestor = py.path.local() elif common_ancestor.isfile(): common_ancestor = common_ancestor.dirpath() return common_ancestor def get_dirs_from_args(args): def is_option(x): return str(x).startswith("-") def get_file_part_from_node_id(x): return str(x).split("::")[0] def get_dir_from_path(path): if path.isdir(): return path return py.path.local(path.dirname) # These look like paths but may not exist possible_paths = ( py.path.local(get_file_part_from_node_id(arg)) for arg in args if not is_option(arg) ) return [get_dir_from_path(path) for path in possible_paths if path.exists()] def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None): dirs = get_dirs_from_args(args) if inifile: iniconfig = py.iniconfig.IniConfig(inifile) is_cfg_file = str(inifile).endswith(".cfg") sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"] for section in sections: try: inicfg = iniconfig[section] if is_cfg_file and section == "pytest" and config is not None: from _pytest.deprecated import CFG_PYTEST_SECTION fail( CFG_PYTEST_SECTION.format(filename=str(inifile)), pytrace=False ) break except KeyError: inicfg = None if rootdir_cmd_arg is None: rootdir = get_common_ancestor(dirs) else: ancestor = get_common_ancestor(dirs) rootdir, inifile, inicfg = getcfg([ancestor], config=config) if rootdir is None and rootdir_cmd_arg is None: for possible_rootdir in ancestor.parts(reverse=True): if possible_rootdir.join("setup.py").exists(): rootdir = possible_rootdir break else: if dirs != [ancestor]: rootdir, inifile, inicfg = getcfg(dirs, config=config) if rootdir is None: if config is not None: cwd = config.invocation_dir else: cwd = py.path.local() rootdir = get_common_ancestor([cwd, ancestor]) is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/" if is_fs_root: rootdir = ancestor if rootdir_cmd_arg: rootdir = py.path.local(os.path.expandvars(rootdir_cmd_arg)) if not rootdir.isdir(): raise UsageError( "Directory '{}' not found. Check your '--rootdir' option.".format( rootdir ) ) return rootdir, inifile, inicfg or {} pytest-4.6.9/src/_pytest/config/argparsing.py0000664000372000037200000003665113604173400022167 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import argparse import warnings import py import six from _pytest.config.exceptions import UsageError FILE_OR_DIR = "file_or_dir" class Parser(object): """ Parser for command line arguments and ini-file values. :ivar extra_info: dict of generic param -> value to display in case there's an error processing the command line arguments. """ prog = None def __init__(self, usage=None, processopt=None): self._anonymous = OptionGroup("custom options", parser=self) self._groups = [] self._processopt = processopt self._usage = usage self._inidict = {} self._ininames = [] self.extra_info = {} def processoption(self, option): if self._processopt: if option.dest: self._processopt(option) def getgroup(self, name, description="", after=None): """ get (or create) a named option Group. :name: name of the option group. :description: long description for --help output. :after: name of other group, used for ordering --help output. The returned group object has an ``addoption`` method with the same signature as :py:func:`parser.addoption <_pytest.config.Parser.addoption>` but will be shown in the respective group in the output of ``pytest. --help``. """ for group in self._groups: if group.name == name: return group group = OptionGroup(name, description, parser=self) i = 0 for i, grp in enumerate(self._groups): if grp.name == after: break self._groups.insert(i + 1, group) return group def addoption(self, *opts, **attrs): """ register a command line option. :opts: option names, can be short or long options. :attrs: same attributes which the ``add_option()`` function of the `argparse library `_ accepts. After command line parsing options are available on the pytest config object via ``config.option.NAME`` where ``NAME`` is usually set by passing a ``dest`` attribute, for example ``addoption("--long", dest="NAME", ...)``. """ self._anonymous.addoption(*opts, **attrs) def parse(self, args, namespace=None): from _pytest._argcomplete import try_argcomplete self.optparser = self._getparser() try_argcomplete(self.optparser) args = [str(x) if isinstance(x, py.path.local) else x for x in args] return self.optparser.parse_args(args, namespace=namespace) def _getparser(self): from _pytest._argcomplete import filescompleter optparser = MyOptionParser(self, self.extra_info, prog=self.prog) groups = self._groups + [self._anonymous] for group in groups: if group.options: desc = group.description or group.name arggroup = optparser.add_argument_group(desc) for option in group.options: n = option.names() a = option.attrs() arggroup.add_argument(*n, **a) # bash like autocompletion for dirs (appending '/') optparser.add_argument(FILE_OR_DIR, nargs="*").completer = filescompleter return optparser def parse_setoption(self, args, option, namespace=None): parsedoption = self.parse(args, namespace=namespace) for name, value in parsedoption.__dict__.items(): setattr(option, name, value) return getattr(parsedoption, FILE_OR_DIR) def parse_known_args(self, args, namespace=None): """parses and returns a namespace object with known arguments at this point. """ return self.parse_known_and_unknown_args(args, namespace=namespace)[0] def parse_known_and_unknown_args(self, args, namespace=None): """parses and returns a namespace object with known arguments, and the remaining arguments unknown at this point. """ optparser = self._getparser() args = [str(x) if isinstance(x, py.path.local) else x for x in args] return optparser.parse_known_args(args, namespace=namespace) def addini(self, name, help, type=None, default=None): """ register an ini-file option. :name: name of the ini-variable :type: type of the variable, can be ``pathlist``, ``args``, ``linelist`` or ``bool``. :default: default value if no ini-file option exists but is queried. The value of ini-variables can be retrieved via a call to :py:func:`config.getini(name) <_pytest.config.Config.getini>`. """ assert type in (None, "pathlist", "args", "linelist", "bool") self._inidict[name] = (help, type, default) self._ininames.append(name) class ArgumentError(Exception): """ Raised if an Argument instance is created with invalid or inconsistent arguments. """ def __init__(self, msg, option): self.msg = msg self.option_id = str(option) def __str__(self): if self.option_id: return "option %s: %s" % (self.option_id, self.msg) else: return self.msg class Argument(object): """class that mimics the necessary behaviour of optparse.Option it's currently a least effort implementation and ignoring choices and integer prefixes https://docs.python.org/3/library/optparse.html#optparse-standard-option-types """ _typ_map = {"int": int, "string": str, "float": float, "complex": complex} def __init__(self, *names, **attrs): """store parms in private vars for use in add_argument""" self._attrs = attrs self._short_opts = [] self._long_opts = [] self.dest = attrs.get("dest") if "%default" in (attrs.get("help") or ""): warnings.warn( 'pytest now uses argparse. "%default" should be' ' changed to "%(default)s" ', DeprecationWarning, stacklevel=3, ) try: typ = attrs["type"] except KeyError: pass else: # this might raise a keyerror as well, don't want to catch that if isinstance(typ, six.string_types): if typ == "choice": warnings.warn( "`type` argument to addoption() is the string %r." " For choices this is optional and can be omitted, " " but when supplied should be a type (for example `str` or `int`)." " (options: %s)" % (typ, names), DeprecationWarning, stacklevel=4, ) # argparse expects a type here take it from # the type of the first element attrs["type"] = type(attrs["choices"][0]) else: warnings.warn( "`type` argument to addoption() is the string %r, " " but when supplied should be a type (for example `str` or `int`)." " (options: %s)" % (typ, names), DeprecationWarning, stacklevel=4, ) attrs["type"] = Argument._typ_map[typ] # used in test_parseopt -> test_parse_defaultgetter self.type = attrs["type"] else: self.type = typ try: # attribute existence is tested in Config._processopt self.default = attrs["default"] except KeyError: pass self._set_opt_strings(names) if not self.dest: if self._long_opts: self.dest = self._long_opts[0][2:].replace("-", "_") else: try: self.dest = self._short_opts[0][1:] except IndexError: raise ArgumentError("need a long or short option", self) def names(self): return self._short_opts + self._long_opts def attrs(self): # update any attributes set by processopt attrs = "default dest help".split() if self.dest: attrs.append(self.dest) for attr in attrs: try: self._attrs[attr] = getattr(self, attr) except AttributeError: pass if self._attrs.get("help"): a = self._attrs["help"] a = a.replace("%default", "%(default)s") # a = a.replace('%prog', '%(prog)s') self._attrs["help"] = a return self._attrs def _set_opt_strings(self, opts): """directly from optparse might not be necessary as this is passed to argparse later on""" for opt in opts: if len(opt) < 2: raise ArgumentError( "invalid option string %r: " "must be at least two characters long" % opt, self, ) elif len(opt) == 2: if not (opt[0] == "-" and opt[1] != "-"): raise ArgumentError( "invalid short option string %r: " "must be of the form -x, (x any non-dash char)" % opt, self, ) self._short_opts.append(opt) else: if not (opt[0:2] == "--" and opt[2] != "-"): raise ArgumentError( "invalid long option string %r: " "must start with --, followed by non-dash" % opt, self, ) self._long_opts.append(opt) def __repr__(self): args = [] if self._short_opts: args += ["_short_opts: " + repr(self._short_opts)] if self._long_opts: args += ["_long_opts: " + repr(self._long_opts)] args += ["dest: " + repr(self.dest)] if hasattr(self, "type"): args += ["type: " + repr(self.type)] if hasattr(self, "default"): args += ["default: " + repr(self.default)] return "Argument({})".format(", ".join(args)) class OptionGroup(object): def __init__(self, name, description="", parser=None): self.name = name self.description = description self.options = [] self.parser = parser def addoption(self, *optnames, **attrs): """ add an option to this group. if a shortened version of a long option is specified it will be suppressed in the help. addoption('--twowords', '--two-words') results in help showing '--two-words' only, but --twowords gets accepted **and** the automatic destination is in args.twowords """ conflict = set(optnames).intersection( name for opt in self.options for name in opt.names() ) if conflict: raise ValueError("option names %s already added" % conflict) option = Argument(*optnames, **attrs) self._addoption_instance(option, shortupper=False) def _addoption(self, *optnames, **attrs): option = Argument(*optnames, **attrs) self._addoption_instance(option, shortupper=True) def _addoption_instance(self, option, shortupper=False): if not shortupper: for opt in option._short_opts: if opt[0] == "-" and opt[1].islower(): raise ValueError("lowercase shortoptions reserved") if self.parser: self.parser.processoption(option) self.options.append(option) class MyOptionParser(argparse.ArgumentParser): def __init__(self, parser, extra_info=None, prog=None): if not extra_info: extra_info = {} self._parser = parser argparse.ArgumentParser.__init__( self, prog=prog, usage=parser._usage, add_help=False, formatter_class=DropShorterLongHelpFormatter, ) # extra_info is a dict of (param -> value) to display if there's # an usage error to provide more contextual information to the user self.extra_info = extra_info def error(self, message): """Transform argparse error message into UsageError.""" msg = "%s: error: %s" % (self.prog, message) if hasattr(self._parser, "_config_source_hint"): msg = "%s (%s)" % (msg, self._parser._config_source_hint) raise UsageError(self.format_usage() + msg) def parse_args(self, args=None, namespace=None): """allow splitting of positional arguments""" args, argv = self.parse_known_args(args, namespace) if argv: for arg in argv: if arg and arg[0] == "-": lines = ["unrecognized arguments: %s" % (" ".join(argv))] for k, v in sorted(self.extra_info.items()): lines.append(" %s: %s" % (k, v)) self.error("\n".join(lines)) getattr(args, FILE_OR_DIR).extend(argv) return args class DropShorterLongHelpFormatter(argparse.HelpFormatter): """shorten help for long options that differ only in extra hyphens - collapse **long** options that are the same except for extra hyphens - special action attribute map_long_option allows surpressing additional long options - shortcut if there are only two options and one of them is a short one - cache result on action object as this is called at least 2 times """ def _format_action_invocation(self, action): orgstr = argparse.HelpFormatter._format_action_invocation(self, action) if orgstr and orgstr[0] != "-": # only optional arguments return orgstr res = getattr(action, "_formatted_action_invocation", None) if res: return res options = orgstr.split(", ") if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2): # a shortcut for '-h, --help' or '--abc', '-a' action._formatted_action_invocation = orgstr return orgstr return_list = [] option_map = getattr(action, "map_long_option", {}) if option_map is None: option_map = {} short_long = {} for option in options: if len(option) == 2 or option[2] == " ": continue if not option.startswith("--"): raise ArgumentError( 'long optional argument without "--": [%s]' % (option), self ) xxoption = option[2:] if xxoption.split()[0] not in option_map: shortened = xxoption.replace("-", "") if shortened not in short_long or len(short_long[shortened]) < len( xxoption ): short_long[shortened] = xxoption # now short_long has been filled out to the longest with dashes # **and** we keep the right option ordering from add_argument for option in options: if len(option) == 2 or option[2] == " ": return_list.append(option) if option[2:] == short_long.get(option.replace("-", "")): return_list.append(option.replace(" ", "=", 1)) action._formatted_action_invocation = ", ".join(return_list) return action._formatted_action_invocation pytest-4.6.9/src/_pytest/_io/0000775000372000037200000000000013604173421016751 5ustar travistravis00000000000000pytest-4.6.9/src/_pytest/_io/saferepr.py0000664000372000037200000000505513604173400021134 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pprint from six.moves import reprlib def _call_and_format_exception(call, x, *args): try: # Try the vanilla repr and make sure that the result is a string return call(x, *args) except Exception as exc: exc_name = type(exc).__name__ try: exc_info = str(exc) except Exception: exc_info = "unknown" return '<[%s("%s") raised in repr()] %s object at 0x%x>' % ( exc_name, exc_info, x.__class__.__name__, id(x), ) class SafeRepr(reprlib.Repr): """subclass of repr.Repr that limits the resulting size of repr() and includes information on exceptions raised during the call. """ def repr(self, x): return self._callhelper(reprlib.Repr.repr, self, x) def repr_unicode(self, x, level): # Strictly speaking wrong on narrow builds def repr(u): if "'" not in u: return u"'%s'" % u elif '"' not in u: return u'"%s"' % u else: return u"'%s'" % u.replace("'", r"\'") s = repr(x[: self.maxstring]) if len(s) > self.maxstring: i = max(0, (self.maxstring - 3) // 2) j = max(0, self.maxstring - 3 - i) s = repr(x[:i] + x[len(x) - j :]) s = s[:i] + "..." + s[len(s) - j :] return s def repr_instance(self, x, level): return self._callhelper(repr, x) def _callhelper(self, call, x, *args): s = _call_and_format_exception(call, x, *args) if len(s) > self.maxsize: i = max(0, (self.maxsize - 3) // 2) j = max(0, self.maxsize - 3 - i) s = s[:i] + "..." + s[len(s) - j :] return s def safeformat(obj): """return a pretty printed string for the given object. Failing __repr__ functions of user instances will be represented with a short exception info. """ return _call_and_format_exception(pprint.pformat, obj) def saferepr(obj, maxsize=240): """return a size-limited safe repr-string for the given object. Failing __repr__ functions of user instances will be represented with a short exception info and 'saferepr' generally takes care to never raise exceptions itself. This function is a wrapper around the Repr/reprlib functionality of the standard 2.6 lib. """ # review exception handling srepr = SafeRepr() srepr.maxstring = maxsize srepr.maxsize = maxsize srepr.maxother = 160 return srepr.repr(obj) pytest-4.6.9/src/_pytest/_io/__init__.py0000664000372000037200000000000013604173400021045 0ustar travistravis00000000000000pytest-4.6.9/src/_pytest/fixtures.py0000664000372000037200000014513413604173400020433 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import inspect import itertools import sys import warnings from collections import defaultdict from collections import deque from collections import OrderedDict import attr import py import six import _pytest from _pytest import nodes from _pytest._code.code import FormattedExcinfo from _pytest._code.code import TerminalRepr from _pytest.compat import _format_args from _pytest.compat import _PytestWrapper from _pytest.compat import exc_clear from _pytest.compat import FuncargnamesCompatAttr from _pytest.compat import get_real_func from _pytest.compat import get_real_method from _pytest.compat import getfslineno from _pytest.compat import getfuncargnames from _pytest.compat import getimfunc from _pytest.compat import getlocation from _pytest.compat import is_generator from _pytest.compat import isclass from _pytest.compat import NOTSET from _pytest.compat import safe_getattr from _pytest.deprecated import FIXTURE_FUNCTION_CALL from _pytest.deprecated import FIXTURE_NAMED_REQUEST from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME @attr.s(frozen=True) class PseudoFixtureDef(object): cached_result = attr.ib() scope = attr.ib() def pytest_sessionstart(session): import _pytest.python import _pytest.nodes scopename2class.update( { "package": _pytest.python.Package, "class": _pytest.python.Class, "module": _pytest.python.Module, "function": _pytest.nodes.Item, "session": _pytest.main.Session, } ) session._fixturemanager = FixtureManager(session) scopename2class = {} scope2props = dict(session=()) scope2props["package"] = ("fspath",) scope2props["module"] = ("fspath", "module") scope2props["class"] = scope2props["module"] + ("cls",) scope2props["instance"] = scope2props["class"] + ("instance",) scope2props["function"] = scope2props["instance"] + ("function", "keywords") def scopeproperty(name=None, doc=None): def decoratescope(func): scopename = name or func.__name__ def provide(self): if func.__name__ in scope2props[self.scope]: return func(self) raise AttributeError( "%s not available in %s-scoped context" % (scopename, self.scope) ) return property(provide, None, None, func.__doc__) return decoratescope def get_scope_package(node, fixturedef): import pytest cls = pytest.Package current = node fixture_package_name = "%s/%s" % (fixturedef.baseid, "__init__.py") while current and ( type(current) is not cls or fixture_package_name != current.nodeid ): current = current.parent if current is None: return node.session return current def get_scope_node(node, scope): cls = scopename2class.get(scope) if cls is None: raise ValueError("unknown scope") return node.getparent(cls) def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager): # this function will transform all collected calls to a functions # if they use direct funcargs (i.e. direct parametrization) # because we want later test execution to be able to rely on # an existing FixtureDef structure for all arguments. # XXX we can probably avoid this algorithm if we modify CallSpec2 # to directly care for creating the fixturedefs within its methods. if not metafunc._calls[0].funcargs: return # this function call does not have direct parametrization # collect funcargs of all callspecs into a list of values arg2params = {} arg2scope = {} for callspec in metafunc._calls: for argname, argvalue in callspec.funcargs.items(): assert argname not in callspec.params callspec.params[argname] = argvalue arg2params_list = arg2params.setdefault(argname, []) callspec.indices[argname] = len(arg2params_list) arg2params_list.append(argvalue) if argname not in arg2scope: scopenum = callspec._arg2scopenum.get(argname, scopenum_function) arg2scope[argname] = scopes[scopenum] callspec.funcargs.clear() # register artificial FixtureDef's so that later at test execution # time we can rely on a proper FixtureDef to exist for fixture setup. arg2fixturedefs = metafunc._arg2fixturedefs for argname, valuelist in arg2params.items(): # if we have a scope that is higher than function we need # to make sure we only ever create an according fixturedef on # a per-scope basis. We thus store and cache the fixturedef on the # node related to the scope. scope = arg2scope[argname] node = None if scope != "function": node = get_scope_node(collector, scope) if node is None: assert scope == "class" and isinstance(collector, _pytest.python.Module) # use module-level collector for class-scope (for now) node = collector if node and argname in node._name2pseudofixturedef: arg2fixturedefs[argname] = [node._name2pseudofixturedef[argname]] else: fixturedef = FixtureDef( fixturemanager, "", argname, get_direct_param_fixture_func, arg2scope[argname], valuelist, False, False, ) arg2fixturedefs[argname] = [fixturedef] if node is not None: node._name2pseudofixturedef[argname] = fixturedef def getfixturemarker(obj): """ return fixturemarker or None if it doesn't exist or raised exceptions.""" try: return getattr(obj, "_pytestfixturefunction", None) except TEST_OUTCOME: # some objects raise errors like request (from flask import request) # we don't expect them to be fixture functions return None def get_parametrized_fixture_keys(item, scopenum): """ return list of keys for all parametrized arguments which match the specified scope. """ assert scopenum < scopenum_function # function try: cs = item.callspec except AttributeError: pass else: # cs.indices.items() is random order of argnames. Need to # sort this so that different calls to # get_parametrized_fixture_keys will be deterministic. for argname, param_index in sorted(cs.indices.items()): if cs._arg2scopenum[argname] != scopenum: continue if scopenum == 0: # session key = (argname, param_index) elif scopenum == 1: # package key = (argname, param_index, item.fspath.dirpath()) elif scopenum == 2: # module key = (argname, param_index, item.fspath) elif scopenum == 3: # class key = (argname, param_index, item.fspath, item.cls) yield key # algorithm for sorting on a per-parametrized resource setup basis # it is called for scopenum==0 (session) first and performs sorting # down to the lower scopes such as to minimize number of "high scope" # setups and teardowns def reorder_items(items): argkeys_cache = {} items_by_argkey = {} for scopenum in range(0, scopenum_function): argkeys_cache[scopenum] = d = {} items_by_argkey[scopenum] = item_d = defaultdict(deque) for item in items: keys = OrderedDict.fromkeys(get_parametrized_fixture_keys(item, scopenum)) if keys: d[item] = keys for key in keys: item_d[key].append(item) items = OrderedDict.fromkeys(items) return list(reorder_items_atscope(items, argkeys_cache, items_by_argkey, 0)) def fix_cache_order(item, argkeys_cache, items_by_argkey): for scopenum in range(0, scopenum_function): for key in argkeys_cache[scopenum].get(item, []): items_by_argkey[scopenum][key].appendleft(item) def reorder_items_atscope(items, argkeys_cache, items_by_argkey, scopenum): if scopenum >= scopenum_function or len(items) < 3: return items ignore = set() items_deque = deque(items) items_done = OrderedDict() scoped_items_by_argkey = items_by_argkey[scopenum] scoped_argkeys_cache = argkeys_cache[scopenum] while items_deque: no_argkey_group = OrderedDict() slicing_argkey = None while items_deque: item = items_deque.popleft() if item in items_done or item in no_argkey_group: continue argkeys = OrderedDict.fromkeys( k for k in scoped_argkeys_cache.get(item, []) if k not in ignore ) if not argkeys: no_argkey_group[item] = None else: slicing_argkey, _ = argkeys.popitem() # we don't have to remove relevant items from later in the deque because they'll just be ignored matching_items = [ i for i in scoped_items_by_argkey[slicing_argkey] if i in items ] for i in reversed(matching_items): fix_cache_order(i, argkeys_cache, items_by_argkey) items_deque.appendleft(i) break if no_argkey_group: no_argkey_group = reorder_items_atscope( no_argkey_group, argkeys_cache, items_by_argkey, scopenum + 1 ) for item in no_argkey_group: items_done[item] = None ignore.add(slicing_argkey) return items_done def fillfixtures(function): """ fill missing funcargs for a test function. """ try: request = function._request except AttributeError: # XXX this special code path is only expected to execute # with the oejskit plugin. It uses classes with funcargs # and we thus have to work a bit to allow this. fm = function.session._fixturemanager fi = fm.getfixtureinfo(function.parent, function.obj, None) function._fixtureinfo = fi request = function._request = FixtureRequest(function) request._fillfixtures() # prune out funcargs for jstests newfuncargs = {} for name in fi.argnames: newfuncargs[name] = function.funcargs[name] function.funcargs = newfuncargs else: request._fillfixtures() def get_direct_param_fixture_func(request): return request.param @attr.s(slots=True) class FuncFixtureInfo(object): # original function argument names argnames = attr.ib(type=tuple) # argnames that function immediately requires. These include argnames + # fixture names specified via usefixtures and via autouse=True in fixture # definitions. initialnames = attr.ib(type=tuple) names_closure = attr.ib() # List[str] name2fixturedefs = attr.ib() # List[str, List[FixtureDef]] def prune_dependency_tree(self): """Recompute names_closure from initialnames and name2fixturedefs Can only reduce names_closure, which means that the new closure will always be a subset of the old one. The order is preserved. This method is needed because direct parametrization may shadow some of the fixtures that were included in the originally built dependency tree. In this way the dependency tree can get pruned, and the closure of argnames may get reduced. """ closure = set() working_set = set(self.initialnames) while working_set: argname = working_set.pop() # argname may be smth not included in the original names_closure, # in which case we ignore it. This currently happens with pseudo # FixtureDefs which wrap 'get_direct_param_fixture_func(request)'. # So they introduce the new dependency 'request' which might have # been missing in the original tree (closure). if argname not in closure and argname in self.names_closure: closure.add(argname) if argname in self.name2fixturedefs: working_set.update(self.name2fixturedefs[argname][-1].argnames) self.names_closure[:] = sorted(closure, key=self.names_closure.index) class FixtureRequest(FuncargnamesCompatAttr): """ A request for a fixture from a test or fixture function. A request object gives access to the requesting test context and has an optional ``param`` attribute in case the fixture is parametrized indirectly. """ def __init__(self, pyfuncitem): self._pyfuncitem = pyfuncitem #: fixture for which this request is being performed self.fixturename = None #: Scope string, one of "function", "class", "module", "session" self.scope = "function" self._fixture_defs = {} # argname -> FixtureDef fixtureinfo = pyfuncitem._fixtureinfo self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy() self._arg2index = {} self._fixturemanager = pyfuncitem.session._fixturemanager @property def fixturenames(self): """names of all active fixtures in this request""" result = list(self._pyfuncitem._fixtureinfo.names_closure) result.extend(set(self._fixture_defs).difference(result)) return result @property def node(self): """ underlying collection node (depends on current request scope)""" return self._getscopeitem(self.scope) def _getnextfixturedef(self, argname): fixturedefs = self._arg2fixturedefs.get(argname, None) if fixturedefs is None: # we arrive here because of a dynamic call to # getfixturevalue(argname) usage which was naturally # not known at parsing/collection time parentid = self._pyfuncitem.parent.nodeid fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid) self._arg2fixturedefs[argname] = fixturedefs # fixturedefs list is immutable so we maintain a decreasing index index = self._arg2index.get(argname, 0) - 1 if fixturedefs is None or (-index > len(fixturedefs)): raise FixtureLookupError(argname, self) self._arg2index[argname] = index return fixturedefs[index] @property def config(self): """ the pytest config object associated with this request. """ return self._pyfuncitem.config @scopeproperty() def function(self): """ test function object if the request has a per-function scope. """ return self._pyfuncitem.obj @scopeproperty("class") def cls(self): """ class (can be None) where the test function was collected. """ clscol = self._pyfuncitem.getparent(_pytest.python.Class) if clscol: return clscol.obj @property def instance(self): """ instance (can be None) on which test function was collected. """ # unittest support hack, see _pytest.unittest.TestCaseFunction try: return self._pyfuncitem._testcase except AttributeError: function = getattr(self, "function", None) return getattr(function, "__self__", None) @scopeproperty() def module(self): """ python module object where the test function was collected. """ return self._pyfuncitem.getparent(_pytest.python.Module).obj @scopeproperty() def fspath(self): """ the file system path of the test module which collected this test. """ return self._pyfuncitem.fspath @property def keywords(self): """ keywords/markers dictionary for the underlying node. """ return self.node.keywords @property def session(self): """ pytest session object. """ return self._pyfuncitem.session def addfinalizer(self, finalizer): """ add finalizer/teardown function to be called after the last test within the requesting test context finished execution. """ # XXX usually this method is shadowed by fixturedef specific ones self._addfinalizer(finalizer, scope=self.scope) def _addfinalizer(self, finalizer, scope): colitem = self._getscopeitem(scope) self._pyfuncitem.session._setupstate.addfinalizer( finalizer=finalizer, colitem=colitem ) def applymarker(self, marker): """ Apply a marker to a single test function invocation. This method is useful if you don't want to have a keyword/marker on all function invocations. :arg marker: a :py:class:`_pytest.mark.MarkDecorator` object created by a call to ``pytest.mark.NAME(...)``. """ self.node.add_marker(marker) def raiseerror(self, msg): """ raise a FixtureLookupError with the given message. """ raise self._fixturemanager.FixtureLookupError(None, self, msg) def _fillfixtures(self): item = self._pyfuncitem fixturenames = getattr(item, "fixturenames", self.fixturenames) for argname in fixturenames: if argname not in item.funcargs: item.funcargs[argname] = self.getfixturevalue(argname) def getfixturevalue(self, argname): """ Dynamically run a named fixture function. Declaring fixtures via function argument is recommended where possible. But if you can only decide whether to use another fixture at test setup time, you may use this function to retrieve it inside a fixture or test function body. """ return self._get_active_fixturedef(argname).cached_result[0] def getfuncargvalue(self, argname): """ Deprecated, use getfixturevalue. """ from _pytest import deprecated warnings.warn(deprecated.GETFUNCARGVALUE, stacklevel=2) return self.getfixturevalue(argname) def _get_active_fixturedef(self, argname): try: return self._fixture_defs[argname] except KeyError: try: fixturedef = self._getnextfixturedef(argname) except FixtureLookupError: if argname == "request": cached_result = (self, [0], None) scope = "function" return PseudoFixtureDef(cached_result, scope) raise # remove indent to prevent the python3 exception # from leaking into the call self._compute_fixture_value(fixturedef) self._fixture_defs[argname] = fixturedef return fixturedef def _get_fixturestack(self): current = self values = [] while 1: fixturedef = getattr(current, "_fixturedef", None) if fixturedef is None: values.reverse() return values values.append(fixturedef) current = current._parent_request def _compute_fixture_value(self, fixturedef): """ Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will force the FixtureDef object to throw away any previous results and compute a new fixture value, which will be stored into the FixtureDef object itself. :param FixtureDef fixturedef: """ # prepare a subrequest object before calling fixture function # (latter managed by fixturedef) argname = fixturedef.argname funcitem = self._pyfuncitem scope = fixturedef.scope try: param = funcitem.callspec.getparam(argname) except (AttributeError, ValueError): param = NOTSET param_index = 0 has_params = fixturedef.params is not None fixtures_not_supported = getattr(funcitem, "nofuncargs", False) if has_params and fixtures_not_supported: msg = ( "{name} does not support fixtures, maybe unittest.TestCase subclass?\n" "Node id: {nodeid}\n" "Function type: {typename}" ).format( name=funcitem.name, nodeid=funcitem.nodeid, typename=type(funcitem).__name__, ) fail(msg, pytrace=False) if has_params: frame = inspect.stack()[3] frameinfo = inspect.getframeinfo(frame[0]) source_path = frameinfo.filename source_lineno = frameinfo.lineno source_path = py.path.local(source_path) if source_path.relto(funcitem.config.rootdir): source_path = source_path.relto(funcitem.config.rootdir) msg = ( "The requested fixture has no parameter defined for test:\n" " {}\n\n" "Requested fixture '{}' defined in:\n{}" "\n\nRequested here:\n{}:{}".format( funcitem.nodeid, fixturedef.argname, getlocation(fixturedef.func, funcitem.config.rootdir), source_path, source_lineno, ) ) fail(msg, pytrace=False) else: param_index = funcitem.callspec.indices[argname] # if a parametrize invocation set a scope it will override # the static scope defined with the fixture function paramscopenum = funcitem.callspec._arg2scopenum.get(argname) if paramscopenum is not None: scope = scopes[paramscopenum] subrequest = SubRequest(self, scope, param, param_index, fixturedef) # check if a higher-level scoped fixture accesses a lower level one subrequest._check_scope(argname, self.scope, scope) # clear sys.exc_info before invoking the fixture (python bug?) # if it's not explicitly cleared it will leak into the call exc_clear() try: # call the fixture function fixturedef.execute(request=subrequest) finally: self._schedule_finalizers(fixturedef, subrequest) def _schedule_finalizers(self, fixturedef, subrequest): # if fixture function failed it might have registered finalizers self.session._setupstate.addfinalizer( functools.partial(fixturedef.finish, request=subrequest), subrequest.node ) def _check_scope(self, argname, invoking_scope, requested_scope): if argname == "request": return if scopemismatch(invoking_scope, requested_scope): # try to report something helpful lines = self._factorytraceback() fail( "ScopeMismatch: You tried to access the %r scoped " "fixture %r with a %r scoped request object, " "involved factories\n%s" % ((requested_scope, argname, invoking_scope, "\n".join(lines))), pytrace=False, ) def _factorytraceback(self): lines = [] for fixturedef in self._get_fixturestack(): factory = fixturedef.func fs, lineno = getfslineno(factory) p = self._pyfuncitem.session.fspath.bestrelpath(fs) args = _format_args(factory) lines.append("%s:%d: def %s%s" % (p, lineno + 1, factory.__name__, args)) return lines def _getscopeitem(self, scope): if scope == "function": # this might also be a non-function Item despite its attribute name return self._pyfuncitem if scope == "package": node = get_scope_package(self._pyfuncitem, self._fixturedef) else: node = get_scope_node(self._pyfuncitem, scope) if node is None and scope == "class": # fallback to function item itself node = self._pyfuncitem assert node, 'Could not obtain a node for scope "{}" for function {!r}'.format( scope, self._pyfuncitem ) return node def __repr__(self): return "" % (self.node) class SubRequest(FixtureRequest): """ a sub request for handling getting a fixture from a test function/fixture. """ def __init__(self, request, scope, param, param_index, fixturedef): self._parent_request = request self.fixturename = fixturedef.argname if param is not NOTSET: self.param = param self.param_index = param_index self.scope = scope self._fixturedef = fixturedef self._pyfuncitem = request._pyfuncitem self._fixture_defs = request._fixture_defs self._arg2fixturedefs = request._arg2fixturedefs self._arg2index = request._arg2index self._fixturemanager = request._fixturemanager def __repr__(self): return "" % (self.fixturename, self._pyfuncitem) def addfinalizer(self, finalizer): self._fixturedef.addfinalizer(finalizer) def _schedule_finalizers(self, fixturedef, subrequest): # if the executing fixturedef was not explicitly requested in the argument list (via # getfixturevalue inside the fixture call) then ensure this fixture def will be finished # first if fixturedef.argname not in self.funcargnames: fixturedef.addfinalizer( functools.partial(self._fixturedef.finish, request=self) ) super(SubRequest, self)._schedule_finalizers(fixturedef, subrequest) scopes = "session package module class function".split() scopenum_function = scopes.index("function") def scopemismatch(currentscope, newscope): return scopes.index(newscope) > scopes.index(currentscope) def scope2index(scope, descr, where=None): """Look up the index of ``scope`` and raise a descriptive value error if not defined. """ try: return scopes.index(scope) except ValueError: fail( "{} {}got an unexpected scope value '{}'".format( descr, "from {} ".format(where) if where else "", scope ), pytrace=False, ) class FixtureLookupError(LookupError): """ could not return a requested Fixture (missing or invalid). """ def __init__(self, argname, request, msg=None): self.argname = argname self.request = request self.fixturestack = request._get_fixturestack() self.msg = msg def formatrepr(self): tblines = [] addline = tblines.append stack = [self.request._pyfuncitem.obj] stack.extend(map(lambda x: x.func, self.fixturestack)) msg = self.msg if msg is not None: # the last fixture raise an error, let's present # it at the requesting side stack = stack[:-1] for function in stack: fspath, lineno = getfslineno(function) try: lines, _ = inspect.getsourcelines(get_real_func(function)) except (IOError, IndexError, TypeError): error_msg = "file %s, line %s: source code not available" addline(error_msg % (fspath, lineno + 1)) else: addline("file %s, line %s" % (fspath, lineno + 1)) for i, line in enumerate(lines): line = line.rstrip() addline(" " + line) if line.lstrip().startswith("def"): break if msg is None: fm = self.request._fixturemanager available = set() parentid = self.request._pyfuncitem.parent.nodeid for name, fixturedefs in fm._arg2fixturedefs.items(): faclist = list(fm._matchfactories(fixturedefs, parentid)) if faclist: available.add(name) if self.argname in available: msg = " recursive dependency involving fixture '{}' detected".format( self.argname ) else: msg = "fixture '{}' not found".format(self.argname) msg += "\n available fixtures: {}".format(", ".join(sorted(available))) msg += "\n use 'pytest --fixtures [testpath]' for help on them." return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname) class FixtureLookupErrorRepr(TerminalRepr): def __init__(self, filename, firstlineno, tblines, errorstring, argname): self.tblines = tblines self.errorstring = errorstring self.filename = filename self.firstlineno = firstlineno self.argname = argname def toterminal(self, tw): # tw.line("FixtureLookupError: %s" %(self.argname), red=True) for tbline in self.tblines: tw.line(tbline.rstrip()) lines = self.errorstring.split("\n") if lines: tw.line( "{} {}".format(FormattedExcinfo.fail_marker, lines[0].strip()), red=True, ) for line in lines[1:]: tw.line( "{} {}".format(FormattedExcinfo.flow_marker, line.strip()), red=True, ) tw.line() tw.line("%s:%d" % (self.filename, self.firstlineno + 1)) def fail_fixturefunc(fixturefunc, msg): fs, lineno = getfslineno(fixturefunc) location = "%s:%s" % (fs, lineno + 1) source = _pytest._code.Source(fixturefunc) fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, pytrace=False) def call_fixture_func(fixturefunc, request, kwargs): yieldctx = is_generator(fixturefunc) if yieldctx: it = fixturefunc(**kwargs) res = next(it) finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, it) request.addfinalizer(finalizer) else: res = fixturefunc(**kwargs) return res def _teardown_yield_fixture(fixturefunc, it): """Executes the teardown of a fixture function by advancing the iterator after the yield and ensure the iteration ends (if not it means there is more than one yield in the function)""" try: next(it) except StopIteration: pass else: fail_fixturefunc( fixturefunc, "yield_fixture function has more than one 'yield'" ) class FixtureDef(object): """ A container for a factory definition. """ def __init__( self, fixturemanager, baseid, argname, func, scope, params, unittest=False, ids=None, ): self._fixturemanager = fixturemanager self.baseid = baseid or "" self.has_location = baseid is not None self.func = func self.argname = argname self.scope = scope self.scopenum = scope2index( scope or "function", descr="Fixture '{}'".format(func.__name__), where=baseid, ) self.params = params self.argnames = getfuncargnames(func, is_method=unittest) self.unittest = unittest self.ids = ids self._finalizers = [] def addfinalizer(self, finalizer): self._finalizers.append(finalizer) def finish(self, request): exceptions = [] try: while self._finalizers: try: func = self._finalizers.pop() func() except: # noqa exceptions.append(sys.exc_info()) if exceptions: e = exceptions[0] # Ensure to not keep frame references through traceback. del exceptions six.reraise(*e) finally: hook = self._fixturemanager.session.gethookproxy(request.node.fspath) hook.pytest_fixture_post_finalizer(fixturedef=self, request=request) # even if finalization fails, we invalidate # the cached fixture value and remove # all finalizers because they may be bound methods which will # keep instances alive if hasattr(self, "cached_result"): del self.cached_result self._finalizers = [] def execute(self, request): # get required arguments and register our own finish() # with their finalization for argname in self.argnames: fixturedef = request._get_active_fixturedef(argname) if argname != "request": fixturedef.addfinalizer(functools.partial(self.finish, request=request)) my_cache_key = request.param_index cached_result = getattr(self, "cached_result", None) if cached_result is not None: result, cache_key, err = cached_result if my_cache_key == cache_key: if err is not None: six.reraise(*err) else: return result # we have a previous but differently parametrized fixture instance # so we need to tear it down before creating a new one self.finish(request) assert not hasattr(self, "cached_result") hook = self._fixturemanager.session.gethookproxy(request.node.fspath) return hook.pytest_fixture_setup(fixturedef=self, request=request) def __repr__(self): return "" % ( self.argname, self.scope, self.baseid, ) def resolve_fixture_function(fixturedef, request): """Gets the actual callable that can be called to obtain the fixture value, dealing with unittest-specific instances and bound methods. """ fixturefunc = fixturedef.func if fixturedef.unittest: if request.instance is not None: # bind the unbound method to the TestCase instance fixturefunc = fixturedef.func.__get__(request.instance) else: # the fixture function needs to be bound to the actual # request.instance so that code working with "fixturedef" behaves # as expected. if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) return fixturefunc def pytest_fixture_setup(fixturedef, request): """ Execution of fixture setup. """ kwargs = {} for argname in fixturedef.argnames: fixdef = request._get_active_fixturedef(argname) result, arg_cache_key, exc = fixdef.cached_result request._check_scope(argname, request.scope, fixdef.scope) kwargs[argname] = result fixturefunc = resolve_fixture_function(fixturedef, request) my_cache_key = request.param_index try: result = call_fixture_func(fixturefunc, request, kwargs) except TEST_OUTCOME: fixturedef.cached_result = (None, my_cache_key, sys.exc_info()) raise fixturedef.cached_result = (result, my_cache_key, None) return result def _ensure_immutable_ids(ids): if ids is None: return if callable(ids): return ids return tuple(ids) def wrap_function_to_error_out_if_called_directly(function, fixture_marker): """Wrap the given fixture function so we can raise an error about it being called directly, instead of used as an argument in a test function. """ message = FIXTURE_FUNCTION_CALL.format( name=fixture_marker.name or function.__name__ ) @six.wraps(function) def result(*args, **kwargs): fail(message, pytrace=False) # keep reference to the original function in our own custom attribute so we don't unwrap # further than this point and lose useful wrappings like @mock.patch (#3774) result.__pytest_wrapped__ = _PytestWrapper(function) return result @attr.s(frozen=True) class FixtureFunctionMarker(object): scope = attr.ib() params = attr.ib(converter=attr.converters.optional(tuple)) autouse = attr.ib(default=False) ids = attr.ib(default=None, converter=_ensure_immutable_ids) name = attr.ib(default=None) def __call__(self, function): if isclass(function): raise ValueError("class fixtures not supported (maybe in the future)") if getattr(function, "_pytestfixturefunction", False): raise ValueError( "fixture is being applied more than once to the same function" ) function = wrap_function_to_error_out_if_called_directly(function, self) name = self.name or function.__name__ if name == "request": warnings.warn(FIXTURE_NAMED_REQUEST) function._pytestfixturefunction = self return function def fixture(scope="function", params=None, autouse=False, ids=None, name=None): """Decorator to mark a fixture factory function. This decorator can be used, with or without parameters, to define a fixture function. The name of the fixture function can later be referenced to cause its invocation ahead of running tests: test modules or classes can use the ``pytest.mark.usefixtures(fixturename)`` marker. Test functions can directly use fixture names as input arguments in which case the fixture instance returned from the fixture function will be injected. Fixtures can provide their values to test functions using ``return`` or ``yield`` statements. When using ``yield`` the code block after the ``yield`` statement is executed as teardown code regardless of the test outcome, and must yield exactly once. :arg scope: the scope for which this fixture is shared, one of ``"function"`` (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``. ``"package"`` is considered **experimental** at this time. :arg params: an optional list of parameters which will cause multiple invocations of the fixture function and all of the tests using it. The current parameter is available in ``request.param``. :arg autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture. :arg ids: list of string ids each corresponding to the params so that they are part of the test id. If no ids are provided they will be generated automatically from the params. :arg name: the name of the fixture. This defaults to the name of the decorated function. If a fixture is used in the same module in which it is defined, the function name of the fixture will be shadowed by the function arg that requests the fixture; one way to resolve this is to name the decorated function ``fixture_`` and then use ``@pytest.fixture(name='')``. """ if callable(scope) and params is None and autouse is False: # direct decoration return FixtureFunctionMarker("function", params, autouse, name=name)(scope) if params is not None and not isinstance(params, (list, tuple)): params = list(params) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=None): """ (return a) decorator to mark a yield-fixture factory function. .. deprecated:: 3.0 Use :py:func:`pytest.fixture` directly instead. """ return fixture(scope=scope, params=params, autouse=autouse, ids=ids, name=name) defaultfuncargprefixmarker = fixture() @fixture(scope="session") def pytestconfig(request): """Session-scoped fixture that returns the :class:`_pytest.config.Config` object. Example:: def test_foo(pytestconfig): if pytestconfig.getoption("verbose") > 0: ... """ return request.config def pytest_addoption(parser): parser.addini( "usefixtures", type="args", default=[], help="list of default fixtures to be used with this project", ) class FixtureManager(object): """ pytest fixtures definitions and information is stored and managed from this class. During collection fm.parsefactories() is called multiple times to parse fixture function definitions into FixtureDef objects and internal data structures. During collection of test functions, metafunc-mechanics instantiate a FuncFixtureInfo object which is cached per node/func-name. This FuncFixtureInfo object is later retrieved by Function nodes which themselves offer a fixturenames attribute. The FuncFixtureInfo object holds information about fixtures and FixtureDefs relevant for a particular function. An initial list of fixtures is assembled like this: - ini-defined usefixtures - autouse-marked fixtures along the collection chain up from the function - usefixtures markers at module/class/function level - test function funcargs Subsequently the funcfixtureinfo.fixturenames attribute is computed as the closure of the fixtures needed to setup the initial fixtures, i. e. fixtures needed by fixture functions themselves are appended to the fixturenames list. Upon the test-setup phases all fixturenames are instantiated, retrieved by a lookup of their FuncFixtureInfo. """ FixtureLookupError = FixtureLookupError FixtureLookupErrorRepr = FixtureLookupErrorRepr def __init__(self, session): self.session = session self.config = session.config self._arg2fixturedefs = {} self._holderobjseen = set() self._arg2finish = {} self._nodeid_and_autousenames = [("", self.config.getini("usefixtures"))] session.config.pluginmanager.register(self, "funcmanage") def _get_direct_parametrize_args(self, node): """This function returns all the direct parametrization arguments of a node, so we don't mistake them for fixtures Check https://github.com/pytest-dev/pytest/issues/5036 This things are done later as well when dealing with parametrization so this could be improved """ from _pytest.mark import ParameterSet parametrize_argnames = [] for marker in node.iter_markers(name="parametrize"): if not marker.kwargs.get("indirect", False): p_argnames, _ = ParameterSet._parse_parametrize_args( *marker.args, **marker.kwargs ) parametrize_argnames.extend(p_argnames) return parametrize_argnames def getfixtureinfo(self, node, func, cls, funcargs=True): if funcargs and not getattr(node, "nofuncargs", False): argnames = getfuncargnames(func, cls=cls) else: argnames = () usefixtures = itertools.chain.from_iterable( mark.args for mark in node.iter_markers(name="usefixtures") ) initialnames = tuple(usefixtures) + argnames fm = node.session._fixturemanager initialnames, names_closure, arg2fixturedefs = fm.getfixtureclosure( initialnames, node, ignore_args=self._get_direct_parametrize_args(node) ) return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs) def pytest_plugin_registered(self, plugin): nodeid = None try: p = py.path.local(plugin.__file__).realpath() except AttributeError: pass else: # construct the base nodeid which is later used to check # what fixtures are visible for particular tests (as denoted # by their test id) if p.basename.startswith("conftest.py"): nodeid = p.dirpath().relto(self.config.rootdir) if p.sep != nodes.SEP: nodeid = nodeid.replace(p.sep, nodes.SEP) self.parsefactories(plugin, nodeid) def _getautousenames(self, nodeid): """ return a tuple of fixture names to be used. """ autousenames = [] for baseid, basenames in self._nodeid_and_autousenames: if nodeid.startswith(baseid): if baseid: i = len(baseid) nextchar = nodeid[i : i + 1] if nextchar and nextchar not in ":/": continue autousenames.extend(basenames) return autousenames def getfixtureclosure(self, fixturenames, parentnode, ignore_args=()): # collect the closure of all fixtures , starting with the given # fixturenames as the initial set. As we have to visit all # factory definitions anyway, we also return an arg2fixturedefs # mapping so that the caller can reuse it and does not have # to re-discover fixturedefs again for each fixturename # (discovering matching fixtures for a given name/node is expensive) parentid = parentnode.nodeid fixturenames_closure = self._getautousenames(parentid) def merge(otherlist): for arg in otherlist: if arg not in fixturenames_closure: fixturenames_closure.append(arg) merge(fixturenames) # at this point, fixturenames_closure contains what we call "initialnames", # which is a set of fixturenames the function immediately requests. We # need to return it as well, so save this. initialnames = tuple(fixturenames_closure) arg2fixturedefs = {} lastlen = -1 while lastlen != len(fixturenames_closure): lastlen = len(fixturenames_closure) for argname in fixturenames_closure: if argname in ignore_args: continue if argname in arg2fixturedefs: continue fixturedefs = self.getfixturedefs(argname, parentid) if fixturedefs: arg2fixturedefs[argname] = fixturedefs merge(fixturedefs[-1].argnames) def sort_by_scope(arg_name): try: fixturedefs = arg2fixturedefs[arg_name] except KeyError: return scopes.index("function") else: return fixturedefs[-1].scopenum fixturenames_closure.sort(key=sort_by_scope) return initialnames, fixturenames_closure, arg2fixturedefs def pytest_generate_tests(self, metafunc): for argname in metafunc.fixturenames: faclist = metafunc._arg2fixturedefs.get(argname) if faclist: fixturedef = faclist[-1] if fixturedef.params is not None: markers = list(metafunc.definition.iter_markers("parametrize")) for parametrize_mark in markers: if "argnames" in parametrize_mark.kwargs: argnames = parametrize_mark.kwargs["argnames"] else: argnames = parametrize_mark.args[0] if not isinstance(argnames, (tuple, list)): argnames = [ x.strip() for x in argnames.split(",") if x.strip() ] if argname in argnames: break else: metafunc.parametrize( argname, fixturedef.params, indirect=True, scope=fixturedef.scope, ids=fixturedef.ids, ) else: continue # will raise FixtureLookupError at setup time def pytest_collection_modifyitems(self, items): # separate parametrized setups items[:] = reorder_items(items) def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False): if nodeid is not NOTSET: holderobj = node_or_obj else: holderobj = node_or_obj.obj nodeid = node_or_obj.nodeid if holderobj in self._holderobjseen: return self._holderobjseen.add(holderobj) autousenames = [] for name in dir(holderobj): # The attribute can be an arbitrary descriptor, so the attribute # access below can raise. safe_getatt() ignores such exceptions. obj = safe_getattr(holderobj, name, None) marker = getfixturemarker(obj) if not isinstance(marker, FixtureFunctionMarker): # magic globals with __getattr__ might have got us a wrong # fixture attribute continue if marker.name: name = marker.name # during fixture definition we wrap the original fixture function # to issue a warning if called directly, so here we unwrap it in order to not emit the warning # when pytest itself calls the fixture function if six.PY2 and unittest: # hack on Python 2 because of the unbound methods obj = get_real_func(obj) else: obj = get_real_method(obj, holderobj) fixture_def = FixtureDef( self, nodeid, name, obj, marker.scope, marker.params, unittest=unittest, ids=marker.ids, ) faclist = self._arg2fixturedefs.setdefault(name, []) if fixture_def.has_location: faclist.append(fixture_def) else: # fixturedefs with no location are at the front # so this inserts the current fixturedef after the # existing fixturedefs from external plugins but # before the fixturedefs provided in conftests. i = len([f for f in faclist if not f.has_location]) faclist.insert(i, fixture_def) if marker.autouse: autousenames.append(name) if autousenames: self._nodeid_and_autousenames.append((nodeid or "", autousenames)) def getfixturedefs(self, argname, nodeid): """ Gets a list of fixtures which are applicable to the given node id. :param str argname: name of the fixture to search for :param str nodeid: full node id of the requesting test. :return: list[FixtureDef] """ try: fixturedefs = self._arg2fixturedefs[argname] except KeyError: return None return tuple(self._matchfactories(fixturedefs, nodeid)) def _matchfactories(self, fixturedefs, nodeid): for fixturedef in fixturedefs: if nodes.ischildnode(fixturedef.baseid, nodeid): yield fixturedef pytest-4.6.9/src/pytest.py0000664000372000037200000000602713604173400016420 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # PYTHON_ARGCOMPLETE_OK """ pytest: unit and functional testing with Python. """ # else we are imported from _pytest import __version__ from _pytest.assertion import register_assert_rewrite from _pytest.config import cmdline from _pytest.config import hookimpl from _pytest.config import hookspec from _pytest.config import main from _pytest.config import UsageError from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.fixtures import fillfixtures as _fillfuncargs from _pytest.fixtures import fixture from _pytest.fixtures import yield_fixture from _pytest.freeze_support import freeze_includes from _pytest.main import Session from _pytest.mark import MARK_GEN as mark from _pytest.mark import param from _pytest.nodes import Collector from _pytest.nodes import File from _pytest.nodes import Item from _pytest.outcomes import exit from _pytest.outcomes import fail from _pytest.outcomes import importorskip from _pytest.outcomes import skip from _pytest.outcomes import xfail from _pytest.python import Class from _pytest.python import Function from _pytest.python import Instance from _pytest.python import Module from _pytest.python import Package from _pytest.python_api import approx from _pytest.python_api import raises from _pytest.recwarn import deprecated_call from _pytest.recwarn import warns from _pytest.warning_types import PytestAssertRewriteWarning from _pytest.warning_types import PytestCacheWarning from _pytest.warning_types import PytestCollectionWarning from _pytest.warning_types import PytestConfigWarning from _pytest.warning_types import PytestDeprecationWarning from _pytest.warning_types import PytestExperimentalApiWarning from _pytest.warning_types import PytestUnhandledCoroutineWarning from _pytest.warning_types import PytestUnknownMarkWarning from _pytest.warning_types import PytestWarning from _pytest.warning_types import RemovedInPytest4Warning set_trace = __pytestPDB.set_trace __all__ = [ "__version__", "_fillfuncargs", "approx", "Class", "cmdline", "Collector", "deprecated_call", "exit", "fail", "File", "fixture", "freeze_includes", "Function", "hookimpl", "hookspec", "importorskip", "Instance", "Item", "main", "mark", "Module", "Package", "param", "PytestAssertRewriteWarning", "PytestCacheWarning", "PytestCollectionWarning", "PytestConfigWarning", "PytestDeprecationWarning", "PytestExperimentalApiWarning", "PytestUnhandledCoroutineWarning", "PytestUnknownMarkWarning", "PytestWarning", "raises", "register_assert_rewrite", "RemovedInPytest4Warning", "Session", "set_trace", "skip", "UsageError", "warns", "xfail", "yield_fixture", ] if __name__ == "__main__": # if run as a script or by 'python -m pytest' # we trigger the below "else" condition by the following import import pytest raise SystemExit(pytest.main()) else: from _pytest.compat import _setup_collect_fakemodule _setup_collect_fakemodule() pytest-4.6.9/tox.ini0000664000372000037200000001277713604173400015253 0ustar travistravis00000000000000[tox] isolated_build = True minversion = 3.5.3 distshare = {homedir}/.tox/distshare # make sure to update environment list in travis.yml and appveyor.yml envlist = linting py27 py34 py35 py36 py37 py38 pypy pypy3 {py27,py37}-{pexpect,xdist,twisted,numpy,pluggymaster} py27-nobyte-xdist doctesting py37-freeze docs [testenv] commands = {env:_PYTEST_TOX_COVERAGE_RUN:} pytest {posargs:{env:_PYTEST_TOX_DEFAULT_POSARGS:}} coverage: coverage combine coverage: coverage report passenv = USER USERNAME COVERAGE_* TRAVIS PYTEST_ADDOPTS setenv = _PYTEST_TOX_DEFAULT_POSARGS={env:_PYTEST_TOX_POSARGS_LSOF:} {env:_PYTEST_TOX_POSARGS_PEXPECT:} {env:_PYTEST_TOX_POSARGS_TWISTED:} {env:_PYTEST_TOX_POSARGS_XDIST:} # Configuration to run with coverage similar to Travis/Appveyor, e.g. # "tox -e py37-coverage". coverage: _PYTEST_TOX_COVERAGE_RUN=coverage run -m coverage: _PYTEST_TOX_EXTRA_DEP=coverage-enable-subprocess coverage: COVERAGE_FILE={toxinidir}/.coverage coverage: COVERAGE_PROCESS_START={toxinidir}/.coveragerc nobyte: PYTHONDONTWRITEBYTECODE=1 lsof: _PYTEST_TOX_POSARGS_LSOF=--lsof pexpect: _PYTEST_TOX_PLATFORM=linux|darwin pexpect: _PYTEST_TOX_POSARGS_PEXPECT=-m uses_pexpect twisted: _PYTEST_TOX_POSARGS_TWISTED=testing/test_unittest.py xdist: _PYTEST_TOX_POSARGS_XDIST=-n auto extras = testing deps = numpy: numpy pexpect: pexpect pluggymaster: git+https://github.com/pytest-dev/pluggy.git@master twisted: twisted twisted: unittest2 xdist: pytest-xdist>=1.13 {env:_PYTEST_TOX_EXTRA_DEP:} platform = {env:_PYTEST_TOX_PLATFORM:.*} [testenv:py27-subprocess] deps = pytest-xdist>=1.13 py27: mock nose commands = pytest -n auto --runpytest=subprocess {posargs} [testenv:linting] skip_install = True basepython = python3 deps = pre-commit>=1.11.0 commands = pre-commit run --all-files --show-diff-on-failure [testenv:docs] basepython = python3 usedevelop = True changedir = doc/en deps = -r{toxinidir}/doc/en/requirements.txt commands = sphinx-build -W -b html . _build [testenv:doctesting] basepython = python3 skipsdist = True deps = {[testenv]deps} PyYAML commands = {env:_PYTEST_TOX_COVERAGE_RUN:} pytest doc/en {env:_PYTEST_TOX_COVERAGE_RUN:} pytest --doctest-modules --pyargs _pytest [testenv:regen] changedir = doc/en skipsdist = True basepython = python3 deps = sphinx PyYAML regendoc>=0.6.1 dataclasses whitelist_externals = rm make commands = # don't show hypothesis plugin info in docs, see #4602 pip uninstall hypothesis -y rm -rf /tmp/doc-exec* rm -rf {envdir}/.pytest_cache make regen [testenv:jython] changedir = testing commands = {envpython} {envbindir}/py.test-jython {posargs} [testenv:py37-freeze] changedir = testing/freeze # Disable PEP 517 with pip, which does not work with PyInstaller currently. deps = pyinstaller commands = {envpython} create_executable.py {envpython} tox_run.py [testenv:release] decription = do a release, required posarg of the version number basepython = python3 usedevelop = True passenv = * deps = colorama gitpython pre-commit>=1.11.0 towncrier wheel commands = python scripts/release.py {posargs} [testenv:publish_gh_release_notes] description = create GitHub release after deployment basepython = python3 usedevelop = True passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG TRAVIS_REPO_SLUG deps = github3.py pypandoc commands = python scripts/publish_gh_release_notes.py [pytest] minversion = 2.0 addopts = -ra -p pytester --strict-markers rsyncdirs = tox.ini doc src testing python_files = test_*.py *_test.py testing/*/*.py python_classes = Test Acceptance python_functions = test # NOTE: "doc" is not included here, but gets tested explicitly via "doctesting". testpaths = testing norecursedirs = testing/example_scripts xfail_strict=true filterwarnings = error ignore:yield tests are deprecated, and scheduled to be removed in pytest 4.0:pytest.RemovedInPytest4Warning ignore:Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0:pytest.RemovedInPytest4Warning ignore::pytest.RemovedInPytest4Warning ignore:Module already imported so cannot be rewritten:pytest.PytestWarning # produced by path.local ignore:bad escape.*:DeprecationWarning:re # produced by path.readlines ignore:.*U.*mode is deprecated:DeprecationWarning # produced by pytest-xdist ignore:.*type argument to addoption.*:DeprecationWarning # produced by python >=3.5 on execnet (pytest-xdist) ignore:.*inspect.getargspec.*deprecated, use inspect.signature.*:DeprecationWarning # pytest's own futurewarnings ignore::pytest.PytestExperimentalApiWarning # Do not cause SyntaxError for invalid escape sequences in py37. default:invalid escape sequence:DeprecationWarning # ignore use of unregistered marks, because we use many to test the implementation ignore::_pytest.warning_types.PytestUnknownMarkWarning pytester_example_dir = testing/example_scripts markers = # dummy markers for testing foo bar baz # conftest.py reorders tests moving slow ones to the end of the list slow # experimental mark for all tests using pexpect uses_pexpect [flake8] max-line-length = 120 ignore = E203,W503 [isort] ; This config mimics what reorder-python-imports does. force_single_line = 1 known_localfolder = pytest,_pytest known_third_party = test_source,test_excinfo force_alphabetical_sort_within_sections = 1 pytest-4.6.9/.gitattributes0000664000372000037200000000002513604173400016612 0ustar travistravis00000000000000*.bat text eol=crlf pytest-4.6.9/.gitignore0000664000372000037200000000111613604173400015711 0ustar travistravis00000000000000# Automatically generated by `hgimportsvn` .svn .hgsvn # Ignore local virtualenvs lib/ bin/ include/ .Python/ # These lines are suggested according to the svn:ignore property # Feel free to enable them by uncommenting them *.pyc *.pyo *.swp *.class *.orig *~ .hypothesis/ # autogenerated src/_pytest/_version.py # setuptools .eggs/ doc/*/_build doc/*/.doctrees build/ dist/ *.egg-info issue/ env/ .env/ 3rdparty/ .tox .cache .pytest_cache .coverage .coverage.* coverage.xml .ropeproject .idea .hypothesis .pydevproject .project .settings .vscode # generated by pip pip-wheel-metadata/ pytest-4.6.9/CITATION0000664000372000037200000000073313604173400015062 0ustar travistravis00000000000000NOTE: Change "x.y" by the version you use. If you are unsure about which version you are using run: `pip show pytest`. Text: [pytest] pytest x.y, 2004 Krekel et al., https://github.com/pytest-dev/pytest BibTeX: @misc{pytestx.y, title = {pytest x.y}, author = {Krekel, Holger and Oliveira, Bruno and Pfannschmidt, Ronny and Bruynooghe, Floris and Laugher, Brianna and Bruhin, Florian}, year = {2004}, url = {https://github.com/pytest-dev/pytest}, } pytest-4.6.9/scripts/0000775000372000037200000000000013604173421015414 5ustar travistravis00000000000000pytest-4.6.9/scripts/release.minor.rst0000664000372000037200000000127713604173400020715 0ustar travistravis00000000000000pytest-{version} ======================================= The pytest team is proud to announce the {version} release! pytest is a mature Python testing tool with more than a 2000 tests against itself, passing on many different interpreters and platforms. This release contains a number of bugs fixes and improvements, so users are encouraged to take a look at the CHANGELOG: https://docs.pytest.org/en/latest/changelog.html For complete documentation, please visit: https://docs.pytest.org/en/latest/ As usual, you can upgrade from pypi via: pip install -U pytest Thanks to all who contributed to this release, among them: {contributors} Happy testing, The Pytest Development Team pytest-4.6.9/scripts/release.py0000664000372000037200000000700013604173400017400 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Invoke development tasks. """ import argparse from pathlib import Path from subprocess import call from subprocess import check_call from subprocess import check_output from colorama import Fore from colorama import init def announce(version): """Generates a new release announcement entry in the docs.""" # Get our list of authors stdout = check_output(["git", "describe", "--abbrev=0", "--tags"]) stdout = stdout.decode("utf-8") last_version = stdout.strip() stdout = check_output( ["git", "log", "{}..HEAD".format(last_version), "--format=%aN"] ) stdout = stdout.decode("utf-8") contributors = set(stdout.splitlines()) template_name = ( "release.minor.rst" if version.endswith(".0") else "release.patch.rst" ) template_text = ( Path(__file__).parent.joinpath(template_name).read_text(encoding="UTF-8") ) contributors_text = ( "\n".join("* {}".format(name) for name in sorted(contributors)) + "\n" ) text = template_text.format(version=version, contributors=contributors_text) target = Path(__file__).parent.joinpath( "../doc/en/announce/release-{}.rst".format(version) ) target.write_text(text, encoding="UTF-8") print(f"{Fore.CYAN}[generate.announce] {Fore.RESET}Generated {target.name}") # Update index with the new release entry index_path = Path(__file__).parent.joinpath("../doc/en/announce/index.rst") lines = index_path.read_text(encoding="UTF-8").splitlines() indent = " " for index, line in enumerate(lines): if line.startswith("{}release-".format(indent)): new_line = indent + target.stem if line != new_line: lines.insert(index, new_line) index_path.write_text("\n".join(lines) + "\n", encoding="UTF-8") print( f"{Fore.CYAN}[generate.announce] {Fore.RESET}Updated {index_path.name}" ) else: print( f"{Fore.CYAN}[generate.announce] {Fore.RESET}Skip {index_path.name} (already contains release)" ) break check_call(["git", "add", str(target)]) def regen(): """Call regendoc tool to update examples and pytest output in the docs.""" print(f"{Fore.CYAN}[generate.regen] {Fore.RESET}Updating docs") check_call(["tox", "-e", "regen"]) def fix_formatting(): """Runs pre-commit in all files to ensure they are formatted correctly""" print( f"{Fore.CYAN}[generate.fix linting] {Fore.RESET}Fixing formatting using pre-commit" ) call(["pre-commit", "run", "--all-files"]) def pre_release(version): """Generates new docs, release announcements and creates a local tag.""" announce(version) regen() changelog(version, write_out=True) fix_formatting() msg = "Preparing release version {}".format(version) check_call(["git", "commit", "-a", "-m", msg]) print() print(f"{Fore.CYAN}[generate.pre_release] {Fore.GREEN}All done!") print() print(f"Please push your branch and open a PR.") def changelog(version, write_out=False): if write_out: addopts = [] else: addopts = ["--draft"] check_call(["towncrier", "--yes", "--version", version] + addopts) def main(): init(autoreset=True) parser = argparse.ArgumentParser() parser.add_argument("version", help="Release version") options = parser.parse_args() pre_release(options.version) if __name__ == "__main__": main() pytest-4.6.9/scripts/setup-coverage-vars.bat0000664000372000037200000000044613604173400022007 0ustar travistravis00000000000000if "%PYTEST_COVERAGE%" == "1" ( set "_PYTEST_TOX_COVERAGE_RUN=coverage run -m" set "_PYTEST_TOX_EXTRA_DEP=coverage-enable-subprocess" echo Coverage vars configured, PYTEST_COVERAGE=%PYTEST_COVERAGE% ) else ( echo Skipping coverage vars setup, PYTEST_COVERAGE=%PYTEST_COVERAGE% ) pytest-4.6.9/scripts/retry.cmd0000664000372000037200000000113713604173400017245 0ustar travistravis00000000000000@echo off rem Source: https://github.com/appveyor/ci/blob/master/scripts/appveyor-retry.cmd rem initiate the retry number set retryNumber=0 set maxRetries=3 :RUN %* set LastErrorLevel=%ERRORLEVEL% IF %LastErrorLevel% == 0 GOTO :EOF set /a retryNumber=%retryNumber%+1 IF %reTryNumber% == %maxRetries% (GOTO :FAILED) :RETRY set /a retryNumberDisp=%retryNumber%+1 @echo Command "%*" failed with exit code %LastErrorLevel%. Retrying %retryNumberDisp% of %maxRetries% GOTO :RUN : FAILED @echo Sorry, we tried running command for %maxRetries% times and all attempts were unsuccessful! EXIT /B %LastErrorLevel% pytest-4.6.9/scripts/release.patch.rst0000664000372000037200000000063713604173400020667 0ustar travistravis00000000000000pytest-{version} ======================================= pytest {version} has just been released to PyPI. This is a bug-fix release, being a drop-in replacement. To upgrade:: pip install --upgrade pytest The full changelog is available at https://docs.pytest.org/en/latest/changelog.html. Thanks to all who contributed to this release, among them: {contributors} Happy testing, The pytest Development Team pytest-4.6.9/scripts/upload-coverage.bat0000664000372000037200000000115313604173400021156 0ustar travistravis00000000000000REM script called by Azure to combine and upload coverage information to codecov if "%PYTEST_COVERAGE%" == "1" ( echo Prepare to upload coverage information if defined CODECOV_TOKEN ( echo CODECOV_TOKEN defined ) else ( echo CODECOV_TOKEN NOT defined ) python -m pip install codecov python -m coverage combine python -m coverage xml python -m coverage report -m scripts\retry python -m codecov --required -X gcov pycov search -f coverage.xml --name %PYTEST_CODECOV_NAME% ) else ( echo Skipping coverage upload, PYTEST_COVERAGE=%PYTEST_COVERAGE% ) pytest-4.6.9/scripts/publish_gh_release_notes.py0000664000372000037200000000554013604173400023023 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Script used to publish GitHub release notes extracted from CHANGELOG.rst. This script is meant to be executed after a successful deployment in Travis. Uses the following environment variables: * GIT_TAG: the name of the tag of the current commit. * GH_RELEASE_NOTES_TOKEN: a personal access token with 'repo' permissions. It should be encrypted using: $travis encrypt GH_RELEASE_NOTES_TOKEN= -r pytest-dev/pytest And the contents pasted in the ``deploy.env.secure`` section in the ``travis.yml`` file. The script also requires ``pandoc`` to be previously installed in the system. Requires Python3.6+. """ import os import re import sys from pathlib import Path import github3 import pypandoc def publish_github_release(slug, token, tag_name, body): github = github3.login(token=token) owner, repo = slug.split("/") repo = github.repository(owner, repo) return repo.create_release(tag_name=tag_name, body=body) def parse_changelog(tag_name): p = Path(__file__).parent.parent / "CHANGELOG.rst" changelog_lines = p.read_text(encoding="UTF-8").splitlines() title_regex = re.compile(r"pytest (\d\.\d+\.\d+) \(\d{4}-\d{2}-\d{2}\)") consuming_version = False version_lines = [] for line in changelog_lines: m = title_regex.match(line) if m: # found the version we want: start to consume lines until we find the next version title if m.group(1) == tag_name: consuming_version = True # found a new version title while parsing the version we want: break out elif consuming_version: break if consuming_version: version_lines.append(line) return "\n".join(version_lines) def convert_rst_to_md(text): return pypandoc.convert_text(text, "md", format="rst") def main(argv): if len(argv) > 1: tag_name = argv[1] else: tag_name = os.environ.get("TRAVIS_TAG") if not tag_name: print("tag_name not given and $TRAVIS_TAG not set", file=sys.stderr) return 1 token = os.environ.get("GH_RELEASE_NOTES_TOKEN") if not token: print("GH_RELEASE_NOTES_TOKEN not set", file=sys.stderr) return 1 slug = os.environ.get("TRAVIS_REPO_SLUG") if not slug: print("TRAVIS_REPO_SLUG not set", file=sys.stderr) return 1 rst_body = parse_changelog(tag_name) md_body = convert_rst_to_md(rst_body) if not publish_github_release(slug, token, tag_name, md_body): print("Could not publish release notes:", file=sys.stderr) print(md_body, file=sys.stderr) return 5 print() print(f"Release notes for {tag_name} published successfully:") print(f"https://github.com/{slug}/releases/tag/{tag_name}") print() return 0 if __name__ == "__main__": sys.exit(main(sys.argv)) pytest-4.6.9/setup.cfg0000664000372000037200000000323013604173421015544 0ustar travistravis00000000000000[metadata] name = pytest description = pytest: simple powerful testing with Python long_description = file: README.rst url = https://docs.pytest.org/en/latest/ project_urls = Source=https://github.com/pytest-dev/pytest Tracker=https://github.com/pytest-dev/pytest/issues author = Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others license = MIT license license_file = LICENSE keywords = test, unittest classifiers = Development Status :: 6 - Mature Intended Audience :: Developers License :: OSI Approved :: MIT License Operating System :: POSIX Operating System :: Microsoft :: Windows Operating System :: MacOS :: MacOS X Topic :: Software Development :: Testing Topic :: Software Development :: Libraries Topic :: Utilities Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 platforms = unix, linux, osx, cygwin, win32 [options] zip_safe = no packages = _pytest _pytest._code _pytest._io _pytest.assertion _pytest.config _pytest.mark py_modules = pytest python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.* [options.entry_points] console_scripts = pytest=pytest:main py.test=pytest:main [build_sphinx] source-dir = doc/en/ build-dir = doc/build all_files = 1 [upload_sphinx] upload-dir = doc/en/build/html [bdist_wheel] universal = 1 [check-manifest] ignore = _pytest/_version.py [devpi:upload] formats = sdist.tgz,bdist_wheel [egg_info] tag_build = tag_date = 0 pytest-4.6.9/bench/0000775000372000037200000000000013604173421015004 5ustar travistravis00000000000000pytest-4.6.9/bench/bench_argcomplete.py0000664000372000037200000000115213604173400021013 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # 10000 iterations, just for relative comparison # 2.7.5 3.3.2 # FilesCompleter 75.1109 69.2116 # FastFilesCompleter 0.7383 1.0760 import timeit imports = [ "from argcomplete.completers import FilesCompleter as completer", "from _pytest._argcomplete import FastFilesCompleter as completer", ] count = 1000 # only a few seconds setup = "%s\nfc = completer()" run = 'fc("/d")' if __name__ == "__main__": print(timeit.timeit(run, setup=setup % imports[0], number=count)) print(timeit.timeit(run, setup=setup % imports[1], number=count)) pytest-4.6.9/bench/empty.py0000664000372000037200000000012713604173400016511 0ustar travistravis00000000000000# -*- coding: utf-8 -*- for i in range(1000): exec("def test_func_%d(): pass" % i) pytest-4.6.9/bench/skip.py0000664000372000037200000000026613604173400016325 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from six.moves import range import pytest SKIP = True @pytest.mark.parametrize("x", range(5000)) def test_foo(x): if SKIP: pytest.skip("heh") pytest-4.6.9/bench/bench.py0000664000372000037200000000056313604173400016436 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys if __name__ == "__main__": import cProfile import pytest # NOQA import pstats script = sys.argv[1:] if len(sys.argv) > 1 else ["empty.py"] stats = cProfile.run("pytest.cmdline.main(%r)" % script, "prof") p = pstats.Stats("prof") p.strip_dirs() p.sort_stats("cumulative") print(p.print_stats(500)) pytest-4.6.9/bench/manyparam.py0000664000372000037200000000030113604173400017332 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture(scope="module", params=range(966)) def foo(request): return request.param def test_it(foo): pass def test_it2(foo): pass pytest-4.6.9/.travis.yml0000664000372000037200000001344213604173400016037 0ustar travistravis00000000000000language: python dist: xenial stages: - baseline - name: test if: repo = pytest-dev/pytest AND tag IS NOT present - name: deploy if: repo = pytest-dev/pytest AND tag IS present python: '3.7' cache: false env: global: - PYTEST_ADDOPTS=-vv # setuptools-scm needs all tags in order to obtain a proper version git: depth: false install: - python -m pip install --upgrade --pre tox jobs: include: # OSX tests - first (in test stage), since they are the slower ones. - &test-macos os: osx osx_image: xcode10.1 language: generic # Coverage for: # - py2 with symlink in test_cmdline_python_package_symlink. env: TOXENV=py27-xdist PYTEST_COVERAGE=1 before_install: - python -V - test $(python -c 'import sys; print("%d%d" % sys.version_info[0:2])') = 27 - <<: *test-macos env: TOXENV=py37-pexpect,py37-xdist PYTEST_COVERAGE=1 before_install: - which python3 - python3 -V - ln -sfn "$(which python3)" /usr/local/bin/python - python -V - test $(python -c 'import sys; print("%d%d" % sys.version_info[0:2])') = 37 # Full run of latest (major) supported versions, without xdist. - env: TOXENV=py27 python: '2.7' - env: TOXENV=py37 python: '3.7' # Coverage tracking is slow with pypy, skip it. - env: TOXENV=pypy-xdist python: 'pypy' - env: TOXENV=pypy3-xdist python: 'pypy3' - env: TOXENV=py34-xdist python: '3.4' - env: TOXENV=py35-xdist python: '3.5' # Coverage for: # - pytester's LsofFdLeakChecker # - TestArgComplete (linux only) # - numpy # Empty PYTEST_ADDOPTS to run this non-verbose. - env: TOXENV=py37-lsof-numpy-xdist PYTEST_COVERAGE=1 PYTEST_ADDOPTS= # Specialized factors for py27. - env: TOXENV=py27-nobyte-numpy-xdist python: '2.7' - env: TOXENV=py27-pluggymaster-xdist python: '2.7' # Specialized factors for py37. # Coverage for: # - test_sys_breakpoint_interception (via pexpect). - env: TOXENV=py37-pexpect,py37-twisted PYTEST_COVERAGE=1 - env: TOXENV=py37-pluggymaster-xdist - env: TOXENV=py37-freeze # Jobs only run via Travis cron jobs (currently daily). - env: TOXENV=py38-xdist python: '3.8-dev' if: type = cron - stage: baseline # Coverage for: # - _pytest.unittest._handle_skip (via pexpect). env: TOXENV=py27-pexpect,py27-twisted PYTEST_COVERAGE=1 python: '2.7' # Use py36 here for faster baseline. - env: TOXENV=py36-xdist python: '3.6' - env: TOXENV=linting,docs,doctesting PYTEST_COVERAGE=1 cache: directories: - $HOME/.cache/pre-commit - stage: deploy python: '3.6' install: pip install -U setuptools setuptools_scm tox script: skip # token to upload github release notes: GH_RELEASE_NOTES_TOKEN env: - secure: "OjOeL7/0JUDkV00SsTs732e8vQjHynpbG9FKTNtZZJ+1Zn4Cib+hAlwmlBnvVukML0X60YpcfjnC4quDOIGLPsh5zeXnvJmYtAIIUNQXjWz8NhcGYrhyzuP1rqV22U68RTCdmOq3lMYU/W2acwHP7T49PwJtOiUM5kF120UAQ0Zi5EmkqkIvH8oM5mO9Dlver+/U7Htpz9rhKrHBXQNCMZI6yj2aUyukqB2PN2fjAlDbCF//+FmvYw9NjT4GeFOSkTCf4ER9yfqs7yglRfwiLtOCZ2qKQhWZNsSJDB89rxIRXWavJUjJKeY2EW2/NkomYJDpqJLIF4JeFRw/HhA47CYPeo6BJqyyNV+0CovL1frpWfi9UQw2cMbgFUkUIUk3F6DD59PHNIOX2R/HX56dQsw7WKl3QuHlCOkICXYg8F7Ta684IoKjeTX03/6QNOkURfDBwfGszY0FpbxrjCSWKom6RyZdyidnESaxv9RzjcIRZVh1rp8KMrwS1OrwRSdG0zjlsPr49hWMenN/8fKgcHTV4/r1Tj6mip0dorSRCrgUNIeRBKgmui6FS8642ab5JNKOxMteVPVR2sFuhjOQ0Jy+PmvceYY9ZMWc3+/B/KVh0dZ3hwvLGZep/vxDS2PwCA5/xw31714vT5LxidKo8yECjBynMU/wUTTS695D3NY=" addons: apt: packages: # required by publish_gh_release_notes - pandoc after_deploy: tox -e publish_gh_release_notes deploy: provider: pypi user: nicoddemus distributions: sdist bdist_wheel skip_upload_docs: true password: secure: xanTgTUu6XDQVqB/0bwJQXoDMnU5tkwZc5koz6mBkkqZhKdNOi2CLoC1XhiSZ+ah24l4V1E0GAqY5kBBcy9d7NVe4WNg4tD095LsHw+CRU6/HCVIFfyk2IZ+FPAlguesCcUiJSXOrlBF+Wj68wEvLoK7EoRFbJeiZ/f91Ww1sbtDlqXABWGHrmhPJL5Wva7o7+wG7JwJowqdZg1pbQExsCc7b53w4v2RBu3D6TJaTAzHiVsW+nUSI67vKI/uf+cR/OixsTfy37wlHgSwihYmrYLFls3V0bSpahCim3bCgMaFZx8S8xrdgJ++PzBCof2HeflFKvW+VCkoYzGEG4NrTWJoNz6ni4red9GdvfjGH3YCjAKS56h9x58zp2E5rpsb/kVq5/45xzV+dq6JRuhQ1nJWjBC6fSKAc/bfwnuFK3EBxNLkvBssLHvsNjj5XG++cB8DdS9wVGUqjpoK4puaXUWFqy4q3S9F86HEsKNgExtieA9qNx+pCIZVs6JCXZNjr0I5eVNzqJIyggNgJG6RyravsU35t9Zd9doL5g4Y7UKmAGTn1Sz24HQ4sMQgXdm2SyD8gEK5je4tlhUvfGtDvMSlstq71kIn9nRpFnqB6MFlbYSEAZmo8dGbCquoUc++6Rum208wcVbrzzVtGlXB/Ow9AbFMYeAGA0+N/K1e59c= on: tags: true repo: pytest-dev/pytest matrix: allow_failures: - python: '3.8-dev' env: TOXENV=py38-xdist # Temporary (https://github.com/pytest-dev/pytest/pull/5334). - env: TOXENV=pypy3-xdist python: 'pypy3' before_script: - | # Do not (re-)upload coverage with cron runs. if [[ "$TRAVIS_EVENT_TYPE" = cron ]]; then PYTEST_COVERAGE=0 fi - | if [[ "$PYTEST_COVERAGE" = 1 ]]; then export COVERAGE_FILE="$PWD/.coverage" export COVERAGE_PROCESS_START="$PWD/.coveragerc" export _PYTEST_TOX_COVERAGE_RUN="coverage run -m" export _PYTEST_TOX_EXTRA_DEP=coverage-enable-subprocess fi script: tox after_success: - | if [[ "$PYTEST_COVERAGE" = 1 ]]; then set -e # Add last TOXENV to $PATH. PATH="$PWD/.tox/${TOXENV##*,}/bin:$PATH" coverage combine coverage xml coverage report -m bash <(curl -s https://codecov.io/bash) -Z -X gcov -X coveragepy -X search -X xcode -X gcovout -X fix -f coverage.xml -n $TOXENV-$TRAVIS_OS_NAME fi notifications: irc: channels: - "chat.freenode.net#pytest" on_success: change on_failure: change skip_join: true email: - pytest-commit@python.org pytest-4.6.9/LICENSE0000664000372000037200000000211013604173400014721 0ustar travistravis00000000000000The MIT License (MIT) Copyright (c) 2004-2020 Holger Krekel and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pytest-4.6.9/.coveragerc0000664000372000037200000000056613604173400016052 0ustar travistravis00000000000000[run] include = src/* testing/* */lib/python*/site-packages/_pytest/* */lib/python*/site-packages/pytest.py */pypy*/site-packages/_pytest/* */pypy*/site-packages/pytest.py *\Lib\site-packages\_pytest\* *\Lib\site-packages\pytest.py parallel = 1 branch = 1 [paths] source = src/ */lib/python*/site-packages/ */pypy*/site-packages/ *\Lib\site-packages\ pytest-4.6.9/README.rst0000664000372000037200000001057513604173400015421 0ustar travistravis00000000000000.. image:: https://docs.pytest.org/en/latest/_static/pytest1.png :target: https://docs.pytest.org/en/latest/ :align: center :alt: pytest ------ .. image:: https://img.shields.io/pypi/v/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest .. image:: https://img.shields.io/pypi/pyversions/pytest.svg :target: https://pypi.org/project/pytest/ .. image:: https://codecov.io/gh/pytest-dev/pytest/branch/master/graph/badge.svg :target: https://codecov.io/gh/pytest-dev/pytest :alt: Code coverage Status .. image:: https://travis-ci.org/pytest-dev/pytest.svg?branch=master :target: https://travis-ci.org/pytest-dev/pytest .. image:: https://dev.azure.com/pytest-dev/pytest/_apis/build/status/pytest-CI?branchName=master :target: https://dev.azure.com/pytest-dev/pytest .. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/python/black .. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg :target: https://www.codetriage.com/pytest-dev/pytest The ``pytest`` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. An example of a simple test: .. code-block:: python # content of test_sample.py def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 To execute it:: $ pytest ============================= test session starts ============================= collected 1 items test_sample.py F ================================== FAILURES =================================== _________________________________ test_answer _________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:5: AssertionError ========================== 1 failed in 0.04 seconds =========================== Due to ``pytest``'s detailed assertion introspection, only plain ``assert`` statements are used. See `getting-started `_ for more examples. Features -------- - Detailed info on failing `assert statements `_ (no need to remember ``self.assert*`` names); - `Auto-discovery `_ of test modules and functions; - `Modular fixtures `_ for managing small or parametrized long-lived test resources; - Can run `unittest `_ (or trial), `nose `_ test suites out of the box; - Python 2.7, Python 3.4+, PyPy 2.3, Jython 2.5 (untested); - Rich plugin architecture, with over 315+ `external plugins `_ and thriving community; Documentation ------------- For full documentation, including installation, tutorials and PDF documents, please see https://docs.pytest.org/en/latest/. Bugs/Requests ------------- Please use the `GitHub issue tracker `_ to submit bugs or request features. Changelog --------- Consult the `Changelog `__ page for fixes and enhancements of each version. Support pytest -------------- You can support pytest by obtaining a `Tideflift subscription`_. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools. .. _`Tideflift subscription`: https://tidelift.com/subscription/pkg/pypi-pytest?utm_source=pypi-pytest&utm_medium=referral&utm_campaign=readme Security ^^^^^^^^ pytest has never been associated with a security vunerability, but in any case, to report a security vulnerability please use the `Tidelift security contact `_. Tidelift will coordinate the fix and disclosure. License ------- Copyright Holger Krekel and others, 2004-2020. Distributed under the terms of the `MIT`_ license, pytest is free and open source software. .. _`MIT`: https://github.com/pytest-dev/pytest/blob/master/LICENSE pytest-4.6.9/pyproject.toml0000664000372000037200000000204713604173400016641 0ustar travistravis00000000000000[build-system] requires = [ # sync with setup.py until we discard non-pep-517/518 "setuptools>=40.0", "setuptools-scm", "wheel", ] build-backend = "setuptools.build_meta" [tool.towncrier] package = "pytest" package_dir = "src" filename = "CHANGELOG.rst" directory = "changelog/" title_format = "pytest {version} ({project_date})" template = "changelog/_template.rst" [[tool.towncrier.type]] directory = "removal" name = "Removals" showcontent = true [[tool.towncrier.type]] directory = "deprecation" name = "Deprecations" showcontent = true [[tool.towncrier.type]] directory = "feature" name = "Features" showcontent = true [[tool.towncrier.type]] directory = "bugfix" name = "Bug Fixes" showcontent = true [[tool.towncrier.type]] directory = "vendor" name = "Vendored Libraries" showcontent = true [[tool.towncrier.type]] directory = "doc" name = "Improved Documentation" showcontent = true [[tool.towncrier.type]] directory = "trivial" name = "Trivial/Internal Changes" showcontent = true pytest-4.6.9/TIDELIFT.rst0000664000372000037200000000423213604173400015661 0ustar travistravis00000000000000======== Tidelift ======== pytest is a member of `Tidelift`_. This document describes how the core team manages Tidelift-related activities. What is it ========== Tidelift aims to make Open Source sustainable by offering subscriptions to companies which rely on Open Source packages. This subscription allows it to pay maintainers of those Open Source packages to aid sustainability of the work. Funds ===== It was decided in the `mailing list`_ that the Tidelift contribution will be split evenly between members of the `contributors team`_ interested in receiving funding. The current list of contributors receiving funding are: * `@asottile`_ * `@blueyed`_ * `@nicoddemus`_ Contributors interested in receiving a part of the funds just need to submit a PR adding their name to the list. Contributors that want to stop receiving the funds should also submit a PR in the same way. The PR should mention `@pytest-dev/tidelift-admins`_ so appropriate changes can be made in the Tidelift platform. After the PR has been accepted and merged, the contributor should register in the `Tidelift`_ platform and follow the instructions there, including signing an `agreement`_. Admins ====== A few people have admin access to the Tidelift dashboard to make changes. Those people are part of the `@pytest-dev/tidelift-admins`_ team. `Core contributors`_ interested in helping out with Tidelift maintenance are welcome! We don't expect much work here other than the occasional adding/removal of a contributor from receiving funds. Just drop a line to one of the `@pytest-dev/tidelift-admins`_ or use the mailing list. .. _`Tidelift`: https://tidelift.com .. _`mailing list`: https://mail.python.org/pipermail/pytest-dev/2019-May/004716.html .. _`contributors team`: https://github.com/orgs/pytest-dev/teams/contributors .. _`core contributors`: https://github.com/orgs/pytest-dev/teams/core/members .. _`@pytest-dev/tidelift-admins`: https://github.com/orgs/pytest-dev/teams/tidelift-admins/members .. _`agreement`: https://tidelift.com/docs/lifting/agreement .. _`@asottile`: https://github.com/asottile .. _`@blueyed`: https://github.com/blueyed .. _`@nicoddemus`: https://github.com/nicoddemus pytest-4.6.9/setup.py0000664000372000037200000000233113604173400015433 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from setuptools import setup # TODO: if py gets upgrade to >=1.6, # remove _width_of_current_line in terminal.py INSTALL_REQUIRES = [ "py>=1.5.0", "six>=1.10.0", "packaging", "attrs>=17.4.0", 'more-itertools>=4.0.0,<6.0.0;python_version<="2.7"', 'more-itertools>=4.0.0;python_version>"2.7"', "atomicwrites>=1.0", 'funcsigs>=1.0;python_version<"3.0"', 'pathlib2>=2.2.0;python_version<"3.6"', 'colorama<=0.4.1;sys_platform=="win32" and python_version=="3.4"', 'colorama;sys_platform=="win32" and python_version!="3.4"', "pluggy>=0.12,<1.0", 'importlib-metadata>=0.12;python_version<"3.8"', "wcwidth", ] def main(): setup( use_scm_version={"write_to": "src/_pytest/_version.py"}, setup_requires=["setuptools-scm", "setuptools>=40.0"], package_dir={"": "src"}, # fmt: off extras_require={ "testing": [ "argcomplete", "hypothesis>=3.56", "nose", "requests", "mock;python_version=='2.7'", ], }, # fmt: on install_requires=INSTALL_REQUIRES, ) if __name__ == "__main__": main() pytest-4.6.9/.pre-commit-config.yaml0000664000372000037200000000425413604173400020210 0ustar travistravis00000000000000exclude: doc/en/example/py2py3/test_py2.py repos: - repo: https://github.com/psf/black rev: 19.10b0 hooks: - id: black args: [--safe, --quiet] language_version: python3 - repo: https://github.com/asottile/blacken-docs rev: v0.5.0 hooks: - id: blacken-docs additional_dependencies: [black==19.3b0] language_version: python3 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.2.2 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: fix-encoding-pragma - id: check-yaml - id: debug-statements exclude: _pytest/debugging.py language_version: python3 - repo: https://gitlab.com/pycqa/flake8 rev: 3.7.7 hooks: - id: flake8 language_version: python3 - repo: https://github.com/asottile/reorder_python_imports rev: v1.4.0 hooks: - id: reorder-python-imports args: ['--application-directories=.:src'] - repo: https://github.com/asottile/pyupgrade rev: v1.15.0 hooks: - id: pyupgrade args: [--keep-percent-format] - repo: https://github.com/pre-commit/pygrep-hooks rev: v1.3.0 hooks: - id: rst-backticks - repo: local hooks: - id: rst name: rst entry: rst-lint --encoding utf-8 files: ^(CHANGELOG.rst|HOWTORELEASE.rst|README.rst|TIDELIFT.rst|changelog/.*)$ language: python additional_dependencies: [pygments, restructuredtext_lint] - id: changelogs-rst name: changelog filenames language: fail entry: 'changelog files must be named ####.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst' exclude: changelog/(\d+\.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst|README.rst|_template.rst) files: ^changelog/ - id: py-deprecated name: py library is deprecated language: pygrep entry: > (?x)\bpy\.( _code\.| builtin\.| code\.| io\.(BytesIO|saferepr)| path\.local\.sysfind| process\.| std\. ) types: [python] pytest-4.6.9/extra/0000775000372000037200000000000013604173421015050 5ustar travistravis00000000000000pytest-4.6.9/extra/get_issues.py0000664000372000037200000000440213604173400017571 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import json import py import requests issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues" def get_issues(): issues = [] url = issues_url while 1: get_data = {"state": "all"} r = requests.get(url, params=get_data) data = r.json() if r.status_code == 403: # API request limit exceeded print(data["message"]) exit(1) issues.extend(data) # Look for next page links = requests.utils.parse_header_links(r.headers["Link"]) another_page = False for link in links: if link["rel"] == "next": url = link["url"] another_page = True if not another_page: return issues def main(args): cachefile = py.path.local(args.cache) if not cachefile.exists() or args.refresh: issues = get_issues() cachefile.write(json.dumps(issues)) else: issues = json.loads(cachefile.read()) open_issues = [x for x in issues if x["state"] == "open"] open_issues.sort(key=lambda x: x["number"]) report(open_issues) def _get_kind(issue): labels = [l["name"] for l in issue["labels"]] for key in ("bug", "enhancement", "proposal"): if key in labels: return key return "issue" def report(issues): for issue in issues: title = issue["title"] # body = issue["body"] kind = _get_kind(issue) status = issue["state"] number = issue["number"] link = "https://github.com/pytest-dev/pytest/issues/%s/" % number print("----") print(status, kind, link) print(title) # print() # lines = body.split("\n") # print("\n".join(lines[:3])) # if len(lines) > 3 or len(body) > 240: # print("...") print("\n\nFound %s open issues" % len(issues)) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser("process bitbucket issues") parser.add_argument( "--refresh", action="store_true", help="invalidate cache, refresh issues" ) parser.add_argument( "--cache", action="store", default="issues.json", help="cache file" ) args = parser.parse_args() main(args) pytest-4.6.9/extra/setup-py.test/0000775000372000037200000000000013604173421017614 5ustar travistravis00000000000000pytest-4.6.9/extra/setup-py.test/setup.py0000664000372000037200000000052213604173400021322 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys from distutils.core import setup if __name__ == "__main__": if "sdist" not in sys.argv[1:]: raise ValueError("please use 'pytest' pypi package instead of 'py.test'") setup( name="py.test", version="0.0", description="please use 'pytest' for installation", ) pytest-4.6.9/HOWTORELEASE.rst0000664000372000037200000000315513604173400016361 0ustar travistravis00000000000000Release Procedure ----------------- Our current policy for releasing is to aim for a bugfix every few weeks and a minor release every 2-3 months. The idea is to get fixes and new features out instead of trying to cram a ton of features into a release and by consequence taking a lot of time to make a new one. .. important:: pytest releases must be prepared on **Linux** because the docs and examples expect to be executed in that platform. #. Create a branch ``release-X.Y.Z`` with the version for the release. * **patch releases**: from the latest ``master``; * **minor releases**: from the latest ``features``; then merge with the latest ``master``; Ensure your are in a clean work tree. #. Using ``tox``, generate docs, changelog, announcements:: $ tox -e release -- This will generate a commit with all the changes ready for pushing. #. Open a PR for this branch targeting ``master``. #. After all tests pass and the PR has been approved, publish to PyPI by pushing the tag:: git tag git push git@github.com:pytest-dev/pytest.git Wait for the deploy to complete, then make sure it is `available on PyPI `_. #. Merge the PR into ``master``. #. Send an email announcement with the contents from:: doc/en/announce/release-.rst To the following mailing lists: * pytest-dev@python.org (all releases) * python-announce-list@python.org (all releases) * testing-in-python@lists.idyll.org (only major/minor releases) And announce it on `Twitter `_ with the ``#pytest`` hashtag. pytest-4.6.9/testing/0000775000372000037200000000000013604173421015402 5ustar travistravis00000000000000pytest-4.6.9/testing/test_mark.py0000664000372000037200000007225113604173400017751 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys import six import pytest from _pytest.main import EXIT_INTERRUPTED from _pytest.mark import EMPTY_PARAMETERSET_OPTION from _pytest.mark import MarkGenerator as Mark from _pytest.nodes import Collector from _pytest.nodes import Node from _pytest.warning_types import PytestDeprecationWarning from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG try: import mock except ImportError: import unittest.mock as mock ignore_markinfo = pytest.mark.filterwarnings( "ignore:MarkInfo objects:pytest.RemovedInPytest4Warning" ) class TestMark(object): @pytest.mark.parametrize("attr", ["mark", "param"]) @pytest.mark.parametrize("modulename", ["py.test", "pytest"]) def test_pytest_exists_in_namespace_all(self, attr, modulename): module = sys.modules[modulename] assert attr in module.__all__ def test_pytest_mark_notcallable(self): mark = Mark() pytest.raises((AttributeError, TypeError), mark) def test_mark_with_param(self): def some_function(abc): pass class SomeClass(object): pass assert pytest.mark.foo(some_function) is some_function assert pytest.mark.foo.with_args(some_function) is not some_function assert pytest.mark.foo(SomeClass) is SomeClass assert pytest.mark.foo.with_args(SomeClass) is not SomeClass def test_pytest_mark_name_starts_with_underscore(self): mark = Mark() with pytest.raises(AttributeError): mark._some_name def test_marked_class_run_twice(testdir, request): """Test fails file is run twice that contains marked class. See issue#683. """ py_file = testdir.makepyfile( """ import pytest @pytest.mark.parametrize('abc', [1, 2, 3]) class Test1(object): def test_1(self, abc): assert abc in [1, 2, 3] """ ) file_name = os.path.basename(py_file.strpath) rec = testdir.inline_run(file_name, file_name) rec.assertoutcome(passed=6) def test_ini_markers(testdir): testdir.makeini( """ [pytest] markers = a1: this is a webtest marker a2: this is a smoke marker """ ) testdir.makepyfile( """ def test_markers(pytestconfig): markers = pytestconfig.getini("markers") print(markers) assert len(markers) >= 2 assert markers[0].startswith("a1:") assert markers[1].startswith("a2:") """ ) rec = testdir.inline_run() rec.assertoutcome(passed=1) def test_markers_option(testdir): testdir.makeini( """ [pytest] markers = a1: this is a webtest marker a1some: another marker nodescription """ ) result = testdir.runpytest("--markers") result.stdout.fnmatch_lines( ["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"] ) def test_ini_markers_whitespace(testdir): testdir.makeini( """ [pytest] markers = a1 : this is a whitespace marker """ ) testdir.makepyfile( """ import pytest @pytest.mark.a1 def test_markers(): assert True """ ) rec = testdir.inline_run("--strict-markers", "-m", "a1") rec.assertoutcome(passed=1) def test_marker_without_description(testdir): testdir.makefile( ".cfg", setup=""" [tool:pytest] markers=slow """, ) testdir.makeconftest( """ import pytest pytest.mark.xfail('FAIL') """ ) ftdir = testdir.mkdir("ft1_dummy") testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py")) rec = testdir.runpytest("--strict-markers") rec.assert_outcomes() def test_markers_option_with_plugin_in_current_dir(testdir): testdir.makeconftest('pytest_plugins = "flip_flop"') testdir.makepyfile( flip_flop="""\ def pytest_configure(config): config.addinivalue_line("markers", "flip:flop") def pytest_generate_tests(metafunc): try: mark = metafunc.function.flipper except AttributeError: return metafunc.parametrize("x", (10, 20))""" ) testdir.makepyfile( """\ import pytest @pytest.mark.flipper def test_example(x): assert x""" ) result = testdir.runpytest("--markers") result.stdout.fnmatch_lines(["*flip*flop*"]) def test_mark_on_pseudo_function(testdir): testdir.makepyfile( """ import pytest @pytest.mark.r(lambda x: 0/0) def test_hello(): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"]) def test_strict_prohibits_unregistered_markers(testdir, option_name): testdir.makepyfile( """ import pytest @pytest.mark.unregisteredmark def test_hello(): pass """ ) result = testdir.runpytest(option_name) assert result.ret != 0 result.stdout.fnmatch_lines( ["'unregisteredmark' not found in `markers` configuration option"] ) @pytest.mark.parametrize( "spec", [ ("xyz", ("test_one",)), ("xyz and xyz2", ()), ("xyz2", ("test_two",)), ("xyz or xyz2", ("test_one", "test_two")), ], ) def test_mark_option(spec, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xyz def test_one(): pass @pytest.mark.xyz2 def test_two(): pass """ ) opt, passed_result = spec rec = testdir.inline_run("-m", opt) passed, skipped, fail = rec.listoutcomes() passed = [x.nodeid.split("::")[-1] for x in passed] assert len(passed) == len(passed_result) assert list(passed) == list(passed_result) @pytest.mark.parametrize( "spec", [("interface", ("test_interface",)), ("not interface", ("test_nointer",))] ) def test_mark_option_custom(spec, testdir): testdir.makeconftest( """ import pytest def pytest_collection_modifyitems(items): for item in items: if "interface" in item.nodeid: item.add_marker(pytest.mark.interface) """ ) testdir.makepyfile( """ def test_interface(): pass def test_nointer(): pass """ ) opt, passed_result = spec rec = testdir.inline_run("-m", opt) passed, skipped, fail = rec.listoutcomes() passed = [x.nodeid.split("::")[-1] for x in passed] assert len(passed) == len(passed_result) assert list(passed) == list(passed_result) @pytest.mark.parametrize( "spec", [ ("interface", ("test_interface",)), ("not interface", ("test_nointer", "test_pass")), ("pass", ("test_pass",)), ("not pass", ("test_interface", "test_nointer")), ], ) def test_keyword_option_custom(spec, testdir): testdir.makepyfile( """ def test_interface(): pass def test_nointer(): pass def test_pass(): pass """ ) opt, passed_result = spec rec = testdir.inline_run("-k", opt) passed, skipped, fail = rec.listoutcomes() passed = [x.nodeid.split("::")[-1] for x in passed] assert len(passed) == len(passed_result) assert list(passed) == list(passed_result) def test_keyword_option_considers_mark(testdir): testdir.copy_example("marks/marks_considered_keywords") rec = testdir.inline_run("-k", "foo") passed = rec.listoutcomes()[0] assert len(passed) == 1 @pytest.mark.parametrize( "spec", [ ("None", ("test_func[None]",)), ("1.3", ("test_func[1.3]",)), ("2-3", ("test_func[2-3]",)), ], ) def test_keyword_option_parametrize(spec, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) def test_func(arg): pass """ ) opt, passed_result = spec rec = testdir.inline_run("-k", opt) passed, skipped, fail = rec.listoutcomes() passed = [x.nodeid.split("::")[-1] for x in passed] assert len(passed) == len(passed_result) assert list(passed) == list(passed_result) @pytest.mark.parametrize( "spec", [ ( "foo or import", "ERROR: Python keyword 'import' not accepted in expressions passed to '-k'", ), ("foo or", "ERROR: Wrong expression passed to '-k': foo or"), ], ) def test_keyword_option_wrong_arguments(spec, testdir, capsys): testdir.makepyfile( """ def test_func(arg): pass """ ) opt, expected_result = spec testdir.inline_run("-k", opt) out = capsys.readouterr().err assert expected_result in out def test_parametrized_collected_from_command_line(testdir): """Parametrized test not collected if test named specified in command line issue#649. """ py_file = testdir.makepyfile( """ import pytest @pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) def test_func(arg): pass """ ) file_name = os.path.basename(py_file.strpath) rec = testdir.inline_run(file_name + "::" + "test_func") rec.assertoutcome(passed=3) def test_parametrized_collect_with_wrong_args(testdir): """Test collect parametrized func with wrong number of args.""" py_file = testdir.makepyfile( """ import pytest @pytest.mark.parametrize('foo, bar', [(1, 2, 3)]) def test_func(foo, bar): pass """ ) result = testdir.runpytest(py_file) result.stdout.fnmatch_lines( [ 'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):', " ['foo', 'bar']", "must be equal to the number of values (3):", " (1, 2, 3)", ] ) def test_parametrized_with_kwargs(testdir): """Test collect parametrized func with wrong number of args.""" py_file = testdir.makepyfile( """ import pytest @pytest.fixture(params=[1,2]) def a(request): return request.param @pytest.mark.parametrize(argnames='b', argvalues=[1, 2]) def test_func(a, b): pass """ ) result = testdir.runpytest(py_file) assert result.ret == 0 def test_parametrize_iterator(testdir): """parametrize should work with generators (#5354).""" py_file = testdir.makepyfile( """\ import pytest def gen(): yield 1 yield 2 yield 3 @pytest.mark.parametrize('a', gen()) def test(a): assert a >= 1 """ ) result = testdir.runpytest(py_file) assert result.ret == 0 # should not skip any tests result.stdout.fnmatch_lines(["*3 passed*"]) class TestFunctional(object): def test_merging_markers_deep(self, testdir): # issue 199 - propagate markers into nested classes p = testdir.makepyfile( """ import pytest class TestA(object): pytestmark = pytest.mark.a def test_b(self): assert True class TestC(object): # this one didnt get marked def test_d(self): assert True """ ) items, rec = testdir.inline_genitems(p) for item in items: print(item, item.keywords) assert [x for x in item.iter_markers() if x.name == "a"] def test_mark_decorator_subclass_does_not_propagate_to_base(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.mark.a class Base(object): pass @pytest.mark.b class Test1(Base): def test_foo(self): pass class Test2(Base): def test_bar(self): pass """ ) items, rec = testdir.inline_genitems(p) self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",)) def test_mark_should_not_pass_to_siebling_class(self, testdir): """#568""" p = testdir.makepyfile( """ import pytest class TestBase(object): def test_foo(self): pass @pytest.mark.b class TestSub(TestBase): pass class TestOtherSub(TestBase): pass """ ) items, rec = testdir.inline_genitems(p) base_item, sub_item, sub_item_other = items print(items, [x.nodeid for x in items]) # new api seregates assert not list(base_item.iter_markers(name="b")) assert not list(sub_item_other.iter_markers(name="b")) assert list(sub_item.iter_markers(name="b")) def test_mark_decorator_baseclasses_merged(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.mark.a class Base(object): pass @pytest.mark.b class Base2(Base): pass @pytest.mark.c class Test1(Base2): def test_foo(self): pass class Test2(Base2): @pytest.mark.d def test_bar(self): pass """ ) items, rec = testdir.inline_genitems(p) self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d")) def test_mark_closest(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.mark.c(location="class") class Test: @pytest.mark.c(location="function") def test_has_own(): pass def test_has_inherited(): pass """ ) items, rec = testdir.inline_genitems(p) has_own, has_inherited = items assert has_own.get_closest_marker("c").kwargs == {"location": "function"} assert has_inherited.get_closest_marker("c").kwargs == {"location": "class"} assert has_own.get_closest_marker("missing") is None def test_mark_with_wrong_marker(self, testdir): reprec = testdir.inline_runsource( """ import pytest class pytestmark(object): pass def test_func(): pass """ ) values = reprec.getfailedcollections() assert len(values) == 1 assert "TypeError" in str(values[0].longrepr) def test_mark_dynamically_in_funcarg(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.hello) def pytest_terminal_summary(terminalreporter): values = terminalreporter.stats['passed'] terminalreporter._tw.line("keyword: %s" % values[0].keywords) """ ) testdir.makepyfile( """ def test_func(arg): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["keyword: *hello*"]) def test_no_marker_match_on_unmarked_names(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.mark.shouldmatch def test_marked(): assert 1 def test_unmarked(): assert 1 """ ) reprec = testdir.inline_run("-m", "test_unmarked", p) passed, skipped, failed = reprec.listoutcomes() assert len(passed) + len(skipped) + len(failed) == 0 dlist = reprec.getcalls("pytest_deselected") deselected_tests = dlist[0].items assert len(deselected_tests) == 2 def test_invalid_m_option(self, testdir): testdir.makepyfile( """ def test_a(): pass """ ) result = testdir.runpytest("-m bogus/") result.stdout.fnmatch_lines( ["INTERNALERROR> Marker expression must be valid Python!"] ) def test_keywords_at_node_level(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="session", autouse=True) def some(request): request.keywords["hello"] = 42 assert "world" not in request.keywords @pytest.fixture(scope="function", autouse=True) def funcsetup(request): assert "world" in request.keywords assert "hello" in request.keywords @pytest.mark.world def test_function(): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @ignore_markinfo def test_keyword_added_for_session(self, testdir): testdir.makeconftest( """ import pytest def pytest_collection_modifyitems(session): session.add_marker("mark1") session.add_marker(pytest.mark.mark2) session.add_marker(pytest.mark.mark3) pytest.raises(ValueError, lambda: session.add_marker(10)) """ ) testdir.makepyfile( """ def test_some(request): assert "mark1" in request.keywords assert "mark2" in request.keywords assert "mark3" in request.keywords assert 10 not in request.keywords marker = request.node.get_closest_marker("mark1") assert marker.name == "mark1" assert marker.args == () assert marker.kwargs == {} """ ) reprec = testdir.inline_run("-m", "mark1", SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=1) def assert_markers(self, items, **expected): """assert that given items have expected marker names applied to them. expected should be a dict of (item name -> seq of expected marker names) .. note:: this could be moved to ``testdir`` if proven to be useful to other modules. """ items = {x.name: x for x in items} for name, expected_markers in expected.items(): markers = {m.name for m in items[name].iter_markers()} assert markers == set(expected_markers) @pytest.mark.filterwarnings("ignore") def test_mark_from_parameters(self, testdir): """#1540""" testdir.makepyfile( """ import pytest pytestmark = pytest.mark.skipif(True, reason='skip all') # skipifs inside fixture params params = [pytest.mark.skipif(False, reason='dont skip')('parameter')] @pytest.fixture(params=params) def parameter(request): return request.param def test_1(parameter): assert True """ ) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(skipped=1) class TestKeywordSelection(object): def test_select_simple(self, testdir): file_test = testdir.makepyfile( """ def test_one(): assert 0 class TestClass(object): def test_method_one(self): assert 42 == 43 """ ) def check(keyword, name): reprec = testdir.inline_run("-s", "-k", keyword, file_test) passed, skipped, failed = reprec.listoutcomes() assert len(failed) == 1 assert failed[0].nodeid.split("::")[-1] == name assert len(reprec.getcalls("pytest_deselected")) == 1 for keyword in ["test_one", "est_on"]: check(keyword, "test_one") check("TestClass and test", "test_method_one") @pytest.mark.parametrize( "keyword", [ "xxx", "xxx and test_2", "TestClass", "xxx and not test_1", "TestClass and test_2", "xxx and TestClass and test_2", ], ) def test_select_extra_keywords(self, testdir, keyword): p = testdir.makepyfile( test_select=""" def test_1(): pass class TestClass(object): def test_2(self): pass """ ) testdir.makepyfile( conftest=""" import pytest @pytest.hookimpl(hookwrapper=True) def pytest_pycollect_makeitem(name): outcome = yield if name == "TestClass": item = outcome.get_result() item.extra_keyword_matches.add("xxx") """ ) reprec = testdir.inline_run(p.dirpath(), "-s", "-k", keyword) print("keyword", repr(keyword)) passed, skipped, failed = reprec.listoutcomes() assert len(passed) == 1 assert passed[0].nodeid.endswith("test_2") dlist = reprec.getcalls("pytest_deselected") assert len(dlist) == 1 assert dlist[0].items[0].name == "test_1" def test_select_starton(self, testdir): threepass = testdir.makepyfile( test_threepass=""" def test_one(): assert 1 def test_two(): assert 1 def test_three(): assert 1 """ ) reprec = testdir.inline_run("-k", "test_two:", threepass) passed, skipped, failed = reprec.listoutcomes() assert len(passed) == 2 assert not failed dlist = reprec.getcalls("pytest_deselected") assert len(dlist) == 1 item = dlist[0].items[0] assert item.name == "test_one" def test_keyword_extra(self, testdir): p = testdir.makepyfile( """ def test_one(): assert 0 test_one.mykeyword = True """ ) reprec = testdir.inline_run("-k", "mykeyword", p) passed, skipped, failed = reprec.countoutcomes() assert failed == 1 @pytest.mark.xfail def test_keyword_extra_dash(self, testdir): p = testdir.makepyfile( """ def test_one(): assert 0 test_one.mykeyword = True """ ) # with argparse the argument to an option cannot # start with '-' reprec = testdir.inline_run("-k", "-mykeyword", p) passed, skipped, failed = reprec.countoutcomes() assert passed + skipped + failed == 0 def test_no_magic_values(self, testdir): """Make sure the tests do not match on magic values, no double underscored values, like '__dict__', and no instance values, like '()'. """ p = testdir.makepyfile( """ def test_one(): assert 1 """ ) def assert_test_is_not_selected(keyword): reprec = testdir.inline_run("-k", keyword, p) passed, skipped, failed = reprec.countoutcomes() dlist = reprec.getcalls("pytest_deselected") assert passed + skipped + failed == 0 deselected_tests = dlist[0].items assert len(deselected_tests) == 1 assert_test_is_not_selected("__") assert_test_is_not_selected("()") class TestMarkDecorator(object): @pytest.mark.parametrize( "lhs, rhs, expected", [ (pytest.mark.foo(), pytest.mark.foo(), True), (pytest.mark.foo(), pytest.mark.bar(), False), (pytest.mark.foo(), "bar", False), ("foo", pytest.mark.bar(), False), ], ) def test__eq__(self, lhs, rhs, expected): assert (lhs == rhs) == expected @pytest.mark.parametrize("mark", [None, "", "skip", "xfail"]) def test_parameterset_for_parametrize_marks(testdir, mark): if mark is not None: testdir.makeini( """ [pytest] {}={} """.format( EMPTY_PARAMETERSET_OPTION, mark ) ) config = testdir.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark pytest_configure(config) result_mark = get_empty_parameterset_mark(config, ["a"], all) if mark in (None, ""): # normalize to the requested name mark = "skip" assert result_mark.name == mark assert result_mark.kwargs["reason"].startswith("got empty parameter set ") if mark == "xfail": assert result_mark.kwargs.get("run") is False def test_parameterset_for_fail_at_collect(testdir): testdir.makeini( """ [pytest] {}=fail_at_collect """.format( EMPTY_PARAMETERSET_OPTION ) ) config = testdir.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark pytest_configure(config) with pytest.raises( Collector.CollectError, match=r"Empty parameter set in 'pytest_configure' at line \d\d+", ): get_empty_parameterset_mark(config, ["a"], pytest_configure) p1 = testdir.makepyfile( """ import pytest @pytest.mark.parametrize("empty", []) def test(): pass """ ) result = testdir.runpytest(str(p1)) result.stdout.fnmatch_lines( [ "collected 0 items / 1 errors", "* ERROR collecting test_parameterset_for_fail_at_collect.py *", "Empty parameter set in 'test' at line 3", "*= 1 error in *", ] ) assert result.ret == EXIT_INTERRUPTED def test_parameterset_for_parametrize_bad_markname(testdir): with pytest.raises(pytest.UsageError): test_parameterset_for_parametrize_marks(testdir, "bad") def test_mark_expressions_no_smear(testdir): testdir.makepyfile( """ import pytest class BaseTests(object): def test_something(self): pass @pytest.mark.FOO class TestFooClass(BaseTests): pass @pytest.mark.BAR class TestBarClass(BaseTests): pass """ ) reprec = testdir.inline_run("-m", "FOO") passed, skipped, failed = reprec.countoutcomes() dlist = reprec.getcalls("pytest_deselected") assert passed == 1 assert skipped == failed == 0 deselected_tests = dlist[0].items assert len(deselected_tests) == 1 # todo: fixed # keywords smear - expected behaviour # reprec_keywords = testdir.inline_run("-k", "FOO") # passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes() # assert passed_k == 2 # assert skipped_k == failed_k == 0 def test_addmarker_order(): node = Node("Test", config=mock.Mock(), session=mock.Mock(), nodeid="Test") node.add_marker("foo") node.add_marker("bar") node.add_marker("baz", append=False) extracted = [x.name for x in node.iter_markers()] assert extracted == ["baz", "foo", "bar"] @pytest.mark.filterwarnings("ignore") def test_markers_from_parametrize(testdir): """#3605""" testdir.makepyfile( """ from __future__ import print_function import pytest first_custom_mark = pytest.mark.custom_marker custom_mark = pytest.mark.custom_mark @pytest.fixture(autouse=True) def trigger(request): custom_mark = list(request.node.iter_markers('custom_mark')) print("Custom mark %s" % custom_mark) @custom_mark("custom mark non parametrized") def test_custom_mark_non_parametrized(): print("Hey from test") @pytest.mark.parametrize( "obj_type", [ first_custom_mark("first custom mark")("template"), pytest.param( # Think this should be recommended way? "disk", marks=custom_mark('custom mark1') ), custom_mark("custom mark2")("vm"), # Tried also this ] ) def test_custom_mark_parametrized(obj_type): print("obj_type is:", obj_type) """ ) result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG) result.assert_outcomes(passed=4) def test_pytest_param_id_requires_string(): with pytest.raises(TypeError) as excinfo: pytest.param(id=True) (msg,) = excinfo.value.args if six.PY2: assert msg == "Expected id to be a string, got : True" else: assert msg == "Expected id to be a string, got : True" @pytest.mark.parametrize("s", (None, "hello world")) def test_pytest_param_id_allows_none_or_string(s): assert pytest.param(id=s) def test_pytest_param_warning_on_unknown_kwargs(): with pytest.warns(PytestDeprecationWarning) as warninfo: # typo, should be marks= pytest.param(1, 2, mark=pytest.mark.xfail()) assert warninfo[0].filename == __file__ (msg,) = warninfo[0].message.args assert msg == ( "pytest.param() got unexpected keyword arguments: ['mark'].\n" "This will be an error in future versions." ) pytest-4.6.9/testing/test_junitxml.py0000664000372000037200000012730713604173400020674 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import platform import sys from datetime import datetime from xml.dom import minidom import py import pytest from _pytest.junitxml import LogXML from _pytest.reports import BaseReport def runandparse(testdir, *args): resultpath = testdir.tmpdir.join("junit.xml") result = testdir.runpytest("--junitxml=%s" % resultpath, *args) xmldoc = minidom.parse(str(resultpath)) return result, DomNode(xmldoc) def assert_attr(node, **kwargs): __tracebackhide__ = True def nodeval(node, name): anode = node.getAttributeNode(name) if anode is not None: return anode.value expected = {name: str(value) for name, value in kwargs.items()} on_node = {name: nodeval(node, name) for name in expected} assert on_node == expected class DomNode(object): def __init__(self, dom): self.__node = dom def __repr__(self): return self.__node.toxml() def find_first_by_tag(self, tag): return self.find_nth_by_tag(tag, 0) def _by_tag(self, tag): return self.__node.getElementsByTagName(tag) @property def children(self): return [type(self)(x) for x in self.__node.childNodes] @property def get_unique_child(self): children = self.children assert len(children) == 1 return children[0] def find_nth_by_tag(self, tag, n): items = self._by_tag(tag) try: nth = items[n] except IndexError: pass else: return type(self)(nth) def find_by_tag(self, tag): t = type(self) return [t(x) for x in self.__node.getElementsByTagName(tag)] def __getitem__(self, key): node = self.__node.getAttributeNode(key) if node is not None: return node.value def assert_attr(self, **kwargs): __tracebackhide__ = True return assert_attr(self.__node, **kwargs) def toxml(self): return self.__node.toxml() @property def text(self): return self.__node.childNodes[0].wholeText @property def tag(self): return self.__node.tagName @property def next_sibling(self): return type(self)(self.__node.nextSibling) class TestPython(object): def test_summing_simple(self, testdir): testdir.makepyfile( """ import pytest def test_pass(): pass def test_fail(): assert 0 def test_skip(): pytest.skip("") @pytest.mark.xfail def test_xfail(): assert 0 @pytest.mark.xfail def test_xpass(): assert 1 """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(name="pytest", errors=0, failures=1, skipped=2, tests=5) def test_summing_simple_with_errors(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def fixture(): raise Exception() def test_pass(): pass def test_fail(): assert 0 def test_error(fixture): pass @pytest.mark.xfail def test_xfail(): assert False @pytest.mark.xfail(strict=True) def test_xpass(): assert True """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5) def test_hostname_in_xml(self, testdir): testdir.makepyfile( """ def test_pass(): pass """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") node.assert_attr(hostname=platform.node()) def test_timestamp_in_xml(self, testdir): testdir.makepyfile( """ def test_pass(): pass """ ) start_time = datetime.now() result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f") assert start_time <= timestamp < datetime.now() def test_timing_function(self, testdir): testdir.makepyfile( """ import time, pytest def setup_module(): time.sleep(0.01) def teardown_module(): time.sleep(0.01) def test_sleep(): time.sleep(0.01) """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") val = tnode["time"] assert round(float(val), 2) >= 0.03 @pytest.mark.parametrize("duration_report", ["call", "total"]) def test_junit_duration_report(self, testdir, monkeypatch, duration_report): # mock LogXML.node_reporter so it always sets a known duration to each test report object original_node_reporter = LogXML.node_reporter def node_reporter_wrapper(s, report): report.duration = 1.0 reporter = original_node_reporter(s, report) return reporter monkeypatch.setattr(LogXML, "node_reporter", node_reporter_wrapper) testdir.makepyfile( """ def test_foo(): pass """ ) result, dom = runandparse( testdir, "-o", "junit_duration_report={}".format(duration_report) ) node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") val = float(tnode["time"]) if duration_report == "total": assert val == 3.0 else: assert duration_report == "call" assert val == 1.0 def test_setup_error(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def arg(request): raise ValueError() def test_function(arg): pass """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_setup_error", name="test_function") fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="test setup failure") assert "ValueError" in fnode.toxml() def test_teardown_error(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def arg(): yield raise ValueError() def test_function(arg): pass """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_teardown_error", name="test_function") fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="test teardown failure") assert "ValueError" in fnode.toxml() def test_call_failure_teardown_error(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def arg(): yield raise Exception("Teardown Exception") def test_function(arg): raise Exception("Call Exception") """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=1, failures=1, tests=1) first, second = dom.find_by_tag("testcase") if not first or not second or first == second: assert 0 fnode = first.find_first_by_tag("failure") fnode.assert_attr(message="Exception: Call Exception") snode = second.find_first_by_tag("error") snode.assert_attr(message="test teardown failure") def test_skip_contains_name_reason(self, testdir): testdir.makepyfile( """ import pytest def test_skip(): pytest.skip("hello23") """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_skip_contains_name_reason", name="test_skip") snode = tnode.find_first_by_tag("skipped") snode.assert_attr(type="pytest.skip", message="hello23") def test_mark_skip_contains_name_reason(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip(reason="hello24") def test_skip(): assert True """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr( classname="test_mark_skip_contains_name_reason", name="test_skip" ) snode = tnode.find_first_by_tag("skipped") snode.assert_attr(type="pytest.skip", message="hello24") def test_mark_skipif_contains_name_reason(self, testdir): testdir.makepyfile( """ import pytest GLOBAL_CONDITION = True @pytest.mark.skipif(GLOBAL_CONDITION, reason="hello25") def test_skip(): assert True """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr( classname="test_mark_skipif_contains_name_reason", name="test_skip" ) snode = tnode.find_first_by_tag("skipped") snode.assert_attr(type="pytest.skip", message="hello25") def test_mark_skip_doesnt_capture_output(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip(reason="foo") def test_skip(): print("bar!") """ ) result, dom = runandparse(testdir) assert result.ret == 0 node_xml = dom.find_first_by_tag("testsuite").toxml() assert "bar!" not in node_xml def test_classname_instance(self, testdir): testdir.makepyfile( """ class TestClass(object): def test_method(self): assert 0 """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr( classname="test_classname_instance.TestClass", name="test_method" ) def test_classname_nested_dir(self, testdir): p = testdir.tmpdir.ensure("sub", "test_hello.py") p.write("def test_func(): 0/0") result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="sub.test_hello", name="test_func") def test_internal_error(self, testdir): testdir.makeconftest("def pytest_runtest_protocol(): 0 / 0") testdir.makepyfile("def test_function(): pass") result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="pytest", name="internal") fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="internal error") assert "Division" in fnode.toxml() @pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"]) def test_failure_function(self, testdir, junit_logging): testdir.makepyfile( """ import logging import sys def test_fail(): print("hello-stdout") sys.stderr.write("hello-stderr\\n") logging.info('info msg') logging.warning('warning msg') raise ValueError(42) """ ) result, dom = runandparse(testdir, "-o", "junit_logging=%s" % junit_logging) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_failure_function", name="test_fail") fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="ValueError: 42") assert "ValueError" in fnode.toxml() systemout = fnode.next_sibling assert systemout.tag == "system-out" assert "hello-stdout" in systemout.toxml() assert "info msg" not in systemout.toxml() systemerr = systemout.next_sibling assert systemerr.tag == "system-err" assert "hello-stderr" in systemerr.toxml() assert "info msg" not in systemerr.toxml() if junit_logging == "system-out": assert "warning msg" in systemout.toxml() assert "warning msg" not in systemerr.toxml() elif junit_logging == "system-err": assert "warning msg" not in systemout.toxml() assert "warning msg" in systemerr.toxml() elif junit_logging == "no": assert "warning msg" not in systemout.toxml() assert "warning msg" not in systemerr.toxml() def test_failure_verbose_message(self, testdir): testdir.makepyfile( """ import sys def test_fail(): assert 0, "An error" """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="AssertionError: An error assert 0") def test_failure_escape(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize('arg1', "<&'", ids="<&'") def test_func(arg1): print(arg1) assert 0 """ ) result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=3, tests=3) for index, char in enumerate("<&'"): tnode = node.find_nth_by_tag("testcase", index) tnode.assert_attr( classname="test_failure_escape", name="test_func[%s]" % char ) sysout = tnode.find_first_by_tag("system-out") text = sysout.text assert text == "%s\n" % char def test_junit_prefixing(self, testdir): testdir.makepyfile( """ def test_func(): assert 0 class TestHello(object): def test_hello(self): pass """ ) result, dom = runandparse(testdir, "--junitprefix=xyz") assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1, tests=2) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="xyz.test_junit_prefixing", name="test_func") tnode = node.find_nth_by_tag("testcase", 1) tnode.assert_attr( classname="xyz.test_junit_prefixing.TestHello", name="test_hello" ) def test_xfailure_function(self, testdir): testdir.makepyfile( """ import pytest def test_xfail(): pytest.xfail("42") """ ) result, dom = runandparse(testdir) assert not result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_xfailure_function", name="test_xfail") fnode = tnode.find_first_by_tag("skipped") fnode.assert_attr(type="pytest.xfail", message="42") # assert "ValueError" in fnode.toxml() def test_xfailure_marker(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail(reason="42") def test_xfail(): assert False """ ) result, dom = runandparse(testdir) assert not result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_xfailure_marker", name="test_xfail") fnode = tnode.find_first_by_tag("skipped") fnode.assert_attr(type="pytest.xfail", message="42") def test_xfail_captures_output_once(self, testdir): testdir.makepyfile( """ import sys import pytest @pytest.mark.xfail() def test_fail(): sys.stdout.write('XFAIL This is stdout') sys.stderr.write('XFAIL This is stderr') assert 0 """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") assert len(tnode.find_by_tag("system-err")) == 1 assert len(tnode.find_by_tag("system-out")) == 1 def test_xfailure_xpass(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail def test_xpass(): pass """ ) result, dom = runandparse(testdir) # assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=0, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_xfailure_xpass", name="test_xpass") def test_xfailure_xpass_strict(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail(strict=True, reason="This needs to fail!") def test_xpass(): pass """ ) result, dom = runandparse(testdir) # assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(skipped=0, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(classname="test_xfailure_xpass_strict", name="test_xpass") fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="[XPASS(strict)] This needs to fail!") def test_collect_error(self, testdir): testdir.makepyfile("syntax error") result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=1, tests=1) tnode = node.find_first_by_tag("testcase") fnode = tnode.find_first_by_tag("error") fnode.assert_attr(message="collection failure") assert "SyntaxError" in fnode.toxml() def test_unicode(self, testdir): value = "hx\xc4\x85\xc4\x87\n" testdir.makepyfile( """ # coding: latin1 def test_hello(): print(%r) assert 0 """ % value ) result, dom = runandparse(testdir) assert result.ret == 1 tnode = dom.find_first_by_tag("testcase") fnode = tnode.find_first_by_tag("failure") if not sys.platform.startswith("java"): assert "hx" in fnode.toxml() def test_assertion_binchars(self, testdir): """this test did fail when the escaping wasnt strict""" testdir.makepyfile( """ M1 = '\x01\x02\x03\x04' M2 = '\x01\x02\x03\x05' def test_str_compare(): assert M1 == M2 """ ) result, dom = runandparse(testdir) print(dom.toxml()) def test_pass_captures_stdout(self, testdir): testdir.makepyfile( """ def test_pass(): print('hello-stdout') """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") systemout = pnode.find_first_by_tag("system-out") assert "hello-stdout" in systemout.toxml() def test_pass_captures_stderr(self, testdir): testdir.makepyfile( """ import sys def test_pass(): sys.stderr.write('hello-stderr') """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") systemout = pnode.find_first_by_tag("system-err") assert "hello-stderr" in systemout.toxml() def test_setup_error_captures_stdout(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def arg(request): print('hello-stdout') raise ValueError() def test_function(arg): pass """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") systemout = pnode.find_first_by_tag("system-out") assert "hello-stdout" in systemout.toxml() def test_setup_error_captures_stderr(self, testdir): testdir.makepyfile( """ import sys import pytest @pytest.fixture def arg(request): sys.stderr.write('hello-stderr') raise ValueError() def test_function(arg): pass """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") systemout = pnode.find_first_by_tag("system-err") assert "hello-stderr" in systemout.toxml() def test_avoid_double_stdout(self, testdir): testdir.makepyfile( """ import sys import pytest @pytest.fixture def arg(request): yield sys.stdout.write('hello-stdout teardown') raise ValueError() def test_function(arg): sys.stdout.write('hello-stdout call') """ ) result, dom = runandparse(testdir) node = dom.find_first_by_tag("testsuite") pnode = node.find_first_by_tag("testcase") systemout = pnode.find_first_by_tag("system-out") assert "hello-stdout call" in systemout.toxml() assert "hello-stdout teardown" in systemout.toxml() def test_mangle_test_address(): from _pytest.junitxml import mangle_test_address address = "::".join(["a/my.py.thing.py", "Class", "()", "method", "[a-1-::]"]) newnames = mangle_test_address(address) assert newnames == ["a.my.py.thing", "Class", "method", "[a-1-::]"] def test_dont_configure_on_slaves(tmpdir): gotten = [] class FakeConfig(object): def __init__(self): self.pluginmanager = self self.option = self def getini(self, name): return "pytest" junitprefix = None # XXX: shouldnt need tmpdir ? xmlpath = str(tmpdir.join("junix.xml")) register = gotten.append fake_config = FakeConfig() from _pytest import junitxml junitxml.pytest_configure(fake_config) assert len(gotten) == 1 FakeConfig.slaveinput = None junitxml.pytest_configure(fake_config) assert len(gotten) == 1 class TestNonPython(object): def test_summing_simple(self, testdir): testdir.makeconftest( """ import pytest def pytest_collect_file(path, parent): if path.ext == ".xyz": return MyItem(path, parent) class MyItem(pytest.Item): def __init__(self, path, parent): super(MyItem, self).__init__(path.basename, parent) self.fspath = path def runtest(self): raise ValueError(42) def repr_failure(self, excinfo): return "custom item runtest failed" """ ) testdir.tmpdir.join("myfile.xyz").write("hello") result, dom = runandparse(testdir) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(errors=0, failures=1, skipped=0, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr(name="myfile.xyz") fnode = tnode.find_first_by_tag("failure") fnode.assert_attr(message="custom item runtest failed") assert "custom item runtest failed" in fnode.toxml() def test_nullbyte(testdir): # A null byte can not occur in XML (see section 2.2 of the spec) testdir.makepyfile( """ import sys def test_print_nullbyte(): sys.stdout.write('Here the null -->' + chr(0) + '<--') sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') assert False """ ) xmlf = testdir.tmpdir.join("junit.xml") testdir.runpytest("--junitxml=%s" % xmlf) text = xmlf.read() assert "\x00" not in text assert "#x00" in text def test_nullbyte_replace(testdir): # Check if the null byte gets replaced testdir.makepyfile( """ import sys def test_print_nullbyte(): sys.stdout.write('Here the null -->' + chr(0) + '<--') sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') assert False """ ) xmlf = testdir.tmpdir.join("junit.xml") testdir.runpytest("--junitxml=%s" % xmlf) text = xmlf.read() assert "#x0" in text def test_invalid_xml_escape(): # Test some more invalid xml chars, the full range should be # tested really but let's just thest the edges of the ranges # intead. # XXX This only tests low unicode character points for now as # there are some issues with the testing infrastructure for # the higher ones. # XXX Testing 0xD (\r) is tricky as it overwrites the just written # line in the output, so we skip it too. global unichr try: unichr(65) except NameError: unichr = chr invalid = ( 0x00, 0x1, 0xB, 0xC, 0xE, 0x19, 27, # issue #126 0xD800, 0xDFFF, 0xFFFE, 0x0FFFF, ) # , 0x110000) valid = (0x9, 0xA, 0x20) # 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF) from _pytest.junitxml import bin_xml_escape for i in invalid: got = bin_xml_escape(unichr(i)).uniobj if i <= 0xFF: expected = "#x%02X" % i else: expected = "#x%04X" % i assert got == expected for i in valid: assert chr(i) == bin_xml_escape(unichr(i)).uniobj def test_logxml_path_expansion(tmpdir, monkeypatch): home_tilde = py.path.local(os.path.expanduser("~")).join("test.xml") xml_tilde = LogXML("~%stest.xml" % tmpdir.sep, None) assert xml_tilde.logfile == home_tilde monkeypatch.setenv("HOME", str(tmpdir)) home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml")) xml_var = LogXML("$HOME%stest.xml" % tmpdir.sep, None) assert xml_var.logfile == home_var def test_logxml_changingdir(testdir): testdir.makepyfile( """ def test_func(): import os os.chdir("a") """ ) testdir.tmpdir.mkdir("a") result = testdir.runpytest("--junitxml=a/x.xml") assert result.ret == 0 assert testdir.tmpdir.join("a/x.xml").check() def test_logxml_makedir(testdir): """--junitxml should automatically create directories for the xml file""" testdir.makepyfile( """ def test_pass(): pass """ ) result = testdir.runpytest("--junitxml=path/to/results.xml") assert result.ret == 0 assert testdir.tmpdir.join("path/to/results.xml").check() def test_logxml_check_isdir(testdir): """Give an error if --junit-xml is a directory (#2089)""" result = testdir.runpytest("--junit-xml=.") result.stderr.fnmatch_lines(["*--junitxml must be a filename*"]) def test_escaped_parametrized_names_xml(testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize('char', [u"\\x00"]) def test_func(char): assert char """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") node.assert_attr(name="test_func[\\x00]") def test_double_colon_split_function_issue469(testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize('param', ["double::colon"]) def test_func(param): pass """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") node.assert_attr(classname="test_double_colon_split_function_issue469") node.assert_attr(name="test_func[double::colon]") def test_double_colon_split_method_issue469(testdir): testdir.makepyfile( """ import pytest class TestClass(object): @pytest.mark.parametrize('param', ["double::colon"]) def test_func(self, param): pass """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") node.assert_attr(classname="test_double_colon_split_method_issue469.TestClass") node.assert_attr(name="test_func[double::colon]") def test_unicode_issue368(testdir): path = testdir.tmpdir.join("test.xml") log = LogXML(str(path), None) ustr = u"ВНИ!" class Report(BaseReport): longrepr = ustr sections = [] nodeid = "something" location = "tests/filename.py", 42, "TestClass.method" test_report = Report() # hopefully this is not too brittle ... log.pytest_sessionstart() node_reporter = log._opentestcase(test_report) node_reporter.append_failure(test_report) node_reporter.append_collect_error(test_report) node_reporter.append_collect_skipped(test_report) node_reporter.append_error(test_report) test_report.longrepr = "filename", 1, ustr node_reporter.append_skipped(test_report) test_report.longrepr = "filename", 1, "Skipped: 卡嘣嘣" node_reporter.append_skipped(test_report) test_report.wasxfail = ustr node_reporter.append_skipped(test_report) log.pytest_sessionfinish() def test_record_property(testdir): testdir.makepyfile( """ import pytest @pytest.fixture def other(record_property): record_property("bar", 1) def test_record(record_property, other): record_property("foo", "<1"); """ ) result, dom = runandparse(testdir, "-rwv") node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") psnode = tnode.find_first_by_tag("properties") pnodes = psnode.find_by_tag("property") pnodes[0].assert_attr(name="bar", value="1") pnodes[1].assert_attr(name="foo", value="<1") def test_record_property_same_name(testdir): testdir.makepyfile( """ def test_record_with_same_name(record_property): record_property("foo", "bar") record_property("foo", "baz") """ ) result, dom = runandparse(testdir, "-rw") node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") psnode = tnode.find_first_by_tag("properties") pnodes = psnode.find_by_tag("property") pnodes[0].assert_attr(name="foo", value="bar") pnodes[1].assert_attr(name="foo", value="baz") @pytest.mark.parametrize("fixture_name", ["record_property", "record_xml_attribute"]) def test_record_fixtures_without_junitxml(testdir, fixture_name): testdir.makepyfile( """ def test_record({fixture_name}): {fixture_name}("foo", "bar") """.format( fixture_name=fixture_name ) ) result = testdir.runpytest() assert result.ret == 0 @pytest.mark.filterwarnings("default") def test_record_attribute(testdir): testdir.makeini( """ [pytest] junit_family = xunit1 """ ) testdir.makepyfile( """ import pytest @pytest.fixture def other(record_xml_attribute): record_xml_attribute("bar", 1) def test_record(record_xml_attribute, other): record_xml_attribute("foo", "<1"); """ ) result, dom = runandparse(testdir, "-rw") node = dom.find_first_by_tag("testsuite") tnode = node.find_first_by_tag("testcase") tnode.assert_attr(bar="1") tnode.assert_attr(foo="<1") result.stdout.fnmatch_lines( ["*test_record_attribute.py:6:*record_xml_attribute is an experimental feature"] ) @pytest.mark.filterwarnings("default") @pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"]) def test_record_fixtures_xunit2(testdir, fixture_name): """Ensure record_xml_attribute and record_property drop values when outside of legacy family """ testdir.makeini( """ [pytest] junit_family = xunit2 """ ) testdir.makepyfile( """ import pytest @pytest.fixture def other({fixture_name}): {fixture_name}("bar", 1) def test_record({fixture_name}, other): {fixture_name}("foo", "<1"); """.format( fixture_name=fixture_name ) ) result, dom = runandparse(testdir, "-rw") expected_lines = [] if fixture_name == "record_xml_attribute": expected_lines.append( "*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature" ) expected_lines = [ "*test_record_fixtures_xunit2.py:6:*{fixture_name} is incompatible " "with junit_family 'xunit2' (use 'legacy' or 'xunit1')".format( fixture_name=fixture_name ) ] result.stdout.fnmatch_lines(expected_lines) def test_random_report_log_xdist(testdir, monkeypatch): """xdist calls pytest_runtest_logreport as they are executed by the slaves, with nodes from several nodes overlapping, so junitxml must cope with that to produce correct reports. #1064 """ pytest.importorskip("xdist") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) testdir.makepyfile( """ import pytest, time @pytest.mark.parametrize('i', list(range(30))) def test_x(i): assert i != 22 """ ) _, dom = runandparse(testdir, "-n2") suite_node = dom.find_first_by_tag("testsuite") failed = [] for case_node in suite_node.find_by_tag("testcase"): if case_node.find_first_by_tag("failure"): failed.append(case_node["name"]) assert failed == ["test_x[22]"] def test_root_testsuites_tag(testdir): testdir.makepyfile( """ def test_x(): pass """ ) _, dom = runandparse(testdir) root = dom.get_unique_child assert root.tag == "testsuites" suite_node = root.get_unique_child assert suite_node.tag == "testsuite" def test_runs_twice(testdir): f = testdir.makepyfile( """ def test_pass(): pass """ ) result, dom = runandparse(testdir, f, f) assert "INTERNALERROR" not in result.stdout.str() first, second = [x["classname"] for x in dom.find_by_tag("testcase")] assert first == second @pytest.mark.xfail(reason="hangs", run=False) def test_runs_twice_xdist(testdir): pytest.importorskip("xdist") f = testdir.makepyfile( """ def test_pass(): pass """ ) result, dom = runandparse(testdir, f, "--dist", "each", "--tx", "2*popen") assert "INTERNALERROR" not in result.stdout.str() first, second = [x["classname"] for x in dom.find_by_tag("testcase")] assert first == second def test_fancy_items_regression(testdir): # issue 1259 testdir.makeconftest( """ import pytest class FunItem(pytest.Item): def runtest(self): pass class NoFunItem(pytest.Item): def runtest(self): pass class FunCollector(pytest.File): def collect(self): return [ FunItem('a', self), NoFunItem('a', self), NoFunItem('b', self), ] def pytest_collect_file(path, parent): if path.check(ext='.py'): return FunCollector(path, parent) """ ) testdir.makepyfile( """ def test_pass(): pass """ ) result, dom = runandparse(testdir) assert "INTERNALERROR" not in result.stdout.str() items = sorted("%(classname)s %(name)s" % x for x in dom.find_by_tag("testcase")) import pprint pprint.pprint(items) assert items == [ u"conftest a", u"conftest a", u"conftest b", u"test_fancy_items_regression a", u"test_fancy_items_regression a", u"test_fancy_items_regression b", u"test_fancy_items_regression test_pass", ] def test_global_properties(testdir): path = testdir.tmpdir.join("test_global_properties.xml") log = LogXML(str(path), None) class Report(BaseReport): sections = [] nodeid = "test_node_id" log.pytest_sessionstart() log.add_global_property("foo", 1) log.add_global_property("bar", 2) log.pytest_sessionfinish() dom = minidom.parse(str(path)) properties = dom.getElementsByTagName("properties") assert properties.length == 1, "There must be one node" property_list = dom.getElementsByTagName("property") assert property_list.length == 2, "There most be only 2 property nodes" expected = {"foo": "1", "bar": "2"} actual = {} for p in property_list: k = str(p.getAttribute("name")) v = str(p.getAttribute("value")) actual[k] = v assert actual == expected def test_url_property(testdir): test_url = "http://www.github.com/pytest-dev" path = testdir.tmpdir.join("test_url_property.xml") log = LogXML(str(path), None) class Report(BaseReport): longrepr = "FooBarBaz" sections = [] nodeid = "something" location = "tests/filename.py", 42, "TestClass.method" url = test_url test_report = Report() log.pytest_sessionstart() node_reporter = log._opentestcase(test_report) node_reporter.append_failure(test_report) log.pytest_sessionfinish() test_case = minidom.parse(str(path)).getElementsByTagName("testcase")[0] assert ( test_case.getAttribute("url") == test_url ), "The URL did not get written to the xml" def test_record_testsuite_property(testdir): testdir.makepyfile( """ def test_func1(record_testsuite_property): record_testsuite_property("stats", "all good") def test_func2(record_testsuite_property): record_testsuite_property("stats", 10) """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") properties_node = node.find_first_by_tag("properties") p1_node = properties_node.find_nth_by_tag("property", 0) p2_node = properties_node.find_nth_by_tag("property", 1) p1_node.assert_attr(name="stats", value="all good") p2_node.assert_attr(name="stats", value="10") def test_record_testsuite_property_junit_disabled(testdir): testdir.makepyfile( """ def test_func1(record_testsuite_property): record_testsuite_property("stats", "all good") """ ) result = testdir.runpytest() assert result.ret == 0 @pytest.mark.parametrize("junit", [True, False]) def test_record_testsuite_property_type_checking(testdir, junit): testdir.makepyfile( """ def test_func1(record_testsuite_property): record_testsuite_property(1, 2) """ ) args = ("--junitxml=tests.xml",) if junit else () result = testdir.runpytest(*args) assert result.ret == 1 result.stdout.fnmatch_lines( ["*TypeError: name parameter needs to be a string, but int given"] ) @pytest.mark.parametrize("suite_name", ["my_suite", ""]) def test_set_suite_name(testdir, suite_name): if suite_name: testdir.makeini( """ [pytest] junit_suite_name={} """.format( suite_name ) ) expected = suite_name else: expected = "pytest" testdir.makepyfile( """ import pytest def test_func(): pass """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testsuite") node.assert_attr(name=expected) def test_escaped_skipreason_issue3533(testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip(reason='1 <> 2') def test_skip(): pass """ ) _, dom = runandparse(testdir) node = dom.find_first_by_tag("testcase") snode = node.find_first_by_tag("skipped") assert "1 <> 2" in snode.text snode.assert_attr(message="1 <> 2") def test_logging_passing_tests_disabled_does_not_log_test_output(testdir): testdir.makeini( """ [pytest] junit_log_passing_tests=False junit_logging=system-out """ ) testdir.makepyfile( """ import pytest import logging import sys def test_func(): sys.stdout.write('This is stdout') sys.stderr.write('This is stderr') logging.warning('hello') """ ) result, dom = runandparse(testdir) assert result.ret == 0 node = dom.find_first_by_tag("testcase") assert len(node.find_by_tag("system-err")) == 0 assert len(node.find_by_tag("system-out")) == 0 @pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"]) def test_logging_passing_tests_disabled_logs_output_for_failing_test_issue5430( testdir, junit_logging ): testdir.makeini( """ [pytest] junit_log_passing_tests=False """ ) testdir.makepyfile( """ import pytest import logging import sys def test_func(): logging.warning('hello') assert 0 """ ) result, dom = runandparse(testdir, "-o", "junit_logging=%s" % junit_logging) assert result.ret == 1 node = dom.find_first_by_tag("testcase") if junit_logging == "system-out": assert len(node.find_by_tag("system-err")) == 0 assert len(node.find_by_tag("system-out")) == 1 elif junit_logging == "system-err": assert len(node.find_by_tag("system-err")) == 1 assert len(node.find_by_tag("system-out")) == 0 else: assert junit_logging == "no" assert len(node.find_by_tag("system-err")) == 0 assert len(node.find_by_tag("system-out")) == 0 pytest-4.6.9/testing/acceptance_test.py0000664000372000037200000011642213604173400021104 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys import textwrap import types import attr import py import six import pytest from _pytest.compat import importlib_metadata from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_USAGEERROR from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG def prepend_pythonpath(*dirs): cur = os.getenv("PYTHONPATH") if cur: dirs += (cur,) return os.pathsep.join(str(p) for p in dirs) class TestGeneralUsage(object): def test_config_error(self, testdir): testdir.copy_example("conftest_usageerror/conftest.py") result = testdir.runpytest(testdir.tmpdir) assert result.ret == EXIT_USAGEERROR result.stderr.fnmatch_lines(["*ERROR: hello"]) result.stdout.fnmatch_lines(["*pytest_unconfigure_called"]) def test_root_conftest_syntax_error(self, testdir): testdir.makepyfile(conftest="raise SyntaxError\n") result = testdir.runpytest() result.stderr.fnmatch_lines(["*raise SyntaxError*"]) assert result.ret != 0 def test_early_hook_error_issue38_1(self, testdir): testdir.makeconftest( """ def pytest_sessionstart(): 0 / 0 """ ) result = testdir.runpytest(testdir.tmpdir) assert result.ret != 0 # tracestyle is native by default for hook failures result.stdout.fnmatch_lines( ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] ) result = testdir.runpytest(testdir.tmpdir, "--fulltrace") assert result.ret != 0 # tracestyle is native by default for hook failures result.stdout.fnmatch_lines( ["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"] ) def test_early_hook_configure_error_issue38(self, testdir): testdir.makeconftest( """ def pytest_configure(): 0 / 0 """ ) result = testdir.runpytest(testdir.tmpdir) assert result.ret != 0 # here we get it on stderr result.stderr.fnmatch_lines( ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] ) def test_file_not_found(self, testdir): result = testdir.runpytest("asd") assert result.ret != 0 result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) def test_file_not_found_unconfigure_issue143(self, testdir): testdir.makeconftest( """ def pytest_configure(): print("---configure") def pytest_unconfigure(): print("---unconfigure") """ ) result = testdir.runpytest("-s", "asd") assert result.ret == 4 # EXIT_USAGEERROR result.stderr.fnmatch_lines(["ERROR: file not found*asd"]) result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"]) def test_config_preparse_plugin_option(self, testdir): testdir.makepyfile( pytest_xyz=""" def pytest_addoption(parser): parser.addoption("--xyz", dest="xyz", action="store") """ ) testdir.makepyfile( test_one=""" def test_option(pytestconfig): assert pytestconfig.option.xyz == "123" """ ) result = testdir.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.parametrize("load_cov_early", [True, False]) def test_early_load_setuptools_name(self, testdir, monkeypatch, load_cov_early): testdir.makepyfile(mytestplugin1_module="") testdir.makepyfile(mytestplugin2_module="") testdir.makepyfile(mycov_module="") testdir.syspathinsert() loaded = [] @attr.s class DummyEntryPoint(object): name = attr.ib() module = attr.ib() group = "pytest11" def load(self): __import__(self.module) loaded.append(self.name) return sys.modules[self.module] entry_points = [ DummyEntryPoint("myplugin1", "mytestplugin1_module"), DummyEntryPoint("myplugin2", "mytestplugin2_module"), DummyEntryPoint("mycov", "mycov_module"), ] @attr.s class DummyDist(object): entry_points = attr.ib() files = () def my_dists(): return (DummyDist(entry_points),) monkeypatch.setattr(importlib_metadata, "distributions", my_dists) params = ("-p", "mycov") if load_cov_early else () testdir.runpytest_inprocess(*params) if load_cov_early: assert loaded == ["mycov", "myplugin1", "myplugin2"] else: assert loaded == ["myplugin1", "myplugin2", "mycov"] def test_assertion_magic(self, testdir): p = testdir.makepyfile( """ def test_this(): x = 0 assert x """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["> assert x", "E assert 0"]) assert result.ret == 1 def test_nested_import_error(self, testdir): p = testdir.makepyfile( """ import import_fails def test_this(): assert import_fails.a == 1 """ ) testdir.makepyfile(import_fails="import does_not_work") result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ # XXX on jython this fails: "> import import_fails", "ImportError while importing test module*", "*No module named *does_not_work*", ] ) assert result.ret == 2 def test_not_collectable_arguments(self, testdir): p1 = testdir.makepyfile("") p2 = testdir.makefile(".pyc", "123") result = testdir.runpytest(p1, p2) assert result.ret result.stderr.fnmatch_lines(["*ERROR: not found:*{}".format(p2.basename)]) @pytest.mark.filterwarnings("default") def test_better_reporting_on_conftest_load_failure(self, testdir, request): """Show a user-friendly traceback on conftest import failures (#486, #3332)""" testdir.makepyfile("") testdir.makeconftest( """ def foo(): import qwerty foo() """ ) result = testdir.runpytest("--help") result.stdout.fnmatch_lines( """ *--version* *warning*conftest.py* """ ) result = testdir.runpytest() dirname = request.node.name + "0" exc_name = ( "ModuleNotFoundError" if sys.version_info >= (3, 6) else "ImportError" ) result.stderr.fnmatch_lines( [ "ImportError while loading conftest '*{sep}{dirname}{sep}conftest.py'.".format( dirname=dirname, sep=os.sep ), "conftest.py:3: in ", " foo()", "conftest.py:2: in foo", " import qwerty", "E {}: No module named {q}qwerty{q}".format( exc_name, q="" if six.PY2 else "'" ), ] ) def test_early_skip(self, testdir): testdir.mkdir("xyz") testdir.makeconftest( """ import pytest def pytest_collect_directory(): pytest.skip("early") """ ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED result.stdout.fnmatch_lines(["*1 skip*"]) def test_issue88_initial_file_multinodes(self, testdir): testdir.copy_example("issue88_initial_file_multinodes") p = testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest(p, "--collect-only") result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"]) def test_issue93_initialnode_importing_capturing(self, testdir): testdir.makeconftest( """ import sys print("should not be seen") sys.stderr.write("stder42\\n") """ ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED assert "should not be seen" not in result.stdout.str() assert "stderr42" not in result.stderr.str() def test_conftest_printing_shows_if_error(self, testdir): testdir.makeconftest( """ print("should be seen") assert 0 """ ) result = testdir.runpytest() assert result.ret != 0 assert "should be seen" in result.stdout.str() @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) def test_chdir(self, testdir): testdir.tmpdir.join("py").mksymlinkto(py._pydir) p = testdir.tmpdir.join("main.py") p.write( textwrap.dedent( """\ import sys, os sys.path.insert(0, '') import py print(py.__file__) print(py.__path__) os.chdir(os.path.dirname(os.getcwd())) print(py.log) """ ) ) result = testdir.runpython(p) assert not result.ret def test_issue109_sibling_conftests_not_loaded(self, testdir): sub1 = testdir.mkdir("sub1") sub2 = testdir.mkdir("sub2") sub1.join("conftest.py").write("assert 0") result = testdir.runpytest(sub2) assert result.ret == EXIT_NOTESTSCOLLECTED sub2.ensure("__init__.py") p = sub2.ensure("test_hello.py") result = testdir.runpytest(p) assert result.ret == EXIT_NOTESTSCOLLECTED result = testdir.runpytest(sub1) assert result.ret == EXIT_USAGEERROR def test_directory_skipped(self, testdir): testdir.makeconftest( """ import pytest def pytest_ignore_collect(): pytest.skip("intentional") """ ) testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED result.stdout.fnmatch_lines(["*1 skipped*"]) def test_multiple_items_per_collector_byid(self, testdir): c = testdir.makeconftest( """ import pytest class MyItem(pytest.Item): def runtest(self): pass class MyCollector(pytest.File): def collect(self): return [MyItem(name="xyz", parent=self)] def pytest_collect_file(path, parent): if path.basename.startswith("conftest"): return MyCollector(path, parent) """ ) result = testdir.runpytest(c.basename + "::" + "xyz") assert result.ret == 0 result.stdout.fnmatch_lines(["*1 pass*"]) def test_skip_on_generated_funcarg_id(self, testdir): testdir.makeconftest( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize('x', [3], ids=['hello-123']) def pytest_runtest_setup(item): print(item.keywords) if 'hello-123' in item.keywords: pytest.skip("hello") assert 0 """ ) p = testdir.makepyfile("""def test_func(x): pass""") res = testdir.runpytest(p, SHOW_PYTEST_WARNINGS_ARG) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 skipped*"]) def test_direct_addressing_selects(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize('i', [1, 2], ids=["1", "2"]) def test_func(i): pass """ ) res = testdir.runpytest( p.basename + "::" + "test_func[1]", SHOW_PYTEST_WARNINGS_ARG ) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 passed*"]) def test_direct_addressing_notfound(self, testdir): p = testdir.makepyfile( """ def test_func(): pass """ ) res = testdir.runpytest(p.basename + "::" + "test_notfound") assert res.ret res.stderr.fnmatch_lines(["*ERROR*not found*"]) def test_docstring_on_hookspec(self): from _pytest import hookspec for name, value in vars(hookspec).items(): if name.startswith("pytest_"): assert value.__doc__, "no docstring for %s" % name def test_initialization_error_issue49(self, testdir): testdir.makeconftest( """ def pytest_configure(): x """ ) result = testdir.runpytest() assert result.ret == 3 # internal error result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"]) assert "sessionstarttime" not in result.stderr.str() @pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"]) def test_issue134_report_error_when_collecting_member(self, testdir, lookfor): testdir.makepyfile( test_fun=""" def test_a(): pass def""" ) result = testdir.runpytest(lookfor) result.stdout.fnmatch_lines(["*SyntaxError*"]) if "::" in lookfor: result.stderr.fnmatch_lines(["*ERROR*"]) assert result.ret == 4 # usage error only if item not found def test_report_all_failed_collections_initargs(self, testdir): testdir.makeconftest( """ from _pytest.main import EXIT_USAGEERROR def pytest_sessionfinish(exitstatus): assert exitstatus == EXIT_USAGEERROR print("pytest_sessionfinish_called") """ ) testdir.makepyfile(test_a="def", test_b="def") result = testdir.runpytest("test_a.py::a", "test_b.py::b") result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"]) result.stdout.fnmatch_lines(["pytest_sessionfinish_called"]) assert result.ret == EXIT_USAGEERROR @pytest.mark.usefixtures("recwarn") def test_namespace_import_doesnt_confuse_import_hook(self, testdir): """ Ref #383. Python 3.3's namespace package messed with our import hooks Importing a module that didn't exist, even if the ImportError was gracefully handled, would make our test crash. Use recwarn here to silence this warning in Python 2.7: ImportWarning: Not importing directory '...\not_a_package': missing __init__.py """ testdir.mkdir("not_a_package") p = testdir.makepyfile( """ try: from not_a_package import doesnt_exist except ImportError: # We handle the import error gracefully here pass def test_whatever(): pass """ ) res = testdir.runpytest(p.basename) assert res.ret == 0 def test_unknown_option(self, testdir): result = testdir.runpytest("--qwlkej") result.stderr.fnmatch_lines( """ *unrecognized* """ ) def test_getsourcelines_error_issue553(self, testdir, monkeypatch): monkeypatch.setattr("inspect.getsourcelines", None) p = testdir.makepyfile( """ def raise_error(obj): raise IOError('source code not available') import inspect inspect.getsourcelines = raise_error def test_foo(invalid_fixture): pass """ ) res = testdir.runpytest(p) res.stdout.fnmatch_lines( ["*source code not available*", "E*fixture 'invalid_fixture' not found"] ) def test_plugins_given_as_strings(self, tmpdir, monkeypatch, _sys_snapshot): """test that str values passed to main() as `plugins` arg are interpreted as module names to be imported and registered. #855. """ with pytest.raises(ImportError) as excinfo: pytest.main([str(tmpdir)], plugins=["invalid.module"]) assert "invalid" in str(excinfo.value) p = tmpdir.join("test_test_plugins_given_as_strings.py") p.write("def test_foo(): pass") mod = types.ModuleType("myplugin") monkeypatch.setitem(sys.modules, "myplugin", mod) assert pytest.main(args=[str(tmpdir)], plugins=["myplugin"]) == 0 def test_parametrized_with_bytes_regex(self, testdir): p = testdir.makepyfile( """ import re import pytest @pytest.mark.parametrize('r', [re.compile(b'foo')]) def test_stuff(r): pass """ ) res = testdir.runpytest(p) res.stdout.fnmatch_lines(["*1 passed*"]) def test_parametrized_with_null_bytes(self, testdir): """Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)""" p = testdir.makepyfile( u""" # encoding: UTF-8 import pytest @pytest.mark.parametrize("data", [b"\\x00", "\\x00", u'ação']) def test_foo(data): assert data """ ) res = testdir.runpytest(p) res.assert_outcomes(passed=3) class TestInvocationVariants(object): def test_earlyinit(self, testdir): p = testdir.makepyfile( """ import pytest assert hasattr(pytest, 'mark') """ ) result = testdir.runpython(p) assert result.ret == 0 @pytest.mark.xfail("sys.platform.startswith('java')") def test_pydoc(self, testdir): for name in ("py.test", "pytest"): result = testdir.runpython_c("import {};help({})".format(name, name)) assert result.ret == 0 s = result.stdout.str() assert "MarkGenerator" in s def test_import_star_py_dot_test(self, testdir): p = testdir.makepyfile( """ from py.test import * #collect #cmdline #Item # assert collect.Item is Item # assert collect.Collector is Collector main skip xfail """ ) result = testdir.runpython(p) assert result.ret == 0 def test_import_star_pytest(self, testdir): p = testdir.makepyfile( """ from pytest import * #Item #File main skip xfail """ ) result = testdir.runpython(p) assert result.ret == 0 def test_double_pytestcmdline(self, testdir): p = testdir.makepyfile( run=""" import pytest pytest.main() pytest.main() """ ) testdir.makepyfile( """ def test_hello(): pass """ ) result = testdir.runpython(p) result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"]) def test_python_minus_m_invocation_ok(self, testdir): p1 = testdir.makepyfile("def test_hello(): pass") res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 def test_python_minus_m_invocation_fail(self, testdir): p1 = testdir.makepyfile("def test_fail(): 0/0") res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 1 def test_python_pytest_package(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 passed*"]) def test_equivalence_pytest_pytest(self): assert pytest.main == py.test.cmdline.main def test_invoke_with_invalid_type(self, capsys): with pytest.raises( TypeError, match="expected to be a list or tuple of strings, got: '-h'" ): pytest.main("-h") def test_invoke_with_path(self, tmpdir, capsys): retcode = pytest.main(tmpdir) assert retcode == EXIT_NOTESTSCOLLECTED out, err = capsys.readouterr() def test_invoke_plugin_api(self, testdir, capsys): class MyPlugin(object): def pytest_addoption(self, parser): parser.addoption("--myopt") pytest.main(["-h"], plugins=[MyPlugin()]) out, err = capsys.readouterr() assert "--myopt" in out def test_pyargs_importerror(self, testdir, monkeypatch): monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = testdir.mkpydir("tpkg") path.join("test_hello.py").write("raise ImportError") result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret != 0 result.stdout.fnmatch_lines(["collected*0*items*/*1*errors"]) def test_cmdline_python_package(self, testdir, monkeypatch): import warnings monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = testdir.mkpydir("tpkg") path.join("test_hello.py").write("def test_hello(): pass") path.join("test_world.py").write("def test_world(): pass") result = testdir.runpytest("--pyargs", "tpkg") assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) empty_package = testdir.mkpydir("empty_package") monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep) # the path which is not a package raises a warning on pypy; # no idea why only pypy and not normal python warn about it here with warnings.catch_warnings(): warnings.simplefilter("ignore", ImportWarning) result = testdir.runpytest("--pyargs", ".") assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) monkeypatch.setenv("PYTHONPATH", str(testdir), prepend=os.pathsep) result = testdir.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True) assert result.ret != 0 result.stderr.fnmatch_lines(["*not*found*test_missing*"]) def test_cmdline_python_namespace_package(self, testdir, monkeypatch): """ test --pyargs option with namespace packages (#1567) Ref: https://packaging.python.org/guides/packaging-namespace-packages/ """ monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) search_path = [] for dirname in "hello", "world": d = testdir.mkdir(dirname) search_path.append(d) ns = d.mkdir("ns_pkg") ns.join("__init__.py").write( "__import__('pkg_resources').declare_namespace(__name__)" ) lib = ns.mkdir(dirname) lib.ensure("__init__.py") lib.join("test_{}.py".format(dirname)).write( "def test_{}(): pass\ndef test_other():pass".format(dirname) ) # The structure of the test directory is now: # . # ├── hello # │ └── ns_pkg # │ ├── __init__.py # │ └── hello # │ ├── __init__.py # │ └── test_hello.py # └── world # └── ns_pkg # ├── __init__.py # └── world # ├── __init__.py # └── test_world.py # NOTE: the different/reversed ordering is intentional here. monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path)) for p in search_path: monkeypatch.syspath_prepend(p) # mixed module and filenames: monkeypatch.chdir("world") result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world") assert result.ret == 0 result.stdout.fnmatch_lines( [ "test_hello.py::test_hello*PASSED*", "test_hello.py::test_other*PASSED*", "ns_pkg/world/test_world.py::test_world*PASSED*", "ns_pkg/world/test_world.py::test_other*PASSED*", "*4 passed in*", ] ) # specify tests within a module testdir.chdir() result = testdir.runpytest( "--pyargs", "-v", "ns_pkg.world.test_world::test_other" ) assert result.ret == 0 result.stdout.fnmatch_lines( ["*test_world.py::test_other*PASSED*", "*1 passed*"] ) def test_invoke_test_and_doctestmodules(self, testdir): p = testdir.makepyfile( """ def test(): pass """ ) result = testdir.runpytest(str(p) + "::test", "--doctest-modules") result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks") def test_cmdline_python_package_symlink(self, testdir, monkeypatch): """ test --pyargs option with packages with path containing symlink can have conftest.py in their package (#2985) """ # dummy check that we can actually create symlinks: on Windows `os.symlink` is available, # but normal users require special admin privileges to create symlinks. if sys.platform == "win32": try: os.symlink( str(testdir.tmpdir.ensure("tmpfile")), str(testdir.tmpdir.join("tmpfile2")), ) except OSError as e: pytest.skip(six.text_type(e.args[0])) monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) dirname = "lib" d = testdir.mkdir(dirname) foo = d.mkdir("foo") foo.ensure("__init__.py") lib = foo.mkdir("bar") lib.ensure("__init__.py") lib.join("test_bar.py").write( "def test_bar(): pass\ndef test_other(a_fixture):pass" ) lib.join("conftest.py").write( "import pytest\n@pytest.fixture\ndef a_fixture():pass" ) d_local = testdir.mkdir("local") symlink_location = os.path.join(str(d_local), "lib") if six.PY2: os.symlink(str(d), symlink_location) else: os.symlink(str(d), symlink_location, target_is_directory=True) # The structure of the test directory is now: # . # ├── local # │ └── lib -> ../lib # └── lib # └── foo # ├── __init__.py # └── bar # ├── __init__.py # ├── conftest.py # └── test_bar.py # NOTE: the different/reversed ordering is intentional here. search_path = ["lib", os.path.join("local", "lib")] monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path)) for p in search_path: monkeypatch.syspath_prepend(p) # module picked up in symlink-ed directory: # It picks up local/lib/foo/bar (symlink) via sys.path. result = testdir.runpytest("--pyargs", "-v", "foo.bar") testdir.chdir() assert result.ret == 0 if hasattr(py.path.local, "mksymlinkto"): result.stdout.fnmatch_lines( [ "lib/foo/bar/test_bar.py::test_bar PASSED*", "lib/foo/bar/test_bar.py::test_other PASSED*", "*2 passed*", ] ) else: result.stdout.fnmatch_lines( [ "*lib/foo/bar/test_bar.py::test_bar PASSED*", "*lib/foo/bar/test_bar.py::test_other PASSED*", "*2 passed*", ] ) def test_cmdline_python_package_not_exists(self, testdir): result = testdir.runpytest("--pyargs", "tpkgwhatv") assert result.ret result.stderr.fnmatch_lines(["ERROR*file*or*package*not*found*"]) @pytest.mark.xfail(reason="decide: feature or bug") def test_noclass_discovery_if_not_testcase(self, testdir): testpath = testdir.makepyfile( """ import unittest class TestHello(object): def test_hello(self): assert self.attr class RealTest(unittest.TestCase, TestHello): attr = 42 """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=1) def test_doctest_id(self, testdir): testdir.makefile( ".txt", """ >>> x=3 >>> x 4 """, ) result = testdir.runpytest("-rf") lines = result.stdout.str().splitlines() for line in lines: if line.startswith(("FAIL ", "FAILED ")): _fail, _sep, testid = line.partition(" ") break result = testdir.runpytest(testid, "-rf") result.stdout.fnmatch_lines( ["FAILED test_doctest_id.txt::test_doctest_id.txt", "*1 failed*"] ) def test_core_backward_compatibility(self): """Test backward compatibility for get_plugin_manager function. See #787.""" import _pytest.config assert ( type(_pytest.config.get_plugin_manager()) is _pytest.config.PytestPluginManager ) def test_has_plugin(self, request): """Test hasplugin function of the plugin manager (#932).""" assert request.config.pluginmanager.hasplugin("python") class TestDurations(object): source = """ import time frag = 0.002 def test_something(): pass def test_2(): time.sleep(frag*5) def test_1(): time.sleep(frag) def test_3(): time.sleep(frag*10) """ def test_calls(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=10") assert result.ret == 0 result.stdout.fnmatch_lines_random( ["*durations*", "*call*test_3*", "*call*test_2*"] ) result.stdout.fnmatch_lines( ["(0.00 durations hidden. Use -vv to show these durations.)"] ) def test_calls_show_2(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=2") assert result.ret == 0 lines = result.stdout.get_lines_after("*slowest*durations*") assert "4 passed" in lines[2] def test_calls_showall(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=0") assert result.ret == 0 for x in "23": for y in ("call",): # 'setup', 'call', 'teardown': for line in result.stdout.lines: if ("test_%s" % x) in line and y in line: break else: raise AssertionError("not found {} {}".format(x, y)) def test_calls_showall_verbose(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=0", "-vv") assert result.ret == 0 for x in "123": for y in ("call",): # 'setup', 'call', 'teardown': for line in result.stdout.lines: if ("test_%s" % x) in line and y in line: break else: raise AssertionError("not found {} {}".format(x, y)) def test_with_deselected(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=2", "-k test_2") assert result.ret == 0 result.stdout.fnmatch_lines(["*durations*", "*call*test_2*"]) def test_with_failing_collection(self, testdir): testdir.makepyfile(self.source) testdir.makepyfile(test_collecterror="""xyz""") result = testdir.runpytest("--durations=2", "-k test_1") assert result.ret == 2 result.stdout.fnmatch_lines(["*Interrupted: 1 errors during collection*"]) # Collection errors abort test execution, therefore no duration is # output assert "duration" not in result.stdout.str() def test_with_not(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("-k not 1") assert result.ret == 0 class TestDurationWithFixture(object): source = """ import pytest import time frag = 0.01 @pytest.fixture def setup_fixt(): time.sleep(frag) def test_1(setup_fixt): time.sleep(frag) """ def test_setup_function(self, testdir): testdir.makepyfile(self.source) result = testdir.runpytest("--durations=10") assert result.ret == 0 result.stdout.fnmatch_lines_random( """ *durations* * setup *test_1* * call *test_1* """ ) def test_zipimport_hook(testdir, tmpdir): """Test package loader is being used correctly (see #1837).""" zipapp = pytest.importorskip("zipapp") testdir.tmpdir.join("app").ensure(dir=1) testdir.makepyfile( **{ "app/foo.py": """ import pytest def main(): pytest.main(['--pyarg', 'foo']) """ } ) target = tmpdir.join("foo.zip") zipapp.create_archive(str(testdir.tmpdir.join("app")), str(target), main="foo:main") result = testdir.runpython(target) assert result.ret == 0 result.stderr.fnmatch_lines(["*not found*foo*"]) assert "INTERNALERROR>" not in result.stdout.str() def test_import_plugin_unicode_name(testdir): testdir.makepyfile(myplugin="") testdir.makepyfile( """ def test(): pass """ ) testdir.makeconftest( """ pytest_plugins = [u'myplugin'] """ ) r = testdir.runpytest() assert r.ret == 0 def test_pytest_plugins_as_module(testdir): """Do not raise an error if pytest_plugins attribute is a module (#3899)""" testdir.makepyfile( **{ "__init__.py": "", "pytest_plugins.py": "", "conftest.py": "from . import pytest_plugins", "test_foo.py": "def test(): pass", } ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 passed in *"]) def test_deferred_hook_checking(testdir): """ Check hooks as late as possible (#1821). """ testdir.syspathinsert() testdir.makepyfile( **{ "plugin.py": """ class Hooks(object): def pytest_my_hook(self, config): pass def pytest_configure(config): config.pluginmanager.add_hookspecs(Hooks) """, "conftest.py": """ pytest_plugins = ['plugin'] def pytest_my_hook(config): return 40 """, "test_foo.py": """ def test(request): assert request.config.hook.pytest_my_hook(config=request.config) == [40] """, } ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 passed *"]) def test_fixture_values_leak(testdir): """Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected life-times (#2981). """ testdir.makepyfile( """ import attr import gc import pytest import weakref @attr.s class SomeObj(object): name = attr.ib() fix_of_test1_ref = None session_ref = None @pytest.fixture(scope='session') def session_fix(): global session_ref obj = SomeObj(name='session-fixture') session_ref = weakref.ref(obj) return obj @pytest.fixture def fix(session_fix): global fix_of_test1_ref obj = SomeObj(name='local-fixture') fix_of_test1_ref = weakref.ref(obj) return obj def test1(fix): assert fix_of_test1_ref() is fix def test2(): gc.collect() # fixture "fix" created during test1 must have been destroyed by now assert fix_of_test1_ref() is None """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed *"]) def test_fixture_order_respects_scope(testdir): """Ensure that fixtures are created according to scope order, regression test for #2405 """ testdir.makepyfile( """ import pytest data = {} @pytest.fixture(scope='module') def clean_data(): data.clear() @pytest.fixture(autouse=True) def add_data(): data.update(value=True) @pytest.mark.usefixtures('clean_data') def test_value(): assert data.get('value') """ ) result = testdir.runpytest() assert result.ret == 0 def test_frame_leak_on_failing_test(testdir): """pytest would leak garbage referencing the frames of tests that failed that could never be reclaimed (#2798) Unfortunately it was not possible to remove the actual circles because most of them are made of traceback objects which cannot be weakly referenced. Those objects at least can be eventually claimed by the garbage collector. """ testdir.makepyfile( """ import gc import weakref class Obj: pass ref = None def test1(): obj = Obj() global ref ref = weakref.ref(obj) assert 0 def test2(): gc.collect() assert ref() is None """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"]) def test_fixture_mock_integration(testdir): """Test that decorators applied to fixture are left working (#3774)""" p = testdir.copy_example("acceptance/fixture_mock_integration.py") result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 passed*"]) def test_usage_error_code(testdir): result = testdir.runpytest("-unknown-option-") assert result.ret == EXIT_USAGEERROR @pytest.mark.skipif( sys.version_info[:2] < (3, 5), reason="async def syntax python 3.5+ only" ) @pytest.mark.filterwarnings("default") def test_warn_on_async_function(testdir): testdir.makepyfile( test_async=""" async def test_1(): pass async def test_2(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "test_async.py::test_1", "test_async.py::test_2", "*Coroutine functions are not natively supported*", "*2 skipped, 2 warnings in*", ] ) # ensure our warning message appears only once assert ( result.stdout.str().count("Coroutine functions are not natively supported") == 1 ) pytest-4.6.9/testing/test_runner.py0000664000372000037200000007272713604173400020340 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import inspect import os import sys import types import py import _pytest._code import pytest from _pytest import main from _pytest import outcomes from _pytest import reports from _pytest import runner class TestSetupState(object): def test_setup(self, testdir): ss = runner.SetupState() item = testdir.getitem("def test_func(): pass") values = [1] ss.prepare(item) ss.addfinalizer(values.pop, colitem=item) assert values ss._pop_and_teardown() assert not values def test_teardown_exact_stack_empty(self, testdir): item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() ss.teardown_exact(item, None) ss.teardown_exact(item, None) ss.teardown_exact(item, None) def test_setup_fails_and_failure_is_cached(self, testdir): item = testdir.getitem( """ def setup_module(mod): raise ValueError(42) def test_func(): pass """ ) ss = runner.SetupState() pytest.raises(ValueError, lambda: ss.prepare(item)) pytest.raises(ValueError, lambda: ss.prepare(item)) def test_teardown_multiple_one_fails(self, testdir): r = [] def fin1(): r.append("fin1") def fin2(): raise Exception("oops") def fin3(): r.append("fin3") item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() ss.addfinalizer(fin1, item) ss.addfinalizer(fin2, item) ss.addfinalizer(fin3, item) with pytest.raises(Exception) as err: ss._callfinalizers(item) assert err.value.args == ("oops",) assert r == ["fin3", "fin1"] def test_teardown_multiple_fail(self, testdir): # Ensure the first exception is the one which is re-raised. # Ideally both would be reported however. def fin1(): raise Exception("oops1") def fin2(): raise Exception("oops2") item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() ss.addfinalizer(fin1, item) ss.addfinalizer(fin2, item) with pytest.raises(Exception) as err: ss._callfinalizers(item) assert err.value.args == ("oops2",) def test_teardown_multiple_scopes_one_fails(self, testdir): module_teardown = [] def fin_func(): raise Exception("oops1") def fin_module(): module_teardown.append("fin_module") item = testdir.getitem("def test_func(): pass") ss = runner.SetupState() ss.addfinalizer(fin_module, item.listchain()[-2]) ss.addfinalizer(fin_func, item) ss.prepare(item) with pytest.raises(Exception, match="oops1"): ss.teardown_exact(item, None) assert module_teardown class BaseFunctionalTests(object): def test_passfunction(self, testdir): reports = testdir.runitem( """ def test_func(): pass """ ) rep = reports[1] assert rep.passed assert not rep.failed assert rep.outcome == "passed" assert not rep.longrepr def test_failfunction(self, testdir): reports = testdir.runitem( """ def test_func(): assert 0 """ ) rep = reports[1] assert not rep.passed assert not rep.skipped assert rep.failed assert rep.when == "call" assert rep.outcome == "failed" # assert isinstance(rep.longrepr, ReprExceptionInfo) def test_skipfunction(self, testdir): reports = testdir.runitem( """ import pytest def test_func(): pytest.skip("hello") """ ) rep = reports[1] assert not rep.failed assert not rep.passed assert rep.skipped assert rep.outcome == "skipped" # assert rep.skipped.when == "call" # assert rep.skipped.when == "call" # assert rep.skipped == "%sreason == "hello" # assert rep.skipped.location.lineno == 3 # assert rep.skipped.location.path # assert not rep.skipped.failurerepr def test_skip_in_setup_function(self, testdir): reports = testdir.runitem( """ import pytest def setup_function(func): pytest.skip("hello") def test_func(): pass """ ) print(reports) rep = reports[0] assert not rep.failed assert not rep.passed assert rep.skipped # assert rep.skipped.reason == "hello" # assert rep.skipped.location.lineno == 3 # assert rep.skipped.location.lineno == 3 assert len(reports) == 2 assert reports[1].passed # teardown def test_failure_in_setup_function(self, testdir): reports = testdir.runitem( """ import pytest def setup_function(func): raise ValueError(42) def test_func(): pass """ ) rep = reports[0] assert not rep.skipped assert not rep.passed assert rep.failed assert rep.when == "setup" assert len(reports) == 2 def test_failure_in_teardown_function(self, testdir): reports = testdir.runitem( """ import pytest def teardown_function(func): raise ValueError(42) def test_func(): pass """ ) print(reports) assert len(reports) == 3 rep = reports[2] assert not rep.skipped assert not rep.passed assert rep.failed assert rep.when == "teardown" # assert rep.longrepr.reprcrash.lineno == 3 # assert rep.longrepr.reprtraceback.reprentries def test_custom_failure_repr(self, testdir): testdir.makepyfile( conftest=""" import pytest class Function(pytest.Function): def repr_failure(self, excinfo): return "hello" """ ) reports = testdir.runitem( """ import pytest def test_func(): assert 0 """ ) rep = reports[1] assert not rep.skipped assert not rep.passed assert rep.failed # assert rep.outcome.when == "call" # assert rep.failed.where.lineno == 3 # assert rep.failed.where.path.basename == "test_func.py" # assert rep.failed.failurerepr == "hello" def test_teardown_final_returncode(self, testdir): rec = testdir.inline_runsource( """ def test_func(): pass def teardown_function(func): raise ValueError(42) """ ) assert rec.ret == 1 def test_logstart_logfinish_hooks(self, testdir): rec = testdir.inline_runsource( """ import pytest def test_func(): pass """ ) reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish") assert [x._name for x in reps] == [ "pytest_runtest_logstart", "pytest_runtest_logfinish", ] for rep in reps: assert rep.nodeid == "test_logstart_logfinish_hooks.py::test_func" assert rep.location == ("test_logstart_logfinish_hooks.py", 1, "test_func") def test_exact_teardown_issue90(self, testdir): rec = testdir.inline_runsource( """ import pytest class TestClass(object): def test_method(self): pass def teardown_class(cls): raise Exception() def test_func(): import sys # on python2 exc_info is keept till a function exits # so we would end up calling test functions while # sys.exc_info would return the indexerror # from guessing the lastitem excinfo = sys.exc_info() import traceback assert excinfo[0] is None, \ traceback.format_exception(*excinfo) def teardown_function(func): raise ValueError(42) """ ) reps = rec.getreports("pytest_runtest_logreport") print(reps) for i in range(2): assert reps[i].nodeid.endswith("test_method") assert reps[i].passed assert reps[2].when == "teardown" assert reps[2].failed assert len(reps) == 6 for i in range(3, 5): assert reps[i].nodeid.endswith("test_func") assert reps[i].passed assert reps[5].when == "teardown" assert reps[5].nodeid.endswith("test_func") assert reps[5].failed def test_exact_teardown_issue1206(self, testdir): """issue shadowing error with wrong number of arguments on teardown_method.""" rec = testdir.inline_runsource( """ import pytest class TestClass(object): def teardown_method(self, x, y, z): pass def test_method(self): assert True """ ) reps = rec.getreports("pytest_runtest_logreport") print(reps) assert len(reps) == 3 # assert reps[0].nodeid.endswith("test_method") assert reps[0].passed assert reps[0].when == "setup" # assert reps[1].nodeid.endswith("test_method") assert reps[1].passed assert reps[1].when == "call" # assert reps[2].nodeid.endswith("test_method") assert reps[2].failed assert reps[2].when == "teardown" assert reps[2].longrepr.reprcrash.message in ( # python3 error "TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'", # python2 error "TypeError: teardown_method() takes exactly 4 arguments (2 given)", ) def test_failure_in_setup_function_ignores_custom_repr(self, testdir): testdir.makepyfile( conftest=""" import pytest class Function(pytest.Function): def repr_failure(self, excinfo): assert 0 """ ) reports = testdir.runitem( """ def setup_function(func): raise ValueError(42) def test_func(): pass """ ) assert len(reports) == 2 rep = reports[0] print(rep) assert not rep.skipped assert not rep.passed assert rep.failed # assert rep.outcome.when == "setup" # assert rep.outcome.where.lineno == 3 # assert rep.outcome.where.path.basename == "test_func.py" # assert instanace(rep.failed.failurerepr, PythonFailureRepr) def test_systemexit_does_not_bail_out(self, testdir): try: reports = testdir.runitem( """ def test_func(): raise SystemExit(42) """ ) except SystemExit: pytest.fail("runner did not catch SystemExit") rep = reports[1] assert rep.failed assert rep.when == "call" def test_exit_propagates(self, testdir): try: testdir.runitem( """ import pytest def test_func(): raise pytest.exit.Exception() """ ) except pytest.exit.Exception: pass else: pytest.fail("did not raise") class TestExecutionNonForked(BaseFunctionalTests): def getrunner(self): def f(item): return runner.runtestprotocol(item, log=False) return f def test_keyboardinterrupt_propagates(self, testdir): try: testdir.runitem( """ def test_func(): raise KeyboardInterrupt("fake") """ ) except KeyboardInterrupt: pass else: pytest.fail("did not raise") class TestExecutionForked(BaseFunctionalTests): pytestmark = pytest.mark.skipif("not hasattr(os, 'fork')") def getrunner(self): # XXX re-arrange this test to live in pytest-xdist boxed = pytest.importorskip("xdist.boxed") return boxed.forked_run_report def test_suicide(self, testdir): reports = testdir.runitem( """ def test_func(): import os os.kill(os.getpid(), 15) """ ) rep = reports[0] assert rep.failed assert rep.when == "???" class TestSessionReports(object): def test_collect_result(self, testdir): col = testdir.getmodulecol( """ def test_func1(): pass class TestClass(object): pass """ ) rep = runner.collect_one_node(col) assert not rep.failed assert not rep.skipped assert rep.passed locinfo = rep.location assert locinfo[0] == col.fspath.basename assert not locinfo[1] assert locinfo[2] == col.fspath.basename res = rep.result assert len(res) == 2 assert res[0].name == "test_func1" assert res[1].name == "TestClass" reporttypes = [reports.BaseReport, reports.TestReport, reports.CollectReport] @pytest.mark.parametrize( "reporttype", reporttypes, ids=[x.__name__ for x in reporttypes] ) def test_report_extra_parameters(reporttype): if hasattr(inspect, "signature"): args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:] else: args = inspect.getargspec(reporttype.__init__)[0][1:] basekw = dict.fromkeys(args, []) report = reporttype(newthing=1, **basekw) assert report.newthing == 1 def test_callinfo(): ci = runner.CallInfo.from_call(lambda: 0, "123") assert ci.when == "123" assert ci.result == 0 assert "result" in repr(ci) assert repr(ci) == "" ci = runner.CallInfo.from_call(lambda: 0 / 0, "123") assert ci.when == "123" assert not hasattr(ci, "result") assert repr(ci) == "" assert ci.excinfo assert "exc" in repr(ci) # design question: do we want general hooks in python files? # then something like the following functional tests makes sense @pytest.mark.xfail def test_runtest_in_module_ordering(testdir): p1 = testdir.makepyfile( """ import pytest def pytest_runtest_setup(item): # runs after class-level! item.function.mylist.append("module") class TestClass(object): def pytest_runtest_setup(self, item): assert not hasattr(item.function, 'mylist') item.function.mylist = ['class'] @pytest.fixture def mylist(self, request): return request.function.mylist def pytest_runtest_call(self, item, __multicall__): try: __multicall__.execute() except ValueError: pass def test_hello1(self, mylist): assert mylist == ['class', 'module'], mylist raise ValueError() def test_hello2(self, mylist): assert mylist == ['class', 'module'], mylist def pytest_runtest_teardown(item): del item.function.mylist """ ) result = testdir.runpytest(p1) result.stdout.fnmatch_lines(["*2 passed*"]) def test_outcomeexception_exceptionattributes(): outcome = outcomes.OutcomeException("test") assert outcome.args[0] == outcome.msg def test_outcomeexception_passes_except_Exception(): with pytest.raises(outcomes.OutcomeException): try: raise outcomes.OutcomeException("test") except Exception: pass def test_pytest_exit(): with pytest.raises(pytest.exit.Exception) as excinfo: pytest.exit("hello") assert excinfo.errisinstance(pytest.exit.Exception) def test_pytest_fail(): with pytest.raises(pytest.fail.Exception) as excinfo: pytest.fail("hello") s = excinfo.exconly(tryshort=True) assert s.startswith("Failed") def test_pytest_exit_msg(testdir): testdir.makeconftest( """ import pytest def pytest_configure(config): pytest.exit('oh noes') """ ) result = testdir.runpytest() result.stderr.fnmatch_lines(["Exit: oh noes"]) def test_pytest_exit_returncode(testdir): testdir.makepyfile( """ import pytest def test_foo(): pytest.exit("some exit msg", 99) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"]) # Assert no output on stderr, except for unreliable ResourceWarnings. # (https://github.com/pytest-dev/pytest/issues/5088) assert [ x for x in result.stderr.lines if not x.startswith("Exception ignored in:") and not x.startswith("ResourceWarning") ] == [""] assert result.ret == 99 # It prints to stderr also in case of exit during pytest_sessionstart. testdir.makeconftest( """ import pytest def pytest_sessionstart(): pytest.exit("during_sessionstart", 98) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*! *Exit: during_sessionstart !*"]) assert result.stderr.lines == ["Exit: during_sessionstart", ""] assert result.ret == 98 def test_pytest_fail_notrace_runtest(testdir): """Test pytest.fail(..., pytrace=False) does not show tracebacks during test run.""" testdir.makepyfile( """ import pytest def test_hello(): pytest.fail("hello", pytrace=False) def teardown_function(function): pytest.fail("world", pytrace=False) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["world", "hello"]) assert "def teardown_function" not in result.stdout.str() def test_pytest_fail_notrace_collection(testdir): """Test pytest.fail(..., pytrace=False) does not show tracebacks during collection.""" testdir.makepyfile( """ import pytest def some_internal_function(): pytest.fail("hello", pytrace=False) some_internal_function() """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["hello"]) assert "def some_internal_function()" not in result.stdout.str() @pytest.mark.parametrize("str_prefix", ["u", ""]) def test_pytest_fail_notrace_non_ascii(testdir, str_prefix): """Fix pytest.fail with pytrace=False with non-ascii characters (#1178). This tests with native and unicode strings containing non-ascii chars. """ testdir.makepyfile( u""" # -*- coding: utf-8 -*- import pytest def test_hello(): pytest.fail(%s'oh oh: ☺', pytrace=False) """ % str_prefix ) result = testdir.runpytest() if sys.version_info[0] >= 3: result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"]) else: result.stdout.fnmatch_lines(["*test_hello*", "oh oh: *"]) assert "def test_hello" not in result.stdout.str() def test_pytest_no_tests_collected_exit_status(testdir): result = testdir.runpytest() result.stdout.fnmatch_lines(["*collected 0 items*"]) assert result.ret == main.EXIT_NOTESTSCOLLECTED testdir.makepyfile( test_foo=""" def test_foo(): assert 1 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*collected 1 item*"]) result.stdout.fnmatch_lines(["*1 passed*"]) assert result.ret == main.EXIT_OK result = testdir.runpytest("-k nonmatch") result.stdout.fnmatch_lines(["*collected 1 item*"]) result.stdout.fnmatch_lines(["*1 deselected*"]) assert result.ret == main.EXIT_NOTESTSCOLLECTED def test_exception_printing_skip(): try: pytest.skip("hello") except pytest.skip.Exception: excinfo = _pytest._code.ExceptionInfo.from_current() s = excinfo.exconly(tryshort=True) assert s.startswith("Skipped") def test_importorskip(monkeypatch): importorskip = pytest.importorskip def f(): importorskip("asdlkj") try: sysmod = importorskip("sys") assert sysmod is sys # path = pytest.importorskip("os.path") # assert path == os.path excinfo = pytest.raises(pytest.skip.Exception, f) path = py.path.local(excinfo.getrepr().reprcrash.path) # check that importorskip reports the actual call # in this test the test_runner.py file assert path.purebasename == "test_runner" pytest.raises(SyntaxError, pytest.importorskip, "x y z") pytest.raises(SyntaxError, pytest.importorskip, "x=y") mod = types.ModuleType("hello123") mod.__version__ = "1.3" monkeypatch.setitem(sys.modules, "hello123", mod) with pytest.raises(pytest.skip.Exception): pytest.importorskip("hello123", minversion="1.3.1") mod2 = pytest.importorskip("hello123", minversion="1.3") assert mod2 == mod except pytest.skip.Exception: print(_pytest._code.ExceptionInfo.from_current()) pytest.fail("spurious skip") def test_importorskip_imports_last_module_part(): ospath = pytest.importorskip("os.path") assert os.path == ospath def test_importorskip_dev_module(monkeypatch): try: mod = types.ModuleType("mockmodule") mod.__version__ = "0.13.0.dev-43290" monkeypatch.setitem(sys.modules, "mockmodule", mod) mod2 = pytest.importorskip("mockmodule", minversion="0.12.0") assert mod2 == mod with pytest.raises(pytest.skip.Exception): pytest.importorskip("mockmodule1", minversion="0.14.0") except pytest.skip.Exception: print(_pytest._code.ExceptionInfo.from_current()) pytest.fail("spurious skip") def test_importorskip_module_level(testdir): """importorskip must be able to skip entire modules when used at module level""" testdir.makepyfile( """ import pytest foobarbaz = pytest.importorskip("foobarbaz") def test_foo(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"]) def test_importorskip_custom_reason(testdir): """make sure custom reasons are used""" testdir.makepyfile( """ import pytest foobarbaz = pytest.importorskip("foobarbaz2", reason="just because") def test_foo(): pass """ ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines(["*just because*"]) result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"]) def test_pytest_cmdline_main(testdir): p = testdir.makepyfile( """ import pytest def test_hello(): assert 1 if __name__ == '__main__': pytest.cmdline.main([__file__]) """ ) import subprocess popen = subprocess.Popen([sys.executable, str(p)], stdout=subprocess.PIPE) popen.communicate() ret = popen.wait() assert ret == 0 def test_unicode_in_longrepr(testdir): testdir.makeconftest( """ # -*- coding: utf-8 -*- import pytest @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(): outcome = yield rep = outcome.get_result() if rep.when == "call": rep.longrepr = u'ä' """ ) testdir.makepyfile( """ def test_out(): assert 0 """ ) result = testdir.runpytest() assert result.ret == 1 assert "UnicodeEncodeError" not in result.stderr.str() def test_failure_in_setup(testdir): testdir.makepyfile( """ def setup_module(): 0/0 def test_func(): pass """ ) result = testdir.runpytest("--tb=line") assert "def setup_module" not in result.stdout.str() def test_makereport_getsource(testdir): testdir.makepyfile( """ def test_foo(): if False: pass else: assert False """ ) result = testdir.runpytest() assert "INTERNALERROR" not in result.stdout.str() result.stdout.fnmatch_lines(["*else: assert False*"]) def test_makereport_getsource_dynamic_code(testdir, monkeypatch): """Test that exception in dynamically generated code doesn't break getting the source line.""" import inspect original_findsource = inspect.findsource def findsource(obj, *args, **kwargs): # Can be triggered by dynamically created functions if obj.__name__ == "foo": raise IndexError() return original_findsource(obj, *args, **kwargs) monkeypatch.setattr(inspect, "findsource", findsource) testdir.makepyfile( """ import pytest @pytest.fixture def foo(missing): pass def test_fix(foo): assert False """ ) result = testdir.runpytest("-vv") assert "INTERNALERROR" not in result.stdout.str() result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"]) def test_store_except_info_on_error(): """ Test that upon test failure, the exception info is stored on sys.last_traceback and friends. """ # Simulate item that might raise a specific exception, depending on `raise_error` class var class ItemMightRaise(object): nodeid = "item_that_raises" raise_error = True def runtest(self): if self.raise_error: raise IndexError("TEST") try: runner.pytest_runtest_call(ItemMightRaise()) except IndexError: pass # Check that exception info is stored on sys assert sys.last_type is IndexError assert sys.last_value.args[0] == "TEST" assert sys.last_traceback # The next run should clear the exception info stored by the previous run ItemMightRaise.raise_error = False runner.pytest_runtest_call(ItemMightRaise()) assert sys.last_type is None assert sys.last_value is None assert sys.last_traceback is None def test_current_test_env_var(testdir, monkeypatch): pytest_current_test_vars = [] monkeypatch.setattr( sys, "pytest_current_test_vars", pytest_current_test_vars, raising=False ) testdir.makepyfile( """ import pytest import sys import os @pytest.fixture def fix(): sys.pytest_current_test_vars.append(('setup', os.environ['PYTEST_CURRENT_TEST'])) yield sys.pytest_current_test_vars.append(('teardown', os.environ['PYTEST_CURRENT_TEST'])) def test(fix): sys.pytest_current_test_vars.append(('call', os.environ['PYTEST_CURRENT_TEST'])) """ ) result = testdir.runpytest_inprocess() assert result.ret == 0 test_id = "test_current_test_env_var.py::test" assert pytest_current_test_vars == [ ("setup", test_id + " (setup)"), ("call", test_id + " (call)"), ("teardown", test_id + " (teardown)"), ] assert "PYTEST_CURRENT_TEST" not in os.environ class TestReportContents(object): """ Test user-level API of ``TestReport`` objects. """ def getrunner(self): return lambda item: runner.runtestprotocol(item, log=False) def test_longreprtext_pass(self, testdir): reports = testdir.runitem( """ def test_func(): pass """ ) rep = reports[1] assert rep.longreprtext == "" def test_longreprtext_failure(self, testdir): reports = testdir.runitem( """ def test_func(): x = 1 assert x == 4 """ ) rep = reports[1] assert "assert 1 == 4" in rep.longreprtext def test_captured_text(self, testdir): reports = testdir.runitem( """ import pytest import sys @pytest.fixture def fix(): sys.stdout.write('setup: stdout\\n') sys.stderr.write('setup: stderr\\n') yield sys.stdout.write('teardown: stdout\\n') sys.stderr.write('teardown: stderr\\n') assert 0 def test_func(fix): sys.stdout.write('call: stdout\\n') sys.stderr.write('call: stderr\\n') assert 0 """ ) setup, call, teardown = reports assert setup.capstdout == "setup: stdout\n" assert call.capstdout == "setup: stdout\ncall: stdout\n" assert teardown.capstdout == "setup: stdout\ncall: stdout\nteardown: stdout\n" assert setup.capstderr == "setup: stderr\n" assert call.capstderr == "setup: stderr\ncall: stderr\n" assert teardown.capstderr == "setup: stderr\ncall: stderr\nteardown: stderr\n" def test_no_captured_text(self, testdir): reports = testdir.runitem( """ def test_func(): pass """ ) rep = reports[1] assert rep.capstdout == "" assert rep.capstderr == "" pytest-4.6.9/testing/code/0000775000372000037200000000000013604173421016314 5ustar travistravis00000000000000pytest-4.6.9/testing/code/test_code.py0000664000372000037200000001215713604173400020642 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys from six import text_type from test_excinfo import TWMock import _pytest._code import pytest try: import mock except ImportError: import unittest.mock as mock def test_ne(): code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec")) assert code1 == code1 code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec")) assert code2 != code1 def test_code_gives_back_name_for_not_existing_file(): name = "abc-123" co_code = compile("pass\n", name, "exec") assert co_code.co_filename == name code = _pytest._code.Code(co_code) assert str(code.path) == name assert code.fullsource is None def test_code_with_class(): class A(object): pass pytest.raises(TypeError, _pytest._code.Code, A) def x(): raise NotImplementedError() def test_code_fullsource(): code = _pytest._code.Code(x) full = code.fullsource assert "test_code_fullsource()" in str(full) def test_code_source(): code = _pytest._code.Code(x) src = code.source() expected = """def x(): raise NotImplementedError()""" assert str(src) == expected def test_frame_getsourcelineno_myself(): def func(): return sys._getframe(0) f = func() f = _pytest._code.Frame(f) source, lineno = f.code.fullsource, f.lineno assert source[lineno].startswith(" return sys._getframe(0)") def test_getstatement_empty_fullsource(): def func(): return sys._getframe(0) f = func() f = _pytest._code.Frame(f) with mock.patch.object(f.code.__class__, "fullsource", None): assert f.statement == "" def test_code_from_func(): co = _pytest._code.Code(test_frame_getsourcelineno_myself) assert co.firstlineno assert co.path def test_unicode_handling(): value = u"ąć".encode("UTF-8") def f(): raise Exception(value) excinfo = pytest.raises(Exception, f) text_type(excinfo) if sys.version_info < (3,): bytes(excinfo) @pytest.mark.skipif(sys.version_info[0] >= 3, reason="python 2 only issue") def test_unicode_handling_syntax_error(): value = u"ąć".encode("UTF-8") def f(): raise SyntaxError("invalid syntax", (None, 1, 3, value)) excinfo = pytest.raises(Exception, f) str(excinfo) if sys.version_info[0] < 3: text_type(excinfo) def test_code_getargs(): def f1(x): raise NotImplementedError() c1 = _pytest._code.Code(f1) assert c1.getargs(var=True) == ("x",) def f2(x, *y): raise NotImplementedError() c2 = _pytest._code.Code(f2) assert c2.getargs(var=True) == ("x", "y") def f3(x, **z): raise NotImplementedError() c3 = _pytest._code.Code(f3) assert c3.getargs(var=True) == ("x", "z") def f4(x, *y, **z): raise NotImplementedError() c4 = _pytest._code.Code(f4) assert c4.getargs(var=True) == ("x", "y", "z") def test_frame_getargs(): def f1(x): return sys._getframe(0) fr1 = _pytest._code.Frame(f1("a")) assert fr1.getargs(var=True) == [("x", "a")] def f2(x, *y): return sys._getframe(0) fr2 = _pytest._code.Frame(f2("a", "b", "c")) assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))] def f3(x, **z): return sys._getframe(0) fr3 = _pytest._code.Frame(f3("a", b="c")) assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})] def f4(x, *y, **z): return sys._getframe(0) fr4 = _pytest._code.Frame(f4("a", "b", c="d")) assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})] class TestExceptionInfo(object): def test_bad_getsource(self): try: if False: pass else: assert False except AssertionError: exci = _pytest._code.ExceptionInfo.from_current() assert exci.getrepr() def test_from_current_with_missing(self): with pytest.raises(AssertionError, match="no current exception"): _pytest._code.ExceptionInfo.from_current() class TestTracebackEntry(object): def test_getsource(self): try: if False: pass else: assert False except AssertionError: exci = _pytest._code.ExceptionInfo.from_current() entry = exci.traceback[0] source = entry.getsource() assert len(source) == 6 assert "assert False" in source[5] class TestReprFuncArgs(object): def test_not_raise_exception_with_mixed_encoding(self): from _pytest._code.code import ReprFuncArgs tw = TWMock() args = [("unicode_string", u"São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")] r = ReprFuncArgs(args) r.toterminal(tw) if sys.version_info[0] >= 3: assert ( tw.lines[0] == r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'" ) else: assert tw.lines[0] == "unicode_string = São Paulo, utf8_string = São Paulo" pytest-4.6.9/testing/code/test_source.py0000664000372000037200000004253713604173400021235 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # flake8: noqa # disable flake check on this file because some constructs are strange # or redundant on purpose and can't be disable on a line-by-line basis from __future__ import absolute_import from __future__ import division from __future__ import print_function import ast import inspect import sys import six import _pytest._code import pytest from _pytest._code import Source failsonjython = pytest.mark.xfail("sys.platform.startswith('java')") def test_source_str_function(): x = Source("3") assert str(x) == "3" x = Source(" 3") assert str(x) == "3" x = Source( """ 3 """ ) assert str(x) == "\n3" def test_unicode(): x = Source(u"4") assert str(x) == "4" co = _pytest._code.compile(u'u"å"', mode="eval") val = eval(co) assert isinstance(val, six.text_type) def test_source_from_function(): source = _pytest._code.Source(test_source_str_function) assert str(source).startswith("def test_source_str_function():") def test_source_from_method(): class TestClass(object): def test_method(self): pass source = _pytest._code.Source(TestClass().test_method) assert source.lines == ["def test_method(self):", " pass"] def test_source_from_lines(): lines = ["a \n", "b\n", "c"] source = _pytest._code.Source(lines) assert source.lines == ["a ", "b", "c"] def test_source_from_inner_function(): def f(): pass source = _pytest._code.Source(f, deindent=False) assert str(source).startswith(" def f():") source = _pytest._code.Source(f) assert str(source).startswith("def f():") def test_source_putaround_simple(): source = Source("raise ValueError") source = source.putaround( "try:", """\ except ValueError: x = 42 else: x = 23""", ) assert ( str(source) == """\ try: raise ValueError except ValueError: x = 42 else: x = 23""" ) def test_source_putaround(): source = Source() source = source.putaround( """ if 1: x=1 """ ) assert str(source).strip() == "if 1:\n x=1" def test_source_strips(): source = Source("") assert source == Source() assert str(source) == "" assert source.strip() == source def test_source_strip_multiline(): source = Source() source.lines = ["", " hello", " "] source2 = source.strip() assert source2.lines == [" hello"] def test_syntaxerror_rerepresentation(): ex = pytest.raises(SyntaxError, _pytest._code.compile, "xyz xyz") assert ex.value.lineno == 1 assert ex.value.offset in (4, 5, 7) # XXX pypy/jython versus cpython? assert ex.value.text.strip(), "x x" def test_isparseable(): assert Source("hello").isparseable() assert Source("if 1:\n pass").isparseable() assert Source(" \nif 1:\n pass").isparseable() assert not Source("if 1:\n").isparseable() assert not Source(" \nif 1:\npass").isparseable() assert not Source(chr(0)).isparseable() class TestAccesses(object): source = Source( """\ def f(x): pass def g(x): pass """ ) def test_getrange(self): x = self.source[0:2] assert x.isparseable() assert len(x.lines) == 2 assert str(x) == "def f(x):\n pass" def test_getline(self): x = self.source[0] assert x == "def f(x):" def test_len(self): assert len(self.source) == 4 def test_iter(self): values = [x for x in self.source] assert len(values) == 4 class TestSourceParsingAndCompiling(object): source = Source( """\ def f(x): assert (x == 3 + 4) """ ).strip() def test_compile(self): co = _pytest._code.compile("x=3") d = {} exec(co, d) assert d["x"] == 3 def test_compile_and_getsource_simple(self): co = _pytest._code.compile("x=3") exec(co) source = _pytest._code.Source(co) assert str(source) == "x=3" def test_compile_and_getsource_through_same_function(self): def gensource(source): return _pytest._code.compile(source) co1 = gensource( """ def f(): raise KeyError() """ ) co2 = gensource( """ def f(): raise ValueError() """ ) source1 = inspect.getsource(co1) assert "KeyError" in source1 source2 = inspect.getsource(co2) assert "ValueError" in source2 def test_getstatement(self): # print str(self.source) ass = str(self.source[1:]) for i in range(1, 4): # print "trying start in line %r" % self.source[i] s = self.source.getstatement(i) # x = s.deindent() assert str(s) == ass def test_getstatementrange_triple_quoted(self): # print str(self.source) source = Source( """hello(''' ''')""" ) s = source.getstatement(0) assert s == str(source) s = source.getstatement(1) assert s == str(source) def test_getstatementrange_within_constructs(self): source = Source( """\ try: try: raise ValueError except SomeThing: pass finally: 42 """ ) assert len(source) == 7 # check all lineno's that could occur in a traceback # assert source.getstatementrange(0) == (0, 7) # assert source.getstatementrange(1) == (1, 5) assert source.getstatementrange(2) == (2, 3) assert source.getstatementrange(3) == (3, 4) assert source.getstatementrange(4) == (4, 5) # assert source.getstatementrange(5) == (0, 7) assert source.getstatementrange(6) == (6, 7) def test_getstatementrange_bug(self): source = Source( """\ try: x = ( y + z) except: pass """ ) assert len(source) == 6 assert source.getstatementrange(2) == (1, 4) def test_getstatementrange_bug2(self): source = Source( """\ assert ( 33 == [ X(3, b=1, c=2 ), ] ) """ ) assert len(source) == 9 assert source.getstatementrange(5) == (0, 9) def test_getstatementrange_ast_issue58(self): source = Source( """\ def test_some(): for a in [a for a in CAUSE_ERROR]: pass x = 3 """ ) assert getstatement(2, source).lines == source.lines[2:3] assert getstatement(3, source).lines == source.lines[3:4] def test_getstatementrange_out_of_bounds_py3(self): source = Source("if xxx:\n from .collections import something") r = source.getstatementrange(1) assert r == (1, 2) def test_getstatementrange_with_syntaxerror_issue7(self): source = Source(":") pytest.raises(SyntaxError, lambda: source.getstatementrange(0)) def test_compile_to_ast(self): source = Source("x = 4") mod = source.compile(flag=ast.PyCF_ONLY_AST) assert isinstance(mod, ast.Module) compile(mod, "", "exec") def test_compile_and_getsource(self): co = self.source.compile() exec(co, globals()) f(7) excinfo = pytest.raises(AssertionError, f, 6) frame = excinfo.traceback[-1].frame stmt = frame.code.fullsource.getstatement(frame.lineno) assert str(stmt).strip().startswith("assert") @pytest.mark.parametrize("name", ["", None, "my"]) def test_compilefuncs_and_path_sanity(self, name): def check(comp, name): co = comp(self.source, name) if not name: expected = "codegen %s:%d>" % (mypath, mylineno + 2 + 2) else: expected = "codegen %r %s:%d>" % (name, mypath, mylineno + 2 + 2) fn = co.co_filename assert fn.endswith(expected) mycode = _pytest._code.Code(self.test_compilefuncs_and_path_sanity) mylineno = mycode.firstlineno mypath = mycode.path for comp in _pytest._code.compile, _pytest._code.Source.compile: check(comp, name) def test_offsetless_synerr(self): pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode="eval") def test_getstartingblock_singleline(): class A(object): def __init__(self, *args): frame = sys._getframe(1) self.source = _pytest._code.Frame(frame).statement x = A("x", "y") values = [i for i in x.source.lines if i.strip()] assert len(values) == 1 def test_getline_finally(): def c(): pass with pytest.raises(TypeError) as excinfo: teardown = None try: c(1) finally: if teardown: teardown() source = excinfo.traceback[-1].statement assert str(source).strip() == "c(1)" def test_getfuncsource_dynamic(): source = """ def f(): raise ValueError def g(): pass """ co = _pytest._code.compile(source) exec(co, globals()) assert str(_pytest._code.Source(f)).strip() == "def f():\n raise ValueError" assert str(_pytest._code.Source(g)).strip() == "def g(): pass" def test_getfuncsource_with_multine_string(): def f(): c = """while True: pass """ expected = '''\ def f(): c = """while True: pass """ ''' assert str(_pytest._code.Source(f)) == expected.rstrip() def test_deindent(): from _pytest._code.source import deindent as deindent assert deindent(["\tfoo", "\tbar"]) == ["foo", "bar"] source = """\ def f(): def g(): pass """ lines = deindent(source.splitlines()) assert lines == ["def f():", " def g():", " pass"] def test_source_of_class_at_eof_without_newline(tmpdir, _sys_snapshot): # this test fails because the implicit inspect.getsource(A) below # does not return the "x = 1" last line. source = _pytest._code.Source( """ class A(object): def method(self): x = 1 """ ) path = tmpdir.join("a.py") path.write(source) s2 = _pytest._code.Source(tmpdir.join("a.py").pyimport().A) assert str(source).strip() == str(s2).strip() if True: def x(): pass def test_getsource_fallback(): from _pytest._code.source import getsource expected = """def x(): pass""" src = getsource(x) assert src == expected def test_idem_compile_and_getsource(): from _pytest._code.source import getsource expected = "def x(): pass" co = _pytest._code.compile(expected) src = getsource(co) assert src == expected def test_findsource_fallback(): from _pytest._code.source import findsource src, lineno = findsource(x) assert "test_findsource_simple" in str(src) assert src[lineno] == " def x():" def test_findsource(): from _pytest._code.source import findsource co = _pytest._code.compile( """if 1: def x(): pass """ ) src, lineno = findsource(co) assert "if 1:" in str(src) d = {} eval(co, d) src, lineno = findsource(d["x"]) assert "if 1:" in str(src) assert src[lineno] == " def x():" def test_getfslineno(): from _pytest._code import getfslineno def f(x): pass fspath, lineno = getfslineno(f) assert fspath.basename == "test_source.py" assert lineno == _pytest._code.getrawcode(f).co_firstlineno - 1 # see findsource class A(object): pass fspath, lineno = getfslineno(A) _, A_lineno = inspect.findsource(A) assert fspath.basename == "test_source.py" assert lineno == A_lineno assert getfslineno(3) == ("", -1) class B(object): pass B.__name__ = "B2" assert getfslineno(B)[1] == -1 def test_code_of_object_instance_with_call(): class A(object): pass pytest.raises(TypeError, lambda: _pytest._code.Source(A())) class WithCall(object): def __call__(self): pass code = _pytest._code.Code(WithCall()) assert "pass" in str(code.source()) class Hello(object): def __call__(self): pass pytest.raises(TypeError, lambda: _pytest._code.Code(Hello)) def getstatement(lineno, source): from _pytest._code.source import getstatementrange_ast source = _pytest._code.Source(source, deindent=False) ast, start, end = getstatementrange_ast(lineno, source) return source[start:end] def test_oneline(): source = getstatement(0, "raise ValueError") assert str(source) == "raise ValueError" def test_comment_and_no_newline_at_end(): from _pytest._code.source import getstatementrange_ast source = Source( [ "def test_basic_complex():", " assert 1 == 2", "# vim: filetype=pyopencl:fdm=marker", ] ) ast, start, end = getstatementrange_ast(1, source) assert end == 2 def test_oneline_and_comment(): source = getstatement(0, "raise ValueError\n#hello") assert str(source) == "raise ValueError" def test_comments(): source = '''def test(): "comment 1" x = 1 # comment 2 # comment 3 assert False """ comment 4 """ ''' for line in range(2, 6): assert str(getstatement(line, source)) == " x = 1" if sys.version_info >= (3, 8) or hasattr(sys, "pypy_version_info"): tqs_start = 8 else: tqs_start = 10 assert str(getstatement(10, source)) == '"""' for line in range(6, tqs_start): assert str(getstatement(line, source)) == " assert False" for line in range(tqs_start, 10): assert str(getstatement(line, source)) == '"""\ncomment 4\n"""' def test_comment_in_statement(): source = """test(foo=1, # comment 1 bar=2) """ for line in range(1, 3): assert ( str(getstatement(line, source)) == "test(foo=1,\n # comment 1\n bar=2)" ) def test_single_line_else(): source = getstatement(1, "if False: 2\nelse: 3") assert str(source) == "else: 3" def test_single_line_finally(): source = getstatement(1, "try: 1\nfinally: 3") assert str(source) == "finally: 3" def test_issue55(): source = ( "def round_trip(dinp):\n assert 1 == dinp\n" 'def test_rt():\n round_trip("""\n""")\n' ) s = getstatement(3, source) assert str(s) == ' round_trip("""\n""")' def test_multiline(): source = getstatement( 0, """\ raise ValueError( 23 ) x = 3 """, ) assert str(source) == "raise ValueError(\n 23\n)" class TestTry(object): source = """\ try: raise ValueError except Something: raise IndexError(1) else: raise KeyError() """ def test_body(self): source = getstatement(1, self.source) assert str(source) == " raise ValueError" def test_except_line(self): source = getstatement(2, self.source) assert str(source) == "except Something:" def test_except_body(self): source = getstatement(3, self.source) assert str(source) == " raise IndexError(1)" def test_else(self): source = getstatement(5, self.source) assert str(source) == " raise KeyError()" class TestTryFinally(object): source = """\ try: raise ValueError finally: raise IndexError(1) """ def test_body(self): source = getstatement(1, self.source) assert str(source) == " raise ValueError" def test_finally(self): source = getstatement(3, self.source) assert str(source) == " raise IndexError(1)" class TestIf(object): source = """\ if 1: y = 3 elif False: y = 5 else: y = 7 """ def test_body(self): source = getstatement(1, self.source) assert str(source) == " y = 3" def test_elif_clause(self): source = getstatement(2, self.source) assert str(source) == "elif False:" def test_elif(self): source = getstatement(3, self.source) assert str(source) == " y = 5" def test_else(self): source = getstatement(5, self.source) assert str(source) == " y = 7" def test_semicolon(): s = """\ hello ; pytest.skip() """ source = getstatement(0, s) assert str(source) == s.strip() def test_def_online(): s = """\ def func(): raise ValueError(42) def something(): pass """ source = getstatement(0, s) assert str(source) == "def func(): raise ValueError(42)" def XXX_test_expression_multiline(): source = """\ something ''' '''""" result = getstatement(1, source) assert str(result) == "'''\n'''" def test_getstartingblock_multiline(): class A(object): def __init__(self, *args): frame = sys._getframe(1) self.source = _pytest._code.Frame(frame).statement # fmt: off x = A('x', 'y' , 'z') # fmt: on values = [i for i in x.source.lines if i.strip()] assert len(values) == 4 pytest-4.6.9/testing/code/test_excinfo.py0000664000372000037200000013247013604173400021364 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import operator import os import sys import textwrap import py import six from six.moves import queue import _pytest import pytest from _pytest._code.code import ExceptionChainRepr from _pytest._code.code import ExceptionInfo from _pytest._code.code import FormattedExcinfo from _pytest._code.code import ReprExceptionInfo try: import importlib except ImportError: invalidate_import_caches = None else: invalidate_import_caches = getattr(importlib, "invalidate_caches", None) failsonjython = pytest.mark.xfail("sys.platform.startswith('java')") pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3])) @pytest.fixture def limited_recursion_depth(): before = sys.getrecursionlimit() sys.setrecursionlimit(150) yield sys.setrecursionlimit(before) class TWMock(object): WRITE = object() def __init__(self): self.lines = [] self.is_writing = False def sep(self, sep, line=None): self.lines.append((sep, line)) def write(self, msg, **kw): self.lines.append((TWMock.WRITE, msg)) def line(self, line, **kw): self.lines.append(line) def markup(self, text, **kw): return text def get_write_msg(self, idx): flag, msg = self.lines[idx] assert flag == TWMock.WRITE return msg fullwidth = 80 def test_excinfo_simple(): try: raise ValueError except ValueError: info = _pytest._code.ExceptionInfo.from_current() assert info.type == ValueError def test_excinfo_getstatement(): def g(): raise ValueError def f(): g() try: f() except ValueError: excinfo = _pytest._code.ExceptionInfo.from_current() linenumbers = [ _pytest._code.getrawcode(f).co_firstlineno - 1 + 4, _pytest._code.getrawcode(f).co_firstlineno - 1 + 1, _pytest._code.getrawcode(g).co_firstlineno - 1 + 1, ] values = list(excinfo.traceback) foundlinenumbers = [x.lineno for x in values] assert foundlinenumbers == linenumbers # for x in info: # print "%s:%d %s" %(x.path.relto(root), x.lineno, x.statement) # xxx # testchain for getentries test below def f(): # raise ValueError # def g(): # __tracebackhide__ = True f() # def h(): # g() # class TestTraceback_f_g_h(object): def setup_method(self, method): try: h() except ValueError: self.excinfo = _pytest._code.ExceptionInfo.from_current() def test_traceback_entries(self): tb = self.excinfo.traceback entries = list(tb) assert len(tb) == 4 # maybe fragile test assert len(entries) == 4 # maybe fragile test names = ["f", "g", "h"] for entry in entries: try: names.remove(entry.frame.code.name) except ValueError: pass assert not names def test_traceback_entry_getsource(self): tb = self.excinfo.traceback s = str(tb[-1].getsource()) assert s.startswith("def f():") assert s.endswith("raise ValueError") @failsonjython def test_traceback_entry_getsource_in_construct(self): source = _pytest._code.Source( """\ def xyz(): try: raise ValueError except somenoname: pass xyz() """ ) try: exec(source.compile()) except NameError: tb = _pytest._code.ExceptionInfo.from_current().traceback print(tb[-1].getsource()) s = str(tb[-1].getsource()) assert s.startswith("def xyz():\n try:") assert s.strip().endswith("except somenoname:") def test_traceback_cut(self): co = _pytest._code.Code(f) path, firstlineno = co.path, co.firstlineno traceback = self.excinfo.traceback newtraceback = traceback.cut(path=path, firstlineno=firstlineno) assert len(newtraceback) == 1 newtraceback = traceback.cut(path=path, lineno=firstlineno + 2) assert len(newtraceback) == 1 def test_traceback_cut_excludepath(self, testdir): p = testdir.makepyfile("def f(): raise ValueError") with pytest.raises(ValueError) as excinfo: p.pyimport().f() basedir = py.path.local(pytest.__file__).dirpath() newtraceback = excinfo.traceback.cut(excludepath=basedir) for x in newtraceback: if hasattr(x, "path"): assert not py.path.local(x.path).relto(basedir) assert newtraceback[-1].frame.code.path == p def test_traceback_filter(self): traceback = self.excinfo.traceback ntraceback = traceback.filter() assert len(ntraceback) == len(traceback) - 1 @pytest.mark.parametrize( "tracebackhide, matching", [ (lambda info: True, True), (lambda info: False, False), (operator.methodcaller("errisinstance", ValueError), True), (operator.methodcaller("errisinstance", IndexError), False), ], ) def test_traceback_filter_selective(self, tracebackhide, matching): def f(): # raise ValueError # def g(): # __tracebackhide__ = tracebackhide f() # def h(): # g() # excinfo = pytest.raises(ValueError, h) traceback = excinfo.traceback ntraceback = traceback.filter() print("old: {!r}".format(traceback)) print("new: {!r}".format(ntraceback)) if matching: assert len(ntraceback) == len(traceback) - 2 else: # -1 because of the __tracebackhide__ in pytest.raises assert len(ntraceback) == len(traceback) - 1 def test_traceback_recursion_index(self): def f(n): if n < 10: n += 1 f(n) excinfo = pytest.raises(RuntimeError, f, 8) traceback = excinfo.traceback recindex = traceback.recursionindex() assert recindex == 3 def test_traceback_only_specific_recursion_errors(self, monkeypatch): def f(n): if n == 0: raise RuntimeError("hello") f(n - 1) excinfo = pytest.raises(RuntimeError, f, 25) monkeypatch.delattr(excinfo.traceback.__class__, "recursionindex") repr = excinfo.getrepr() assert "RuntimeError: hello" in str(repr.reprcrash) def test_traceback_no_recursion_index(self): def do_stuff(): raise RuntimeError def reraise_me(): import sys exc, val, tb = sys.exc_info() six.reraise(exc, val, tb) def f(n): try: do_stuff() except: # noqa reraise_me() excinfo = pytest.raises(RuntimeError, f, 8) traceback = excinfo.traceback recindex = traceback.recursionindex() assert recindex is None def test_traceback_messy_recursion(self): # XXX: simplified locally testable version decorator = pytest.importorskip("decorator").decorator def log(f, *k, **kw): print("{} {}".format(k, kw)) f(*k, **kw) log = decorator(log) def fail(): raise ValueError("") fail = log(log(fail)) excinfo = pytest.raises(ValueError, fail) assert excinfo.traceback.recursionindex() is None def test_traceback_getcrashentry(self): def i(): __tracebackhide__ = True raise ValueError def h(): i() def g(): __tracebackhide__ = True h() def f(): g() excinfo = pytest.raises(ValueError, f) tb = excinfo.traceback entry = tb.getcrashentry() co = _pytest._code.Code(h) assert entry.frame.code.path == co.path assert entry.lineno == co.firstlineno + 1 assert entry.frame.code.name == "h" def test_traceback_getcrashentry_empty(self): def g(): __tracebackhide__ = True raise ValueError def f(): __tracebackhide__ = True g() excinfo = pytest.raises(ValueError, f) tb = excinfo.traceback entry = tb.getcrashentry() co = _pytest._code.Code(g) assert entry.frame.code.path == co.path assert entry.lineno == co.firstlineno + 2 assert entry.frame.code.name == "g" def test_excinfo_exconly(): excinfo = pytest.raises(ValueError, h) assert excinfo.exconly().startswith("ValueError") with pytest.raises(ValueError) as excinfo: raise ValueError("hello\nworld") msg = excinfo.exconly(tryshort=True) assert msg.startswith("ValueError") assert msg.endswith("world") def test_excinfo_repr(): excinfo = pytest.raises(ValueError, h) s = repr(excinfo) assert s == "" def test_excinfo_str(): excinfo = pytest.raises(ValueError, h) s = str(excinfo) assert s.startswith(__file__[:-9]) # pyc file and $py.class assert s.endswith("ValueError") assert len(s.split(":")) >= 3 # on windows it's 4 def test_excinfo_for_later(): e = ExceptionInfo.for_later() assert "for raises" in repr(e) assert "for raises" in str(e) def test_excinfo_errisinstance(): excinfo = pytest.raises(ValueError, h) assert excinfo.errisinstance(ValueError) def test_excinfo_no_sourcecode(): try: exec("raise ValueError()") except ValueError: excinfo = _pytest._code.ExceptionInfo.from_current() s = str(excinfo.traceback[-1]) assert s == " File '':1 in \n ???\n" def test_excinfo_no_python_sourcecode(tmpdir): # XXX: simplified locally testable version tmpdir.join("test.txt").write("{{ h()}}:") jinja2 = pytest.importorskip("jinja2") loader = jinja2.FileSystemLoader(str(tmpdir)) env = jinja2.Environment(loader=loader) template = env.get_template("test.txt") excinfo = pytest.raises(ValueError, template.render, h=h) for item in excinfo.traceback: print(item) # XXX: for some reason jinja.Template.render is printed in full item.source # shouldnt fail if item.path.basename == "test.txt": assert str(item.source) == "{{ h()}}:" def test_entrysource_Queue_example(): try: queue.Queue().get(timeout=0.001) except queue.Empty: excinfo = _pytest._code.ExceptionInfo.from_current() entry = excinfo.traceback[-1] source = entry.getsource() assert source is not None s = str(source).strip() assert s.startswith("def get") def test_codepath_Queue_example(): try: queue.Queue().get(timeout=0.001) except queue.Empty: excinfo = _pytest._code.ExceptionInfo.from_current() entry = excinfo.traceback[-1] path = entry.path assert isinstance(path, py.path.local) assert path.basename.lower() == "queue.py" assert path.check() def test_match_succeeds(): with pytest.raises(ZeroDivisionError) as excinfo: 0 // 0 excinfo.match(r".*zero.*") def test_match_raises_error(testdir): testdir.makepyfile( """ import pytest def test_division_zero(): with pytest.raises(ZeroDivisionError) as excinfo: 0 / 0 excinfo.match(r'[123]+') """ ) result = testdir.runpytest() assert result.ret != 0 result.stdout.fnmatch_lines(["*AssertionError*Pattern*[123]*not found*"]) class TestFormattedExcinfo(object): @pytest.fixture def importasmod(self, request, _sys_snapshot): def importasmod(source): source = textwrap.dedent(source) tmpdir = request.getfixturevalue("tmpdir") modpath = tmpdir.join("mod.py") tmpdir.ensure("__init__.py") modpath.write(source) if invalidate_import_caches is not None: invalidate_import_caches() return modpath.pyimport() return importasmod def excinfo_from_exec(self, source): source = _pytest._code.Source(source).strip() try: exec(source.compile()) except KeyboardInterrupt: raise except: # noqa return _pytest._code.ExceptionInfo.from_current() assert 0, "did not raise" def test_repr_source(self): pr = FormattedExcinfo() source = _pytest._code.Source( """\ def f(x): pass """ ).strip() pr.flow_marker = "|" lines = pr.get_source(source, 0) assert len(lines) == 2 assert lines[0] == "| def f(x):" assert lines[1] == " pass" def test_repr_source_excinfo(self): """ check if indentation is right """ pr = FormattedExcinfo() excinfo = self.excinfo_from_exec( """ def f(): assert 0 f() """ ) pr = FormattedExcinfo() source = pr._getentrysource(excinfo.traceback[-1]) lines = pr.get_source(source, 1, excinfo) assert lines == [" def f():", "> assert 0", "E AssertionError"] def test_repr_source_not_existing(self): pr = FormattedExcinfo() co = compile("raise ValueError()", "", "exec") try: exec(co) except ValueError: excinfo = _pytest._code.ExceptionInfo.from_current() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_many_line_source_not_existing(self): pr = FormattedExcinfo() co = compile( """ a = 1 raise ValueError() """, "", "exec", ) try: exec(co) except ValueError: excinfo = _pytest._code.ExceptionInfo.from_current() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_source_failing_fullsource(self): pr = FormattedExcinfo() class FakeCode(object): class raw(object): co_filename = "?" path = "?" firstlineno = 5 def fullsource(self): return None fullsource = property(fullsource) class FakeFrame(object): code = FakeCode() f_locals = {} f_globals = {} class FakeTracebackEntry(_pytest._code.Traceback.Entry): def __init__(self, tb, excinfo=None): self.lineno = 5 + 3 @property def frame(self): return FakeFrame() class Traceback(_pytest._code.Traceback): Entry = FakeTracebackEntry class FakeExcinfo(_pytest._code.ExceptionInfo): typename = "Foo" value = Exception() def __init__(self): pass def exconly(self, tryshort): return "EXC" def errisinstance(self, cls): return False excinfo = FakeExcinfo() class FakeRawTB(object): tb_next = None tb = FakeRawTB() excinfo.traceback = Traceback(tb) fail = IOError() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" fail = py.error.ENOENT # noqa repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" def test_repr_local(self): p = FormattedExcinfo(showlocals=True) loc = {"y": 5, "z": 7, "x": 3, "@x": 2, "__builtins__": {}} reprlocals = p.repr_locals(loc) assert reprlocals.lines assert reprlocals.lines[0] == "__builtins__ = " assert reprlocals.lines[1] == "x = 3" assert reprlocals.lines[2] == "y = 5" assert reprlocals.lines[3] == "z = 7" def test_repr_local_with_error(self): class ObjWithErrorInRepr: def __repr__(self): raise NotImplementedError p = FormattedExcinfo(showlocals=True, truncate_locals=False) loc = {"x": ObjWithErrorInRepr(), "__builtins__": {}} reprlocals = p.repr_locals(loc) assert reprlocals.lines assert reprlocals.lines[0] == "__builtins__ = " assert '[NotImplementedError("") raised in repr()]' in reprlocals.lines[1] def test_repr_local_with_exception_in_class_property(self): class ExceptionWithBrokenClass(Exception): @property def __class__(self): raise TypeError("boom!") class ObjWithErrorInRepr: def __repr__(self): raise ExceptionWithBrokenClass() p = FormattedExcinfo(showlocals=True, truncate_locals=False) loc = {"x": ObjWithErrorInRepr(), "__builtins__": {}} reprlocals = p.repr_locals(loc) assert reprlocals.lines assert reprlocals.lines[0] == "__builtins__ = " assert '[ExceptionWithBrokenClass("") raised in repr()]' in reprlocals.lines[1] def test_repr_local_truncated(self): loc = {"l": [i for i in range(10)]} p = FormattedExcinfo(showlocals=True) truncated_reprlocals = p.repr_locals(loc) assert truncated_reprlocals.lines assert truncated_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, ...]" q = FormattedExcinfo(showlocals=True, truncate_locals=False) full_reprlocals = q.repr_locals(loc) assert full_reprlocals.lines assert full_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" def test_repr_tracebackentry_lines(self, importasmod): mod = importasmod( """ def func1(): raise ValueError("hello\\nworld") """ ) excinfo = pytest.raises(ValueError, mod.func1) excinfo.traceback = excinfo.traceback.filter() p = FormattedExcinfo() reprtb = p.repr_traceback_entry(excinfo.traceback[-1]) # test as intermittent entry lines = reprtb.lines assert lines[0] == " def func1():" assert lines[1] == '> raise ValueError("hello\\nworld")' # test as last entry p = FormattedExcinfo(showlocals=True) repr_entry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = repr_entry.lines assert lines[0] == " def func1():" assert lines[1] == '> raise ValueError("hello\\nworld")' assert lines[2] == "E ValueError: hello" assert lines[3] == "E world" assert not lines[4:] loc = repr_entry.reprlocals is not None loc = repr_entry.reprfileloc assert loc.path == mod.__file__ assert loc.lineno == 3 # assert loc.message == "ValueError: hello" def test_repr_tracebackentry_lines2(self, importasmod): mod = importasmod( """ def func1(m, x, y, z): raise ValueError("hello\\nworld") """ ) excinfo = pytest.raises(ValueError, mod.func1, "m" * 90, 5, 13, "z" * 120) excinfo.traceback = excinfo.traceback.filter() entry = excinfo.traceback[-1] p = FormattedExcinfo(funcargs=True) reprfuncargs = p.repr_args(entry) assert reprfuncargs.args[0] == ("m", repr("m" * 90)) assert reprfuncargs.args[1] == ("x", "5") assert reprfuncargs.args[2] == ("y", "13") assert reprfuncargs.args[3] == ("z", repr("z" * 120)) p = FormattedExcinfo(funcargs=True) repr_entry = p.repr_traceback_entry(entry) assert repr_entry.reprfuncargs.args == reprfuncargs.args tw = TWMock() repr_entry.toterminal(tw) assert tw.lines[0] == "m = " + repr("m" * 90) assert tw.lines[1] == "x = 5, y = 13" assert tw.lines[2] == "z = " + repr("z" * 120) def test_repr_tracebackentry_lines_var_kw_args(self, importasmod): mod = importasmod( """ def func1(x, *y, **z): raise ValueError("hello\\nworld") """ ) excinfo = pytest.raises(ValueError, mod.func1, "a", "b", c="d") excinfo.traceback = excinfo.traceback.filter() entry = excinfo.traceback[-1] p = FormattedExcinfo(funcargs=True) reprfuncargs = p.repr_args(entry) assert reprfuncargs.args[0] == ("x", repr("a")) assert reprfuncargs.args[1] == ("y", repr(("b",))) assert reprfuncargs.args[2] == ("z", repr({"c": "d"})) p = FormattedExcinfo(funcargs=True) repr_entry = p.repr_traceback_entry(entry) assert repr_entry.reprfuncargs.args == reprfuncargs.args tw = TWMock() repr_entry.toterminal(tw) assert tw.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}" def test_repr_tracebackentry_short(self, importasmod): mod = importasmod( """ def func1(): raise ValueError("hello") def entry(): func1() """ ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(style="short") reprtb = p.repr_traceback_entry(excinfo.traceback[-2]) lines = reprtb.lines basename = py.path.local(mod.__file__).basename assert lines[0] == " func1()" assert basename in str(reprtb.reprfileloc.path) assert reprtb.reprfileloc.lineno == 5 # test last entry p = FormattedExcinfo(style="short") reprtb = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = reprtb.lines assert lines[0] == ' raise ValueError("hello")' assert lines[1] == "E ValueError: hello" assert basename in str(reprtb.reprfileloc.path) assert reprtb.reprfileloc.lineno == 3 def test_repr_tracebackentry_no(self, importasmod): mod = importasmod( """ def func1(): raise ValueError("hello") def entry(): func1() """ ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(style="no") p.repr_traceback_entry(excinfo.traceback[-2]) p = FormattedExcinfo(style="no") reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo) lines = reprentry.lines assert lines[0] == "E ValueError: hello" assert not lines[1:] def test_repr_traceback_tbfilter(self, importasmod): mod = importasmod( """ def f(x): raise ValueError(x) def entry(): f(0) """ ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo(tbfilter=True) reprtb = p.repr_traceback(excinfo) assert len(reprtb.reprentries) == 2 p = FormattedExcinfo(tbfilter=False) reprtb = p.repr_traceback(excinfo) assert len(reprtb.reprentries) == 3 def test_traceback_short_no_source(self, importasmod, monkeypatch): mod = importasmod( """ def func1(): raise ValueError("hello") def entry(): func1() """ ) excinfo = pytest.raises(ValueError, mod.entry) from _pytest._code.code import Code monkeypatch.setattr(Code, "path", "bogus") excinfo.traceback[0].frame.code.path = "bogus" p = FormattedExcinfo(style="short") reprtb = p.repr_traceback_entry(excinfo.traceback[-2]) lines = reprtb.lines last_p = FormattedExcinfo(style="short") last_reprtb = last_p.repr_traceback_entry(excinfo.traceback[-1], excinfo) last_lines = last_reprtb.lines monkeypatch.undo() assert lines[0] == " func1()" assert last_lines[0] == ' raise ValueError("hello")' assert last_lines[1] == "E ValueError: hello" def test_repr_traceback_and_excinfo(self, importasmod): mod = importasmod( """ def f(x): raise ValueError(x) def entry(): f(0) """ ) excinfo = pytest.raises(ValueError, mod.entry) for style in ("long", "short"): p = FormattedExcinfo(style=style) reprtb = p.repr_traceback(excinfo) assert len(reprtb.reprentries) == 2 assert reprtb.style == style assert not reprtb.extraline repr = p.repr_excinfo(excinfo) assert repr.reprtraceback assert len(repr.reprtraceback.reprentries) == len(reprtb.reprentries) if sys.version_info[0] >= 3: assert repr.chain[0][0] assert len(repr.chain[0][0].reprentries) == len(reprtb.reprentries) assert repr.reprcrash.path.endswith("mod.py") assert repr.reprcrash.message == "ValueError: 0" def test_repr_traceback_with_invalid_cwd(self, importasmod, monkeypatch): mod = importasmod( """ def f(x): raise ValueError(x) def entry(): f(0) """ ) excinfo = pytest.raises(ValueError, mod.entry) p = FormattedExcinfo() def raiseos(): raise OSError(2) monkeypatch.setattr(os, "getcwd", raiseos) assert p._makepath(__file__) == __file__ p.repr_traceback(excinfo) def test_repr_excinfo_addouterr(self, importasmod): mod = importasmod( """ def entry(): raise ValueError() """ ) excinfo = pytest.raises(ValueError, mod.entry) repr = excinfo.getrepr() repr.addsection("title", "content") twmock = TWMock() repr.toterminal(twmock) assert twmock.lines[-1] == "content" assert twmock.lines[-2] == ("-", "title") def test_repr_excinfo_reprcrash(self, importasmod): mod = importasmod( """ def entry(): raise ValueError() """ ) excinfo = pytest.raises(ValueError, mod.entry) repr = excinfo.getrepr() assert repr.reprcrash.path.endswith("mod.py") assert repr.reprcrash.lineno == 3 assert repr.reprcrash.message == "ValueError" assert str(repr.reprcrash).endswith("mod.py:3: ValueError") def test_repr_traceback_recursion(self, importasmod): mod = importasmod( """ def rec2(x): return rec1(x+1) def rec1(x): return rec2(x-1) def entry(): rec1(42) """ ) excinfo = pytest.raises(RuntimeError, mod.entry) for style in ("short", "long", "no"): p = FormattedExcinfo(style="short") reprtb = p.repr_traceback(excinfo) assert reprtb.extraline == "!!! Recursion detected (same locals & position)" assert str(reprtb) def test_reprexcinfo_getrepr(self, importasmod): mod = importasmod( """ def f(x): raise ValueError(x) def entry(): f(0) """ ) excinfo = pytest.raises(ValueError, mod.entry) for style in ("short", "long", "no"): for showlocals in (True, False): repr = excinfo.getrepr(style=style, showlocals=showlocals) if sys.version_info[0] < 3: assert isinstance(repr, ReprExceptionInfo) assert repr.reprtraceback.style == style if sys.version_info[0] >= 3: assert isinstance(repr, ExceptionChainRepr) for repr in repr.chain: assert repr[0].style == style def test_reprexcinfo_unicode(self): from _pytest._code.code import TerminalRepr class MyRepr(TerminalRepr): def toterminal(self, tw): tw.line(u"я") x = six.text_type(MyRepr()) assert x == u"я" def test_toterminal_long(self, importasmod): mod = importasmod( """ def g(x): raise ValueError(x) def f(): g(3) """ ) excinfo = pytest.raises(ValueError, mod.f) excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() tw = TWMock() repr.toterminal(tw) assert tw.lines[0] == "" tw.lines.pop(0) assert tw.lines[0] == " def f():" assert tw.lines[1] == "> g(3)" assert tw.lines[2] == "" line = tw.get_write_msg(3) assert line.endswith("mod.py") assert tw.lines[4] == (":5: ") assert tw.lines[5] == ("_ ", None) assert tw.lines[6] == "" assert tw.lines[7] == " def g(x):" assert tw.lines[8] == "> raise ValueError(x)" assert tw.lines[9] == "E ValueError: 3" assert tw.lines[10] == "" line = tw.get_write_msg(11) assert line.endswith("mod.py") assert tw.lines[12] == ":3: ValueError" def test_toterminal_long_missing_source(self, importasmod, tmpdir): mod = importasmod( """ def g(x): raise ValueError(x) def f(): g(3) """ ) excinfo = pytest.raises(ValueError, mod.f) tmpdir.join("mod.py").remove() excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() tw = TWMock() repr.toterminal(tw) assert tw.lines[0] == "" tw.lines.pop(0) assert tw.lines[0] == "> ???" assert tw.lines[1] == "" line = tw.get_write_msg(2) assert line.endswith("mod.py") assert tw.lines[3] == ":5: " assert tw.lines[4] == ("_ ", None) assert tw.lines[5] == "" assert tw.lines[6] == "> ???" assert tw.lines[7] == "E ValueError: 3" assert tw.lines[8] == "" line = tw.get_write_msg(9) assert line.endswith("mod.py") assert tw.lines[10] == ":3: ValueError" def test_toterminal_long_incomplete_source(self, importasmod, tmpdir): mod = importasmod( """ def g(x): raise ValueError(x) def f(): g(3) """ ) excinfo = pytest.raises(ValueError, mod.f) tmpdir.join("mod.py").write("asdf") excinfo.traceback = excinfo.traceback.filter() repr = excinfo.getrepr() tw = TWMock() repr.toterminal(tw) assert tw.lines[0] == "" tw.lines.pop(0) assert tw.lines[0] == "> ???" assert tw.lines[1] == "" line = tw.get_write_msg(2) assert line.endswith("mod.py") assert tw.lines[3] == ":5: " assert tw.lines[4] == ("_ ", None) assert tw.lines[5] == "" assert tw.lines[6] == "> ???" assert tw.lines[7] == "E ValueError: 3" assert tw.lines[8] == "" line = tw.get_write_msg(9) assert line.endswith("mod.py") assert tw.lines[10] == ":3: ValueError" def test_toterminal_long_filenames(self, importasmod): mod = importasmod( """ def f(): raise ValueError() """ ) excinfo = pytest.raises(ValueError, mod.f) tw = TWMock() path = py.path.local(mod.__file__) old = path.dirpath().chdir() try: repr = excinfo.getrepr(abspath=False) repr.toterminal(tw) x = py.path.local().bestrelpath(path) if len(x) < len(str(path)): msg = tw.get_write_msg(-2) assert msg == "mod.py" assert tw.lines[-1] == ":3: ValueError" repr = excinfo.getrepr(abspath=True) repr.toterminal(tw) msg = tw.get_write_msg(-2) assert msg == path line = tw.lines[-1] assert line == ":3: ValueError" finally: old.chdir() @pytest.mark.parametrize( "reproptions", [ { "style": style, "showlocals": showlocals, "funcargs": funcargs, "tbfilter": tbfilter, } for style in ("long", "short", "no") for showlocals in (True, False) for tbfilter in (True, False) for funcargs in (True, False) ], ) def test_format_excinfo(self, importasmod, reproptions): mod = importasmod( """ def g(x): raise ValueError(x) def f(): g(3) """ ) excinfo = pytest.raises(ValueError, mod.f) tw = py.io.TerminalWriter(stringio=True) repr = excinfo.getrepr(**reproptions) repr.toterminal(tw) assert tw.stringio.getvalue() def test_traceback_repr_style(self, importasmod): mod = importasmod( """ def f(): g() def g(): h() def h(): i() def i(): raise ValueError() """ ) excinfo = pytest.raises(ValueError, mod.f) excinfo.traceback = excinfo.traceback.filter() excinfo.traceback[1].set_repr_style("short") excinfo.traceback[2].set_repr_style("short") r = excinfo.getrepr(style="long") tw = TWMock() r.toterminal(tw) for line in tw.lines: print(line) assert tw.lines[0] == "" assert tw.lines[1] == " def f():" assert tw.lines[2] == "> g()" assert tw.lines[3] == "" msg = tw.get_write_msg(4) assert msg.endswith("mod.py") assert tw.lines[5] == ":3: " assert tw.lines[6] == ("_ ", None) tw.get_write_msg(7) assert tw.lines[8].endswith("in g") assert tw.lines[9] == " h()" tw.get_write_msg(10) assert tw.lines[11].endswith("in h") assert tw.lines[12] == " i()" assert tw.lines[13] == ("_ ", None) assert tw.lines[14] == "" assert tw.lines[15] == " def i():" assert tw.lines[16] == "> raise ValueError()" assert tw.lines[17] == "E ValueError" assert tw.lines[18] == "" msg = tw.get_write_msg(19) msg.endswith("mod.py") assert tw.lines[20] == ":9: ValueError" @pytest.mark.skipif("sys.version_info[0] < 3") def test_exc_chain_repr(self, importasmod): mod = importasmod( """ class Err(Exception): pass def f(): try: g() except Exception as e: raise Err() from e finally: h() def g(): raise ValueError() def h(): raise AttributeError() """ ) excinfo = pytest.raises(AttributeError, mod.f) r = excinfo.getrepr(style="long") tw = TWMock() r.toterminal(tw) for line in tw.lines: print(line) assert tw.lines[0] == "" assert tw.lines[1] == " def f():" assert tw.lines[2] == " try:" assert tw.lines[3] == "> g()" assert tw.lines[4] == "" line = tw.get_write_msg(5) assert line.endswith("mod.py") assert tw.lines[6] == ":6: " assert tw.lines[7] == ("_ ", None) assert tw.lines[8] == "" assert tw.lines[9] == " def g():" assert tw.lines[10] == "> raise ValueError()" assert tw.lines[11] == "E ValueError" assert tw.lines[12] == "" line = tw.get_write_msg(13) assert line.endswith("mod.py") assert tw.lines[14] == ":12: ValueError" assert tw.lines[15] == "" assert ( tw.lines[16] == "The above exception was the direct cause of the following exception:" ) assert tw.lines[17] == "" assert tw.lines[18] == " def f():" assert tw.lines[19] == " try:" assert tw.lines[20] == " g()" assert tw.lines[21] == " except Exception as e:" assert tw.lines[22] == "> raise Err() from e" assert tw.lines[23] == "E test_exc_chain_repr0.mod.Err" assert tw.lines[24] == "" line = tw.get_write_msg(25) assert line.endswith("mod.py") assert tw.lines[26] == ":8: Err" assert tw.lines[27] == "" assert ( tw.lines[28] == "During handling of the above exception, another exception occurred:" ) assert tw.lines[29] == "" assert tw.lines[30] == " def f():" assert tw.lines[31] == " try:" assert tw.lines[32] == " g()" assert tw.lines[33] == " except Exception as e:" assert tw.lines[34] == " raise Err() from e" assert tw.lines[35] == " finally:" assert tw.lines[36] == "> h()" assert tw.lines[37] == "" line = tw.get_write_msg(38) assert line.endswith("mod.py") assert tw.lines[39] == ":10: " assert tw.lines[40] == ("_ ", None) assert tw.lines[41] == "" assert tw.lines[42] == " def h():" assert tw.lines[43] == "> raise AttributeError()" assert tw.lines[44] == "E AttributeError" assert tw.lines[45] == "" line = tw.get_write_msg(46) assert line.endswith("mod.py") assert tw.lines[47] == ":15: AttributeError" @pytest.mark.skipif("sys.version_info[0] < 3") @pytest.mark.parametrize("mode", ["from_none", "explicit_suppress"]) def test_exc_repr_chain_suppression(self, importasmod, mode): """Check that exc repr does not show chained exceptions in Python 3. - When the exception is raised with "from None" - Explicitly suppressed with "chain=False" to ExceptionInfo.getrepr(). """ raise_suffix = " from None" if mode == "from_none" else "" mod = importasmod( """ def f(): try: g() except Exception: raise AttributeError(){raise_suffix} def g(): raise ValueError() """.format( raise_suffix=raise_suffix ) ) excinfo = pytest.raises(AttributeError, mod.f) r = excinfo.getrepr(style="long", chain=mode != "explicit_suppress") tw = TWMock() r.toterminal(tw) for line in tw.lines: print(line) assert tw.lines[0] == "" assert tw.lines[1] == " def f():" assert tw.lines[2] == " try:" assert tw.lines[3] == " g()" assert tw.lines[4] == " except Exception:" assert tw.lines[5] == "> raise AttributeError(){}".format( raise_suffix ) assert tw.lines[6] == "E AttributeError" assert tw.lines[7] == "" line = tw.get_write_msg(8) assert line.endswith("mod.py") assert tw.lines[9] == ":6: AttributeError" assert len(tw.lines) == 10 @pytest.mark.skipif("sys.version_info[0] < 3") @pytest.mark.parametrize( "reason, description", [ ( "cause", "The above exception was the direct cause of the following exception:", ), ( "context", "During handling of the above exception, another exception occurred:", ), ], ) def test_exc_chain_repr_without_traceback(self, importasmod, reason, description): """ Handle representation of exception chains where one of the exceptions doesn't have a real traceback, such as those raised in a subprocess submitted by the multiprocessing module (#1984). """ from _pytest.pytester import LineMatcher exc_handling_code = " from e" if reason == "cause" else "" mod = importasmod( """ def f(): try: g() except Exception as e: raise RuntimeError('runtime problem'){exc_handling_code} def g(): raise ValueError('invalid value') """.format( exc_handling_code=exc_handling_code ) ) with pytest.raises(RuntimeError) as excinfo: mod.f() # emulate the issue described in #1984 attr = "__%s__" % reason getattr(excinfo.value, attr).__traceback__ = None r = excinfo.getrepr() tw = py.io.TerminalWriter(stringio=True) tw.hasmarkup = False r.toterminal(tw) matcher = LineMatcher(tw.stringio.getvalue().splitlines()) matcher.fnmatch_lines( [ "ValueError: invalid value", description, "* except Exception as e:", "> * raise RuntimeError('runtime problem')" + exc_handling_code, "E *RuntimeError: runtime problem", ] ) @pytest.mark.skipif("sys.version_info[0] < 3") def test_exc_chain_repr_cycle(self, importasmod): mod = importasmod( """ class Err(Exception): pass def fail(): return 0 / 0 def reraise(): try: fail() except ZeroDivisionError as e: raise Err() from e def unreraise(): try: reraise() except Err as e: raise e.__cause__ """ ) excinfo = pytest.raises(ZeroDivisionError, mod.unreraise) r = excinfo.getrepr(style="short") tw = TWMock() r.toterminal(tw) out = "\n".join(line for line in tw.lines if isinstance(line, str)) expected_out = textwrap.dedent( """\ :13: in unreraise reraise() :10: in reraise raise Err() from e E test_exc_chain_repr_cycle0.mod.Err During handling of the above exception, another exception occurred: :15: in unreraise raise e.__cause__ :8: in reraise fail() :5: in fail return 0 / 0 E ZeroDivisionError: division by zero""" ) assert out == expected_out @pytest.mark.parametrize("style", ["short", "long"]) @pytest.mark.parametrize("encoding", [None, "utf8", "utf16"]) def test_repr_traceback_with_unicode(style, encoding): msg = u"☹" if encoding is not None: msg = msg.encode(encoding) try: raise RuntimeError(msg) except RuntimeError: e_info = ExceptionInfo.from_current() formatter = FormattedExcinfo(style=style) repr_traceback = formatter.repr_traceback(e_info) assert repr_traceback is not None def test_cwd_deleted(testdir): testdir.makepyfile( """ def test(tmpdir): tmpdir.chdir() tmpdir.remove() assert False """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 failed in *"]) assert "INTERNALERROR" not in result.stdout.str() + result.stderr.str() @pytest.mark.usefixtures("limited_recursion_depth") def test_exception_repr_extraction_error_on_recursion(): """ Ensure we can properly detect a recursion error even if some locals raise error on comparison (#2459). """ from _pytest.pytester import LineMatcher class numpy_like(object): def __eq__(self, other): if type(other) is numpy_like: raise ValueError( "The truth value of an array " "with more than one element is ambiguous." ) def a(x): return b(numpy_like()) def b(x): return a(numpy_like()) with pytest.raises(RuntimeError) as excinfo: a(numpy_like()) matcher = LineMatcher(str(excinfo.getrepr()).splitlines()) matcher.fnmatch_lines( [ "!!! Recursion error detected, but an error occurred locating the origin of recursion.", "*The following exception happened*", "*ValueError: The truth value of an array*", ] ) @pytest.mark.usefixtures("limited_recursion_depth") def test_no_recursion_index_on_recursion_error(): """ Ensure that we don't break in case we can't find the recursion index during a recursion error (#2486). """ class RecursionDepthError(object): def __getattr__(self, attr): return getattr(self, "_" + attr) with pytest.raises(RuntimeError) as excinfo: RecursionDepthError().trigger assert "maximum recursion" in str(excinfo.getrepr()) pytest-4.6.9/testing/io/0000775000372000037200000000000013604173421016011 5ustar travistravis00000000000000pytest-4.6.9/testing/io/test_saferepr.py0000664000372000037200000000301313604173400021223 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from _pytest._io.saferepr import saferepr def test_simple_repr(): assert saferepr(1) == "1" assert saferepr(None) == "None" def test_maxsize(): s = saferepr("x" * 50, maxsize=25) assert len(s) == 25 expected = repr("x" * 10 + "..." + "x" * 10) assert s == expected def test_maxsize_error_on_instance(): class A: def __repr__(): raise ValueError("...") s = saferepr(("*" * 50, A()), maxsize=25) assert len(s) == 25 assert s[0] == "(" and s[-1] == ")" def test_exceptions(): class BrokenRepr: def __init__(self, ex): self.ex = ex def __repr__(self): raise self.ex class BrokenReprException(Exception): __str__ = None __repr__ = None assert "Exception" in saferepr(BrokenRepr(Exception("broken"))) s = saferepr(BrokenReprException("really broken")) assert "TypeError" in s assert "TypeError" in saferepr(BrokenRepr("string")) s2 = saferepr(BrokenRepr(BrokenReprException("omg even worse"))) assert "NameError" not in s2 assert "unknown" in s2 def test_big_repr(): from _pytest._io.saferepr import SafeRepr assert len(saferepr(range(1000))) <= len("[" + SafeRepr().maxlist * "1000" + "]") def test_repr_on_newstyle(): class Function(object): def __repr__(self): return "<%s>" % (self.name) assert saferepr(Function()) def test_unicode(): val = u"£€" reprval = u"'£€'" assert saferepr(val) == reprval pytest-4.6.9/testing/examples/0000775000372000037200000000000013604173421017220 5ustar travistravis00000000000000pytest-4.6.9/testing/examples/test_issue519.py0000664000372000037200000000017613604173400022221 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_510(testdir): testdir.copy_example("issue_519.py") testdir.runpytest("issue_519.py") pytest-4.6.9/testing/test_recwarn.py0000664000372000037200000003230313604173400020452 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import re import warnings import pytest from _pytest.recwarn import WarningsRecorder from _pytest.warning_types import PytestDeprecationWarning def test_recwarn_stacklevel(recwarn): warnings.warn("hello") warn = recwarn.pop() assert warn.filename == __file__ def test_recwarn_functional(testdir): testdir.makepyfile( """ import warnings def test_method(recwarn): warnings.warn("hello") warn = recwarn.pop() assert isinstance(warn.message, UserWarning) """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) class TestWarningsRecorderChecker(object): def test_recording(self): rec = WarningsRecorder() with rec: assert not rec.list warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" values = rec.list rec.clear() assert len(rec.list) == 0 assert values is rec.list pytest.raises(AssertionError, rec.pop) def test_warn_stacklevel(self): """#4243""" rec = WarningsRecorder() with rec: warnings.warn("test", DeprecationWarning, 2) def test_typechecking(self): from _pytest.recwarn import WarningsChecker with pytest.raises(TypeError): WarningsChecker(5) with pytest.raises(TypeError): WarningsChecker(("hi", RuntimeWarning)) with pytest.raises(TypeError): WarningsChecker([DeprecationWarning, RuntimeWarning]) def test_invalid_enter_exit(self): # wrap this test in WarningsRecorder to ensure warning state gets reset with WarningsRecorder(): with pytest.raises(RuntimeError): rec = WarningsRecorder() rec.__exit__(None, None, None) # can't exit before entering with pytest.raises(RuntimeError): rec = WarningsRecorder() with rec: with rec: pass # can't enter twice class TestDeprecatedCall(object): """test pytest.deprecated_call()""" def dep(self, i, j=None): if i == 0: warnings.warn("is deprecated", DeprecationWarning, stacklevel=1) return 42 def dep_explicit(self, i): if i == 0: warnings.warn_explicit( "dep_explicit", category=DeprecationWarning, filename="hello", lineno=3 ) def test_deprecated_call_raises(self): with pytest.raises(pytest.fail.Exception, match="No warnings of type"): pytest.deprecated_call(self.dep, 3, 5) def test_deprecated_call(self): pytest.deprecated_call(self.dep, 0, 5) def test_deprecated_call_ret(self): ret = pytest.deprecated_call(self.dep, 0) assert ret == 42 def test_deprecated_call_preserves(self): onceregistry = warnings.onceregistry.copy() filters = warnings.filters[:] warn = warnings.warn warn_explicit = warnings.warn_explicit self.test_deprecated_call_raises() self.test_deprecated_call() assert onceregistry == warnings.onceregistry assert filters == warnings.filters assert warn is warnings.warn assert warn_explicit is warnings.warn_explicit def test_deprecated_explicit_call_raises(self): with pytest.raises(pytest.fail.Exception): pytest.deprecated_call(self.dep_explicit, 3) def test_deprecated_explicit_call(self): pytest.deprecated_call(self.dep_explicit, 0) pytest.deprecated_call(self.dep_explicit, 0) @pytest.mark.parametrize("mode", ["context_manager", "call"]) def test_deprecated_call_no_warning(self, mode): """Ensure deprecated_call() raises the expected failure when its block/function does not raise a deprecation warning. """ def f(): pass msg = "No warnings of type (.*DeprecationWarning.*, .*PendingDeprecationWarning.*)" with pytest.raises(pytest.fail.Exception, match=msg): if mode == "call": pytest.deprecated_call(f) else: with pytest.deprecated_call(): f() @pytest.mark.parametrize( "warning_type", [PendingDeprecationWarning, DeprecationWarning] ) @pytest.mark.parametrize("mode", ["context_manager", "call"]) @pytest.mark.parametrize("call_f_first", [True, False]) @pytest.mark.filterwarnings("ignore") def test_deprecated_call_modes(self, warning_type, mode, call_f_first): """Ensure deprecated_call() captures a deprecation warning as expected inside its block/function. """ def f(): warnings.warn(warning_type("hi")) return 10 # ensure deprecated_call() can capture the warning even if it has already been triggered if call_f_first: assert f() == 10 if mode == "call": assert pytest.deprecated_call(f) == 10 else: with pytest.deprecated_call(): assert f() == 10 @pytest.mark.parametrize("mode", ["context_manager", "call"]) def test_deprecated_call_exception_is_raised(self, mode): """If the block of the code being tested by deprecated_call() raises an exception, it must raise the exception undisturbed. """ def f(): raise ValueError("some exception") with pytest.raises(ValueError, match="some exception"): if mode == "call": pytest.deprecated_call(f) else: with pytest.deprecated_call(): f() def test_deprecated_call_specificity(self): other_warnings = [ Warning, UserWarning, SyntaxWarning, RuntimeWarning, FutureWarning, ImportWarning, UnicodeWarning, ] for warning in other_warnings: def f(): warnings.warn(warning("hi")) with pytest.raises(pytest.fail.Exception): pytest.deprecated_call(f) with pytest.raises(pytest.fail.Exception): with pytest.deprecated_call(): f() def test_deprecated_call_supports_match(self): with pytest.deprecated_call(match=r"must be \d+$"): warnings.warn("value must be 42", DeprecationWarning) with pytest.raises(pytest.fail.Exception): with pytest.deprecated_call(match=r"must be \d+$"): warnings.warn("this is not here", DeprecationWarning) class TestWarns(object): def test_strings(self): # different messages, b/c Python suppresses multiple identical warnings source1 = "warnings.warn('w1', RuntimeWarning)" source2 = "warnings.warn('w2', RuntimeWarning)" source3 = "warnings.warn('w3', RuntimeWarning)" with pytest.warns(PytestDeprecationWarning) as warninfo: # yo dawg pytest.warns(RuntimeWarning, source1) pytest.raises( pytest.fail.Exception, lambda: pytest.warns(UserWarning, source2) ) pytest.warns(RuntimeWarning, source3) assert len(warninfo) == 3 for w in warninfo: assert w.filename == __file__ (msg,) = w.message.args assert msg.startswith("warns(..., 'code(as_a_string)') is deprecated") def test_function(self): pytest.warns( SyntaxWarning, lambda msg: warnings.warn(msg, SyntaxWarning), "syntax" ) def test_warning_tuple(self): pytest.warns( (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w1", RuntimeWarning) ) pytest.warns( (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w2", SyntaxWarning) ) pytest.raises( pytest.fail.Exception, lambda: pytest.warns( (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w3", UserWarning), ), ) def test_as_contextmanager(self): with pytest.warns(RuntimeWarning): warnings.warn("runtime", RuntimeWarning) with pytest.warns(UserWarning): warnings.warn("user", UserWarning) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(RuntimeWarning): warnings.warn("user", UserWarning) excinfo.match( r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. " r"The list of emitted warnings is: \[UserWarning\('user',?\)\]." ) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(UserWarning): warnings.warn("runtime", RuntimeWarning) excinfo.match( r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]." ) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(UserWarning): pass excinfo.match( r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. " r"The list of emitted warnings is: \[\]." ) warning_classes = (UserWarning, FutureWarning) with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns(warning_classes) as warninfo: warnings.warn("runtime", RuntimeWarning) warnings.warn("import", ImportWarning) message_template = ( "DID NOT WARN. No warnings of type {0} was emitted. " "The list of emitted warnings is: {1}." ) excinfo.match( re.escape( message_template.format( warning_classes, [each.message for each in warninfo] ) ) ) def test_record(self): with pytest.warns(UserWarning) as record: warnings.warn("user", UserWarning) assert len(record) == 1 assert str(record[0].message) == "user" def test_record_only(self): with pytest.warns(None) as record: warnings.warn("user", UserWarning) warnings.warn("runtime", RuntimeWarning) assert len(record) == 2 assert str(record[0].message) == "user" assert str(record[1].message) == "runtime" def test_record_by_subclass(self): with pytest.warns(Warning) as record: warnings.warn("user", UserWarning) warnings.warn("runtime", RuntimeWarning) assert len(record) == 2 assert str(record[0].message) == "user" assert str(record[1].message) == "runtime" class MyUserWarning(UserWarning): pass class MyRuntimeWarning(RuntimeWarning): pass with pytest.warns((UserWarning, RuntimeWarning)) as record: warnings.warn("user", MyUserWarning) warnings.warn("runtime", MyRuntimeWarning) assert len(record) == 2 assert str(record[0].message) == "user" assert str(record[1].message) == "runtime" def test_double_test(self, testdir): """If a test is run again, the warning should still be raised""" testdir.makepyfile( """ import pytest import warnings @pytest.mark.parametrize('run', [1, 2]) def test(run): with pytest.warns(RuntimeWarning): warnings.warn("runtime", RuntimeWarning) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 passed in*"]) def test_match_regex(self): with pytest.warns(UserWarning, match=r"must be \d+$"): warnings.warn("value must be 42", UserWarning) with pytest.raises(pytest.fail.Exception): with pytest.warns(UserWarning, match=r"must be \d+$"): warnings.warn("this is not here", UserWarning) with pytest.raises(pytest.fail.Exception): with pytest.warns(FutureWarning, match=r"must be \d+$"): warnings.warn("value must be 42", UserWarning) def test_one_from_multiple_warns(self): with pytest.warns(UserWarning, match=r"aaa"): warnings.warn("cccccccccc", UserWarning) warnings.warn("bbbbbbbbbb", UserWarning) warnings.warn("aaaaaaaaaa", UserWarning) def test_none_of_multiple_warns(self): with pytest.raises(pytest.fail.Exception): with pytest.warns(UserWarning, match=r"aaa"): warnings.warn("bbbbbbbbbb", UserWarning) warnings.warn("cccccccccc", UserWarning) @pytest.mark.filterwarnings("ignore") def test_can_capture_previously_warned(self): def f(): warnings.warn(UserWarning("ohai")) return 10 assert f() == 10 assert pytest.warns(UserWarning, f) == 10 assert pytest.warns(UserWarning, f) == 10 pytest-4.6.9/testing/test_pastebin.py0000664000372000037200000001000513604173400020611 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import pytest class TestPasteCapture(object): @pytest.fixture def pastebinlist(self, monkeypatch, request): pastebinlist = [] plugin = request.config.pluginmanager.getplugin("pastebin") monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append) return pastebinlist def test_failed(self, testdir, pastebinlist): testpath = testdir.makepyfile( """ import pytest def test_pass(): pass def test_fail(): assert 0 def test_skip(): pytest.skip("") """ ) reprec = testdir.inline_run(testpath, "--paste=failed") assert len(pastebinlist) == 1 s = pastebinlist[0] assert s.find("def test_fail") != -1 assert reprec.countoutcomes() == [1, 1, 1] def test_all(self, testdir, pastebinlist): from _pytest.pytester import LineMatcher testpath = testdir.makepyfile( """ import pytest def test_pass(): pass def test_fail(): assert 0 def test_skip(): pytest.skip("") """ ) reprec = testdir.inline_run(testpath, "--pastebin=all", "-v") assert reprec.countoutcomes() == [1, 1, 1] assert len(pastebinlist) == 1 contents = pastebinlist[0].decode("utf-8") matcher = LineMatcher(contents.splitlines()) matcher.fnmatch_lines( [ "*test_pass PASSED*", "*test_fail FAILED*", "*test_skip SKIPPED*", "*== 1 failed, 1 passed, 1 skipped in *", ] ) def test_non_ascii_paste_text(self, testdir): """Make sure that text which contains non-ascii characters is pasted correctly. See #1219. """ testdir.makepyfile( test_unicode=""" # -*- coding: utf-8 -*- def test(): assert '☺' == 1 """ ) result = testdir.runpytest("--pastebin=all") if sys.version_info[0] >= 3: expected_msg = "*assert '☺' == 1*" else: expected_msg = "*assert '\\xe2\\x98\\xba' == 1*" result.stdout.fnmatch_lines( [ expected_msg, "*== 1 failed in *", "*Sending information to Paste Service*", ] ) class TestPaste(object): @pytest.fixture def pastebin(self, request): return request.config.pluginmanager.getplugin("pastebin") @pytest.fixture def mocked_urlopen(self, monkeypatch): """ monkeypatch the actual urlopen calls done by the internal plugin function that connects to bpaste service. """ calls = [] def mocked(url, data): calls.append((url, data)) class DummyFile(object): def read(self): # part of html of a normal response return b'View raw.' return DummyFile() if sys.version_info < (3, 0): import urllib monkeypatch.setattr(urllib, "urlopen", mocked) else: import urllib.request monkeypatch.setattr(urllib.request, "urlopen", mocked) return calls def test_create_new_paste(self, pastebin, mocked_urlopen): result = pastebin.create_new_paste(b"full-paste-contents") assert result == "https://bpaste.net/show/3c0c6750bd" assert len(mocked_urlopen) == 1 url, data = mocked_urlopen[0] assert type(data) is bytes lexer = "text" assert url == "https://bpaste.net" assert "lexer=%s" % lexer in data.decode() assert "code=full-paste-contents" in data.decode() assert "expiry=1week" in data.decode() pytest-4.6.9/testing/test_pathlib.py0000664000372000037200000000510013604173400020427 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import py import pytest from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import get_lock_path from _pytest.pathlib import maybe_delete_a_numbered_dir from _pytest.pathlib import Path class TestPort: """Test that our port of py.common.FNMatcher (fnmatch_ex) produces the same results as the original py.path.local.fnmatch method. """ @pytest.fixture(params=["pathlib", "py.path"]) def match(self, request): if request.param == "py.path": def match_(pattern, path): return py.path.local(path).fnmatch(pattern) else: assert request.param == "pathlib" def match_(pattern, path): return fnmatch_ex(pattern, path) return match_ if sys.platform == "win32": drv1 = "c:" drv2 = "d:" else: drv1 = "/c" drv2 = "/d" @pytest.mark.parametrize( "pattern, path", [ ("*.py", "foo.py"), ("*.py", "bar/foo.py"), ("test_*.py", "foo/test_foo.py"), ("tests/*.py", "tests/foo.py"), (drv1 + "/*.py", drv1 + "/foo.py"), (drv1 + "/foo/*.py", drv1 + "/foo/foo.py"), ("tests/**/test*.py", "tests/foo/test_foo.py"), ("tests/**/doc/test*.py", "tests/foo/bar/doc/test_foo.py"), ("tests/**/doc/**/test*.py", "tests/foo/doc/bar/test_foo.py"), ], ) def test_matching(self, match, pattern, path): assert match(pattern, path) @pytest.mark.parametrize( "pattern, path", [ ("*.py", "foo.pyc"), ("*.py", "foo/foo.pyc"), ("tests/*.py", "foo/foo.py"), (drv1 + "/*.py", drv2 + "/foo.py"), (drv1 + "/foo/*.py", drv2 + "/foo/foo.py"), ("tests/**/test*.py", "tests/foo.py"), ("tests/**/test*.py", "foo/test_foo.py"), ("tests/**/doc/test*.py", "tests/foo/bar/doc/foo.py"), ("tests/**/doc/test*.py", "tests/foo/bar/test_foo.py"), ], ) def test_not_matching(self, match, pattern, path): assert not match(pattern, path) def test_access_denied_during_cleanup(tmp_path, monkeypatch): """Ensure that deleting a numbered dir does not fail because of OSErrors (#4262).""" path = tmp_path / "temp-1" path.mkdir() def renamed_failed(*args): raise OSError("access denied") monkeypatch.setattr(Path, "rename", renamed_failed) lock_path = get_lock_path(path) maybe_delete_a_numbered_dir(path) assert not lock_path.is_file() pytest-4.6.9/testing/freeze/0000775000372000037200000000000013604173421016662 5ustar travistravis00000000000000pytest-4.6.9/testing/freeze/runtests_script.py0000664000372000037200000000033113604173400022501 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ This is the script that is actually frozen into an executable: simply executes py.test main(). """ if __name__ == "__main__": import sys import pytest sys.exit(pytest.main()) pytest-4.6.9/testing/freeze/tox_run.py0000664000372000037200000000060013604173400020723 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Called by tox.ini: uses the generated executable to run the tests in ./tests/ directory. """ if __name__ == "__main__": import os import sys executable = os.path.join(os.getcwd(), "dist", "runtests_script", "runtests_script") if sys.platform.startswith("win"): executable += ".exe" sys.exit(os.system("%s tests" % executable)) pytest-4.6.9/testing/freeze/.gitignore0000664000372000037200000000002413604173400020643 0ustar travistravis00000000000000build/ dist/ *.spec pytest-4.6.9/testing/freeze/tests/0000775000372000037200000000000013604173421020024 5ustar travistravis00000000000000pytest-4.6.9/testing/freeze/tests/test_doctest.txt0000664000372000037200000000005113604173400023262 0ustar travistravis00000000000000 Testing doctest:: >>> 1 + 1 2 pytest-4.6.9/testing/freeze/tests/test_trivial.py0000664000372000037200000000020213604173400023076 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_upper(): assert "foo".upper() == "FOO" def test_lower(): assert "FOO".lower() == "foo" pytest-4.6.9/testing/freeze/create_executable.py0000664000372000037200000000070613604173400022700 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Generates an executable with pytest runner embedded using PyInstaller. """ if __name__ == "__main__": import pytest import subprocess hidden = [] for x in pytest.freeze_includes(): hidden.extend(["--hidden-import", x]) hidden.extend(["--hidden-import", "distutils"]) args = ["pyinstaller", "--noconfirm"] + hidden + ["runtests_script.py"] subprocess.check_call(" ".join(args), shell=True) pytest-4.6.9/testing/test_cacheprovider.py0000664000372000037200000010352513604173400021634 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import shutil import sys import textwrap import py import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED pytest_plugins = ("pytester",) class TestNewAPI(object): def test_config_cache_makedir(self, testdir): testdir.makeini("[pytest]") config = testdir.parseconfigure() with pytest.raises(ValueError): config.cache.makedir("key/name") p = config.cache.makedir("name") assert p.check() def test_config_cache_dataerror(self, testdir): testdir.makeini("[pytest]") config = testdir.parseconfigure() cache = config.cache pytest.raises(TypeError, lambda: cache.set("key/name", cache)) config.cache.set("key/name", 0) config.cache._getvaluepath("key/name").write_bytes(b"123invalid") val = config.cache.get("key/name", -2) assert val == -2 @pytest.mark.filterwarnings("default") def test_cache_writefail_cachfile_silent(self, testdir): testdir.makeini("[pytest]") testdir.tmpdir.join(".pytest_cache").write("gone wrong") config = testdir.parseconfigure() cache = config.cache cache.set("test/broken", []) @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") @pytest.mark.filterwarnings( "ignore:could not create cache path:pytest.PytestWarning" ) def test_cache_writefail_permissions(self, testdir): testdir.makeini("[pytest]") testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) config = testdir.parseconfigure() cache = config.cache cache.set("test/broken", []) @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") @pytest.mark.filterwarnings("default") def test_cache_failure_warns(self, testdir): testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) testdir.makepyfile( """ def test_error(): raise Exception """ ) result = testdir.runpytest("-rw") assert result.ret == 1 # warnings from nodeids, lastfailed, and stepwise result.stdout.fnmatch_lines(["*could not create cache path*", "*3 warnings*"]) def test_config_cache(self, testdir): testdir.makeconftest( """ def pytest_configure(config): # see that we get cache information early on assert hasattr(config, "cache") """ ) testdir.makepyfile( """ def test_session(pytestconfig): assert hasattr(pytestconfig, "cache") """ ) result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_cachefuncarg(self, testdir): testdir.makepyfile( """ import pytest def test_cachefuncarg(cache): val = cache.get("some/thing", None) assert val is None cache.set("some/thing", [1]) pytest.raises(TypeError, lambda: cache.get("some/thing")) val = cache.get("some/thing", []) assert val == [1] """ ) result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_custom_rel_cache_dir(self, testdir): rel_cache_dir = os.path.join("custom_cache_dir", "subdir") testdir.makeini( """ [pytest] cache_dir = {cache_dir} """.format( cache_dir=rel_cache_dir ) ) testdir.makepyfile(test_errored="def test_error():\n assert False") testdir.runpytest() assert testdir.tmpdir.join(rel_cache_dir).isdir() def test_custom_abs_cache_dir(self, testdir, tmpdir_factory): tmp = str(tmpdir_factory.mktemp("tmp")) abs_cache_dir = os.path.join(tmp, "custom_cache_dir") testdir.makeini( """ [pytest] cache_dir = {cache_dir} """.format( cache_dir=abs_cache_dir ) ) testdir.makepyfile(test_errored="def test_error():\n assert False") testdir.runpytest() assert py.path.local(abs_cache_dir).isdir() def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch): monkeypatch.setenv("env_var", "custom_cache_dir") testdir.makeini( """ [pytest] cache_dir = {cache_dir} """.format( cache_dir="$env_var" ) ) testdir.makepyfile(test_errored="def test_error():\n assert False") testdir.runpytest() assert testdir.tmpdir.join("custom_cache_dir").isdir() @pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir"))) def test_cache_reportheader(env, testdir, monkeypatch): testdir.makepyfile("""def test_foo(): pass""") if env: monkeypatch.setenv(*env) expected = os.path.join(env[1], ".pytest_cache") else: monkeypatch.delenv("TOX_ENV_DIR", raising=False) expected = ".pytest_cache" result = testdir.runpytest("-v") result.stdout.fnmatch_lines(["cachedir: %s" % expected]) def test_cache_reportheader_external_abspath(testdir, tmpdir_factory): external_cache = tmpdir_factory.mktemp( "test_cache_reportheader_external_abspath_abs" ) testdir.makepyfile( """ def test_hello(): pass """ ) testdir.makeini( """ [pytest] cache_dir = {abscache} """.format( abscache=external_cache ) ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( ["cachedir: {abscache}".format(abscache=external_cache)] ) def test_cache_show(testdir): result = testdir.runpytest("--cache-show") assert result.ret == 0 result.stdout.fnmatch_lines(["*cache is empty*"]) testdir.makeconftest( """ def pytest_configure(config): config.cache.set("my/name", [1,2,3]) config.cache.set("my/hello", "world") config.cache.set("other/some", {1:2}) dp = config.cache.makedir("mydb") dp.ensure("hello") dp.ensure("world") """ ) result = testdir.runpytest() assert result.ret == 5 # no tests executed result = testdir.runpytest("--cache-show") result.stdout.fnmatch_lines( [ "*cachedir:*", "*- cache values for '[*]' -*", "cache/nodeids contains:", "my/name contains:", " [1, 2, 3]", "other/some contains:", " {*'1': 2}", "*- cache directories for '[*]' -*", "*mydb/hello*length 0*", "*mydb/world*length 0*", ] ) assert result.ret == 0 result = testdir.runpytest("--cache-show", "*/hello") result.stdout.fnmatch_lines( [ "*cachedir:*", "*- cache values for '[*]/hello' -*", "my/hello contains:", " *'world'", "*- cache directories for '[*]/hello' -*", "d/mydb/hello*length 0*", ] ) stdout = result.stdout.str() assert "other/some" not in stdout assert "d/mydb/world" not in stdout assert result.ret == 0 class TestLastFailed(object): def test_lastfailed_usecase(self, testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") p = testdir.makepyfile( """ def test_1(): assert 0 def test_2(): assert 0 def test_3(): assert 1 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 failed*"]) p.write( textwrap.dedent( """\ def test_1(): assert 1 def test_2(): assert 1 def test_3(): assert 0 """ ) ) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines(["*2 passed*1 desel*"]) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 3 items", "run-last-failure: no previously failed tests, not deselecting items.", "*1 failed*2 passed*", ] ) result = testdir.runpytest("--lf", "--cache-clear") result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) # Run this again to make sure clear-cache is robust if os.path.isdir(".pytest_cache"): shutil.rmtree(".pytest_cache") result = testdir.runpytest("--lf", "--cache-clear") result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) def test_failedfirst_order(self, testdir): testdir.tmpdir.join("test_a.py").write( textwrap.dedent( """\ def test_always_passes(): assert 1 """ ) ) testdir.tmpdir.join("test_b.py").write( textwrap.dedent( """\ def test_always_fails(): assert 0 """ ) ) result = testdir.runpytest() # Test order will be collection order; alphabetical result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) result = testdir.runpytest("--ff") # Test order will be failing tests firs result.stdout.fnmatch_lines(["test_b.py*", "test_a.py*"]) def test_lastfailed_failedfirst_order(self, testdir): testdir.makepyfile( **{ "test_a.py": """\ def test_always_passes(): assert 1 """, "test_b.py": """\ def test_always_fails(): assert 0 """, } ) result = testdir.runpytest() # Test order will be collection order; alphabetical result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) result = testdir.runpytest("--lf", "--ff") # Test order will be failing tests firs result.stdout.fnmatch_lines(["test_b.py*"]) assert "test_a.py" not in result.stdout.str() def test_lastfailed_difference_invocations(self, testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") testdir.makepyfile( test_a="""\ def test_a1(): assert 0 def test_a2(): assert 1 """, test_b="""\ def test_b1(): assert 0 """, ) p = testdir.tmpdir.join("test_a.py") p2 = testdir.tmpdir.join("test_b.py") result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 failed*"]) result = testdir.runpytest("--lf", p2) result.stdout.fnmatch_lines(["*1 failed*"]) p2.write( textwrap.dedent( """\ def test_b1(): assert 1 """ ) ) result = testdir.runpytest("--lf", p2) result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest("--lf", p) result.stdout.fnmatch_lines(["*1 failed*1 desel*"]) def test_lastfailed_usecase_splice(self, testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") testdir.makepyfile( """\ def test_1(): assert 0 """ ) p2 = testdir.tmpdir.join("test_something.py") p2.write( textwrap.dedent( """\ def test_2(): assert 0 """ ) ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 failed*"]) result = testdir.runpytest("--lf", p2) result.stdout.fnmatch_lines(["*1 failed*"]) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines(["*2 failed*"]) def test_lastfailed_xpass(self, testdir): testdir.inline_runsource( """ import pytest @pytest.mark.xfail def test_hello(): assert 1 """ ) config = testdir.parseconfigure() lastfailed = config.cache.get("cache/lastfailed", -1) assert lastfailed == -1 def test_non_serializable_parametrize(self, testdir): """Test that failed parametrized tests with unmarshable parameters don't break pytest-cache. """ testdir.makepyfile( r""" import pytest @pytest.mark.parametrize('val', [ b'\xac\x10\x02G', ]) def test_fail(val): assert False """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 failed in*"]) def test_terminal_report_lastfailed(self, testdir): test_a = testdir.makepyfile( test_a=""" def test_a1(): pass def test_a2(): pass """ ) test_b = testdir.makepyfile( test_b=""" def test_b1(): assert 0 def test_b2(): assert 0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"]) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 2 items", "run-last-failure: rerun previous 2 failures (skipped 1 file)", "*2 failed in*", ] ) result = testdir.runpytest(test_a, "--lf") result.stdout.fnmatch_lines( [ "collected 2 items", "run-last-failure: 2 known failures not in selected tests", "*2 passed in*", ] ) result = testdir.runpytest(test_b, "--lf") result.stdout.fnmatch_lines( [ "collected 2 items", "run-last-failure: rerun previous 2 failures", "*2 failed in*", ] ) result = testdir.runpytest("test_b.py::test_b1", "--lf") result.stdout.fnmatch_lines( [ "collected 1 item", "run-last-failure: rerun previous 1 failure", "*1 failed in*", ] ) def test_terminal_report_failedfirst(self, testdir): testdir.makepyfile( test_a=""" def test_a1(): assert 0 def test_a2(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 2 items", "*1 failed, 1 passed in*"]) result = testdir.runpytest("--ff") result.stdout.fnmatch_lines( [ "collected 2 items", "run-last-failure: rerun previous 1 failure first", "*1 failed, 1 passed in*", ] ) def test_lastfailed_collectfailure(self, testdir, monkeypatch): testdir.makepyfile( test_maybe=""" import os env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): assert '0' == env['FAILTEST'] """ ) def rlf(fail_import, fail_run): monkeypatch.setenv("FAILIMPORT", str(fail_import)) monkeypatch.setenv("FAILTEST", str(fail_run)) testdir.runpytest("-q") config = testdir.parseconfigure() lastfailed = config.cache.get("cache/lastfailed", -1) return lastfailed lastfailed = rlf(fail_import=0, fail_run=0) assert lastfailed == -1 lastfailed = rlf(fail_import=1, fail_run=0) assert list(lastfailed) == ["test_maybe.py"] lastfailed = rlf(fail_import=0, fail_run=1) assert list(lastfailed) == ["test_maybe.py::test_hello"] def test_lastfailed_failure_subset(self, testdir, monkeypatch): testdir.makepyfile( test_maybe=""" import os env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): assert '0' == env['FAILTEST'] """ ) testdir.makepyfile( test_maybe2=""" import os env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): assert '0' == env['FAILTEST'] def test_pass(): pass """ ) def rlf(fail_import, fail_run, args=()): monkeypatch.setenv("FAILIMPORT", str(fail_import)) monkeypatch.setenv("FAILTEST", str(fail_run)) result = testdir.runpytest("-q", "--lf", *args) config = testdir.parseconfigure() lastfailed = config.cache.get("cache/lastfailed", -1) return result, lastfailed result, lastfailed = rlf(fail_import=0, fail_run=0) assert lastfailed == -1 result.stdout.fnmatch_lines(["*3 passed*"]) result, lastfailed = rlf(fail_import=1, fail_run=0) assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"] result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) assert list(lastfailed) == ["test_maybe.py"] # edge case of test selection - even if we remember failures # from other tests we still need to run all tests if no test # matches the failures result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) assert list(lastfailed) == ["test_maybe.py"] result.stdout.fnmatch_lines(["*2 passed*"]) def test_lastfailed_creates_cache_when_needed(self, testdir): # Issue #1342 testdir.makepyfile(test_empty="") testdir.runpytest("-q", "--lf") assert not os.path.exists(".pytest_cache/v/cache/lastfailed") testdir.makepyfile(test_successful="def test_success():\n assert True") testdir.runpytest("-q", "--lf") assert not os.path.exists(".pytest_cache/v/cache/lastfailed") testdir.makepyfile(test_errored="def test_error():\n assert False") testdir.runpytest("-q", "--lf") assert os.path.exists(".pytest_cache/v/cache/lastfailed") def test_xfail_not_considered_failure(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail def test(): assert 0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 xfailed*"]) assert self.get_cached_last_failed(testdir) == [] def test_xfail_strict_considered_failure(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail(strict=True) def test(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 failed*"]) assert self.get_cached_last_failed(testdir) == [ "test_xfail_strict_considered_failure.py::test" ] @pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"]) def test_failed_changed_to_xfail_or_skip(self, testdir, mark): testdir.makepyfile( """ import pytest def test(): assert 0 """ ) result = testdir.runpytest() assert self.get_cached_last_failed(testdir) == [ "test_failed_changed_to_xfail_or_skip.py::test" ] assert result.ret == 1 testdir.makepyfile( """ import pytest @pytest.{mark} def test(): assert 0 """.format( mark=mark ) ) result = testdir.runpytest() assert result.ret == 0 assert self.get_cached_last_failed(testdir) == [] assert result.ret == 0 @pytest.mark.parametrize("quiet", [True, False]) @pytest.mark.parametrize("opt", ["--ff", "--lf"]) def test_lf_and_ff_prints_no_needless_message(self, quiet, opt, testdir): # Issue 3853 testdir.makepyfile("def test(): assert 0") args = [opt] if quiet: args.append("-q") result = testdir.runpytest(*args) assert "run all" not in result.stdout.str() result = testdir.runpytest(*args) if quiet: assert "run all" not in result.stdout.str() else: assert "rerun previous" in result.stdout.str() def get_cached_last_failed(self, testdir): config = testdir.parseconfigure() return sorted(config.cache.get("cache/lastfailed", {})) def test_cache_cumulative(self, testdir): """ Test workflow where user fixes errors gradually file by file using --lf. """ # 1. initial run test_bar = testdir.makepyfile( test_bar=""" def test_bar_1(): pass def test_bar_2(): assert 0 """ ) test_foo = testdir.makepyfile( test_foo=""" def test_foo_3(): pass def test_foo_4(): assert 0 """ ) testdir.runpytest() assert self.get_cached_last_failed(testdir) == [ "test_bar.py::test_bar_2", "test_foo.py::test_foo_4", ] # 2. fix test_bar_2, run only test_bar.py testdir.makepyfile( test_bar=""" def test_bar_1(): pass def test_bar_2(): pass """ ) result = testdir.runpytest(test_bar) result.stdout.fnmatch_lines(["*2 passed*"]) # ensure cache does not forget that test_foo_4 failed once before assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"] result = testdir.runpytest("--last-failed") result.stdout.fnmatch_lines(["*1 failed, 1 deselected*"]) assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"] # 3. fix test_foo_4, run only test_foo.py test_foo = testdir.makepyfile( test_foo=""" def test_foo_3(): pass def test_foo_4(): pass """ ) result = testdir.runpytest(test_foo, "--last-failed") result.stdout.fnmatch_lines(["*1 passed, 1 deselected*"]) assert self.get_cached_last_failed(testdir) == [] result = testdir.runpytest("--last-failed") result.stdout.fnmatch_lines(["*4 passed*"]) assert self.get_cached_last_failed(testdir) == [] def test_lastfailed_no_failures_behavior_all_passed(self, testdir): testdir.makepyfile( """ def test_1(): assert True def test_2(): assert True """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 passed*"]) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines(["*2 passed*"]) result = testdir.runpytest("--lf", "--lfnf", "all") result.stdout.fnmatch_lines(["*2 passed*"]) result = testdir.runpytest("--lf", "--lfnf", "none") result.stdout.fnmatch_lines( [ "collected 2 items / 2 deselected", "run-last-failure: no previously failed tests, deselecting all items.", "* 2 deselected in *", ] ) assert result.ret == EXIT_NOTESTSCOLLECTED def test_lastfailed_no_failures_behavior_empty_cache(self, testdir): testdir.makepyfile( """ def test_1(): assert True def test_2(): assert False """ ) result = testdir.runpytest("--lf", "--cache-clear") result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "all") result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "none") result.stdout.fnmatch_lines(["*2 desel*"]) def test_lastfailed_skip_collection(self, testdir): """ Test --lf behavior regarding skipping collection of files that are not marked as failed in the cache (#5172). """ testdir.makepyfile( **{ "pkg1/test_1.py": """ import pytest @pytest.mark.parametrize('i', range(3)) def test_1(i): pass """, "pkg2/test_2.py": """ import pytest @pytest.mark.parametrize('i', range(5)) def test_1(i): assert i not in (1, 3) """, } ) # first run: collects 8 items (test_1: 3, test_2: 5) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 8 items", "*2 failed*6 passed*"]) # second run: collects only 5 items from test_2, because all tests from test_1 have passed result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 5 items / 3 deselected / 2 selected", "run-last-failure: rerun previous 2 failures (skipped 1 file)", "*2 failed*3 deselected*", ] ) # add another file and check if message is correct when skipping more than 1 file testdir.makepyfile( **{ "pkg1/test_3.py": """ def test_3(): pass """ } ) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 5 items / 3 deselected / 2 selected", "run-last-failure: rerun previous 2 failures (skipped 2 files)", "*2 failed*3 deselected*", ] ) def test_lastfailed_with_known_failures_not_being_selected(self, testdir): testdir.makepyfile( **{ "pkg1/test_1.py": """def test_1(): assert 0""", "pkg1/test_2.py": """def test_2(): pass""", } ) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"]) py.path.local("pkg1/test_1.py").remove() result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 1 item", "run-last-failure: 1 known failures not in selected tests", "* 1 passed in *", ] ) # Recreate file with known failure. testdir.makepyfile(**{"pkg1/test_1.py": """def test_1(): assert 0"""}) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 1 item", "run-last-failure: rerun previous 1 failure (skipped 1 file)", "* 1 failed in *", ] ) # Remove/rename test. testdir.makepyfile(**{"pkg1/test_1.py": """def test_renamed(): assert 0"""}) result = testdir.runpytest("--lf") result.stdout.fnmatch_lines( [ "collected 1 item", "run-last-failure: 1 known failures not in selected tests (skipped 1 file)", "* 1 failed in *", ] ) class TestNewFirst(object): def test_newfirst_usecase(self, testdir): testdir.makepyfile( **{ "test_1/test_1.py": """ def test_1(): assert 1 def test_2(): assert 1 def test_3(): assert 1 """, "test_2/test_2.py": """ def test_1(): assert 1 def test_2(): assert 1 def test_3(): assert 1 """, } ) testdir.tmpdir.join("test_1/test_1.py").setmtime(1) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( [ "*test_1/test_1.py::test_1 PASSED*", "*test_1/test_1.py::test_2 PASSED*", "*test_1/test_1.py::test_3 PASSED*", "*test_2/test_2.py::test_1 PASSED*", "*test_2/test_2.py::test_2 PASSED*", "*test_2/test_2.py::test_3 PASSED*", ] ) result = testdir.runpytest("-v", "--nf") result.stdout.fnmatch_lines( [ "*test_2/test_2.py::test_1 PASSED*", "*test_2/test_2.py::test_2 PASSED*", "*test_2/test_2.py::test_3 PASSED*", "*test_1/test_1.py::test_1 PASSED*", "*test_1/test_1.py::test_2 PASSED*", "*test_1/test_1.py::test_3 PASSED*", ] ) testdir.tmpdir.join("test_1/test_1.py").write( "def test_1(): assert 1\n" "def test_2(): assert 1\n" "def test_3(): assert 1\n" "def test_4(): assert 1\n" ) testdir.tmpdir.join("test_1/test_1.py").setmtime(1) result = testdir.runpytest("-v", "--nf") result.stdout.fnmatch_lines( [ "*test_1/test_1.py::test_4 PASSED*", "*test_2/test_2.py::test_1 PASSED*", "*test_2/test_2.py::test_2 PASSED*", "*test_2/test_2.py::test_3 PASSED*", "*test_1/test_1.py::test_1 PASSED*", "*test_1/test_1.py::test_2 PASSED*", "*test_1/test_1.py::test_3 PASSED*", ] ) def test_newfirst_parametrize(self, testdir): testdir.makepyfile( **{ "test_1/test_1.py": """ import pytest @pytest.mark.parametrize('num', [1, 2]) def test_1(num): assert num """, "test_2/test_2.py": """ import pytest @pytest.mark.parametrize('num', [1, 2]) def test_1(num): assert num """, } ) testdir.tmpdir.join("test_1/test_1.py").setmtime(1) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( [ "*test_1/test_1.py::test_1[1*", "*test_1/test_1.py::test_1[2*", "*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*", ] ) result = testdir.runpytest("-v", "--nf") result.stdout.fnmatch_lines( [ "*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*", "*test_1/test_1.py::test_1[1*", "*test_1/test_1.py::test_1[2*", ] ) testdir.tmpdir.join("test_1/test_1.py").write( "import pytest\n" "@pytest.mark.parametrize('num', [1, 2, 3])\n" "def test_1(num): assert num\n" ) testdir.tmpdir.join("test_1/test_1.py").setmtime(1) result = testdir.runpytest("-v", "--nf") result.stdout.fnmatch_lines( [ "*test_1/test_1.py::test_1[3*", "*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*", "*test_1/test_1.py::test_1[1*", "*test_1/test_1.py::test_1[2*", ] ) class TestReadme(object): def check_readme(self, testdir): config = testdir.parseconfigure() readme = config.cache._cachedir.joinpath("README.md") return readme.is_file() def test_readme_passed(self, testdir): testdir.makepyfile( """ def test_always_passes(): assert 1 """ ) testdir.runpytest() assert self.check_readme(testdir) is True def test_readme_failed(self, testdir): testdir.makepyfile( """ def test_always_fails(): assert 0 """ ) testdir.runpytest() assert self.check_readme(testdir) is True def test_gitignore(testdir): """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" from _pytest.cacheprovider import Cache config = testdir.parseconfig() cache = Cache.for_config(config) cache.set("foo", "bar") msg = "# Created by pytest automatically.\n*" gitignore_path = cache._cachedir.joinpath(".gitignore") assert gitignore_path.read_text(encoding="UTF-8") == msg # Does not overwrite existing/custom one. gitignore_path.write_text(u"custom") cache.set("something", "else") assert gitignore_path.read_text(encoding="UTF-8") == "custom" def test_does_not_create_boilerplate_in_existing_dirs(testdir): from _pytest.cacheprovider import Cache testdir.makeini( """ [pytest] cache_dir = . """ ) config = testdir.parseconfig() cache = Cache.for_config(config) cache.set("foo", "bar") assert os.path.isdir("v") # cache contents assert not os.path.exists(".gitignore") assert not os.path.exists("README.md") def test_cachedir_tag(testdir): """Ensure we automatically create CACHEDIR.TAG file in the pytest_cache directory (#4278).""" from _pytest.cacheprovider import Cache from _pytest.cacheprovider import CACHEDIR_TAG_CONTENT config = testdir.parseconfig() cache = Cache.for_config(config) cache.set("foo", "bar") cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG") assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT pytest-4.6.9/testing/test_terminal.py0000664000372000037200000015237613604173400020641 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ terminal reporting of the full testing process. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import os import sys import textwrap import pluggy import py import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.reports import BaseReport from _pytest.terminal import _folded_skips from _pytest.terminal import _get_line_with_reprcrash_message from _pytest.terminal import _plugin_nameversions from _pytest.terminal import build_summary_stats_line from _pytest.terminal import getreportopt from _pytest.terminal import TerminalReporter DistInfo = collections.namedtuple("DistInfo", ["project_name", "version"]) class Option(object): def __init__(self, verbosity=0, fulltrace=False): self.verbosity = verbosity self.fulltrace = fulltrace @property def args(self): values = [] values.append("--verbosity=%d" % self.verbosity) if self.fulltrace: values.append("--fulltrace") return values @pytest.fixture( params=[ Option(verbosity=0), Option(verbosity=1), Option(verbosity=-1), Option(fulltrace=True), ], ids=["default", "verbose", "quiet", "fulltrace"], ) def option(request): return request.param @pytest.mark.parametrize( "input,expected", [ ([DistInfo(project_name="test", version=1)], ["test-1"]), ([DistInfo(project_name="pytest-test", version=1)], ["test-1"]), ( [ DistInfo(project_name="test", version=1), DistInfo(project_name="test", version=1), ], ["test-1"], ), ], ids=["normal", "prefix-strip", "deduplicate"], ) def test_plugin_nameversion(input, expected): pluginlist = [(None, x) for x in input] result = _plugin_nameversions(pluginlist) assert result == expected class TestTerminal(object): def test_pass_skip_fail(self, testdir, option): testdir.makepyfile( """ import pytest def test_ok(): pass def test_skip(): pytest.skip("xx") def test_func(): assert 0 """ ) result = testdir.runpytest(*option.args) if option.verbosity > 0: result.stdout.fnmatch_lines( [ "*test_pass_skip_fail.py::test_ok PASS*", "*test_pass_skip_fail.py::test_skip SKIP*", "*test_pass_skip_fail.py::test_func FAIL*", ] ) elif option.verbosity == 0: result.stdout.fnmatch_lines(["*test_pass_skip_fail.py .sF*"]) else: result.stdout.fnmatch_lines([".sF*"]) result.stdout.fnmatch_lines( [" def test_func():", "> assert 0", "E assert 0"] ) def test_internalerror(self, testdir, linecomp): modcol = testdir.getmodulecol("def test_one(): pass") rep = TerminalReporter(modcol.config, file=linecomp.stringio) with pytest.raises(ValueError) as excinfo: raise ValueError("hello") rep.pytest_internalerror(excinfo.getrepr()) linecomp.assert_contains_lines(["INTERNALERROR> *ValueError*hello*"]) def test_writeline(self, testdir, linecomp): modcol = testdir.getmodulecol("def test_one(): pass") rep = TerminalReporter(modcol.config, file=linecomp.stringio) rep.write_fspath_result(modcol.nodeid, ".") rep.write_line("hello world") lines = linecomp.stringio.getvalue().split("\n") assert not lines[0] assert lines[1].endswith(modcol.name + " .") assert lines[2] == "hello world" def test_show_runtest_logstart(self, testdir, linecomp): item = testdir.getitem("def test_func(): pass") tr = TerminalReporter(item.config, file=linecomp.stringio) item.config.pluginmanager.register(tr) location = item.reportinfo() tr.config.hook.pytest_runtest_logstart( nodeid=item.nodeid, location=location, fspath=str(item.fspath) ) linecomp.assert_contains_lines(["*test_show_runtest_logstart.py*"]) def test_runtest_location_shown_before_test_starts(self, testdir): testdir.makepyfile( """ def test_1(): import time time.sleep(20) """ ) child = testdir.spawn_pytest("") child.expect(".*test_runtest_location.*py") child.sendeof() child.kill(15) def test_report_collect_after_half_a_second(self, testdir): """Test for "collecting" being updated after 0.5s""" testdir.makepyfile( **{ "test1.py": """ import _pytest.terminal _pytest.terminal.REPORT_COLLECTING_RESOLUTION = 0 def test_1(): pass """, "test2.py": "def test_2(): pass", } ) child = testdir.spawn_pytest("-v test1.py test2.py") child.expect(r"collecting \.\.\.") child.expect(r"collecting 1 item") child.expect(r"collecting 2 items") child.expect(r"collected 2 items") rest = child.read().decode("utf8") assert "2 passed in" in rest def test_itemreport_subclasses_show_subclassed_file(self, testdir): testdir.makepyfile( test_p1=""" class BaseTests(object): def test_p1(self): pass class TestClass(BaseTests): pass """ ) p2 = testdir.makepyfile( test_p2=""" from test_p1 import BaseTests class TestMore(BaseTests): pass """ ) result = testdir.runpytest(p2) result.stdout.fnmatch_lines(["*test_p2.py .*", "*1 passed*"]) result = testdir.runpytest("-vv", p2) result.stdout.fnmatch_lines( ["*test_p2.py::TestMore::test_p1* <- *test_p1.py*PASSED*"] ) def test_itemreport_directclasses_not_shown_as_subclasses(self, testdir): a = testdir.mkpydir("a123") a.join("test_hello123.py").write( textwrap.dedent( """\ class TestClass(object): def test_method(self): pass """ ) ) result = testdir.runpytest("-vv") assert result.ret == 0 result.stdout.fnmatch_lines(["*a123/test_hello123.py*PASS*"]) assert " <- " not in result.stdout.str() def test_keyboard_interrupt(self, testdir, option): testdir.makepyfile( """ def test_foobar(): assert 0 def test_spamegg(): import py; pytest.skip('skip me please!') def test_interrupt_me(): raise KeyboardInterrupt # simulating the user """ ) result = testdir.runpytest(*option.args, no_reraise_ctrlc=True) result.stdout.fnmatch_lines( [ " def test_foobar():", "> assert 0", "E assert 0", "*_keyboard_interrupt.py:6: KeyboardInterrupt*", ] ) if option.fulltrace: result.stdout.fnmatch_lines( ["*raise KeyboardInterrupt # simulating the user*"] ) else: result.stdout.fnmatch_lines( ["(to show a full traceback on KeyboardInterrupt use --fulltrace)"] ) result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) def test_keyboard_in_sessionstart(self, testdir): testdir.makeconftest( """ def pytest_sessionstart(): raise KeyboardInterrupt """ ) testdir.makepyfile( """ def test_foobar(): pass """ ) result = testdir.runpytest(no_reraise_ctrlc=True) assert result.ret == 2 result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) def test_collect_single_item(self, testdir): """Use singular 'item' when reporting a single test item""" testdir.makepyfile( """ def test_foobar(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 1 item"]) def test_rewrite(self, testdir, monkeypatch): config = testdir.parseconfig() f = py.io.TextIO() monkeypatch.setattr(f, "isatty", lambda *args: True) tr = TerminalReporter(config, f) tr._tw.fullwidth = 10 tr.write("hello") tr.rewrite("hey", erase=True) assert f.getvalue() == "hello" + "\r" + "hey" + (6 * " ") class TestCollectonly(object): def test_collectonly_basic(self, testdir): testdir.makepyfile( """ def test_func(): pass """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines( ["", " "] ) def test_collectonly_skipped_module(self, testdir): testdir.makepyfile( """ import pytest pytest.skip("hello") """ ) result = testdir.runpytest("--collect-only", "-rs") result.stdout.fnmatch_lines(["*ERROR collecting*"]) def test_collectonly_display_test_description(self, testdir): testdir.makepyfile( """ def test_with_description(): \""" This test has a description. \""" assert True """ ) result = testdir.runpytest("--collect-only", "--verbose") result.stdout.fnmatch_lines([" This test has a description."]) def test_collectonly_failed_module(self, testdir): testdir.makepyfile("""raise ValueError(0)""") result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*raise ValueError*", "*1 error*"]) def test_collectonly_fatal(self, testdir): testdir.makeconftest( """ def pytest_collectstart(collector): assert 0, "urgs" """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*INTERNAL*args*"]) assert result.ret == 3 def test_collectonly_simple(self, testdir): p = testdir.makepyfile( """ def test_func1(): pass class TestClass(object): def test_method(self): pass """ ) result = testdir.runpytest("--collect-only", p) # assert stderr.startswith("inserting into sys.path") assert result.ret == 0 result.stdout.fnmatch_lines( [ "*", "* ", "* ", "* ", ] ) def test_collectonly_error(self, testdir): p = testdir.makepyfile("import Errlkjqweqwe") result = testdir.runpytest("--collect-only", p) assert result.ret == 2 result.stdout.fnmatch_lines( textwrap.dedent( """\ *ERROR* *ImportError* *No module named *Errlk* *1 error* """ ).strip() ) def test_collectonly_missing_path(self, testdir): """this checks issue 115, failure in parseargs will cause session not to have the items attribute """ result = testdir.runpytest("--collect-only", "uhm_missing_path") assert result.ret == 4 result.stderr.fnmatch_lines(["*ERROR: file not found*"]) def test_collectonly_quiet(self, testdir): testdir.makepyfile("def test_foo(): pass") result = testdir.runpytest("--collect-only", "-q") result.stdout.fnmatch_lines(["*test_foo*"]) def test_collectonly_more_quiet(self, testdir): testdir.makepyfile(test_fun="def test_foo(): pass") result = testdir.runpytest("--collect-only", "-qq") result.stdout.fnmatch_lines(["*test_fun.py: 1*"]) class TestFixtureReporting(object): def test_setup_fixture_error(self, testdir): testdir.makepyfile( """ def setup_function(function): print("setup func") assert 0 def test_nada(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ERROR at setup of test_nada*", "*setup_function(function):*", "*setup func*", "*assert 0*", "*1 error*", ] ) assert result.ret != 0 def test_teardown_fixture_error(self, testdir): testdir.makepyfile( """ def test_nada(): pass def teardown_function(function): print("teardown func") assert 0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ERROR at teardown*", "*teardown_function(function):*", "*assert 0*", "*Captured stdout*", "*teardown func*", "*1 passed*1 error*", ] ) def test_teardown_fixture_error_and_test_failure(self, testdir): testdir.makepyfile( """ def test_fail(): assert 0, "failingfunc" def teardown_function(function): print("teardown func") assert False """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ERROR at teardown of test_fail*", "*teardown_function(function):*", "*assert False*", "*Captured stdout*", "*teardown func*", "*test_fail*", "*def test_fail():", "*failingfunc*", "*1 failed*1 error*", ] ) def test_setup_teardown_output_and_test_failure(self, testdir): """ Test for issue #442 """ testdir.makepyfile( """ def setup_function(function): print("setup func") def test_fail(): assert 0, "failingfunc" def teardown_function(function): print("teardown func") """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_fail*", "*def test_fail():", "*failingfunc*", "*Captured stdout setup*", "*setup func*", "*Captured stdout teardown*", "*teardown func*", "*1 failed*", ] ) class TestTerminalFunctional(object): def test_deselected(self, testdir): testpath = testdir.makepyfile( """ def test_one(): pass def test_two(): pass def test_three(): pass """ ) result = testdir.runpytest("-k", "test_two:", testpath) result.stdout.fnmatch_lines( ["collected 3 items / 1 deselected / 2 selected", "*test_deselected.py ..*"] ) assert result.ret == 0 def test_deselected_with_hookwrapper(self, testdir): testpath = testdir.makeconftest( """ import pytest @pytest.hookimpl(hookwrapper=True) def pytest_collection_modifyitems(config, items): yield deselected = items.pop() config.hook.pytest_deselected(items=[deselected]) """ ) testpath = testdir.makepyfile( """ def test_one(): pass def test_two(): pass def test_three(): pass """ ) result = testdir.runpytest(testpath) result.stdout.fnmatch_lines( [ "collected 3 items / 1 deselected / 2 selected", "*= 2 passed, 1 deselected in*", ] ) assert result.ret == 0 def test_show_deselected_items_using_markexpr_before_test_execution(self, testdir): testdir.makepyfile( test_show_deselected=""" import pytest @pytest.mark.foo def test_foobar(): pass @pytest.mark.bar def test_bar(): pass def test_pass(): pass """ ) result = testdir.runpytest("-m", "not foo") result.stdout.fnmatch_lines( [ "collected 3 items / 1 deselected / 2 selected", "*test_show_deselected.py ..*", "*= 2 passed, 1 deselected in * =*", ] ) assert "= 1 deselected =" not in result.stdout.str() assert result.ret == 0 def test_no_skip_summary_if_failure(self, testdir): testdir.makepyfile( """ import pytest def test_ok(): pass def test_fail(): assert 0 def test_skip(): pytest.skip("dontshow") """ ) result = testdir.runpytest() assert result.stdout.str().find("skip test summary") == -1 assert result.ret == 1 def test_passes(self, testdir): p1 = testdir.makepyfile( """ def test_passes(): pass class TestClass(object): def test_method(self): pass """ ) old = p1.dirpath().chdir() try: result = testdir.runpytest() finally: old.chdir() result.stdout.fnmatch_lines(["test_passes.py ..*", "* 2 pass*"]) assert result.ret == 0 def test_header_trailer_info(self, testdir, request): testdir.makepyfile( """ def test_passes(): pass """ ) result = testdir.runpytest() verinfo = ".".join(map(str, sys.version_info[:3])) result.stdout.fnmatch_lines( [ "*===== test session starts ====*", "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" % ( sys.platform, verinfo, pytest.__version__, py.__version__, pluggy.__version__, ), "*test_header_trailer_info.py .*", "=* 1 passed*in *.[0-9][0-9] seconds *=", ] ) if request.config.pluginmanager.list_plugin_distinfo(): result.stdout.fnmatch_lines(["plugins: *"]) def test_header(self, testdir, request): testdir.tmpdir.join("tests").ensure_dir() testdir.tmpdir.join("gui").ensure_dir() # no ini file result = testdir.runpytest() result.stdout.fnmatch_lines(["rootdir: *test_header0"]) # with inifile testdir.makeini("""[pytest]""") result = testdir.runpytest() result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile: tox.ini"]) # with testpaths option, and not passing anything in the command-line testdir.makeini( """ [pytest] testpaths = tests gui """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui"] ) # with testpaths option, passing directory in command-line: do not show testpaths then result = testdir.runpytest("tests") result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile: tox.ini"]) def test_showlocals(self, testdir): p1 = testdir.makepyfile( """ def test_showlocals(): x = 3 y = "x" * 5000 assert 0 """ ) result = testdir.runpytest(p1, "-l") result.stdout.fnmatch_lines( [ # "_ _ * Locals *", "x* = 3", "y* = 'xxxxxx*", ] ) @pytest.fixture def verbose_testfile(self, testdir): return testdir.makepyfile( """ import pytest def test_fail(): raise ValueError() def test_pass(): pass class TestClass(object): def test_skip(self): pytest.skip("hello") def test_gen(): def check(x): assert x == 1 yield check, 0 """ ) def test_verbose_reporting(self, verbose_testfile, testdir, pytestconfig): result = testdir.runpytest( verbose_testfile, "-v", "-Walways::pytest.PytestWarning" ) result.stdout.fnmatch_lines( [ "*test_verbose_reporting.py::test_fail *FAIL*", "*test_verbose_reporting.py::test_pass *PASS*", "*test_verbose_reporting.py::TestClass::test_skip *SKIP*", "*test_verbose_reporting.py::test_gen *XFAIL*", ] ) assert result.ret == 1 def test_verbose_reporting_xdist(self, verbose_testfile, testdir, pytestconfig): if not pytestconfig.pluginmanager.get_plugin("xdist"): pytest.skip("xdist plugin not installed") result = testdir.runpytest( verbose_testfile, "-v", "-n 1", "-Walways::pytest.PytestWarning" ) result.stdout.fnmatch_lines( ["*FAIL*test_verbose_reporting_xdist.py::test_fail*"] ) assert result.ret == 1 def test_quiet_reporting(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") result = testdir.runpytest(p1, "-q") s = result.stdout.str() assert "test session starts" not in s assert p1.basename not in s assert "===" not in s assert "passed" in s def test_more_quiet_reporting(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") result = testdir.runpytest(p1, "-qq") s = result.stdout.str() assert "test session starts" not in s assert p1.basename not in s assert "===" not in s assert "passed" not in s @pytest.mark.parametrize( "params", [(), ("--collect-only",)], ids=["no-params", "collect-only"] ) def test_report_collectionfinish_hook(self, testdir, params): testdir.makeconftest( """ def pytest_report_collectionfinish(config, startdir, items): return ['hello from hook: {0} items'.format(len(items))] """ ) testdir.makepyfile( """ import pytest @pytest.mark.parametrize('i', range(3)) def test(i): pass """ ) result = testdir.runpytest(*params) result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"]) def test_fail_extra_reporting(testdir, monkeypatch): monkeypatch.setenv("COLUMNS", "80") testdir.makepyfile("def test_this(): assert 0, 'this_failed' * 100") result = testdir.runpytest() assert "short test summary" not in result.stdout.str() result = testdir.runpytest("-rf") result.stdout.fnmatch_lines( [ "*test summary*", "FAILED test_fail_extra_reporting.py::test_this - AssertionError: this_failedt...", ] ) def test_fail_reporting_on_pass(testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest("-rf") assert "short test summary" not in result.stdout.str() def test_pass_extra_reporting(testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest() assert "short test summary" not in result.stdout.str() result = testdir.runpytest("-rp") result.stdout.fnmatch_lines(["*test summary*", "PASS*test_pass_extra_reporting*"]) def test_pass_reporting_on_fail(testdir): testdir.makepyfile("def test_this(): assert 0") result = testdir.runpytest("-rp") assert "short test summary" not in result.stdout.str() def test_pass_output_reporting(testdir): testdir.makepyfile( """ def test_pass_has_output(): print("Four score and seven years ago...") def test_pass_no_output(): pass """ ) result = testdir.runpytest() s = result.stdout.str() assert "test_pass_has_output" not in s assert "Four score and seven years ago..." not in s assert "test_pass_no_output" not in s result = testdir.runpytest("-rPp") result.stdout.fnmatch_lines( [ "*= PASSES =*", "*_ test_pass_has_output _*", "*- Captured stdout call -*", "Four score and seven years ago...", "*= short test summary info =*", "PASSED test_pass_output_reporting.py::test_pass_has_output", "PASSED test_pass_output_reporting.py::test_pass_no_output", "*= 2 passed in *", ] ) def test_color_yes(testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest("--color=yes") assert "test session starts" in result.stdout.str() assert "\x1b[1m" in result.stdout.str() def test_color_no(testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest("--color=no") assert "test session starts" in result.stdout.str() assert "\x1b[1m" not in result.stdout.str() @pytest.mark.parametrize("verbose", [True, False]) def test_color_yes_collection_on_non_atty(testdir, verbose): """skip collect progress report when working on non-terminals. #1397 """ testdir.makepyfile( """ import pytest @pytest.mark.parametrize('i', range(10)) def test_this(i): assert 1 """ ) args = ["--color=yes"] if verbose: args.append("-vv") result = testdir.runpytest(*args) assert "test session starts" in result.stdout.str() assert "\x1b[1m" in result.stdout.str() assert "collecting 10 items" not in result.stdout.str() if verbose: assert "collecting ..." in result.stdout.str() assert "collected 10 items" in result.stdout.str() def test_getreportopt(): class Config(object): class Option(object): reportchars = "" disable_warnings = True option = Option() config = Config() config.option.reportchars = "sf" assert getreportopt(config) == "sf" config.option.reportchars = "sfxw" assert getreportopt(config) == "sfx" # Now with --disable-warnings. config.option.disable_warnings = False config.option.reportchars = "a" assert getreportopt(config) == "sxXwEf" # NOTE: "w" included! config.option.reportchars = "sfx" assert getreportopt(config) == "sfxw" config.option.reportchars = "sfxw" assert getreportopt(config) == "sfxw" config.option.reportchars = "a" assert getreportopt(config) == "sxXwEf" # NOTE: "w" included! config.option.reportchars = "A" assert getreportopt(config) == "PpsxXwEf" def test_terminalreporter_reportopt_addopts(testdir): testdir.makeini("[pytest]\naddopts=-rs") testdir.makepyfile( """ import pytest @pytest.fixture def tr(request): tr = request.config.pluginmanager.getplugin("terminalreporter") return tr def test_opt(tr): assert tr.hasopt('skipped') assert not tr.hasopt('qwe') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_tbstyle_short(testdir): p = testdir.makepyfile( """ import pytest @pytest.fixture def arg(request): return 42 def test_opt(arg): x = 0 assert x """ ) result = testdir.runpytest("--tb=short") s = result.stdout.str() assert "arg = 42" not in s assert "x = 0" not in s result.stdout.fnmatch_lines(["*%s:8*" % p.basename, " assert x", "E assert*"]) result = testdir.runpytest() s = result.stdout.str() assert "x = 0" in s assert "assert x" in s def test_traceconfig(testdir, monkeypatch): result = testdir.runpytest("--traceconfig") result.stdout.fnmatch_lines(["*active plugins*"]) assert result.ret == EXIT_NOTESTSCOLLECTED class TestGenericReporting(object): """ this test class can be subclassed with a different option provider to run e.g. distributed tests. """ def test_collect_fail(self, testdir, option): testdir.makepyfile("import xyz\n") result = testdir.runpytest(*option.args) result.stdout.fnmatch_lines( ["ImportError while importing*", "*No module named *xyz*", "*1 error*"] ) def test_maxfailures(self, testdir, option): testdir.makepyfile( """ def test_1(): assert 0 def test_2(): assert 0 def test_3(): assert 0 """ ) result = testdir.runpytest("--maxfail=2", *option.args) result.stdout.fnmatch_lines( ["*def test_1():*", "*def test_2():*", "*2 failed*"] ) def test_tb_option(self, testdir, option): testdir.makepyfile( """ import pytest def g(): raise IndexError def test_func(): print(6*7) g() # --calling-- """ ) for tbopt in ["long", "short", "no"]: print("testing --tb=%s..." % tbopt) result = testdir.runpytest("--tb=%s" % tbopt) s = result.stdout.str() if tbopt == "long": assert "print(6*7)" in s else: assert "print(6*7)" not in s if tbopt != "no": assert "--calling--" in s assert "IndexError" in s else: assert "FAILURES" not in s assert "--calling--" not in s assert "IndexError" not in s def test_tb_crashline(self, testdir, option): p = testdir.makepyfile( """ import pytest def g(): raise IndexError def test_func1(): print(6*7) g() # --calling-- def test_func2(): assert 0, "hello" """ ) result = testdir.runpytest("--tb=line") bn = p.basename result.stdout.fnmatch_lines( ["*%s:3: IndexError*" % bn, "*%s:8: AssertionError: hello*" % bn] ) s = result.stdout.str() assert "def test_func2" not in s def test_pytest_report_header(self, testdir, option): testdir.makeconftest( """ def pytest_sessionstart(session): session.config._somevalue = 42 def pytest_report_header(config): return "hello: %s" % config._somevalue """ ) testdir.mkdir("a").join("conftest.py").write( """ def pytest_report_header(config, startdir): return ["line1", str(startdir)] """ ) result = testdir.runpytest("a") result.stdout.fnmatch_lines(["*hello: 42*", "line1", str(testdir.tmpdir)]) def test_show_capture(self, testdir): testdir.makepyfile( """ import sys import logging def test_one(): sys.stdout.write('!This is stdout!') sys.stderr.write('!This is stderr!') logging.warning('!This is a warning log msg!') assert False, 'Something failed' """ ) result = testdir.runpytest("--tb=short") result.stdout.fnmatch_lines( [ "!This is stdout!", "!This is stderr!", "*WARNING*!This is a warning log msg!", ] ) result = testdir.runpytest("--show-capture=all", "--tb=short") result.stdout.fnmatch_lines( [ "!This is stdout!", "!This is stderr!", "*WARNING*!This is a warning log msg!", ] ) stdout = testdir.runpytest("--show-capture=stdout", "--tb=short").stdout.str() assert "!This is stderr!" not in stdout assert "!This is stdout!" in stdout assert "!This is a warning log msg!" not in stdout stdout = testdir.runpytest("--show-capture=stderr", "--tb=short").stdout.str() assert "!This is stdout!" not in stdout assert "!This is stderr!" in stdout assert "!This is a warning log msg!" not in stdout stdout = testdir.runpytest("--show-capture=log", "--tb=short").stdout.str() assert "!This is stdout!" not in stdout assert "!This is stderr!" not in stdout assert "!This is a warning log msg!" in stdout stdout = testdir.runpytest("--show-capture=no", "--tb=short").stdout.str() assert "!This is stdout!" not in stdout assert "!This is stderr!" not in stdout assert "!This is a warning log msg!" not in stdout def test_show_capture_with_teardown_logs(self, testdir): """Ensure that the capturing of teardown logs honor --show-capture setting""" testdir.makepyfile( """ import logging import sys import pytest @pytest.fixture(scope="function", autouse="True") def hook_each_test(request): yield sys.stdout.write("!stdout!") sys.stderr.write("!stderr!") logging.warning("!log!") def test_func(): assert False """ ) result = testdir.runpytest("--show-capture=stdout", "--tb=short").stdout.str() assert "!stdout!" in result assert "!stderr!" not in result assert "!log!" not in result result = testdir.runpytest("--show-capture=stderr", "--tb=short").stdout.str() assert "!stdout!" not in result assert "!stderr!" in result assert "!log!" not in result result = testdir.runpytest("--show-capture=log", "--tb=short").stdout.str() assert "!stdout!" not in result assert "!stderr!" not in result assert "!log!" in result result = testdir.runpytest("--show-capture=no", "--tb=short").stdout.str() assert "!stdout!" not in result assert "!stderr!" not in result assert "!log!" not in result @pytest.mark.xfail("not hasattr(os, 'dup')") def test_fdopen_kept_alive_issue124(testdir): testdir.makepyfile( """ import os, sys k = [] def test_open_file_and_keep_alive(capfd): stdout = os.fdopen(1, 'w', 1) k.append(stdout) def test_close_kept_alive_file(): stdout = k.pop() stdout.close() """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 passed*"]) def test_tbstyle_native_setup_error(testdir): testdir.makepyfile( """ import pytest @pytest.fixture def setup_error_fixture(): raise Exception("error in exception") def test_error_fixture(setup_error_fixture): pass """ ) result = testdir.runpytest("--tb=native") result.stdout.fnmatch_lines( ['*File *test_tbstyle_native_setup_error.py", line *, in setup_error_fixture*'] ) def test_terminal_summary(testdir): testdir.makeconftest( """ def pytest_terminal_summary(terminalreporter, exitstatus): w = terminalreporter w.section("hello") w.line("world") w.line("exitstatus: {0}".format(exitstatus)) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *==== hello ====* world exitstatus: 5 """ ) @pytest.mark.filterwarnings("default") def test_terminal_summary_warnings_are_displayed(testdir): """Test that warnings emitted during pytest_terminal_summary are displayed. (#1305). """ testdir.makeconftest( """ import warnings def pytest_terminal_summary(terminalreporter): warnings.warn(UserWarning('internal warning')) """ ) testdir.makepyfile( """ def test_failure(): import warnings warnings.warn("warning_from_" + "test") assert 0 """ ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines( [ "*= warnings summary =*", "*warning_from_test*", "*= short test summary info =*", "*= warnings summary (final) =*", "*conftest.py:3:*internal warning", "*== 1 failed, 2 warnings in *", ] ) assert "None" not in result.stdout.str() stdout = result.stdout.str() assert stdout.count("warning_from_test") == 1 assert stdout.count("=== warnings summary ") == 2 @pytest.mark.filterwarnings("default") def test_terminal_summary_warnings_header_once(testdir): testdir.makepyfile( """ def test_failure(): import warnings warnings.warn("warning_from_" + "test") assert 0 """ ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines( [ "*= warnings summary =*", "*warning_from_test*", "*= short test summary info =*", "*== 1 failed, 1 warnings in *", ] ) assert "None" not in result.stdout.str() stdout = result.stdout.str() assert stdout.count("warning_from_test") == 1 assert stdout.count("=== warnings summary ") == 1 @pytest.mark.parametrize( "exp_color, exp_line, stats_arg", [ # The method under test only cares about the length of each # dict value, not the actual contents, so tuples of anything # suffice # Important statuses -- the highest priority of these always wins ("red", "1 failed", {"failed": (1,)}), ("red", "1 failed, 1 passed", {"failed": (1,), "passed": (1,)}), ("red", "1 error", {"error": (1,)}), ("red", "1 passed, 1 error", {"error": (1,), "passed": (1,)}), # (a status that's not known to the code) ("yellow", "1 weird", {"weird": (1,)}), ("yellow", "1 passed, 1 weird", {"weird": (1,), "passed": (1,)}), ("yellow", "1 warnings", {"warnings": (1,)}), ("yellow", "1 passed, 1 warnings", {"warnings": (1,), "passed": (1,)}), ("green", "5 passed", {"passed": (1, 2, 3, 4, 5)}), # "Boring" statuses. These have no effect on the color of the summary # line. Thus, if *every* test has a boring status, the summary line stays # at its default color, i.e. yellow, to warn the user that the test run # produced no useful information ("yellow", "1 skipped", {"skipped": (1,)}), ("green", "1 passed, 1 skipped", {"skipped": (1,), "passed": (1,)}), ("yellow", "1 deselected", {"deselected": (1,)}), ("green", "1 passed, 1 deselected", {"deselected": (1,), "passed": (1,)}), ("yellow", "1 xfailed", {"xfailed": (1,)}), ("green", "1 passed, 1 xfailed", {"xfailed": (1,), "passed": (1,)}), ("yellow", "1 xpassed", {"xpassed": (1,)}), ("green", "1 passed, 1 xpassed", {"xpassed": (1,), "passed": (1,)}), # Likewise if no tests were found at all ("yellow", "no tests ran", {}), # Test the empty-key special case ("yellow", "no tests ran", {"": (1,)}), ("green", "1 passed", {"": (1,), "passed": (1,)}), # A couple more complex combinations ( "red", "1 failed, 2 passed, 3 xfailed", {"passed": (1, 2), "failed": (1,), "xfailed": (1, 2, 3)}, ), ( "green", "1 passed, 2 skipped, 3 deselected, 2 xfailed", { "passed": (1,), "skipped": (1, 2), "deselected": (1, 2, 3), "xfailed": (1, 2), }, ), ], ) def test_summary_stats(exp_line, exp_color, stats_arg): print("Based on stats: %s" % stats_arg) print('Expect summary: "{}"; with color "{}"'.format(exp_line, exp_color)) (line, color) = build_summary_stats_line(stats_arg) print('Actually got: "{}"; with color "{}"'.format(line, color)) assert line == exp_line assert color == exp_color def test_skip_counting_towards_summary(): class DummyReport(BaseReport): count_towards_summary = True r1 = DummyReport() r2 = DummyReport() res = build_summary_stats_line({"failed": (r1, r2)}) assert res == ("2 failed", "red") r1.count_towards_summary = False res = build_summary_stats_line({"failed": (r1, r2)}) assert res == ("1 failed", "red") class TestClassicOutputStyle(object): """Ensure classic output style works as expected (#3883)""" @pytest.fixture def test_files(self, testdir): testdir.makepyfile( **{ "test_one.py": "def test_one(): pass", "test_two.py": "def test_two(): assert 0", "sub/test_three.py": """ def test_three_1(): pass def test_three_2(): assert 0 def test_three_3(): pass """, } ) def test_normal_verbosity(self, testdir, test_files): result = testdir.runpytest("-o", "console_output_style=classic") result.stdout.fnmatch_lines( [ "test_one.py .", "test_two.py F", "sub{}test_three.py .F.".format(os.sep), "*2 failed, 3 passed in*", ] ) def test_verbose(self, testdir, test_files): result = testdir.runpytest("-o", "console_output_style=classic", "-v") result.stdout.fnmatch_lines( [ "test_one.py::test_one PASSED", "test_two.py::test_two FAILED", "sub{}test_three.py::test_three_1 PASSED".format(os.sep), "sub{}test_three.py::test_three_2 FAILED".format(os.sep), "sub{}test_three.py::test_three_3 PASSED".format(os.sep), "*2 failed, 3 passed in*", ] ) def test_quiet(self, testdir, test_files): result = testdir.runpytest("-o", "console_output_style=classic", "-q") result.stdout.fnmatch_lines([".F.F.", "*2 failed, 3 passed in*"]) class TestProgressOutputStyle(object): @pytest.fixture def many_tests_files(self, testdir): testdir.makepyfile( test_bar=""" import pytest @pytest.mark.parametrize('i', range(10)) def test_bar(i): pass """, test_foo=""" import pytest @pytest.mark.parametrize('i', range(5)) def test_foo(i): pass """, test_foobar=""" import pytest @pytest.mark.parametrize('i', range(5)) def test_foobar(i): pass """, ) def test_zero_tests_collected(self, testdir): """Some plugins (testmon for example) might issue pytest_runtest_logreport without any tests being actually collected (#2971).""" testdir.makeconftest( """ def pytest_collection_modifyitems(items, config): from _pytest.runner import CollectReport for node_id in ('nodeid1', 'nodeid2'): rep = CollectReport(node_id, 'passed', None, None) rep.when = 'passed' rep.duration = 0.1 config.hook.pytest_runtest_logreport(report=rep) """ ) output = testdir.runpytest() assert "ZeroDivisionError" not in output.stdout.str() output.stdout.fnmatch_lines(["=* 2 passed in *="]) def test_normal(self, many_tests_files, testdir): output = testdir.runpytest() output.stdout.re_match_lines( [ r"test_bar.py \.{10} \s+ \[ 50%\]", r"test_foo.py \.{5} \s+ \[ 75%\]", r"test_foobar.py \.{5} \s+ \[100%\]", ] ) def test_count(self, many_tests_files, testdir): testdir.makeini( """ [pytest] console_output_style = count """ ) output = testdir.runpytest() output.stdout.re_match_lines( [ r"test_bar.py \.{10} \s+ \[10/20\]", r"test_foo.py \.{5} \s+ \[15/20\]", r"test_foobar.py \.{5} \s+ \[20/20\]", ] ) def test_verbose(self, many_tests_files, testdir): output = testdir.runpytest("-v") output.stdout.re_match_lines( [ r"test_bar.py::test_bar\[0\] PASSED \s+ \[ 5%\]", r"test_foo.py::test_foo\[4\] PASSED \s+ \[ 75%\]", r"test_foobar.py::test_foobar\[4\] PASSED \s+ \[100%\]", ] ) def test_verbose_count(self, many_tests_files, testdir): testdir.makeini( """ [pytest] console_output_style = count """ ) output = testdir.runpytest("-v") output.stdout.re_match_lines( [ r"test_bar.py::test_bar\[0\] PASSED \s+ \[ 1/20\]", r"test_foo.py::test_foo\[4\] PASSED \s+ \[15/20\]", r"test_foobar.py::test_foobar\[4\] PASSED \s+ \[20/20\]", ] ) def test_xdist_normal(self, many_tests_files, testdir, monkeypatch): pytest.importorskip("xdist") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) output = testdir.runpytest("-n2") output.stdout.re_match_lines([r"\.{20} \s+ \[100%\]"]) def test_xdist_normal_count(self, many_tests_files, testdir, monkeypatch): pytest.importorskip("xdist") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) testdir.makeini( """ [pytest] console_output_style = count """ ) output = testdir.runpytest("-n2") output.stdout.re_match_lines([r"\.{20} \s+ \[20/20\]"]) def test_xdist_verbose(self, many_tests_files, testdir, monkeypatch): pytest.importorskip("xdist") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) output = testdir.runpytest("-n2", "-v") output.stdout.re_match_lines_random( [ r"\[gw\d\] \[\s*\d+%\] PASSED test_bar.py::test_bar\[1\]", r"\[gw\d\] \[\s*\d+%\] PASSED test_foo.py::test_foo\[1\]", r"\[gw\d\] \[\s*\d+%\] PASSED test_foobar.py::test_foobar\[1\]", ] ) def test_capture_no(self, many_tests_files, testdir): output = testdir.runpytest("-s") output.stdout.re_match_lines( [r"test_bar.py \.{10}", r"test_foo.py \.{5}", r"test_foobar.py \.{5}"] ) output = testdir.runpytest("--capture=no") assert "%]" not in output.stdout.str() class TestProgressWithTeardown(object): """Ensure we show the correct percentages for tests that fail during teardown (#3088)""" @pytest.fixture def contest_with_teardown_fixture(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture def fail_teardown(): yield assert False """ ) @pytest.fixture def many_files(self, testdir, contest_with_teardown_fixture): testdir.makepyfile( test_bar=""" import pytest @pytest.mark.parametrize('i', range(5)) def test_bar(fail_teardown, i): pass """, test_foo=""" import pytest @pytest.mark.parametrize('i', range(15)) def test_foo(fail_teardown, i): pass """, ) def test_teardown_simple(self, testdir, contest_with_teardown_fixture): testdir.makepyfile( """ def test_foo(fail_teardown): pass """ ) output = testdir.runpytest() output.stdout.re_match_lines([r"test_teardown_simple.py \.E\s+\[100%\]"]) def test_teardown_with_test_also_failing( self, testdir, contest_with_teardown_fixture ): testdir.makepyfile( """ def test_foo(fail_teardown): assert False """ ) output = testdir.runpytest() output.stdout.re_match_lines( [r"test_teardown_with_test_also_failing.py FE\s+\[100%\]"] ) def test_teardown_many(self, testdir, many_files): output = testdir.runpytest() output.stdout.re_match_lines( [r"test_bar.py (\.E){5}\s+\[ 25%\]", r"test_foo.py (\.E){15}\s+\[100%\]"] ) def test_teardown_many_verbose(self, testdir, many_files): output = testdir.runpytest("-v") output.stdout.re_match_lines( [ r"test_bar.py::test_bar\[0\] PASSED\s+\[ 5%\]", r"test_bar.py::test_bar\[0\] ERROR\s+\[ 5%\]", r"test_bar.py::test_bar\[4\] PASSED\s+\[ 25%\]", r"test_bar.py::test_bar\[4\] ERROR\s+\[ 25%\]", ] ) def test_xdist_normal(self, many_files, testdir, monkeypatch): pytest.importorskip("xdist") monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) output = testdir.runpytest("-n2") output.stdout.re_match_lines([r"[\.E]{40} \s+ \[100%\]"]) def test_skip_reasons_folding(): path = "xyz" lineno = 3 message = "justso" longrepr = (path, lineno, message) class X(object): pass ev1 = X() ev1.when = "execute" ev1.skipped = True ev1.longrepr = longrepr ev2 = X() ev2.when = "execute" ev2.longrepr = longrepr ev2.skipped = True # ev3 might be a collection report ev3 = X() ev3.when = "collect" ev3.longrepr = longrepr ev3.skipped = True values = _folded_skips([ev1, ev2, ev3]) assert len(values) == 1 num, fspath, lineno, reason = values[0] assert num == 3 assert fspath == path assert lineno == lineno assert reason == message def test_line_with_reprcrash(monkeypatch): import _pytest.terminal from wcwidth import wcswidth mocked_verbose_word = "FAILED" mocked_pos = "some::nodeid" def mock_get_pos(*args): return mocked_pos monkeypatch.setattr(_pytest.terminal, "_get_pos", mock_get_pos) class config(object): pass class rep(object): def _get_verbose_word(self, *args): return mocked_verbose_word class longrepr: class reprcrash: pass def check(msg, width, expected): __tracebackhide__ = True if msg: rep.longrepr.reprcrash.message = msg actual = _get_line_with_reprcrash_message(config, rep(), width) assert actual == expected if actual != "%s %s" % (mocked_verbose_word, mocked_pos): assert len(actual) <= width assert wcswidth(actual) <= width # AttributeError with message check(None, 80, "FAILED some::nodeid") check("msg", 80, "FAILED some::nodeid - msg") check("msg", 3, "FAILED some::nodeid") check("msg", 24, "FAILED some::nodeid") check("msg", 25, "FAILED some::nodeid - msg") check("some longer msg", 24, "FAILED some::nodeid") check("some longer msg", 25, "FAILED some::nodeid - ...") check("some longer msg", 26, "FAILED some::nodeid - s...") check("some\nmessage", 25, "FAILED some::nodeid - ...") check("some\nmessage", 26, "FAILED some::nodeid - some") check("some\nmessage", 80, "FAILED some::nodeid - some") # Test unicode safety. check(u"😄😄😄😄😄\n2nd line", 25, u"FAILED some::nodeid - ...") check(u"😄😄😄😄😄\n2nd line", 26, u"FAILED some::nodeid - ...") check(u"😄😄😄😄😄\n2nd line", 27, u"FAILED some::nodeid - 😄...") check(u"😄😄😄😄😄\n2nd line", 28, u"FAILED some::nodeid - 😄...") check(u"😄😄😄😄😄\n2nd line", 29, u"FAILED some::nodeid - 😄😄...") # NOTE: constructed, not sure if this is supported. # It would fail if not using u"" in Python 2 for mocked_pos. mocked_pos = u"nodeid::😄::withunicode" check(u"😄😄😄😄😄\n2nd line", 29, u"FAILED nodeid::😄::withunicode") check(u"😄😄😄😄😄\n2nd line", 40, u"FAILED nodeid::😄::withunicode - 😄😄...") check(u"😄😄😄😄😄\n2nd line", 41, u"FAILED nodeid::😄::withunicode - 😄😄...") check(u"😄😄😄😄😄\n2nd line", 42, u"FAILED nodeid::😄::withunicode - 😄😄😄...") check(u"😄😄😄😄😄\n2nd line", 80, u"FAILED nodeid::😄::withunicode - 😄😄😄😄😄") pytest-4.6.9/testing/test_helpconfig.py0000664000372000037200000000412713604173400021132 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED def test_version(testdir, pytestconfig): result = testdir.runpytest("--version") assert result.ret == 0 # p = py.path.local(py.__file__).dirpath() result.stderr.fnmatch_lines( ["*pytest*{}*imported from*".format(pytest.__version__)] ) if pytestconfig.pluginmanager.list_plugin_distinfo(): result.stderr.fnmatch_lines(["*setuptools registered plugins:", "*at*"]) def test_help(testdir): result = testdir.runpytest("--help") assert result.ret == 0 result.stdout.fnmatch_lines( """ *-v*verbose* *setup.cfg* *minversion* *to see*markers*pytest --markers* *to see*fixtures*pytest --fixtures* """ ) def test_hookvalidation_unknown(testdir): testdir.makeconftest( """ def pytest_hello(xyz): pass """ ) result = testdir.runpytest() assert result.ret != 0 result.stdout.fnmatch_lines(["*unknown hook*pytest_hello*"]) def test_hookvalidation_optional(testdir): testdir.makeconftest( """ import pytest @pytest.hookimpl(optionalhook=True) def pytest_hello(xyz): pass """ ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED def test_traceconfig(testdir): result = testdir.runpytest("--traceconfig") result.stdout.fnmatch_lines(["*using*pytest*py*", "*active plugins*"]) def test_debug(testdir, monkeypatch): result = testdir.runpytest_subprocess("--debug") assert result.ret == EXIT_NOTESTSCOLLECTED p = testdir.tmpdir.join("pytestdebug.log") assert "pytest_sessionstart" in p.read() def test_PYTEST_DEBUG(testdir, monkeypatch): monkeypatch.setenv("PYTEST_DEBUG", "1") result = testdir.runpytest_subprocess() assert result.ret == EXIT_NOTESTSCOLLECTED result.stderr.fnmatch_lines( ["*pytest_plugin_registered*", "*manager*PluginManager*"] ) pytest-4.6.9/testing/test_entry_points.py0000664000372000037200000000062613604173400021551 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from _pytest.compat import importlib_metadata def test_pytest_entry_points_are_identical(): dist = importlib_metadata.distribution("pytest") entry_map = {ep.name: ep for ep in dist.entry_points} assert entry_map["pytest"].value == entry_map["py.test"].value pytest-4.6.9/testing/test_unittest.py0000664000372000037200000007275413604173400020706 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import gc import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED def test_simple_unittest(testdir): testpath = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): def testpassing(self): self.assertEqual('foo', 'foo') def test_failing(self): self.assertEqual('foo', 'bar') """ ) reprec = testdir.inline_run(testpath) assert reprec.matchreport("testpassing").passed assert reprec.matchreport("test_failing").failed def test_runTest_method(testdir): testdir.makepyfile( """ import unittest class MyTestCaseWithRunTest(unittest.TestCase): def runTest(self): self.assertEqual('foo', 'foo') class MyTestCaseWithoutRunTest(unittest.TestCase): def runTest(self): self.assertEqual('foo', 'foo') def test_something(self): pass """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( """ *MyTestCaseWithRunTest::runTest* *MyTestCaseWithoutRunTest::test_something* *2 passed* """ ) def test_isclasscheck_issue53(testdir): testpath = testdir.makepyfile( """ import unittest class _E(object): def __getattr__(self, tag): pass E = _E() """ ) result = testdir.runpytest(testpath) assert result.ret == EXIT_NOTESTSCOLLECTED def test_setup(testdir): testpath = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): def setUp(self): self.foo = 1 def setup_method(self, method): self.foo2 = 1 def test_both(self): self.assertEqual(1, self.foo) assert self.foo2 == 1 def teardown_method(self, method): assert 0, "42" """ ) reprec = testdir.inline_run("-s", testpath) assert reprec.matchreport("test_both", when="call").passed rep = reprec.matchreport("test_both", when="teardown") assert rep.failed and "42" in str(rep.longrepr) def test_setUpModule(testdir): testpath = testdir.makepyfile( """ values = [] def setUpModule(): values.append(1) def tearDownModule(): del values[0] def test_hello(): assert values == [1] def test_world(): assert values == [1] """ ) result = testdir.runpytest(testpath) result.stdout.fnmatch_lines(["*2 passed*"]) def test_setUpModule_failing_no_teardown(testdir): testpath = testdir.makepyfile( """ values = [] def setUpModule(): 0/0 def tearDownModule(): values.append(1) def test_hello(): pass """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=0, failed=1) call = reprec.getcalls("pytest_runtest_setup")[0] assert not call.item.module.values def test_new_instances(testdir): testpath = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): def test_func1(self): self.x = 2 def test_func2(self): assert not hasattr(self, 'x') """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=2) def test_function_item_obj_is_instance(testdir): """item.obj should be a bound method on unittest.TestCase function items (#5390).""" testdir.makeconftest( """ def pytest_runtest_makereport(item, call): if call.when == 'call': class_ = item.parent.obj assert isinstance(item.obj.__self__, class_) """ ) testdir.makepyfile( """ import unittest class Test(unittest.TestCase): def test_foo(self): pass """ ) result = testdir.runpytest_inprocess() result.stdout.fnmatch_lines(["* 1 passed in*"]) def test_teardown(testdir): testpath = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): values = [] def test_one(self): pass def tearDown(self): self.values.append(None) class Second(unittest.TestCase): def test_check(self): self.assertEqual(MyTestCase.values, [None]) """ ) reprec = testdir.inline_run(testpath) passed, skipped, failed = reprec.countoutcomes() assert failed == 0, failed assert passed == 2 assert passed + skipped + failed == 2 def test_teardown_issue1649(testdir): """ Are TestCase objects cleaned up? Often unittest TestCase objects set attributes that are large and expensive during setUp. The TestCase will not be cleaned up if the test fails, because it would then exist in the stackframe. """ testpath = testdir.makepyfile( """ import unittest class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): def setUp(self): self.an_expensive_object = 1 def test_demo(self): pass """ ) testdir.inline_run("-s", testpath) gc.collect() for obj in gc.get_objects(): assert type(obj).__name__ != "TestCaseObjectsShouldBeCleanedUp" def test_unittest_skip_issue148(testdir): testpath = testdir.makepyfile( """ import unittest @unittest.skip("hello") class MyTestCase(unittest.TestCase): @classmethod def setUpClass(self): xxx def test_one(self): pass @classmethod def tearDownClass(self): xxx """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(skipped=1) def test_method_and_teardown_failing_reporting(testdir): testdir.makepyfile( """ import unittest, pytest class TC(unittest.TestCase): def tearDown(self): assert 0, "down1" def test_method(self): assert False, "down2" """ ) result = testdir.runpytest("-s") assert result.ret == 1 result.stdout.fnmatch_lines( [ "*tearDown*", "*assert 0*", "*test_method*", "*assert False*", "*1 failed*1 error*", ] ) def test_setup_failure_is_shown(testdir): testdir.makepyfile( """ import unittest import pytest class TC(unittest.TestCase): def setUp(self): assert 0, "down1" def test_method(self): print("never42") xyz """ ) result = testdir.runpytest("-s") assert result.ret == 1 result.stdout.fnmatch_lines(["*setUp*", "*assert 0*down1*", "*1 failed*"]) assert "never42" not in result.stdout.str() def test_setup_setUpClass(testdir): testpath = testdir.makepyfile( """ import unittest import pytest class MyTestCase(unittest.TestCase): x = 0 @classmethod def setUpClass(cls): cls.x += 1 def test_func1(self): assert self.x == 1 def test_func2(self): assert self.x == 1 @classmethod def tearDownClass(cls): cls.x -= 1 def test_teareddown(): assert MyTestCase.x == 0 """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=3) def test_setup_class(testdir): testpath = testdir.makepyfile( """ import unittest import pytest class MyTestCase(unittest.TestCase): x = 0 def setup_class(cls): cls.x += 1 def test_func1(self): assert self.x == 1 def test_func2(self): assert self.x == 1 def teardown_class(cls): cls.x -= 1 def test_teareddown(): assert MyTestCase.x == 0 """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=3) @pytest.mark.parametrize("type", ["Error", "Failure"]) def test_testcase_adderrorandfailure_defers(testdir, type): testdir.makepyfile( """ from unittest import TestCase import pytest class MyTestCase(TestCase): def run(self, result): excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0) try: result.add%s(self, excinfo._excinfo) except KeyboardInterrupt: raise except: pytest.fail("add%s should not raise") def test_hello(self): pass """ % (type, type) ) result = testdir.runpytest() assert "should not raise" not in result.stdout.str() @pytest.mark.parametrize("type", ["Error", "Failure"]) def test_testcase_custom_exception_info(testdir, type): testdir.makepyfile( """ from unittest import TestCase import py, pytest import _pytest._code class MyTestCase(TestCase): def run(self, result): excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0) # we fake an incompatible exception info from _pytest.monkeypatch import MonkeyPatch mp = MonkeyPatch() def t(*args): mp.undo() raise TypeError() mp.setattr(_pytest._code, 'ExceptionInfo', t) try: excinfo = excinfo._excinfo result.add%(type)s(self, excinfo) finally: mp.undo() def test_hello(self): pass """ % locals() ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "NOTE: Incompatible Exception Representation*", "*ZeroDivisionError*", "*1 failed*", ] ) def test_testcase_totally_incompatible_exception_info(testdir): (item,) = testdir.getitems( """ from unittest import TestCase class MyTestCase(TestCase): def test_hello(self): pass """ ) item.addError(None, 42) excinfo = item._excinfo.pop(0) assert "ERROR: Unknown Incompatible" in str(excinfo.getrepr()) def test_module_level_pytestmark(testdir): testpath = testdir.makepyfile( """ import unittest import pytest pytestmark = pytest.mark.xfail class MyTestCase(unittest.TestCase): def test_func1(self): assert 0 """ ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) class TestTrialUnittest(object): def setup_class(cls): cls.ut = pytest.importorskip("twisted.trial.unittest") # on windows trial uses a socket for a reactor and apparently doesn't close it properly # https://twistedmatrix.com/trac/ticket/9227 cls.ignore_unclosed_socket_warning = ("-W", "always") def test_trial_testcase_runtest_not_collected(self, testdir): testdir.makepyfile( """ from twisted.trial.unittest import TestCase class TC(TestCase): def test_hello(self): pass """ ) reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1) testdir.makepyfile( """ from twisted.trial.unittest import TestCase class TC(TestCase): def runTest(self): pass """ ) reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1) def test_trial_exceptions_with_skips(self, testdir): testdir.makepyfile( """ from twisted.trial import unittest import pytest class TC(unittest.TestCase): def test_hello(self): pytest.skip("skip_in_method") @pytest.mark.skipif("sys.version_info != 1") def test_hello2(self): pass @pytest.mark.xfail(reason="iwanto") def test_hello3(self): assert 0 def test_hello4(self): pytest.xfail("i2wanto") def test_trial_skip(self): pass test_trial_skip.skip = "trialselfskip" def test_trial_todo(self): assert 0 test_trial_todo.todo = "mytodo" def test_trial_todo_success(self): pass test_trial_todo_success.todo = "mytodo" class TC2(unittest.TestCase): def setup_class(cls): pytest.skip("skip_in_setup_class") def test_method(self): pass """ ) from _pytest.compat import _is_unittest_unexpected_success_a_failure should_fail = _is_unittest_unexpected_success_a_failure() result = testdir.runpytest("-rxs", *self.ignore_unclosed_socket_warning) result.stdout.fnmatch_lines_random( [ "*XFAIL*test_trial_todo*", "*trialselfskip*", "*skip_in_setup_class*", "*iwanto*", "*i2wanto*", "*sys.version_info*", "*skip_in_method*", "*1 failed*4 skipped*3 xfailed*" if should_fail else "*4 skipped*3 xfail*1 xpass*", ] ) assert result.ret == (1 if should_fail else 0) def test_trial_error(self, testdir): testdir.makepyfile( """ from twisted.trial.unittest import TestCase from twisted.internet.defer import Deferred from twisted.internet import reactor class TC(TestCase): def test_one(self): crash def test_two(self): def f(_): crash d = Deferred() d.addCallback(f) reactor.callLater(0.3, d.callback, None) return d def test_three(self): def f(): pass # will never get called reactor.callLater(0.3, f) # will crash at teardown def test_four(self): def f(_): reactor.callLater(0.3, f) crash d = Deferred() d.addCallback(f) reactor.callLater(0.3, d.callback, None) return d # will crash both at test time and at teardown """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ERRORS*", "*DelayedCalls*", "*test_four*", "*NameError*crash*", "*test_one*", "*NameError*crash*", "*test_three*", "*DelayedCalls*", "*test_two*", "*crash*", ] ) def test_trial_pdb(self, testdir): p = testdir.makepyfile( """ from twisted.trial import unittest import pytest class TC(unittest.TestCase): def test_hello(self): assert 0, "hellopdb" """ ) child = testdir.spawn_pytest(p) child.expect("hellopdb") child.sendeof() def test_trial_testcase_skip_property(self, testdir): testpath = testdir.makepyfile( """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): skip = 'dont run' def test_func(self): pass """ ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testfunction_skip_property(self, testdir): testpath = testdir.makepyfile( """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): def test_func(self): pass test_func.skip = 'dont run' """ ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testcase_todo_property(self, testdir): testpath = testdir.makepyfile( """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): todo = 'dont run' def test_func(self): assert 0 """ ) reprec = testdir.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1) def test_trial_testfunction_todo_property(self, testdir): testpath = testdir.makepyfile( """ from twisted.trial import unittest class MyTestCase(unittest.TestCase): def test_func(self): assert 0 test_func.todo = 'dont run' """ ) reprec = testdir.inline_run( testpath, "-s", *self.ignore_unclosed_socket_warning ) reprec.assertoutcome(skipped=1) def test_djangolike_testcase(testdir): # contributed from Morten Breekevold testdir.makepyfile( """ from unittest import TestCase, main class DjangoLikeTestCase(TestCase): def setUp(self): print("setUp()") def test_presetup_has_been_run(self): print("test_thing()") self.assertTrue(hasattr(self, 'was_presetup')) def tearDown(self): print("tearDown()") def __call__(self, result=None): try: self._pre_setup() except (KeyboardInterrupt, SystemExit): raise except Exception: import sys result.addError(self, sys.exc_info()) return super(DjangoLikeTestCase, self).__call__(result) try: self._post_teardown() except (KeyboardInterrupt, SystemExit): raise except Exception: import sys result.addError(self, sys.exc_info()) return def _pre_setup(self): print("_pre_setup()") self.was_presetup = True def _post_teardown(self): print("_post_teardown()") """ ) result = testdir.runpytest("-s") assert result.ret == 0 result.stdout.fnmatch_lines( [ "*_pre_setup()*", "*setUp()*", "*test_thing()*", "*tearDown()*", "*_post_teardown()*", ] ) def test_unittest_not_shown_in_traceback(testdir): testdir.makepyfile( """ import unittest class t(unittest.TestCase): def test_hello(self): x = 3 self.assertEqual(x, 4) """ ) res = testdir.runpytest() assert "failUnlessEqual" not in res.stdout.str() def test_unorderable_types(testdir): testdir.makepyfile( """ import unittest class TestJoinEmpty(unittest.TestCase): pass def make_test(): class Test(unittest.TestCase): pass Test.__name__ = "TestFoo" return Test TestFoo = make_test() """ ) result = testdir.runpytest() assert "TypeError" not in result.stdout.str() assert result.ret == EXIT_NOTESTSCOLLECTED def test_unittest_typerror_traceback(testdir): testdir.makepyfile( """ import unittest class TestJoinEmpty(unittest.TestCase): def test_hello(self, arg1): pass """ ) result = testdir.runpytest() assert "TypeError" in result.stdout.str() assert result.ret == 1 @pytest.mark.parametrize("runner", ["pytest", "unittest"]) def test_unittest_expected_failure_for_failing_test_is_xfail(testdir, runner): script = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): @unittest.expectedFailure def test_failing_test_is_xfail(self): assert False if __name__ == '__main__': unittest.main() """ ) if runner == "pytest": result = testdir.runpytest("-rxX") result.stdout.fnmatch_lines( ["*XFAIL*MyTestCase*test_failing_test_is_xfail*", "*1 xfailed*"] ) else: result = testdir.runpython(script) result.stderr.fnmatch_lines(["*1 test in*", "*OK*(expected failures=1)*"]) assert result.ret == 0 @pytest.mark.parametrize("runner", ["pytest", "unittest"]) def test_unittest_expected_failure_for_passing_test_is_fail(testdir, runner): script = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): @unittest.expectedFailure def test_passing_test_is_fail(self): assert True if __name__ == '__main__': unittest.main() """ ) from _pytest.compat import _is_unittest_unexpected_success_a_failure should_fail = _is_unittest_unexpected_success_a_failure() if runner == "pytest": result = testdir.runpytest("-rxX") result.stdout.fnmatch_lines( [ "*MyTestCase*test_passing_test_is_fail*", "*1 failed*" if should_fail else "*1 xpassed*", ] ) else: result = testdir.runpython(script) result.stderr.fnmatch_lines(["*1 test in*", "*(unexpected successes=1)*"]) assert result.ret == (1 if should_fail else 0) @pytest.mark.parametrize( "fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")] ) def test_unittest_setup_interaction(testdir, fix_type, stmt): testdir.makepyfile( """ import unittest import pytest class MyTestCase(unittest.TestCase): @pytest.{fix_type}(scope="class", autouse=True) def perclass(self, request): request.cls.hello = "world" {stmt} @pytest.{fix_type}(scope="function", autouse=True) def perfunction(self, request): request.instance.funcname = request.function.__name__ {stmt} def test_method1(self): assert self.funcname == "test_method1" assert self.hello == "world" def test_method2(self): assert self.funcname == "test_method2" def test_classattr(self): assert self.__class__.hello == "world" """.format( fix_type=fix_type, stmt=stmt ) ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) def test_non_unittest_no_setupclass_support(testdir): testpath = testdir.makepyfile( """ class TestFoo(object): x = 0 @classmethod def setUpClass(cls): cls.x = 1 def test_method1(self): assert self.x == 0 @classmethod def tearDownClass(cls): cls.x = 1 def test_not_teareddown(): assert TestFoo.x == 0 """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=2) def test_no_teardown_if_setupclass_failed(testdir): testpath = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): x = 0 @classmethod def setUpClass(cls): cls.x = 1 assert False def test_func1(self): cls.x = 10 @classmethod def tearDownClass(cls): cls.x = 100 def test_notTornDown(): assert MyTestCase.x == 1 """ ) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=1, failed=1) def test_issue333_result_clearing(testdir): testdir.makeconftest( """ import pytest @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(item): yield assert 0 """ ) testdir.makepyfile( """ import unittest class TestIt(unittest.TestCase): def test_func(self): 0/0 """ ) reprec = testdir.inline_run() reprec.assertoutcome(failed=1) def test_unittest_raise_skip_issue748(testdir): testdir.makepyfile( test_foo=""" import unittest class MyTestCase(unittest.TestCase): def test_one(self): raise unittest.SkipTest('skipping due to reasons') """ ) result = testdir.runpytest("-v", "-rs") result.stdout.fnmatch_lines( """ *SKIP*[1]*test_foo.py*skipping due to reasons* *1 skipped* """ ) def test_unittest_skip_issue1169(testdir): testdir.makepyfile( test_foo=""" import unittest class MyTestCase(unittest.TestCase): @unittest.skip("skipping due to reasons") def test_skip(self): self.fail() """ ) result = testdir.runpytest("-v", "-rs") result.stdout.fnmatch_lines( """ *SKIP*[1]*skipping due to reasons* *1 skipped* """ ) def test_class_method_containing_test_issue1558(testdir): testdir.makepyfile( test_foo=""" import unittest class MyTestCase(unittest.TestCase): def test_should_run(self): pass def test_should_not_run(self): pass test_should_not_run.__test__ = False """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.parametrize( "base", ["six.moves.builtins.object", "unittest.TestCase", "unittest2.TestCase"] ) def test_usefixtures_marker_on_unittest(base, testdir): """#3498""" module = base.rsplit(".", 1)[0] pytest.importorskip(module) testdir.makepyfile( conftest=""" import pytest @pytest.fixture(scope='function') def fixture1(request, monkeypatch): monkeypatch.setattr(request.instance, 'fixture1', True ) @pytest.fixture(scope='function') def fixture2(request, monkeypatch): monkeypatch.setattr(request.instance, 'fixture2', True ) def node_and_marks(item): print(item.nodeid) for mark in item.iter_markers(): print(" ", mark) @pytest.fixture(autouse=True) def my_marks(request): node_and_marks(request.node) def pytest_collection_modifyitems(items): for item in items: node_and_marks(item) """ ) testdir.makepyfile( """ import pytest import {module} class Tests({base}): fixture1 = False fixture2 = False @pytest.mark.usefixtures("fixture1") def test_one(self): assert self.fixture1 assert not self.fixture2 @pytest.mark.usefixtures("fixture1", "fixture2") def test_two(self): assert self.fixture1 assert self.fixture2 """.format( module=module, base=base ) ) result = testdir.runpytest("-s") result.assert_outcomes(passed=2) def test_testcase_handles_init_exceptions(testdir): """ Regression test to make sure exceptions in the __init__ method are bubbled up correctly. See https://github.com/pytest-dev/pytest/issues/3788 """ testdir.makepyfile( """ from unittest import TestCase import pytest class MyTestCase(TestCase): def __init__(self, *args, **kwargs): raise Exception("should raise this exception") def test_hello(self): pass """ ) result = testdir.runpytest() assert "should raise this exception" in result.stdout.str() assert "ERROR at teardown of MyTestCase.test_hello" not in result.stdout.str() def test_error_message_with_parametrized_fixtures(testdir): testdir.copy_example("unittest/test_parametrized_fixture_error_message.py") result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_two does not support fixtures*", "*TestSomethingElse::test_two", "*Function type: TestCaseFunction", ] ) @pytest.mark.parametrize( "test_name, expected_outcome", [ ("test_setup_skip.py", "1 skipped"), ("test_setup_skip_class.py", "1 skipped"), ("test_setup_skip_module.py", "1 error"), ], ) def test_setup_inheritance_skipping(testdir, test_name, expected_outcome): """Issue #4700""" testdir.copy_example("unittest/{}".format(test_name)) result = testdir.runpytest() result.stdout.fnmatch_lines(["* {} in *".format(expected_outcome)]) pytest-4.6.9/testing/test_skipping.py0000664000372000037200000010061213604173400020634 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import pytest from _pytest.runner import runtestprotocol from _pytest.skipping import MarkEvaluator from _pytest.skipping import pytest_runtest_setup class TestEvaluator(object): def test_no_marker(self, testdir): item = testdir.getitem("def test_func(): pass") evalskipif = MarkEvaluator(item, "skipif") assert not evalskipif assert not evalskipif.istrue() def test_marked_no_args(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xyz def test_func(): pass """ ) ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "" assert not ev.get("run", False) def test_marked_one_arg(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xyz("hasattr(os, 'sep')") def test_func(): pass """ ) ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: hasattr(os, 'sep')" @pytest.mark.skipif("sys.version_info[0] >= 3") def test_marked_one_arg_unicode(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xyz(u"hasattr(os, 'sep')") def test_func(): pass """ ) ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: hasattr(os, 'sep')" def test_marked_one_arg_with_reason(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xyz("hasattr(os, 'sep')", attr=2, reason="hello world") def test_func(): pass """ ) ev = MarkEvaluator(item, "xyz") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "hello world" assert ev.get("attr") == 2 def test_marked_one_arg_twice(self, testdir): lines = [ """@pytest.mark.skipif("not hasattr(os, 'murks')")""", """@pytest.mark.skipif("hasattr(os, 'murks')")""", ] for i in range(0, 2): item = testdir.getitem( """ import pytest %s %s def test_func(): pass """ % (lines[i], lines[(i + 1) % 2]) ) ev = MarkEvaluator(item, "skipif") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: not hasattr(os, 'murks')" def test_marked_one_arg_twice2(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.skipif("hasattr(os, 'murks')") @pytest.mark.skipif("not hasattr(os, 'murks')") def test_func(): pass """ ) ev = MarkEvaluator(item, "skipif") assert ev assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: not hasattr(os, 'murks')" def test_marked_skip_with_not_string(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.skipif(False) def test_func(): pass """ ) ev = MarkEvaluator(item, "skipif") exc = pytest.raises(pytest.fail.Exception, ev.istrue) assert ( """Failed: you need to specify reason=STRING when using booleans as conditions.""" in exc.value.msg ) def test_skipif_class(self, testdir): (item,) = testdir.getitems( """ import pytest class TestClass(object): pytestmark = pytest.mark.skipif("config._hackxyz") def test_func(self): pass """ ) item.config._hackxyz = 3 ev = MarkEvaluator(item, "skipif") assert ev.istrue() expl = ev.getexplanation() assert expl == "condition: config._hackxyz" class TestXFail(object): @pytest.mark.parametrize("strict", [True, False]) def test_xfail_simple(self, testdir, strict): item = testdir.getitem( """ import pytest @pytest.mark.xfail(strict=%s) def test_func(): assert 0 """ % strict ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] assert callreport.skipped assert callreport.wasxfail == "" def test_xfail_xpassed(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xfail(reason="this is an xfail") def test_func(): assert 1 """ ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] assert callreport.passed assert callreport.wasxfail == "this is an xfail" def test_xfail_using_platform(self, testdir): """ Verify that platform can be used with xfail statements. """ item = testdir.getitem( """ import pytest @pytest.mark.xfail("platform.platform() == platform.platform()") def test_func(): assert 0 """ ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] assert callreport.wasxfail def test_xfail_xpassed_strict(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xfail(strict=True, reason="nope") def test_func(): assert 1 """ ) reports = runtestprotocol(item, log=False) assert len(reports) == 3 callreport = reports[1] assert callreport.failed assert callreport.longrepr == "[XPASS(strict)] nope" assert not hasattr(callreport, "wasxfail") def test_xfail_run_anyway(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail def test_func(): assert 0 def test_func2(): pytest.xfail("hello") """ ) result = testdir.runpytest("--runxfail") result.stdout.fnmatch_lines( ["*def test_func():*", "*assert 0*", "*1 failed*1 pass*"] ) def test_xfail_evalfalse_but_fails(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.xfail('False') def test_func(): assert 0 """ ) reports = runtestprotocol(item, log=False) callreport = reports[1] assert callreport.failed assert not hasattr(callreport, "wasxfail") assert "xfail" in callreport.keywords def test_xfail_not_report_default(self, testdir): p = testdir.makepyfile( test_one=""" import pytest @pytest.mark.xfail def test_this(): assert 0 """ ) testdir.runpytest(p, "-v") # result.stdout.fnmatch_lines([ # "*HINT*use*-r*" # ]) def test_xfail_not_run_xfail_reporting(self, testdir): p = testdir.makepyfile( test_one=""" import pytest @pytest.mark.xfail(run=False, reason="noway") def test_this(): assert 0 @pytest.mark.xfail("True", run=False) def test_this_true(): assert 0 @pytest.mark.xfail("False", run=False, reason="huh") def test_this_false(): assert 1 """ ) result = testdir.runpytest(p, "-rx") result.stdout.fnmatch_lines( [ "*test_one*test_this*", "*NOTRUN*noway", "*test_one*test_this_true*", "*NOTRUN*condition:*True*", "*1 passed*", ] ) def test_xfail_not_run_no_setup_run(self, testdir): p = testdir.makepyfile( test_one=""" import pytest @pytest.mark.xfail(run=False, reason="hello") def test_this(): assert 0 def setup_module(mod): raise ValueError(42) """ ) result = testdir.runpytest(p, "-rx") result.stdout.fnmatch_lines( ["*test_one*test_this*", "*NOTRUN*hello", "*1 xfailed*"] ) def test_xfail_xpass(self, testdir): p = testdir.makepyfile( test_one=""" import pytest @pytest.mark.xfail def test_that(): assert 1 """ ) result = testdir.runpytest(p, "-rX") result.stdout.fnmatch_lines(["*XPASS*test_that*", "*1 xpassed*"]) assert result.ret == 0 def test_xfail_imperative(self, testdir): p = testdir.makepyfile( """ import pytest def test_this(): pytest.xfail("hello") """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 xfailed*"]) result = testdir.runpytest(p, "-rx") result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*reason:*hello*"]) result = testdir.runpytest(p, "--runxfail") result.stdout.fnmatch_lines(["*1 pass*"]) def test_xfail_imperative_in_setup_function(self, testdir): p = testdir.makepyfile( """ import pytest def setup_function(function): pytest.xfail("hello") def test_this(): assert 0 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 xfailed*"]) result = testdir.runpytest(p, "-rx") result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*reason:*hello*"]) result = testdir.runpytest(p, "--runxfail") result.stdout.fnmatch_lines( """ *def test_this* *1 fail* """ ) def xtest_dynamic_xfail_set_during_setup(self, testdir): p = testdir.makepyfile( """ import pytest def setup_function(function): pytest.mark.xfail(function) def test_this(): assert 0 def test_that(): assert 1 """ ) result = testdir.runpytest(p, "-rxX") result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*XPASS*test_that*"]) def test_dynamic_xfail_no_run(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.xfail(run=False)) def test_this(arg): assert 0 """ ) result = testdir.runpytest(p, "-rxX") result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*NOTRUN*"]) def test_dynamic_xfail_set_during_funcarg_setup(self, testdir): p = testdir.makepyfile( """ import pytest @pytest.fixture def arg(request): request.applymarker(pytest.mark.xfail) def test_this2(arg): assert 0 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 xfailed*"]) @pytest.mark.parametrize( "expected, actual, matchline", [ ("TypeError", "TypeError", "*1 xfailed*"), ("(AttributeError, TypeError)", "TypeError", "*1 xfailed*"), ("TypeError", "IndexError", "*1 failed*"), ("(AttributeError, TypeError)", "IndexError", "*1 failed*"), ], ) def test_xfail_raises(self, expected, actual, matchline, testdir): p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(raises=%s) def test_raises(): raise %s() """ % (expected, actual) ) result = testdir.runpytest(p) result.stdout.fnmatch_lines([matchline]) def test_strict_sanity(self, testdir): """sanity check for xfail(strict=True): a failing test should behave exactly like a normal xfail. """ p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(reason='unsupported feature', strict=True) def test_foo(): assert 0 """ ) result = testdir.runpytest(p, "-rxX") result.stdout.fnmatch_lines(["*XFAIL*", "*unsupported feature*"]) assert result.ret == 0 @pytest.mark.parametrize("strict", [True, False]) def test_strict_xfail(self, testdir, strict): p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(reason='unsupported feature', strict=%s) def test_foo(): with open('foo_executed', 'w'): pass # make sure test executes """ % strict ) result = testdir.runpytest(p, "-rxX") if strict: result.stdout.fnmatch_lines( ["*test_foo*", "*XPASS(strict)*unsupported feature*"] ) else: result.stdout.fnmatch_lines( [ "*test_strict_xfail*", "XPASS test_strict_xfail.py::test_foo unsupported feature", ] ) assert result.ret == (1 if strict else 0) assert testdir.tmpdir.join("foo_executed").isfile() @pytest.mark.parametrize("strict", [True, False]) def test_strict_xfail_condition(self, testdir, strict): p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(False, reason='unsupported feature', strict=%s) def test_foo(): pass """ % strict ) result = testdir.runpytest(p, "-rxX") result.stdout.fnmatch_lines(["*1 passed*"]) assert result.ret == 0 @pytest.mark.parametrize("strict", [True, False]) def test_xfail_condition_keyword(self, testdir, strict): p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(condition=False, reason='unsupported feature', strict=%s) def test_foo(): pass """ % strict ) result = testdir.runpytest(p, "-rxX") result.stdout.fnmatch_lines(["*1 passed*"]) assert result.ret == 0 @pytest.mark.parametrize("strict_val", ["true", "false"]) def test_strict_xfail_default_from_file(self, testdir, strict_val): testdir.makeini( """ [pytest] xfail_strict = %s """ % strict_val ) p = testdir.makepyfile( """ import pytest @pytest.mark.xfail(reason='unsupported feature') def test_foo(): pass """ ) result = testdir.runpytest(p, "-rxX") strict = strict_val == "true" result.stdout.fnmatch_lines(["*1 failed*" if strict else "*1 xpassed*"]) assert result.ret == (1 if strict else 0) class TestXFailwithSetupTeardown(object): def test_failing_setup_issue9(self, testdir): testdir.makepyfile( """ import pytest def setup_function(func): assert 0 @pytest.mark.xfail def test_func(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 xfail*"]) def test_failing_teardown_issue9(self, testdir): testdir.makepyfile( """ import pytest def teardown_function(func): assert 0 @pytest.mark.xfail def test_func(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 xfail*"]) class TestSkip(object): def test_skip_class(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip class TestSomething(object): def test_foo(self): pass def test_bar(self): pass def test_baz(): pass """ ) rec = testdir.inline_run() rec.assertoutcome(skipped=2, passed=1) def test_skips_on_false_string(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip('False') def test_foo(): pass """ ) rec = testdir.inline_run() rec.assertoutcome(skipped=1) def test_arg_as_reason(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip('testing stuff') def test_bar(): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*testing stuff*", "*1 skipped*"]) def test_skip_no_reason(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip def test_foo(): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) def test_skip_with_reason(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip(reason="for lolz") def test_bar(): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*for lolz*", "*1 skipped*"]) def test_only_skips_marked_test(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip def test_foo(): pass @pytest.mark.skip(reason="nothing in particular") def test_bar(): pass def test_baz(): assert True """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*nothing in particular*", "*1 passed*2 skipped*"]) def test_strict_and_skip(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skip def test_hello(): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) class TestSkipif(object): def test_skipif_conditional(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.skipif("hasattr(os, 'sep')") def test_func(): pass """ ) x = pytest.raises(pytest.skip.Exception, lambda: pytest_runtest_setup(item)) assert x.value.msg == "condition: hasattr(os, 'sep')" @pytest.mark.parametrize( "params", ["\"hasattr(sys, 'platform')\"", 'True, reason="invalid platform"'] ) def test_skipif_reporting(self, testdir, params): p = testdir.makepyfile( test_foo=""" import pytest @pytest.mark.skipif(%(params)s) def test_that(): assert 0 """ % dict(params=params) ) result = testdir.runpytest(p, "-s", "-rs") result.stdout.fnmatch_lines(["*SKIP*1*test_foo.py*platform*", "*1 skipped*"]) assert result.ret == 0 def test_skipif_using_platform(self, testdir): item = testdir.getitem( """ import pytest @pytest.mark.skipif("platform.platform() == platform.platform()") def test_func(): pass """ ) pytest.raises(pytest.skip.Exception, lambda: pytest_runtest_setup(item)) @pytest.mark.parametrize( "marker, msg1, msg2", [("skipif", "SKIP", "skipped"), ("xfail", "XPASS", "xpassed")], ) def test_skipif_reporting_multiple(self, testdir, marker, msg1, msg2): testdir.makepyfile( test_foo=""" import pytest @pytest.mark.{marker}(False, reason='first_condition') @pytest.mark.{marker}(True, reason='second_condition') def test_foobar(): assert 1 """.format( marker=marker ) ) result = testdir.runpytest("-s", "-rsxX") result.stdout.fnmatch_lines( [ "*{msg1}*test_foo.py*second_condition*".format(msg1=msg1), "*1 {msg2}*".format(msg2=msg2), ] ) assert result.ret == 0 def test_skip_not_report_default(testdir): p = testdir.makepyfile( test_one=""" import pytest def test_this(): pytest.skip("hello") """ ) result = testdir.runpytest(p, "-v") result.stdout.fnmatch_lines( [ # "*HINT*use*-r*", "*1 skipped*" ] ) def test_skipif_class(testdir): p = testdir.makepyfile( """ import pytest class TestClass(object): pytestmark = pytest.mark.skipif("True") def test_that(self): assert 0 def test_though(self): assert 0 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*2 skipped*"]) def test_skipped_reasons_functional(testdir): testdir.makepyfile( test_one=""" from conftest import doskip def setup_function(func): doskip() def test_func(): pass class TestClass(object): def test_method(self): doskip() """, conftest=""" import pytest def doskip(): pytest.skip('test') """, ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*SKIP*2*conftest.py:4: test"]) assert result.ret == 0 def test_skipped_folding(testdir): testdir.makepyfile( test_one=""" import pytest pytestmark = pytest.mark.skip("Folding") def setup_function(func): pass def test_func(): pass class TestClass(object): def test_method(self): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines(["*SKIP*2*test_one.py: Folding"]) assert result.ret == 0 def test_reportchars(testdir): testdir.makepyfile( """ import pytest def test_1(): assert 0 @pytest.mark.xfail def test_2(): assert 0 @pytest.mark.xfail def test_3(): pass def test_4(): pytest.skip("four") """ ) result = testdir.runpytest("-rfxXs") result.stdout.fnmatch_lines( ["FAIL*test_1*", "XFAIL*test_2*", "XPASS*test_3*", "SKIP*four*"] ) def test_reportchars_error(testdir): testdir.makepyfile( conftest=""" def pytest_runtest_teardown(): assert 0 """, test_simple=""" def test_foo(): pass """, ) result = testdir.runpytest("-rE") result.stdout.fnmatch_lines(["ERROR*test_foo*"]) def test_reportchars_all(testdir): testdir.makepyfile( """ import pytest def test_1(): assert 0 @pytest.mark.xfail def test_2(): assert 0 @pytest.mark.xfail def test_3(): pass def test_4(): pytest.skip("four") @pytest.fixture def fail(): assert 0 def test_5(fail): pass """ ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines( [ "SKIP*four*", "XFAIL*test_2*", "XPASS*test_3*", "ERROR*test_5*", "FAIL*test_1*", ] ) def test_reportchars_all_error(testdir): testdir.makepyfile( conftest=""" def pytest_runtest_teardown(): assert 0 """, test_simple=""" def test_foo(): pass """, ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines(["ERROR*test_foo*"]) def test_errors_in_xfail_skip_expressions(testdir): testdir.makepyfile( """ import pytest @pytest.mark.skipif("asd") def test_nameerror(): pass @pytest.mark.xfail("syntax error") def test_syntax(): pass def test_func(): pass """ ) result = testdir.runpytest() markline = " ^" if sys.platform.startswith("java"): # XXX report this to java markline = "*" + markline[8:] elif hasattr(sys, "pypy_version_info") and sys.pypy_version_info < (6,): markline = markline[5:] elif sys.version_info >= (3, 8) or hasattr(sys, "pypy_version_info"): markline = markline[4:] result.stdout.fnmatch_lines( [ "*ERROR*test_nameerror*", "*evaluating*skipif*expression*", "*asd*", "*ERROR*test_syntax*", "*evaluating*xfail*expression*", " syntax error", markline, "SyntaxError: invalid syntax", "*1 pass*2 error*", ] ) def test_xfail_skipif_with_globals(testdir): testdir.makepyfile( """ import pytest x = 3 @pytest.mark.skipif("x == 3") def test_skip1(): pass @pytest.mark.xfail("x == 3") def test_boolean(): assert 0 """ ) result = testdir.runpytest("-rsx") result.stdout.fnmatch_lines(["*SKIP*x == 3*", "*XFAIL*test_boolean*", "*x == 3*"]) def test_direct_gives_error(testdir): testdir.makepyfile( """ import pytest @pytest.mark.skipif(True) def test_skip1(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 error*"]) def test_default_markers(testdir): result = testdir.runpytest("--markers") result.stdout.fnmatch_lines( [ "*skipif(*condition)*skip*", "*xfail(*condition, reason=None, run=True, raises=None, strict=False)*expected failure*", ] ) def test_xfail_test_setup_exception(testdir): testdir.makeconftest( """ def pytest_runtest_setup(): 0 / 0 """ ) p = testdir.makepyfile( """ import pytest @pytest.mark.xfail def test_func(): assert 0 """ ) result = testdir.runpytest(p) assert result.ret == 0 assert "xfailed" in result.stdout.str() assert "xpassed" not in result.stdout.str() def test_imperativeskip_on_xfail_test(testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail def test_that_fails(): assert 0 @pytest.mark.skipif("True") def test_hello(): pass """ ) testdir.makeconftest( """ import pytest def pytest_runtest_setup(item): pytest.skip("abc") """ ) result = testdir.runpytest("-rsxX") result.stdout.fnmatch_lines_random( """ *SKIP*abc* *SKIP*condition: True* *2 skipped* """ ) class TestBooleanCondition(object): def test_skipif(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skipif(True, reason="True123") def test_func1(): pass @pytest.mark.skipif(False, reason="True123") def test_func2(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *1 passed*1 skipped* """ ) def test_skipif_noreason(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.skipif(True) def test_func(): pass """ ) result = testdir.runpytest("-rs") result.stdout.fnmatch_lines( """ *1 error* """ ) def test_xfail(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.xfail(True, reason="True123") def test_func(): assert 0 """ ) result = testdir.runpytest("-rxs") result.stdout.fnmatch_lines( """ *XFAIL* *True123* *1 xfail* """ ) def test_xfail_item(testdir): # Ensure pytest.xfail works with non-Python Item testdir.makeconftest( """ import pytest class MyItem(pytest.Item): nodeid = 'foo' def runtest(self): pytest.xfail("Expected Failure") def pytest_collect_file(path, parent): return MyItem("foo", parent) """ ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed def test_module_level_skip_error(testdir): """ Verify that using pytest.skip at module level causes a collection error """ testdir.makepyfile( """ import pytest @pytest.skip def test_func(): assert True """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*Using pytest.skip outside of a test is not allowed*"] ) def test_module_level_skip_with_allow_module_level(testdir): """ Verify that using pytest.skip(allow_module_level=True) is allowed """ testdir.makepyfile( """ import pytest pytest.skip("skip_module_level", allow_module_level=True) def test_func(): assert 0 """ ) result = testdir.runpytest("-rxs") result.stdout.fnmatch_lines(["*SKIP*skip_module_level"]) def test_invalid_skip_keyword_parameter(testdir): """ Verify that using pytest.skip() with unknown parameter raises an error """ testdir.makepyfile( """ import pytest pytest.skip("skip_module_level", unknown=1) def test_func(): assert 0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*TypeError:*['unknown']*"]) def test_mark_xfail_item(testdir): # Ensure pytest.mark.xfail works with non-Python Item testdir.makeconftest( """ import pytest class MyItem(pytest.Item): nodeid = 'foo' def setup(self): marker = pytest.mark.xfail(True, reason="Expected failure") self.add_marker(marker) def runtest(self): assert False def pytest_collect_file(path, parent): return MyItem("foo", parent) """ ) result = testdir.inline_run() passed, skipped, failed = result.listoutcomes() assert not failed xfailed = [r for r in skipped if hasattr(r, "wasxfail")] assert xfailed def test_summary_list_after_errors(testdir): """Ensure the list of errors/fails/xfails/skips appears after tracebacks in terminal reporting.""" testdir.makepyfile( """ import pytest def test_fail(): assert 0 """ ) result = testdir.runpytest("-ra") result.stdout.fnmatch_lines( [ "=* FAILURES *=", "*= short test summary info =*", "FAILED test_summary_list_after_errors.py::test_fail - assert 0", ] ) def test_importorskip(): with pytest.raises( pytest.skip.Exception, match="^could not import 'doesnotexist': No module named .*", ): pytest.importorskip("doesnotexist") pytest-4.6.9/testing/test_pytester.py0000664000372000037200000004244513604173400020700 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import subprocess import sys import time import py.path import _pytest.pytester as pytester import pytest from _pytest.config import PytestPluginManager from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_OK from _pytest.main import EXIT_TESTSFAILED from _pytest.pytester import CwdSnapshot from _pytest.pytester import HookRecorder from _pytest.pytester import LineMatcher from _pytest.pytester import SysModulesSnapshot from _pytest.pytester import SysPathsSnapshot def test_make_hook_recorder(testdir): item = testdir.getitem("def test_func(): pass") recorder = testdir.make_hook_recorder(item.config.pluginmanager) assert not recorder.getfailures() pytest.xfail("internal reportrecorder tests need refactoring") class rep(object): excinfo = None passed = False failed = True skipped = False when = "call" recorder.hook.pytest_runtest_logreport(report=rep) failures = recorder.getfailures() assert failures == [rep] failures = recorder.getfailures() assert failures == [rep] class rep(object): excinfo = None passed = False failed = False skipped = True when = "call" rep.passed = False rep.skipped = True recorder.hook.pytest_runtest_logreport(report=rep) modcol = testdir.getmodulecol("") rep = modcol.config.hook.pytest_make_collect_report(collector=modcol) rep.passed = False rep.failed = True rep.skipped = False recorder.hook.pytest_collectreport(report=rep) passed, skipped, failed = recorder.listoutcomes() assert not passed and skipped and failed numpassed, numskipped, numfailed = recorder.countoutcomes() assert numpassed == 0 assert numskipped == 1 assert numfailed == 1 assert len(recorder.getfailedcollections()) == 1 recorder.unregister() recorder.clear() recorder.hook.pytest_runtest_logreport(report=rep) pytest.raises(ValueError, recorder.getfailures) def test_parseconfig(testdir): config1 = testdir.parseconfig() config2 = testdir.parseconfig() assert config2 != config1 assert config1 != pytest.config def test_testdir_runs_with_plugin(testdir): testdir.makepyfile( """ pytest_plugins = "pytester" def test_hello(testdir): assert 1 """ ) result = testdir.runpytest() result.assert_outcomes(passed=1) def test_runresult_assertion_on_xfail(testdir): testdir.makepyfile( """ import pytest pytest_plugins = "pytester" @pytest.mark.xfail def test_potato(): assert False """ ) result = testdir.runpytest() result.assert_outcomes(xfailed=1) assert result.ret == 0 def test_runresult_assertion_on_xpassed(testdir): testdir.makepyfile( """ import pytest pytest_plugins = "pytester" @pytest.mark.xfail def test_potato(): assert True """ ) result = testdir.runpytest() result.assert_outcomes(xpassed=1) assert result.ret == 0 def test_runresult_repr(): from _pytest.pytester import RunResult assert ( repr( RunResult(ret="ret", outlines=[""], errlines=["some", "errors"], duration=1) ) == "" ) def test_xpassed_with_strict_is_considered_a_failure(testdir): testdir.makepyfile( """ import pytest pytest_plugins = "pytester" @pytest.mark.xfail(strict=True) def test_potato(): assert True """ ) result = testdir.runpytest() result.assert_outcomes(failed=1) assert result.ret != 0 def make_holder(): class apiclass(object): def pytest_xyz(self, arg): "x" def pytest_xyz_noarg(self): "x" apimod = type(os)("api") def pytest_xyz(arg): "x" def pytest_xyz_noarg(): "x" apimod.pytest_xyz = pytest_xyz apimod.pytest_xyz_noarg = pytest_xyz_noarg return apiclass, apimod @pytest.mark.parametrize("holder", make_holder()) def test_hookrecorder_basic(holder): pm = PytestPluginManager() pm.add_hookspecs(holder) rec = HookRecorder(pm) pm.hook.pytest_xyz(arg=123) call = rec.popcall("pytest_xyz") assert call.arg == 123 assert call._name == "pytest_xyz" pytest.raises(pytest.fail.Exception, rec.popcall, "abc") pm.hook.pytest_xyz_noarg() call = rec.popcall("pytest_xyz_noarg") assert call._name == "pytest_xyz_noarg" def test_makepyfile_unicode(testdir): global unichr try: unichr(65) except NameError: unichr = chr testdir.makepyfile(unichr(0xFFFD)) def test_makepyfile_utf8(testdir): """Ensure makepyfile accepts utf-8 bytes as input (#2738)""" utf8_contents = u""" def setup_function(function): mixed_encoding = u'São Paulo' """.encode( "utf-8" ) p = testdir.makepyfile(utf8_contents) assert u"mixed_encoding = u'São Paulo'".encode("utf-8") in p.read("rb") class TestInlineRunModulesCleanup(object): def test_inline_run_test_module_not_cleaned_up(self, testdir): test_mod = testdir.makepyfile("def test_foo(): assert True") result = testdir.inline_run(str(test_mod)) assert result.ret == EXIT_OK # rewrite module, now test should fail if module was re-imported test_mod.write("def test_foo(): assert False") result2 = testdir.inline_run(str(test_mod)) assert result2.ret == EXIT_TESTSFAILED def spy_factory(self): class SysModulesSnapshotSpy(object): instances = [] def __init__(self, preserve=None): SysModulesSnapshotSpy.instances.append(self) self._spy_restore_count = 0 self._spy_preserve = preserve self.__snapshot = SysModulesSnapshot(preserve=preserve) def restore(self): self._spy_restore_count += 1 return self.__snapshot.restore() return SysModulesSnapshotSpy def test_inline_run_taking_and_restoring_a_sys_modules_snapshot( self, testdir, monkeypatch ): spy_factory = self.spy_factory() monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory) testdir.syspathinsert() original = dict(sys.modules) testdir.makepyfile(import1="# you son of a silly person") testdir.makepyfile(import2="# my hovercraft is full of eels") test_mod = testdir.makepyfile( """ import import1 def test_foo(): import import2""" ) testdir.inline_run(str(test_mod)) assert len(spy_factory.instances) == 1 spy = spy_factory.instances[0] assert spy._spy_restore_count == 1 assert sys.modules == original assert all(sys.modules[x] is original[x] for x in sys.modules) def test_inline_run_sys_modules_snapshot_restore_preserving_modules( self, testdir, monkeypatch ): spy_factory = self.spy_factory() monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory) test_mod = testdir.makepyfile("def test_foo(): pass") testdir.inline_run(str(test_mod)) spy = spy_factory.instances[0] assert not spy._spy_preserve("black_knight") assert spy._spy_preserve("zope") assert spy._spy_preserve("zope.interface") assert spy._spy_preserve("zopelicious") def test_external_test_module_imports_not_cleaned_up(self, testdir): testdir.syspathinsert() testdir.makepyfile(imported="data = 'you son of a silly person'") import imported test_mod = testdir.makepyfile( """ def test_foo(): import imported imported.data = 42""" ) testdir.inline_run(str(test_mod)) assert imported.data == 42 def test_assert_outcomes_after_pytest_error(testdir): testdir.makepyfile("def test_foo(): assert True") result = testdir.runpytest("--unexpected-argument") with pytest.raises(ValueError, match="Pytest terminal report not found"): result.assert_outcomes(passed=0) def test_cwd_snapshot(tmpdir): foo = tmpdir.ensure("foo", dir=1) bar = tmpdir.ensure("bar", dir=1) foo.chdir() snapshot = CwdSnapshot() bar.chdir() assert py.path.local() == bar snapshot.restore() assert py.path.local() == foo class TestSysModulesSnapshot(object): key = "my-test-module" def test_remove_added(self): original = dict(sys.modules) assert self.key not in sys.modules snapshot = SysModulesSnapshot() sys.modules[self.key] = "something" assert self.key in sys.modules snapshot.restore() assert sys.modules == original def test_add_removed(self, monkeypatch): assert self.key not in sys.modules monkeypatch.setitem(sys.modules, self.key, "something") assert self.key in sys.modules original = dict(sys.modules) snapshot = SysModulesSnapshot() del sys.modules[self.key] assert self.key not in sys.modules snapshot.restore() assert sys.modules == original def test_restore_reloaded(self, monkeypatch): assert self.key not in sys.modules monkeypatch.setitem(sys.modules, self.key, "something") assert self.key in sys.modules original = dict(sys.modules) snapshot = SysModulesSnapshot() sys.modules[self.key] = "something else" snapshot.restore() assert sys.modules == original def test_preserve_modules(self, monkeypatch): key = [self.key + str(i) for i in range(3)] assert not any(k in sys.modules for k in key) for i, k in enumerate(key): monkeypatch.setitem(sys.modules, k, "something" + str(i)) original = dict(sys.modules) def preserve(name): return name in (key[0], key[1], "some-other-key") snapshot = SysModulesSnapshot(preserve=preserve) sys.modules[key[0]] = original[key[0]] = "something else0" sys.modules[key[1]] = original[key[1]] = "something else1" sys.modules[key[2]] = "something else2" snapshot.restore() assert sys.modules == original def test_preserve_container(self, monkeypatch): original = dict(sys.modules) assert self.key not in original replacement = dict(sys.modules) replacement[self.key] = "life of brian" snapshot = SysModulesSnapshot() monkeypatch.setattr(sys, "modules", replacement) snapshot.restore() assert sys.modules is replacement assert sys.modules == original @pytest.mark.parametrize("path_type", ("path", "meta_path")) class TestSysPathsSnapshot(object): other_path = {"path": "meta_path", "meta_path": "path"} @staticmethod def path(n): return "my-dirty-little-secret-" + str(n) def test_restore(self, monkeypatch, path_type): other_path_type = self.other_path[path_type] for i in range(10): assert self.path(i) not in getattr(sys, path_type) sys_path = [self.path(i) for i in range(6)] monkeypatch.setattr(sys, path_type, sys_path) original = list(sys_path) original_other = list(getattr(sys, other_path_type)) snapshot = SysPathsSnapshot() transformation = {"source": (0, 1, 2, 3, 4, 5), "target": (6, 2, 9, 7, 5, 8)} assert sys_path == [self.path(x) for x in transformation["source"]] sys_path[1] = self.path(6) sys_path[3] = self.path(7) sys_path.append(self.path(8)) del sys_path[4] sys_path[3:3] = [self.path(9)] del sys_path[0] assert sys_path == [self.path(x) for x in transformation["target"]] snapshot.restore() assert getattr(sys, path_type) is sys_path assert getattr(sys, path_type) == original assert getattr(sys, other_path_type) == original_other def test_preserve_container(self, monkeypatch, path_type): other_path_type = self.other_path[path_type] original_data = list(getattr(sys, path_type)) original_other = getattr(sys, other_path_type) original_other_data = list(original_other) new = [] snapshot = SysPathsSnapshot() monkeypatch.setattr(sys, path_type, new) snapshot.restore() assert getattr(sys, path_type) is new assert getattr(sys, path_type) == original_data assert getattr(sys, other_path_type) is original_other assert getattr(sys, other_path_type) == original_other_data def test_testdir_subprocess(testdir): testfile = testdir.makepyfile("def test_one(): pass") assert testdir.runpytest_subprocess(testfile).ret == 0 def test_unicode_args(testdir): result = testdir.runpytest("-k", u"💩") assert result.ret == EXIT_NOTESTSCOLLECTED def test_testdir_run_no_timeout(testdir): testfile = testdir.makepyfile("def test_no_timeout(): pass") assert testdir.runpytest_subprocess(testfile).ret == EXIT_OK def test_testdir_run_with_timeout(testdir): testfile = testdir.makepyfile("def test_no_timeout(): pass") timeout = 120 start = time.time() result = testdir.runpytest_subprocess(testfile, timeout=timeout) end = time.time() duration = end - start assert result.ret == EXIT_OK assert duration < timeout def test_testdir_run_timeout_expires(testdir): testfile = testdir.makepyfile( """ import time def test_timeout(): time.sleep(10)""" ) with pytest.raises(testdir.TimeoutExpired): testdir.runpytest_subprocess(testfile, timeout=1) def test_linematcher_with_nonlist(): """Test LineMatcher with regard to passing in a set (accidentally).""" lm = LineMatcher([]) with pytest.raises(AssertionError): lm.fnmatch_lines(set()) with pytest.raises(AssertionError): lm.fnmatch_lines({}) lm.fnmatch_lines([]) lm.fnmatch_lines(()) assert lm._getlines({}) == {} assert lm._getlines(set()) == set() def test_pytester_addopts(request, monkeypatch): monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused") testdir = request.getfixturevalue("testdir") try: assert "PYTEST_ADDOPTS" not in os.environ finally: testdir.finalize() assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused" def test_run_stdin(testdir): with pytest.raises(testdir.TimeoutExpired): testdir.run( sys.executable, "-c", "import sys, time; time.sleep(1); print(sys.stdin.read())", stdin=subprocess.PIPE, timeout=0.1, ) with pytest.raises(testdir.TimeoutExpired): result = testdir.run( sys.executable, "-c", "import sys, time; time.sleep(1); print(sys.stdin.read())", stdin=b"input\n2ndline", timeout=0.1, ) result = testdir.run( sys.executable, "-c", "import sys; print(sys.stdin.read())", stdin=b"input\n2ndline", ) assert result.stdout.lines == ["input", "2ndline"] assert result.stderr.str() == "" assert result.ret == 0 def test_popen_stdin_pipe(testdir): proc = testdir.popen( [sys.executable, "-c", "import sys; print(sys.stdin.read())"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, ) stdin = b"input\n2ndline" stdout, stderr = proc.communicate(input=stdin) assert stdout.decode("utf8").splitlines() == ["input", "2ndline"] assert stderr == b"" assert proc.returncode == 0 def test_popen_stdin_bytes(testdir): proc = testdir.popen( [sys.executable, "-c", "import sys; print(sys.stdin.read())"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=b"input\n2ndline", ) stdout, stderr = proc.communicate() assert stdout.decode("utf8").splitlines() == ["input", "2ndline"] assert stderr == b"" assert proc.returncode == 0 def test_popen_default_stdin_stderr_and_stdin_None(testdir): # stdout, stderr default to pipes, # stdin can be None to not close the pipe, avoiding # "ValueError: flush of closed file" with `communicate()`. p1 = testdir.makepyfile( """ import sys print(sys.stdin.read()) # empty print('stdout') sys.stderr.write('stderr') """ ) proc = testdir.popen([sys.executable, str(p1)], stdin=None) stdout, stderr = proc.communicate(b"ignored") assert stdout.splitlines() == [b"", b"stdout"] assert stderr.splitlines() == [b"stderr"] assert proc.returncode == 0 def test_spawn_uses_tmphome(testdir): import os tmphome = str(testdir.tmpdir) # Does use HOME only during run. assert os.environ.get("HOME") != tmphome testdir._env_run_update["CUSTOMENV"] = "42" p1 = testdir.makepyfile( """ import os def test(): assert os.environ["HOME"] == {tmphome!r} assert os.environ["CUSTOMENV"] == "42" """.format( tmphome=tmphome ) ) child = testdir.spawn_pytest(str(p1)) out = child.read() assert child.wait() == 0, out.decode("utf8") pytest-4.6.9/testing/test_resultlog.py0000664000372000037200000001553513604173400021041 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import py import _pytest._code import pytest from _pytest.resultlog import pytest_configure from _pytest.resultlog import pytest_unconfigure from _pytest.resultlog import ResultLog pytestmark = pytest.mark.filterwarnings("ignore:--result-log is deprecated") def test_write_log_entry(): reslog = ResultLog(None, None) reslog.logfile = py.io.TextIO() reslog.write_log_entry("name", ".", "") entry = reslog.logfile.getvalue() assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 1 assert entry_lines[0] == ". name" reslog.logfile = py.io.TextIO() reslog.write_log_entry("name", "s", "Skipped") entry = reslog.logfile.getvalue() assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 2 assert entry_lines[0] == "s name" assert entry_lines[1] == " Skipped" reslog.logfile = py.io.TextIO() reslog.write_log_entry("name", "s", "Skipped\n") entry = reslog.logfile.getvalue() assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 2 assert entry_lines[0] == "s name" assert entry_lines[1] == " Skipped" reslog.logfile = py.io.TextIO() longrepr = " tb1\n tb 2\nE tb3\nSome Error" reslog.write_log_entry("name", "F", longrepr) entry = reslog.logfile.getvalue() assert entry[-1] == "\n" entry_lines = entry.splitlines() assert len(entry_lines) == 5 assert entry_lines[0] == "F name" assert entry_lines[1:] == [" " + line for line in longrepr.splitlines()] class TestWithFunctionIntegration(object): # XXX (hpk) i think that the resultlog plugin should # provide a Parser object so that one can remain # ignorant regarding formatting details. def getresultlog(self, testdir, arg): resultlog = testdir.tmpdir.join("resultlog") testdir.plugins.append("resultlog") args = ["--resultlog=%s" % resultlog] + [arg] testdir.runpytest(*args) return [x for x in resultlog.readlines(cr=0) if x] def test_collection_report(self, testdir): ok = testdir.makepyfile(test_collection_ok="") fail = testdir.makepyfile(test_collection_fail="XXX") lines = self.getresultlog(testdir, ok) assert not lines lines = self.getresultlog(testdir, fail) assert lines assert lines[0].startswith("F ") assert lines[0].endswith("test_collection_fail.py"), lines[0] for x in lines[1:]: assert x.startswith(" ") assert "XXX" in "".join(lines[1:]) def test_log_test_outcomes(self, testdir): mod = testdir.makepyfile( test_mod=""" import pytest def test_pass(): pass def test_skip(): pytest.skip("hello") def test_fail(): raise ValueError("FAIL") @pytest.mark.xfail def test_xfail(): raise ValueError("XFAIL") @pytest.mark.xfail def test_xpass(): pass """ ) lines = self.getresultlog(testdir, mod) assert len(lines) >= 3 assert lines[0].startswith(". ") assert lines[0].endswith("test_pass") assert lines[1].startswith("s "), lines[1] assert lines[1].endswith("test_skip") assert lines[2].find("hello") != -1 assert lines[3].startswith("F ") assert lines[3].endswith("test_fail") tb = "".join(lines[4:8]) assert tb.find('raise ValueError("FAIL")') != -1 assert lines[8].startswith("x ") tb = "".join(lines[8:14]) assert tb.find('raise ValueError("XFAIL")') != -1 assert lines[14].startswith("X ") assert len(lines) == 15 @pytest.mark.parametrize("style", ("native", "long", "short")) def test_internal_exception(self, style): # they are produced for example by a teardown failing # at the end of the run or a failing hook invocation try: raise ValueError except ValueError: excinfo = _pytest._code.ExceptionInfo.from_current() reslog = ResultLog(None, py.io.TextIO()) reslog.pytest_internalerror(excinfo.getrepr(style=style)) entry = reslog.logfile.getvalue() entry_lines = entry.splitlines() assert entry_lines[0].startswith("! ") if style != "native": assert os.path.basename(__file__)[:-9] in entry_lines[0] # .pyc/class assert entry_lines[-1][0] == " " assert "ValueError" in entry def test_generic(testdir, LineMatcher): testdir.plugins.append("resultlog") testdir.makepyfile( """ import pytest def test_pass(): pass def test_fail(): assert 0 def test_skip(): pytest.skip("") @pytest.mark.xfail def test_xfail(): assert 0 @pytest.mark.xfail(run=False) def test_xfail_norun(): assert 0 """ ) testdir.runpytest("--resultlog=result.log") lines = testdir.tmpdir.join("result.log").readlines(cr=0) LineMatcher(lines).fnmatch_lines( [ ". *:test_pass", "F *:test_fail", "s *:test_skip", "x *:test_xfail", "x *:test_xfail_norun", ] ) def test_makedir_for_resultlog(testdir, LineMatcher): """--resultlog should automatically create directories for the log file""" testdir.plugins.append("resultlog") testdir.makepyfile( """ import pytest def test_pass(): pass """ ) testdir.runpytest("--resultlog=path/to/result.log") lines = testdir.tmpdir.join("path/to/result.log").readlines(cr=0) LineMatcher(lines).fnmatch_lines([". *:test_pass"]) def test_no_resultlog_on_slaves(testdir): config = testdir.parseconfig("-p", "resultlog", "--resultlog=resultlog") assert not hasattr(config, "_resultlog") pytest_configure(config) assert hasattr(config, "_resultlog") pytest_unconfigure(config) assert not hasattr(config, "_resultlog") config.slaveinput = {} pytest_configure(config) assert not hasattr(config, "_resultlog") pytest_unconfigure(config) assert not hasattr(config, "_resultlog") def test_failure_issue380(testdir): testdir.makeconftest( """ import pytest class MyCollector(pytest.File): def collect(self): raise ValueError() def repr_failure(self, excinfo): return "somestring" def pytest_collect_file(path, parent): return MyCollector(parent=parent, fspath=path) """ ) testdir.makepyfile( """ def test_func(): pass """ ) result = testdir.runpytest("--resultlog=log") assert result.ret == 2 pytest-4.6.9/testing/example_scripts/0000775000372000037200000000000013604173421020604 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/fixtures/0000775000372000037200000000000013604173421022455 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/fixtures/custom_item/0000775000372000037200000000000013604173421025005 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/fixtures/custom_item/foo/0000775000372000037200000000000013604173421025570 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/fixtures/custom_item/foo/test_foo.py0000664000372000037200000000005513604173400027761 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test(): pass pytest-4.6.9/testing/example_scripts/fixtures/custom_item/foo/__init__.py0000664000372000037200000000000013604173400027664 0ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/fixtures/custom_item/conftest.py0000664000372000037200000000030513604173400027177 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest class CustomItem(pytest.Item, pytest.File): def runtest(self): pass def pytest_collect_file(path, parent): return CustomItem(path, parent) pytest-4.6.9/testing/example_scripts/fixtures/test_getfixturevalue_dynamic.py0000664000372000037200000000043213604173400031011 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def dynamic(): pass @pytest.fixture def a(request): request.getfixturevalue("dynamic") @pytest.fixture def b(a): pass def test(b, request): assert request.fixturenames == ["b", "request", "a", "dynamic"] pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/0000775000372000037200000000000013604173421025334 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000015500000000000011216 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000775000372000037200000000000013604173421035560 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000016200000000000011214 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000775000372000037200000000000013604173421035560 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000020100000000000011206 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/test_in_sub2.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000006313604173400035556 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_2(arg2): pass ././@LongLink0000000000000000000000000000017500000000000011220 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/__init__.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000000013604173400035545 0ustar travistravis00000000000000././@LongLink0000000000000000000000000000017500000000000011220 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/conftest.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000021113604173400035551 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def arg2(request): pytest.raises(Exception, request.getfixturevalue, "arg1") ././@LongLink0000000000000000000000000000016200000000000011214 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000775000372000037200000000000013604173421035560 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000020100000000000011206 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/test_in_sub1.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000006313604173400035556 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_1(arg1): pass ././@LongLink0000000000000000000000000000017500000000000011220 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/__init__.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000000013604173400035545 0ustar travistravis00000000000000././@LongLink0000000000000000000000000000017500000000000011220 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in0000664000372000037200000000022613604173400035557 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def arg1(request): with pytest.raises(Exception): request.getfixturevalue("arg2") pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_module_class.py0000664000372000037200000000037013604173400034531 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def spam(): return "spam" class TestSpam(object): @pytest.fixture def spam(self, spam): return spam * 2 def test_spam(self, spam): assert spam == "spamspam" pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookup_classlevel.py0000664000372000037200000000033713604173400034200 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest class TestClass(object): @pytest.fixture def something(self, request): return request.instance def test_method(self, something): assert something is self pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/0000775000372000037200000000000013604173421034702 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000020700000000000011214 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/test_extend_fixture_conftest_module.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/test0000664000372000037200000000022113604173400035574 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def spam(spam): return spam * 2 def test_spam(spam): assert spam == "spamspam" ././@LongLink0000000000000000000000000000015400000000000011215 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/conftest.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/conf0000664000372000037200000000012613604173400035546 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def spam(): return "spam" pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/0000775000372000037200000000000013604173421035242 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000014700000000000011217 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pk0000775000372000037200000000000013604173421035575 5ustar travistravis00000000000000././@LongLink0000000000000000000000000000016200000000000011214 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/__init__.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pk0000664000372000037200000000000013604173400035562 0ustar travistravis00000000000000././@LongLink0000000000000000000000000000016200000000000011214 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/conftest.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pk0000664000372000037200000000013413604173400035572 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def spam(spam): return spam * 2 ././@LongLink0000000000000000000000000000016300000000000011215 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/test_spam.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pk0000664000372000037200000000011313604173400035567 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_spam(spam): assert spam == "spamspam" ././@LongLink0000000000000000000000000000015600000000000011217 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/conftest.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/co0000664000372000037200000000012613604173400035562 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def spam(): return "spam" pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_funcarg_basic.py0000664000372000037200000000031313604173400031525 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def some(request): return request.function.__name__ @pytest.fixture def other(request): return 42 def test_func(some, other): pass pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookup_modulelevel.py0000664000372000037200000000044013604173400034353 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def something(request): return request.function.__name__ class TestClass(object): def test_method(self, something): assert something == "test_method" def test_func(something): assert something == "test_func" ././@LongLink0000000000000000000000000000014600000000000011216 Lustar 00000000000000pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_detect_recursive_dependency_error.pypytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_detect_recursive_dependency_error.p0000664000372000037200000000023713604173400035521 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def fix1(fix2): return 1 @pytest.fixture def fix2(fix1): return 1 def test(fix1): pass pytest-4.6.9/testing/example_scripts/fixtures/fill_fixtures/test_funcarg_lookupfails.py0000664000372000037200000000020113604173400032770 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def xyzsomething(request): return 42 def test_func(some): pass pytest-4.6.9/testing/example_scripts/conftest_usageerror/0000775000372000037200000000000013604173421024667 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/conftest_usageerror/conftest.py0000664000372000037200000000026713604173400027070 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def pytest_configure(config): import pytest raise pytest.UsageError("hello") def pytest_unconfigure(config): print("pytest_unconfigure_called") pytest-4.6.9/testing/example_scripts/tmpdir/0000775000372000037200000000000013604173421022103 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/tmpdir/tmpdir_fixture.py0000664000372000037200000000025413604173400025520 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.mark.parametrize("a", [r"qwe/\abc"]) def test_fixture(tmpdir, a): tmpdir.check(dir=1) assert tmpdir.listdir() == [] pytest-4.6.9/testing/example_scripts/dataclasses/0000775000372000037200000000000013604173421023073 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py0000664000372000037200000000051713604173400032226 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from dataclasses import dataclass from dataclasses import field def test_dataclasses_verbose(): @dataclass class SimpleDataObject(object): field_a: int = field() field_b: int = field() left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "c") assert left == right pytest-4.6.9/testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py0000664000372000037200000000073613604173400033423 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from dataclasses import dataclass from dataclasses import field def test_comparing_two_different_data_classes(): @dataclass class SimpleDataObjectOne(object): field_a: int = field() field_b: int = field() @dataclass class SimpleDataObjectTwo(object): field_a: int = field() field_b: int = field() left = SimpleDataObjectOne(1, "b") right = SimpleDataObjectTwo(1, "c") assert left != right pytest-4.6.9/testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py0000664000372000037200000000056213604173400034730 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from dataclasses import dataclass from dataclasses import field def test_dataclasses_with_attribute_comparison_off(): @dataclass class SimpleDataObject(object): field_a: int = field() field_b: int = field(compare=False) left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "c") assert left == right pytest-4.6.9/testing/example_scripts/dataclasses/test_compare_dataclasses.py0000664000372000037200000000050713604173400030500 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from dataclasses import dataclass from dataclasses import field def test_dataclasses(): @dataclass class SimpleDataObject(object): field_a: int = field() field_b: int = field() left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "c") assert left == right pytest-4.6.9/testing/example_scripts/acceptance/0000775000372000037200000000000013604173421022672 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/acceptance/fixture_mock_integration.py0000664000372000037200000000055713604173400030352 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """Reproduces issue #3774""" try: import mock except ImportError: import unittest.mock as mock import pytest config = {"mykey": "ORIGINAL"} @pytest.fixture(scope="function") @mock.patch.dict(config, {"mykey": "MOCKED"}) def my_fixture(): return config["mykey"] def test_foobar(my_fixture): assert my_fixture == "MOCKED" pytest-4.6.9/testing/example_scripts/deprecated/0000775000372000037200000000000013604173421022704 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/deprecated/test_fixture_named_request.py0000664000372000037200000000014713604173400030716 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture def request(): pass def test(): pass pytest-4.6.9/testing/example_scripts/warnings/0000775000372000037200000000000013604173421022434 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/warnings/test_group_warnings_by_message.py0000664000372000037200000000031613604173400031304 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import warnings import pytest def func(): warnings.warn(UserWarning("foo")) @pytest.mark.parametrize("i", range(5)) def test_foo(i): func() def test_bar(): func() pytest-4.6.9/testing/example_scripts/marks/0000775000372000037200000000000013604173421021721 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/marks/marks_considered_keywords/0000775000372000037200000000000013604173421027164 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/marks/marks_considered_keywords/conftest.py0000664000372000037200000000000013604173400031346 0ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/marks/marks_considered_keywords/test_marks_as_keywords.py0000664000372000037200000000012313604173400034315 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.mark.foo def test_mark(): pass pytest-4.6.9/testing/example_scripts/collect/0000775000372000037200000000000013604173421022231 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/collect_init_tests/0000775000372000037200000000000013604173421026123 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/collect_init_tests/pytest.ini0000664000372000037200000000003513604173400030147 0ustar travistravis00000000000000[pytest] python_files = *.py pytest-4.6.9/testing/example_scripts/collect/collect_init_tests/tests/0000775000372000037200000000000013604173421027265 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/collect_init_tests/tests/test_foo.py0000664000372000037200000000006113604173400031453 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_foo(): pass pytest-4.6.9/testing/example_scripts/collect/collect_init_tests/tests/__init__.py0000664000372000037200000000006213604173400031371 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_init(): pass pytest-4.6.9/testing/example_scripts/collect/package_infinite_recursion/0000775000372000037200000000000013604173421027602 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/package_infinite_recursion/conftest.py0000664000372000037200000000011213604173400031770 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def pytest_ignore_collect(path): return False pytest-4.6.9/testing/example_scripts/collect/package_infinite_recursion/tests/0000775000372000037200000000000013604173421030744 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/package_infinite_recursion/tests/test_basic.py0000664000372000037200000000005513604173400033433 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test(): pass pytest-4.6.9/testing/example_scripts/collect/package_infinite_recursion/tests/__init__.py0000664000372000037200000000000013604173400033040 0ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/package_init_given_as_arg/0000775000372000037200000000000013604173421027353 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/package_init_given_as_arg/pkg/0000775000372000037200000000000013604173421030134 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/collect/package_init_given_as_arg/pkg/test_foo.py0000664000372000037200000000005513604173400032325 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test(): pass pytest-4.6.9/testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py0000664000372000037200000000000013604173400032230 0ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/issue88_initial_file_multinodes/0000775000372000037200000000000013604173421027067 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/issue88_initial_file_multinodes/test_hello.py0000664000372000037200000000006313604173400031577 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_hello(): pass pytest-4.6.9/testing/example_scripts/issue88_initial_file_multinodes/conftest.py0000664000372000037200000000036713604173400031271 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest class MyFile(pytest.File): def collect(self): return [MyItem("hello", parent=self)] def pytest_collect_file(path, parent): return MyFile(path, parent) class MyItem(pytest.Item): pass pytest-4.6.9/testing/example_scripts/issue_519.py0000664000372000037200000000265113604173400022705 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pprint import pytest def pytest_generate_tests(metafunc): if "arg1" in metafunc.fixturenames: metafunc.parametrize("arg1", ["arg1v1", "arg1v2"], scope="module") if "arg2" in metafunc.fixturenames: metafunc.parametrize("arg2", ["arg2v1", "arg2v2"], scope="function") @pytest.fixture(scope="session") def checked_order(): order = [] yield order pprint.pprint(order) assert order == [ ("testing/example_scripts/issue_519.py", "fix1", "arg1v1"), ("test_one[arg1v1-arg2v1]", "fix2", "arg2v1"), ("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"), ("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v1-arg2v2]", "fix2", "arg2v2"), ("testing/example_scripts/issue_519.py", "fix1", "arg1v2"), ("test_one[arg1v2-arg2v1]", "fix2", "arg2v1"), ("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"), ("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"), ("test_two[arg1v2-arg2v2]", "fix2", "arg2v2"), ] @pytest.yield_fixture(scope="module") def fix1(request, arg1, checked_order): checked_order.append((request.node.name, "fix1", arg1)) yield "fix1-" + arg1 @pytest.yield_fixture(scope="function") def fix2(request, fix1, arg2, checked_order): checked_order.append((request.node.name, "fix2", arg2)) yield "fix2-" + arg2 + fix1 def test_one(fix2): pass def test_two(fix2): pass pytest-4.6.9/testing/example_scripts/README.rst0000664000372000037200000000064413604173400022274 0ustar travistravis00000000000000Example test scripts ===================== The files in this folder are not direct tests, but rather example test suites that demonstrate certain issues/behaviours. In the future we will move part of the content of the acceptance tests here in order to have directly testable code instead of writing out things and then running them in nested pytest sessions/subprocesses. This will aid debugging and comprehension. pytest-4.6.9/testing/example_scripts/config/0000775000372000037200000000000013604173421022051 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/config/collect_pytest_prefix/0000775000372000037200000000000013604173421026463 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/config/collect_pytest_prefix/test_foo.py0000664000372000037200000000006113604173400030651 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_foo(): pass pytest-4.6.9/testing/example_scripts/config/collect_pytest_prefix/conftest.py0000664000372000037200000000010113604173400030647 0ustar travistravis00000000000000# -*- coding: utf-8 -*- class pytest_something(object): pass pytest-4.6.9/testing/example_scripts/perf_examples/0000775000372000037200000000000013604173421023436 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/perf_examples/collect_stats/0000775000372000037200000000000013604173421026301 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/perf_examples/collect_stats/.gitignore0000664000372000037200000000000613604173400030262 0ustar travistravis00000000000000foo_* pytest-4.6.9/testing/example_scripts/perf_examples/collect_stats/template_test.py0000664000372000037200000000005713604173400031524 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_x(): pass pytest-4.6.9/testing/example_scripts/perf_examples/collect_stats/generate_folders.py0000664000372000037200000000155713604173400032170 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import argparse import pathlib HERE = pathlib.Path(__file__).parent TEST_CONTENT = (HERE / "template_test.py").read_bytes() parser = argparse.ArgumentParser() parser.add_argument("numbers", nargs="*", type=int) def generate_folders(root, elements, *more_numbers): fill_len = len(str(elements)) if more_numbers: for i in range(elements): new_folder = root.joinpath(f"foo_{i:0>{fill_len}}") new_folder.mkdir() new_folder.joinpath("__init__.py").write_bytes(TEST_CONTENT) generate_folders(new_folder, *more_numbers) else: for i in range(elements): new_test = root.joinpath(f"test_{i:0<{fill_len}}.py") new_test.write_bytes(TEST_CONTENT) if __name__ == "__main__": args = parser.parse_args() generate_folders(HERE, *(args.numbers or (10, 100))) pytest-4.6.9/testing/example_scripts/unittest/0000775000372000037200000000000013604173421022463 5ustar travistravis00000000000000pytest-4.6.9/testing/example_scripts/unittest/test_setup_skip_class.py0000664000372000037200000000051113604173400027441 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """Skipping an entire subclass with unittest.skip() should *not* call setUpClass from a base class.""" import unittest class Base(unittest.TestCase): @classmethod def setUpClass(cls): assert 0 @unittest.skip("skip all tests") class Test(Base): def test_foo(self): assert 0 pytest-4.6.9/testing/example_scripts/unittest/test_setup_skip.py0000664000372000037200000000045713604173400026265 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """Skipping an entire subclass with unittest.skip() should *not* call setUp from a base class.""" import unittest class Base(unittest.TestCase): def setUp(self): assert 0 @unittest.skip("skip all tests") class Test(Base): def test_foo(self): assert 0 pytest-4.6.9/testing/example_scripts/unittest/test_parametrized_fixture_error_message.py0000664000372000037200000000036613604173400033250 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import unittest import pytest @pytest.fixture(params=[1, 2]) def two(request): return request.param @pytest.mark.usefixtures("two") class TestSomethingElse(unittest.TestCase): def test_two(self): pass pytest-4.6.9/testing/example_scripts/unittest/test_setup_skip_module.py0000664000372000037200000000040113604173400027617 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """setUpModule is always called, even if all tests in the module are skipped""" import unittest def setUpModule(): assert 0 @unittest.skip("skip all tests") class Base(unittest.TestCase): def test(self): assert 0 pytest-4.6.9/testing/test_modimport.py0000664000372000037200000000171513604173400021026 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import subprocess import sys import py import _pytest import pytest pytestmark = pytest.mark.slow MODSET = [ x for x in py.path.local(_pytest.__file__).dirpath().visit("*.py") if x.purebasename != "__init__" ] @pytest.mark.parametrize("modfile", MODSET, ids=lambda x: x.purebasename) def test_fileimport(modfile): # this test ensures all internal packages can import # without needing the pytest namespace being set # this is critical for the initialization of xdist p = subprocess.Popen( [ sys.executable, "-c", "import sys, py; py.path.local(sys.argv[1]).pyimport()", modfile.strpath, ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) (out, err) = p.communicate() assert p.returncode == 0, "importing %s failed (exitcode %d): out=%r, err=%r" % ( modfile, p.returncode, out, err, ) pytest-4.6.9/testing/test_runner_xunit.py0000664000372000037200000001675413604173400021565 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ test correct setup/teardowns at module, class, and instance level """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import pytest def test_module_and_function_setup(testdir): reprec = testdir.inline_runsource( """ modlevel = [] def setup_module(module): assert not modlevel module.modlevel.append(42) def teardown_module(module): modlevel.pop() def setup_function(function): function.answer = 17 def teardown_function(function): del function.answer def test_modlevel(): assert modlevel[0] == 42 assert test_modlevel.answer == 17 class TestFromClass(object): def test_module(self): assert modlevel[0] == 42 assert not hasattr(test_modlevel, 'answer') """ ) rep = reprec.matchreport("test_modlevel") assert rep.passed rep = reprec.matchreport("test_module") assert rep.passed def test_module_setup_failure_no_teardown(testdir): reprec = testdir.inline_runsource( """ values = [] def setup_module(module): values.append(1) 0/0 def test_nothing(): pass def teardown_module(module): values.append(2) """ ) reprec.assertoutcome(failed=1) calls = reprec.getcalls("pytest_runtest_setup") assert calls[0].item.module.values == [1] def test_setup_function_failure_no_teardown(testdir): reprec = testdir.inline_runsource( """ modlevel = [] def setup_function(function): modlevel.append(1) 0/0 def teardown_function(module): modlevel.append(2) def test_func(): pass """ ) calls = reprec.getcalls("pytest_runtest_setup") assert calls[0].item.module.modlevel == [1] def test_class_setup(testdir): reprec = testdir.inline_runsource( """ class TestSimpleClassSetup(object): clslevel = [] def setup_class(cls): cls.clslevel.append(23) def teardown_class(cls): cls.clslevel.pop() def test_classlevel(self): assert self.clslevel[0] == 23 class TestInheritedClassSetupStillWorks(TestSimpleClassSetup): def test_classlevel_anothertime(self): assert self.clslevel == [23] def test_cleanup(): assert not TestSimpleClassSetup.clslevel assert not TestInheritedClassSetupStillWorks.clslevel """ ) reprec.assertoutcome(passed=1 + 2 + 1) def test_class_setup_failure_no_teardown(testdir): reprec = testdir.inline_runsource( """ class TestSimpleClassSetup(object): clslevel = [] def setup_class(cls): 0/0 def teardown_class(cls): cls.clslevel.append(1) def test_classlevel(self): pass def test_cleanup(): assert not TestSimpleClassSetup.clslevel """ ) reprec.assertoutcome(failed=1, passed=1) def test_method_setup(testdir): reprec = testdir.inline_runsource( """ class TestSetupMethod(object): def setup_method(self, meth): self.methsetup = meth def teardown_method(self, meth): del self.methsetup def test_some(self): assert self.methsetup == self.test_some def test_other(self): assert self.methsetup == self.test_other """ ) reprec.assertoutcome(passed=2) def test_method_setup_failure_no_teardown(testdir): reprec = testdir.inline_runsource( """ class TestMethodSetup(object): clslevel = [] def setup_method(self, method): self.clslevel.append(1) 0/0 def teardown_method(self, method): self.clslevel.append(2) def test_method(self): pass def test_cleanup(): assert TestMethodSetup.clslevel == [1] """ ) reprec.assertoutcome(failed=1, passed=1) def test_method_setup_uses_fresh_instances(testdir): reprec = testdir.inline_runsource( """ class TestSelfState1(object): memory = [] def test_hello(self): self.memory.append(self) def test_afterhello(self): assert self != self.memory[0] """ ) reprec.assertoutcome(passed=2, failed=0) def test_setup_that_skips_calledagain(testdir): p = testdir.makepyfile( """ import pytest def setup_module(mod): pytest.skip("x") def test_function1(): pass def test_function2(): pass """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(skipped=2) def test_setup_fails_again_on_all_tests(testdir): p = testdir.makepyfile( """ import pytest def setup_module(mod): raise ValueError(42) def test_function1(): pass def test_function2(): pass """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(failed=2) def test_setup_funcarg_setup_when_outer_scope_fails(testdir): p = testdir.makepyfile( """ import pytest def setup_module(mod): raise ValueError(42) @pytest.fixture def hello(request): raise ValueError("xyz43") def test_function1(hello): pass def test_function2(hello): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*function1*", "*ValueError*42*", "*function2*", "*ValueError*42*", "*2 error*", ] ) assert "xyz43" not in result.stdout.str() @pytest.mark.parametrize("arg", ["", "arg"]) def test_setup_teardown_function_level_with_optional_argument( testdir, monkeypatch, arg ): """parameter to setup/teardown xunit-style functions parameter is now optional (#1728).""" import sys trace_setups_teardowns = [] monkeypatch.setattr( sys, "trace_setups_teardowns", trace_setups_teardowns, raising=False ) p = testdir.makepyfile( """ import pytest import sys trace = sys.trace_setups_teardowns.append def setup_module({arg}): trace('setup_module') def teardown_module({arg}): trace('teardown_module') def setup_function({arg}): trace('setup_function') def teardown_function({arg}): trace('teardown_function') def test_function_1(): pass def test_function_2(): pass class Test(object): def setup_method(self, {arg}): trace('setup_method') def teardown_method(self, {arg}): trace('teardown_method') def test_method_1(self): pass def test_method_2(self): pass """.format( arg=arg ) ) result = testdir.inline_run(p) result.assertoutcome(passed=4) expected = [ "setup_module", "setup_function", "teardown_function", "setup_function", "teardown_function", "setup_method", "teardown_method", "setup_method", "teardown_method", "teardown_module", ] assert trace_setups_teardowns == expected pytest-4.6.9/testing/test_assertrewrite.py0000664000372000037200000012200413604173400021712 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import glob import os import py_compile import stat import sys import textwrap import zipfile import py import six import _pytest._code import pytest from _pytest.assertion import util from _pytest.assertion.rewrite import AssertionRewritingHook from _pytest.assertion.rewrite import PYTEST_TAG from _pytest.assertion.rewrite import rewrite_asserts from _pytest.main import EXIT_NOTESTSCOLLECTED ast = pytest.importorskip("ast") if sys.platform.startswith("java"): # XXX should be xfail pytest.skip("assert rewrite does currently not work on jython") def setup_module(mod): mod._old_reprcompare = util._reprcompare _pytest._code._reprcompare = None def teardown_module(mod): util._reprcompare = mod._old_reprcompare del mod._old_reprcompare def rewrite(src): tree = ast.parse(src) rewrite_asserts(tree) return tree def getmsg(f, extra_ns=None, must_pass=False): """Rewrite the assertions in f, run it, and get the failure message.""" src = "\n".join(_pytest._code.Code(f).source().lines) mod = rewrite(src) code = compile(mod, "", "exec") ns = {} if extra_ns is not None: ns.update(extra_ns) exec(code, ns) func = ns[f.__name__] try: func() except AssertionError: if must_pass: pytest.fail("shouldn't have raised") s = six.text_type(sys.exc_info()[1]) if not s.startswith("assert"): return "AssertionError: " + s return s else: if not must_pass: pytest.fail("function didn't raise at all") class TestAssertionRewrite(object): def test_place_initial_imports(self): s = """'Doc string'\nother = stuff""" m = rewrite(s) assert isinstance(m.body[0], ast.Expr) for imp in m.body[1:3]: assert isinstance(imp, ast.Import) assert imp.lineno == 2 assert imp.col_offset == 0 assert isinstance(m.body[3], ast.Assign) s = """from __future__ import division\nother_stuff""" m = rewrite(s) assert isinstance(m.body[0], ast.ImportFrom) for imp in m.body[1:3]: assert isinstance(imp, ast.Import) assert imp.lineno == 2 assert imp.col_offset == 0 assert isinstance(m.body[3], ast.Expr) s = """'doc string'\nfrom __future__ import division""" m = rewrite(s) assert isinstance(m.body[0], ast.Expr) assert isinstance(m.body[1], ast.ImportFrom) for imp in m.body[2:4]: assert isinstance(imp, ast.Import) assert imp.lineno == 2 assert imp.col_offset == 0 s = """'doc string'\nfrom __future__ import division\nother""" m = rewrite(s) assert isinstance(m.body[0], ast.Expr) assert isinstance(m.body[1], ast.ImportFrom) for imp in m.body[2:4]: assert isinstance(imp, ast.Import) assert imp.lineno == 3 assert imp.col_offset == 0 assert isinstance(m.body[4], ast.Expr) s = """from . import relative\nother_stuff""" m = rewrite(s) for imp in m.body[:2]: assert isinstance(imp, ast.Import) assert imp.lineno == 1 assert imp.col_offset == 0 assert isinstance(m.body[3], ast.Expr) def test_dont_rewrite(self): s = """'PYTEST_DONT_REWRITE'\nassert 14""" m = rewrite(s) assert len(m.body) == 2 assert m.body[1].msg is None def test_dont_rewrite_plugin(self, testdir): contents = { "conftest.py": "pytest_plugins = 'plugin'; import plugin", "plugin.py": "'PYTEST_DONT_REWRITE'", "test_foo.py": "def test_foo(): pass", } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess() assert "warnings" not in "".join(result.outlines) def test_name(self, request): def f(): assert False assert getmsg(f) == "assert False" def f(): f = False assert f assert getmsg(f) == "assert False" def f(): assert a_global # noqa assert getmsg(f, {"a_global": False}) == "assert False" def f(): assert sys == 42 verbose = request.config.getoption("verbose") msg = getmsg(f, {"sys": sys}) if verbose > 0: assert msg == ( "assert == 42\n" " -\n" " +42" ) else: assert msg == "assert sys == 42" def f(): assert cls == 42 # noqa: F821 class X(object): pass msg = getmsg(f, {"cls": X}).splitlines() if verbose > 0: if six.PY2: assert msg == [ "assert == 42", " -", " +42", ] else: assert msg == [ "assert .X'> == 42", " -.X'>", " +42", ] else: assert msg == ["assert cls == 42"] def test_dont_rewrite_if_hasattr_fails(self, request): class Y(object): """ A class whos getattr fails, but not with `AttributeError` """ def __getattr__(self, attribute_name): raise KeyError() def __repr__(self): return "Y" def __init__(self): self.foo = 3 def f(): assert cls().foo == 2 # noqa # XXX: looks like the "where" should also be there in verbose mode?! message = getmsg(f, {"cls": Y}).splitlines() if request.config.getoption("verbose") > 0: assert message == ["assert 3 == 2", " -3", " +2"] else: assert message == [ "assert 3 == 2", " + where 3 = Y.foo", " + where Y = cls()", ] def test_assert_already_has_message(self): def f(): assert False, "something bad!" assert getmsg(f) == "AssertionError: something bad!\nassert False" def test_assertion_message(self, testdir): testdir.makepyfile( """ def test_foo(): assert 1 == 2, "The failure message" """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines( ["*AssertionError*The failure message*", "*assert 1 == 2*"] ) def test_assertion_message_multiline(self, testdir): testdir.makepyfile( """ def test_foo(): assert 1 == 2, "A multiline\\nfailure message" """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines( ["*AssertionError*A multiline*", "*failure message*", "*assert 1 == 2*"] ) def test_assertion_message_tuple(self, testdir): testdir.makepyfile( """ def test_foo(): assert 1 == 2, (1, 2) """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines( ["*AssertionError*%s*" % repr((1, 2)), "*assert 1 == 2*"] ) def test_assertion_message_expr(self, testdir): testdir.makepyfile( """ def test_foo(): assert 1 == 2, 1 + 2 """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines(["*AssertionError*3*", "*assert 1 == 2*"]) def test_assertion_message_escape(self, testdir): testdir.makepyfile( """ def test_foo(): assert 1 == 2, 'To be escaped: %' """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines( ["*AssertionError: To be escaped: %", "*assert 1 == 2"] ) @pytest.mark.skipif( sys.version_info < (3,), reason="bytes is a string type in python 2" ) def test_assertion_messages_bytes(self, testdir): testdir.makepyfile("def test_bytes_assertion():\n assert False, b'ohai!'\n") result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines(["*AssertionError: b'ohai!'", "*assert False"]) def test_boolop(self): def f(): f = g = False assert f and g assert getmsg(f) == "assert (False)" def f(): f = True g = False assert f and g assert getmsg(f) == "assert (True and False)" def f(): f = False g = True assert f and g assert getmsg(f) == "assert (False)" def f(): f = g = False assert f or g assert getmsg(f) == "assert (False or False)" def f(): f = g = False assert not f and not g getmsg(f, must_pass=True) def x(): return False def f(): assert x() and x() assert ( getmsg(f, {"x": x}) == """assert (False) + where False = x()""" ) def f(): assert False or x() assert ( getmsg(f, {"x": x}) == """assert (False or False) + where False = x()""" ) def f(): assert 1 in {} and 2 in {} assert getmsg(f) == "assert (1 in {})" def f(): x = 1 y = 2 assert x in {1: None} and y in {} assert getmsg(f) == "assert (1 in {1: None} and 2 in {})" def f(): f = True g = False assert f or g getmsg(f, must_pass=True) def f(): f = g = h = lambda: True assert f() and g() and h() getmsg(f, must_pass=True) def test_short_circuit_evaluation(self): def f(): assert True or explode # noqa getmsg(f, must_pass=True) def f(): x = 1 assert x == 1 or x == 2 getmsg(f, must_pass=True) def test_unary_op(self): def f(): x = True assert not x assert getmsg(f) == "assert not True" def f(): x = 0 assert ~x + 1 assert getmsg(f) == "assert (~0 + 1)" def f(): x = 3 assert -x + x assert getmsg(f) == "assert (-3 + 3)" def f(): x = 0 assert +x + x assert getmsg(f) == "assert (+0 + 0)" def test_binary_op(self): def f(): x = 1 y = -1 assert x + y assert getmsg(f) == "assert (1 + -1)" def f(): assert not 5 % 4 assert getmsg(f) == "assert not (5 % 4)" def test_boolop_percent(self): def f(): assert 3 % 2 and False assert getmsg(f) == "assert ((3 % 2) and False)" def f(): assert False or 4 % 2 assert getmsg(f) == "assert (False or (4 % 2))" @pytest.mark.skipif("sys.version_info < (3,5)") def test_at_operator_issue1290(self, testdir): testdir.makepyfile( """ class Matrix(object): def __init__(self, num): self.num = num def __matmul__(self, other): return self.num * other.num def test_multmat_operator(): assert Matrix(2) @ Matrix(3) == 6""" ) testdir.runpytest().assert_outcomes(passed=1) @pytest.mark.skipif("sys.version_info < (3,5)") def test_starred_with_side_effect(self, testdir): """See #4412""" testdir.makepyfile( """\ def test(): f = lambda x: x x = iter([1, 2, 3]) assert 2 * next(x) == f(*[next(x)]) """ ) testdir.runpytest().assert_outcomes(passed=1) def test_call(self): def g(a=42, *args, **kwargs): return False ns = {"g": g} def f(): assert g() assert ( getmsg(f, ns) == """assert False + where False = g()""" ) def f(): assert g(1) assert ( getmsg(f, ns) == """assert False + where False = g(1)""" ) def f(): assert g(1, 2) assert ( getmsg(f, ns) == """assert False + where False = g(1, 2)""" ) def f(): assert g(1, g=42) assert ( getmsg(f, ns) == """assert False + where False = g(1, g=42)""" ) def f(): assert g(1, 3, g=23) assert ( getmsg(f, ns) == """assert False + where False = g(1, 3, g=23)""" ) def f(): seq = [1, 2, 3] assert g(*seq) assert ( getmsg(f, ns) == """assert False + where False = g(*[1, 2, 3])""" ) def f(): x = "a" assert g(**{x: 2}) assert ( getmsg(f, ns) == """assert False + where False = g(**{'a': 2})""" ) def test_attribute(self): class X(object): g = 3 ns = {"x": X} def f(): assert not x.g # noqa assert ( getmsg(f, ns) == """assert not 3 + where 3 = x.g""" ) def f(): x.a = False # noqa assert x.a # noqa assert ( getmsg(f, ns) == """assert False + where False = x.a""" ) def test_comparisons(self): def f(): a, b = range(2) assert b < a assert getmsg(f) == """assert 1 < 0""" def f(): a, b, c = range(3) assert a > b > c assert getmsg(f) == """assert 0 > 1""" def f(): a, b, c = range(3) assert a < b > c assert getmsg(f) == """assert 1 > 2""" def f(): a, b, c = range(3) assert a < b <= c getmsg(f, must_pass=True) def f(): a, b, c = range(3) assert a < b assert b < c getmsg(f, must_pass=True) def test_len(self, request): def f(): values = list(range(10)) assert len(values) == 11 msg = getmsg(f) if request.config.getoption("verbose") > 0: assert msg == "assert 10 == 11\n -10\n +11" else: assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])" def test_custom_reprcompare(self, monkeypatch): def my_reprcompare(op, left, right): return "42" monkeypatch.setattr(util, "_reprcompare", my_reprcompare) def f(): assert 42 < 3 assert getmsg(f) == "assert 42" def my_reprcompare(op, left, right): return "{} {} {}".format(left, op, right) monkeypatch.setattr(util, "_reprcompare", my_reprcompare) def f(): assert 1 < 3 < 5 <= 4 < 7 assert getmsg(f) == "assert 5 <= 4" def test_assert_raising_nonzero_in_comparison(self): def f(): class A(object): def __nonzero__(self): raise ValueError(42) def __lt__(self, other): return A() def __repr__(self): return "" def myany(x): return False assert myany(A() < 0) assert " < 0" in getmsg(f) def test_formatchar(self): def f(): assert "%test" == "test" assert getmsg(f).startswith("assert '%test' == 'test'") def test_custom_repr(self, request): def f(): class Foo(object): a = 1 def __repr__(self): return "\n{ \n~ \n}" f = Foo() assert 0 == f.a lines = util._format_lines([getmsg(f)]) if request.config.getoption("verbose") > 0: assert lines == ["assert 0 == 1\n -0\n +1"] else: assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"] def test_custom_repr_non_ascii(self): def f(): class A(object): name = u"ä" def __repr__(self): return self.name.encode("UTF-8") # only legal in python2 a = A() assert not a.name msg = getmsg(f) assert "UnicodeDecodeError" not in msg assert "UnicodeEncodeError" not in msg class TestRewriteOnImport(object): def test_pycache_is_a_file(self, testdir): testdir.tmpdir.join("__pycache__").write("Hello") testdir.makepyfile( """ def test_rewritten(): assert "@py_builtins" in globals()""" ) assert testdir.runpytest().ret == 0 def test_pycache_is_readonly(self, testdir): cache = testdir.tmpdir.mkdir("__pycache__") old_mode = cache.stat().mode cache.chmod(old_mode ^ stat.S_IWRITE) testdir.makepyfile( """ def test_rewritten(): assert "@py_builtins" in globals()""" ) try: assert testdir.runpytest().ret == 0 finally: cache.chmod(old_mode) def test_zipfile(self, testdir): z = testdir.tmpdir.join("myzip.zip") z_fn = str(z) f = zipfile.ZipFile(z_fn, "w") try: f.writestr("test_gum/__init__.py", "") f.writestr("test_gum/test_lizard.py", "") finally: f.close() z.chmod(256) testdir.makepyfile( """ import sys sys.path.append(%r) import test_gum.test_lizard""" % (z_fn,) ) assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED def test_readonly(self, testdir): sub = testdir.mkdir("testing") sub.join("test_readonly.py").write( b""" def test_rewritten(): assert "@py_builtins" in globals() """, "wb", ) old_mode = sub.stat().mode sub.chmod(320) try: assert testdir.runpytest().ret == 0 finally: sub.chmod(old_mode) def test_dont_write_bytecode(self, testdir, monkeypatch): testdir.makepyfile( """ import os def test_no_bytecode(): assert "__pycache__" in __cached__ assert not os.path.exists(__cached__) assert not os.path.exists(os.path.dirname(__cached__))""" ) monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") assert testdir.runpytest_subprocess().ret == 0 def test_orphaned_pyc_file(self, testdir): if sys.version_info < (3, 0) and hasattr(sys, "pypy_version_info"): pytest.skip("pypy2 doesn't run orphaned pyc files") testdir.makepyfile( """ import orphan def test_it(): assert orphan.value == 17 """ ) testdir.makepyfile( orphan=""" value = 17 """ ) py_compile.compile("orphan.py") os.remove("orphan.py") # Python 3 puts the .pyc files in a __pycache__ directory, and will # not import from there without source. It will import a .pyc from # the source location though. if not os.path.exists("orphan.pyc"): pycs = glob.glob("__pycache__/orphan.*.pyc") assert len(pycs) == 1 os.rename(pycs[0], "orphan.pyc") assert testdir.runpytest().ret == 0 @pytest.mark.skipif('"__pypy__" in sys.modules') def test_pyc_vs_pyo(self, testdir, monkeypatch): testdir.makepyfile( """ import pytest def test_optimized(): "hello" assert test_optimized.__doc__ is None""" ) p = py.path.local.make_numbered_dir( prefix="runpytest-", keep=None, rootdir=testdir.tmpdir ) tmp = "--basetemp=%s" % p monkeypatch.setenv("PYTHONOPTIMIZE", "2") monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) assert testdir.runpytest_subprocess(tmp).ret == 0 tagged = "test_pyc_vs_pyo." + PYTEST_TAG assert tagged + ".pyo" in os.listdir("__pycache__") monkeypatch.undo() monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) assert testdir.runpytest_subprocess(tmp).ret == 1 assert tagged + ".pyc" in os.listdir("__pycache__") def test_package(self, testdir): pkg = testdir.tmpdir.join("pkg") pkg.mkdir() pkg.join("__init__.py").ensure() pkg.join("test_blah.py").write( """ def test_rewritten(): assert "@py_builtins" in globals()""" ) assert testdir.runpytest().ret == 0 def test_translate_newlines(self, testdir): content = "def test_rewritten():\r\n assert '@py_builtins' in globals()" b = content.encode("utf-8") testdir.tmpdir.join("test_newlines.py").write(b, "wb") assert testdir.runpytest().ret == 0 @pytest.mark.skipif( sys.version_info < (3, 4), reason="packages without __init__.py not supported on python 2", ) def test_package_without__init__py(self, testdir): pkg = testdir.mkdir("a_package_without_init_py") pkg.join("module.py").ensure() testdir.makepyfile("import a_package_without_init_py.module") assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED def test_rewrite_warning(self, testdir): testdir.makeconftest( """ import pytest pytest.register_assert_rewrite("_pytest") """ ) # needs to be a subprocess because pytester explicitly disables this warning result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*Module already imported*: _pytest"]) def test_rewrite_module_imported_from_conftest(self, testdir): testdir.makeconftest( """ import test_rewrite_module_imported """ ) testdir.makepyfile( test_rewrite_module_imported=""" def test_rewritten(): assert "@py_builtins" in globals() """ ) assert testdir.runpytest_subprocess().ret == 0 def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch): """ AssertionRewriteHook should remember rewritten modules so it doesn't give false positives (#2005). """ monkeypatch.syspath_prepend(testdir.tmpdir) testdir.makepyfile(test_remember_rewritten_modules="") warnings = [] hook = AssertionRewritingHook(pytestconfig) monkeypatch.setattr( hook, "_warn_already_imported", lambda code, msg: warnings.append(msg) ) hook.find_module("test_remember_rewritten_modules") hook.load_module("test_remember_rewritten_modules") hook.mark_rewrite("test_remember_rewritten_modules") hook.mark_rewrite("test_remember_rewritten_modules") assert warnings == [] def test_rewrite_warning_using_pytest_plugins(self, testdir): testdir.makepyfile( **{ "conftest.py": "pytest_plugins = ['core', 'gui', 'sci']", "core.py": "", "gui.py": "pytest_plugins = ['core', 'sci']", "sci.py": "pytest_plugins = ['core']", "test_rewrite_warning_pytest_plugins.py": "def test(): pass", } ) testdir.chdir() result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*= 1 passed in *=*"]) assert "pytest-warning summary" not in result.stdout.str() def test_rewrite_warning_using_pytest_plugins_env_var(self, testdir, monkeypatch): monkeypatch.setenv("PYTEST_PLUGINS", "plugin") testdir.makepyfile( **{ "plugin.py": "", "test_rewrite_warning_using_pytest_plugins_env_var.py": """ import plugin pytest_plugins = ['plugin'] def test(): pass """, } ) testdir.chdir() result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*= 1 passed in *=*"]) assert "pytest-warning summary" not in result.stdout.str() @pytest.mark.skipif(sys.version_info[0] > 2, reason="python 2 only") def test_rewrite_future_imports(self, testdir): """Test that rewritten modules don't inherit the __future__ flags from the assertrewrite module. assertion.rewrite imports __future__.division (and others), so ensure rewritten modules don't inherit those flags. The test below will fail if __future__.division is enabled """ testdir.makepyfile( """ def test(): x = 1 / 2 assert type(x) is int """ ) result = testdir.runpytest() assert result.ret == 0 class TestAssertionRewriteHookDetails(object): def test_loader_is_package_false_for_module(self, testdir): testdir.makepyfile( test_fun=""" def test_loader(): assert not __loader__.is_package(__name__) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 passed*"]) def test_loader_is_package_true_for_package(self, testdir): testdir.makepyfile( test_fun=""" def test_loader(): assert not __loader__.is_package(__name__) def test_fun(): assert __loader__.is_package('fun') def test_missing(): assert not __loader__.is_package('pytest_not_there') """ ) testdir.mkpydir("fun") result = testdir.runpytest() result.stdout.fnmatch_lines(["* 3 passed*"]) @pytest.mark.skipif("sys.version_info[0] >= 3") @pytest.mark.xfail("hasattr(sys, 'pypy_translation_info')") def test_assume_ascii(self, testdir): content = "u'\xe2\x99\xa5\x01\xfe'" testdir.tmpdir.join("test_encoding.py").write(content, "wb") res = testdir.runpytest() assert res.ret != 0 assert "SyntaxError: Non-ASCII character" in res.stdout.str() @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie(self, testdir): testdir.makepyfile( test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): assert "@py_builtins" in globals()""" ) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_second_line(self, testdir): testdir.makepyfile( test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): assert "@py_builtins" in globals()""" ) assert testdir.runpytest().ret == 0 @pytest.mark.skipif("sys.version_info[0] >= 3") def test_detect_coding_cookie_crlf(self, testdir): testdir.makepyfile( test_cookie=""" # -*- coding: utf-8 -*- u"St\xc3\xa4d" def test_rewritten(): assert "@py_builtins" in globals()""" ) assert testdir.runpytest().ret == 0 def test_sys_meta_path_munged(self, testdir): testdir.makepyfile( """ def test_meta_path(): import sys; sys.meta_path = []""" ) assert testdir.runpytest().ret == 0 def test_write_pyc(self, testdir, tmpdir, monkeypatch): from _pytest.assertion.rewrite import _write_pyc from _pytest.assertion import AssertionState import atomicwrites from contextlib import contextmanager config = testdir.parseconfig([]) state = AssertionState(config, "rewrite") source_path = tmpdir.ensure("source.py") pycpath = tmpdir.join("pyc").strpath assert _write_pyc(state, [1], source_path.stat(), pycpath) @contextmanager def atomic_write_failed(fn, mode="r", overwrite=False): e = IOError() e.errno = 10 raise e yield monkeypatch.setattr(atomicwrites, "atomic_write", atomic_write_failed) assert not _write_pyc(state, [1], source_path.stat(), pycpath) def test_resources_provider_for_loader(self, testdir): """ Attempts to load resources from a package should succeed normally, even when the AssertionRewriteHook is used to load the modules. See #366 for details. """ pytest.importorskip("pkg_resources") testdir.mkpydir("testpkg") contents = { "testpkg/test_pkg": """ import pkg_resources import pytest from _pytest.assertion.rewrite import AssertionRewritingHook def test_load_resource(): assert isinstance(__loader__, AssertionRewritingHook) res = pkg_resources.resource_string(__name__, 'resource.txt') res = res.decode('ascii') assert res == 'Load me please.' """ } testdir.makepyfile(**contents) testdir.maketxtfile(**{"testpkg/resource": "Load me please."}) result = testdir.runpytest_subprocess() result.assert_outcomes(passed=1) def test_read_pyc(self, tmpdir): """ Ensure that the `_read_pyc` can properly deal with corrupted pyc files. In those circumstances it should just give up instead of generating an exception that is propagated to the caller. """ import py_compile from _pytest.assertion.rewrite import _read_pyc source = tmpdir.join("source.py") pyc = source + "c" source.write("def test(): pass") py_compile.compile(str(source), str(pyc)) contents = pyc.read(mode="rb") strip_bytes = 20 # header is around 8 bytes, strip a little more assert len(contents) > strip_bytes pyc.write(contents[:strip_bytes], mode="wb") assert _read_pyc(source, str(pyc)) is None # no error def test_reload_is_same(self, testdir): # A file that will be picked up during collecting. testdir.tmpdir.join("file.py").ensure() testdir.tmpdir.join("pytest.ini").write( textwrap.dedent( """ [pytest] python_files = *.py """ ) ) testdir.makepyfile( test_fun=""" import sys try: from imp import reload except ImportError: pass def test_loader(): import file assert sys.modules["file"] is reload(file) """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines(["* 1 passed*"]) def test_reload_reloads(self, testdir): """Reloading a module after change picks up the change.""" testdir.tmpdir.join("file.py").write( textwrap.dedent( """ def reloaded(): return False def rewrite_self(): with open(__file__, 'w') as self: self.write('def reloaded(): return True') """ ) ) testdir.tmpdir.join("pytest.ini").write( textwrap.dedent( """ [pytest] python_files = *.py """ ) ) testdir.makepyfile( test_fun=""" import sys try: from imp import reload except ImportError: pass def test_loader(): import file assert not file.reloaded() file.rewrite_self() reload(file) assert file.reloaded() """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines(["* 1 passed*"]) def test_get_data_support(self, testdir): """Implement optional PEP302 api (#808). """ path = testdir.mkpydir("foo") path.join("test_foo.py").write( textwrap.dedent( """\ class Test(object): def test_foo(self): import pkgutil data = pkgutil.get_data('foo.test_foo', 'data.txt') assert data == b'Hey' """ ) ) path.join("data.txt").write("Hey") result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_issue731(testdir): testdir.makepyfile( """ class LongReprWithBraces(object): def __repr__(self): return 'LongReprWithBraces({' + ('a' * 80) + '}' + ('a' * 120) + ')' def some_method(self): return False def test_long_repr(): obj = LongReprWithBraces() assert obj.some_method() """ ) result = testdir.runpytest() assert "unbalanced braces" not in result.stdout.str() class TestIssue925(object): def test_simple_case(self, testdir): testdir.makepyfile( """ def test_ternary_display(): assert (False == False) == False """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*E*assert (False == False) == False"]) def test_long_case(self, testdir): testdir.makepyfile( """ def test_ternary_display(): assert False == (False == True) == True """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*E*assert (False == True) == True"]) def test_many_brackets(self, testdir): testdir.makepyfile( """ def test_ternary_display(): assert True == ((False == True) == True) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*E*assert True == ((False == True) == True)"]) class TestIssue2121: def test_rewrite_python_files_contain_subdirs(self, testdir): testdir.makepyfile( **{ "tests/file.py": """ def test_simple_failure(): assert 1 + 1 == 3 """ } ) testdir.makeini( """ [pytest] python_files = tests/**.py """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"]) @pytest.mark.skipif( sys.maxsize <= (2 ** 31 - 1), reason="Causes OverflowError on 32bit systems" ) @pytest.mark.parametrize("offset", [-1, +1]) def test_source_mtime_long_long(testdir, offset): """Support modification dates after 2038 in rewritten files (#4903). pytest would crash with: fp.write(struct.pack(" (2, 7): return subprocess.check_output(*args, **kargs).decode().splitlines() if "stdout" in kargs: raise ValueError("stdout argument not allowed, it will be overridden.") process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kargs.get("args") if cmd is None: cmd = args[0] raise subprocess.CalledProcessError(retcode, cmd) return output.decode().splitlines() except subprocess.CalledProcessError: return [] class FilesCompleter(object): "File completer class, optionally takes a list of allowed extensions" def __init__(self, allowednames=(), directories=True): # Fix if someone passes in a string instead of a list if type(allowednames) is str: allowednames = [allowednames] self.allowednames = [x.lstrip("*").lstrip(".") for x in allowednames] self.directories = directories def __call__(self, prefix, **kwargs): completion = [] if self.allowednames: if self.directories: files = _wrapcall( ["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)] ) completion += [f + "/" for f in files] for x in self.allowednames: completion += _wrapcall( [ "bash", "-c", "compgen -A file -X '!*.{0}' -- '{p}'".format(x, p=prefix), ] ) else: completion += _wrapcall( ["bash", "-c", "compgen -A file -- '{p}'".format(p=prefix)] ) anticomp = _wrapcall( ["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)] ) completion = list(set(completion) - set(anticomp)) if self.directories: completion += [f + "/" for f in anticomp] return completion class TestArgComplete(object): @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_compare_with_compgen(self, tmpdir): from _pytest._argcomplete import FastFilesCompleter ffc = FastFilesCompleter() fc = FilesCompleter() with tmpdir.as_cwd(): assert equal_with_bash("", ffc, fc, out=sys.stdout) tmpdir.ensure("data") for x in ["d", "data", "doesnotexist", ""]: assert equal_with_bash(x, ffc, fc, out=sys.stdout) @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_remove_dir_prefix(self): """this is not compatible with compgen but it is with bash itself: ls /usr/ """ from _pytest._argcomplete import FastFilesCompleter ffc = FastFilesCompleter() fc = FilesCompleter() for x in "/usr/".split(): assert not equal_with_bash(x, ffc, fc, out=sys.stdout) pytest-4.6.9/testing/test_session.py0000664000372000037200000002751113604173400020501 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED class SessionTests(object): def test_basic_testitem_events(self, testdir): tfile = testdir.makepyfile( """ def test_one(): pass def test_one_one(): assert 0 def test_other(): raise ValueError(23) class TestClass(object): def test_two(self, someargs): pass """ ) reprec = testdir.inline_run(tfile) passed, skipped, failed = reprec.listoutcomes() assert len(skipped) == 0 assert len(passed) == 1 assert len(failed) == 3 def end(x): return x.nodeid.split("::")[-1] assert end(failed[0]) == "test_one_one" assert end(failed[1]) == "test_other" itemstarted = reprec.getcalls("pytest_itemcollected") assert len(itemstarted) == 4 # XXX check for failing funcarg setup # colreports = reprec.getcalls("pytest_collectreport") # assert len(colreports) == 4 # assert colreports[1].report.failed def test_nested_import_error(self, testdir): tfile = testdir.makepyfile( """ import import_fails def test_this(): assert import_fails.a == 1 """, import_fails=""" import does_not_work a = 1 """, ) reprec = testdir.inline_run(tfile) values = reprec.getfailedcollections() assert len(values) == 1 out = str(values[0].longrepr) assert out.find("does_not_work") != -1 def test_raises_output(self, testdir): reprec = testdir.inline_runsource( """ import pytest def test_raises_doesnt(): pytest.raises(ValueError, int, "3") """ ) passed, skipped, failed = reprec.listoutcomes() assert len(failed) == 1 out = failed[0].longrepr.reprcrash.message assert "DID NOT RAISE" in out def test_syntax_error_module(self, testdir): reprec = testdir.inline_runsource("this is really not python") values = reprec.getfailedcollections() assert len(values) == 1 out = str(values[0].longrepr) assert out.find(str("not python")) != -1 def test_exit_first_problem(self, testdir): reprec = testdir.inline_runsource( """ def test_one(): assert 0 def test_two(): assert 0 """, "--exitfirst", ) passed, skipped, failed = reprec.countoutcomes() assert failed == 1 assert passed == skipped == 0 def test_maxfail(self, testdir): reprec = testdir.inline_runsource( """ def test_one(): assert 0 def test_two(): assert 0 def test_three(): assert 0 """, "--maxfail=2", ) passed, skipped, failed = reprec.countoutcomes() assert failed == 2 assert passed == skipped == 0 def test_broken_repr(self, testdir): p = testdir.makepyfile( """ import pytest class BrokenRepr1(object): foo=0 def __repr__(self): raise Exception("Ha Ha fooled you, I'm a broken repr().") class TestBrokenClass(object): def test_explicit_bad_repr(self): t = BrokenRepr1() with pytest.raises(Exception, match="I'm a broken repr"): repr(t) def test_implicit_bad_repr1(self): t = BrokenRepr1() assert t.foo == 1 """ ) reprec = testdir.inline_run(p) passed, skipped, failed = reprec.listoutcomes() assert (len(passed), len(skipped), len(failed)) == (1, 0, 1) out = failed[0].longrepr.reprcrash.message assert ( out.find( """[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""" ) != -1 ) def test_broken_repr_with_showlocals_verbose(self, testdir): p = testdir.makepyfile( """ class ObjWithErrorInRepr: def __repr__(self): raise NotImplementedError def test_repr_error(): x = ObjWithErrorInRepr() assert x == "value" """ ) reprec = testdir.inline_run("--showlocals", "-vv", p) passed, skipped, failed = reprec.listoutcomes() assert (len(passed), len(skipped), len(failed)) == (0, 0, 1) entries = failed[0].longrepr.reprtraceback.reprentries assert len(entries) == 1 repr_locals = entries[0].reprlocals assert repr_locals.lines assert len(repr_locals.lines) == 1 assert repr_locals.lines[0].startswith( 'x = <[NotImplementedError("") raised in repr()] ObjWithErrorInRepr' ) def test_skip_file_by_conftest(self, testdir): testdir.makepyfile( conftest=""" import pytest def pytest_collect_file(): pytest.skip("intentional") """, test_file=""" def test_one(): pass """, ) try: reprec = testdir.inline_run(testdir.tmpdir) except pytest.skip.Exception: # pragma: no cover pytest.fail("wrong skipped caught") reports = reprec.getreports("pytest_collectreport") assert len(reports) == 1 assert reports[0].skipped class TestNewSession(SessionTests): def test_order_of_execution(self, testdir): reprec = testdir.inline_runsource( """ values = [] def test_1(): values.append(1) def test_2(): values.append(2) def test_3(): assert values == [1,2] class Testmygroup(object): reslist = values def test_1(self): self.reslist.append(1) def test_2(self): self.reslist.append(2) def test_3(self): self.reslist.append(3) def test_4(self): assert self.reslist == [1,2,1,2,3] """ ) passed, skipped, failed = reprec.countoutcomes() assert failed == skipped == 0 assert passed == 7 def test_collect_only_with_various_situations(self, testdir): p = testdir.makepyfile( test_one=""" def test_one(): raise ValueError() class TestX(object): def test_method_one(self): pass class TestY(TestX): pass """, test_three="xxxdsadsadsadsa", __init__="", ) reprec = testdir.inline_run("--collect-only", p.dirpath()) itemstarted = reprec.getcalls("pytest_itemcollected") assert len(itemstarted) == 3 assert not reprec.getreports("pytest_runtest_logreport") started = reprec.getcalls("pytest_collectstart") finished = reprec.getreports("pytest_collectreport") assert len(started) == len(finished) assert len(started) == 8 colfail = [x for x in finished if x.failed] assert len(colfail) == 1 def test_minus_x_import_error(self, testdir): testdir.makepyfile(__init__="") testdir.makepyfile(test_one="xxxx", test_two="yyyy") reprec = testdir.inline_run("-x", testdir.tmpdir) finished = reprec.getreports("pytest_collectreport") colfail = [x for x in finished if x.failed] assert len(colfail) == 1 def test_minus_x_overridden_by_maxfail(self, testdir): testdir.makepyfile(__init__="") testdir.makepyfile(test_one="xxxx", test_two="yyyy", test_third="zzz") reprec = testdir.inline_run("-x", "--maxfail=2", testdir.tmpdir) finished = reprec.getreports("pytest_collectreport") colfail = [x for x in finished if x.failed] assert len(colfail) == 2 def test_plugin_specify(testdir): with pytest.raises(ImportError): testdir.parseconfig("-p", "nqweotexistent") # pytest.raises(ImportError, # "config.do_configure(config)" # ) def test_plugin_already_exists(testdir): config = testdir.parseconfig("-p", "terminal") assert config.option.plugins == ["terminal"] config._do_configure() config._ensure_unconfigure() def test_exclude(testdir): hellodir = testdir.mkdir("hello") hellodir.join("test_hello.py").write("x y syntaxerror") hello2dir = testdir.mkdir("hello2") hello2dir.join("test_hello2.py").write("x y syntaxerror") testdir.makepyfile(test_ok="def test_pass(): pass") result = testdir.runpytest("--ignore=hello", "--ignore=hello2") assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_exclude_glob(testdir): hellodir = testdir.mkdir("hello") hellodir.join("test_hello.py").write("x y syntaxerror") hello2dir = testdir.mkdir("hello2") hello2dir.join("test_hello2.py").write("x y syntaxerror") hello3dir = testdir.mkdir("hallo3") hello3dir.join("test_hello3.py").write("x y syntaxerror") subdir = testdir.mkdir("sub") subdir.join("test_hello4.py").write("x y syntaxerror") testdir.makepyfile(test_ok="def test_pass(): pass") result = testdir.runpytest("--ignore-glob=*h[ea]llo*") assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_deselect(testdir): testdir.makepyfile( test_a=""" import pytest def test_a1(): pass @pytest.mark.parametrize('b', range(3)) def test_a2(b): pass class TestClass: def test_c1(self): pass def test_c2(self): pass """ ) result = testdir.runpytest( "-v", "--deselect=test_a.py::test_a2[1]", "--deselect=test_a.py::test_a2[2]", "--deselect=test_a.py::TestClass::test_c1", ) assert result.ret == 0 result.stdout.fnmatch_lines(["*3 passed, 3 deselected*"]) for line in result.stdout.lines: assert not line.startswith(("test_a.py::test_a2[1]", "test_a.py::test_a2[2]")) def test_sessionfinish_with_start(testdir): testdir.makeconftest( """ import os values = [] def pytest_sessionstart(): values.append(os.getcwd()) os.chdir("..") def pytest_sessionfinish(): assert values[0] == os.getcwd() """ ) res = testdir.runpytest("--collect-only") assert res.ret == EXIT_NOTESTSCOLLECTED @pytest.mark.parametrize("path", ["root", "{relative}/root", "{environment}/root"]) def test_rootdir_option_arg(testdir, monkeypatch, path): monkeypatch.setenv("PY_ROOTDIR_PATH", str(testdir.tmpdir)) path = path.format(relative=str(testdir.tmpdir), environment="$PY_ROOTDIR_PATH") rootdir = testdir.mkdir("root") rootdir.mkdir("tests") testdir.makepyfile( """ import os def test_one(): assert 1 """ ) result = testdir.runpytest("--rootdir={}".format(path)) result.stdout.fnmatch_lines( [ "*rootdir: {}/root".format(testdir.tmpdir), "root/test_rootdir_option_arg.py *", "*1 passed*", ] ) def test_rootdir_wrong_option_arg(testdir): testdir.makepyfile( """ import os def test_one(): assert 1 """ ) result = testdir.runpytest("--rootdir=wrong_dir") result.stderr.fnmatch_lines( ["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"] ) pytest-4.6.9/testing/test_doctest.py0000664000372000037200000011146213604173400020462 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import inspect import sys import textwrap import pytest from _pytest.compat import MODULE_NOT_FOUND_ERROR from _pytest.doctest import _is_mocked from _pytest.doctest import _patch_unwrap_mock_aware from _pytest.doctest import DoctestItem from _pytest.doctest import DoctestModule from _pytest.doctest import DoctestTextfile class TestDoctests(object): def test_collect_testtextfile(self, testdir): w = testdir.maketxtfile(whatever="") checkfile = testdir.maketxtfile( test_something=""" alskdjalsdk >>> i = 5 >>> i-1 4 """ ) for x in (testdir.tmpdir, checkfile): # print "checking that %s returns custom items" % (x,) items, reprec = testdir.inline_genitems(x) assert len(items) == 1 assert isinstance(items[0], DoctestItem) assert isinstance(items[0].parent, DoctestTextfile) # Empty file has no items. items, reprec = testdir.inline_genitems(w) assert len(items) == 0 def test_collect_module_empty(self, testdir): path = testdir.makepyfile(whatever="#") for p in (path, testdir.tmpdir): items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 0 def test_collect_module_single_modulelevel_doctest(self, testdir): path = testdir.makepyfile(whatever='""">>> pass"""') for p in (path, testdir.tmpdir): items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 1 assert isinstance(items[0], DoctestItem) assert isinstance(items[0].parent, DoctestModule) def test_collect_module_two_doctest_one_modulelevel(self, testdir): path = testdir.makepyfile( whatever=""" '>>> x = None' def my_func(): ">>> magic = 42 " """ ) for p in (path, testdir.tmpdir): items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 2 assert isinstance(items[0], DoctestItem) assert isinstance(items[1], DoctestItem) assert isinstance(items[0].parent, DoctestModule) assert items[0].parent is items[1].parent def test_collect_module_two_doctest_no_modulelevel(self, testdir): path = testdir.makepyfile( whatever=""" '# Empty' def my_func(): ">>> magic = 42 " def unuseful(): ''' # This is a function # >>> # it doesn't have any doctest ''' def another(): ''' # This is another function >>> import os # this one does have a doctest ''' """ ) for p in (path, testdir.tmpdir): items, reprec = testdir.inline_genitems(p, "--doctest-modules") assert len(items) == 2 assert isinstance(items[0], DoctestItem) assert isinstance(items[1], DoctestItem) assert isinstance(items[0].parent, DoctestModule) assert items[0].parent is items[1].parent def test_simple_doctestfile(self, testdir): p = testdir.maketxtfile( test_doc=""" >>> x = 1 >>> x == 1 False """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(failed=1) def test_new_pattern(self, testdir): p = testdir.maketxtfile( xdoc=""" >>> x = 1 >>> x == 1 False """ ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1) def test_multiple_patterns(self, testdir): """Test support for multiple --doctest-glob arguments (#1255). """ testdir.maketxtfile( xdoc=""" >>> 1 1 """ ) testdir.makefile( ".foo", test=""" >>> 1 1 """, ) testdir.maketxtfile( test_normal=""" >>> 1 1 """ ) expected = {"xdoc.txt", "test.foo", "test_normal.txt"} assert {x.basename for x in testdir.tmpdir.listdir()} == expected args = ["--doctest-glob=xdoc*.txt", "--doctest-glob=*.foo"] result = testdir.runpytest(*args) result.stdout.fnmatch_lines(["*test.foo *", "*xdoc.txt *", "*2 passed*"]) result = testdir.runpytest() result.stdout.fnmatch_lines(["*test_normal.txt *", "*1 passed*"]) @pytest.mark.parametrize( " test_string, encoding", [(u"foo", "ascii"), (u"öäü", "latin1"), (u"öäü", "utf-8")], ) def test_encoding(self, testdir, test_string, encoding): """Test support for doctest_encoding ini option. """ testdir.makeini( """ [pytest] doctest_encoding={} """.format( encoding ) ) doctest = u""" >>> u"{}" {} """.format( test_string, repr(test_string) ) testdir._makefile(".txt", [doctest], {}, encoding=encoding) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_doctest_unexpected_exception(self, testdir): testdir.maketxtfile( """ >>> i = 0 >>> 0 / i 2 """ ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ "*unexpected_exception*", "*>>> i = 0*", "*>>> 0 / i*", "*UNEXPECTED*ZeroDivision*", ] ) def test_doctest_skip(self, testdir): testdir.maketxtfile( """ >>> 1 1 >>> import pytest >>> pytest.skip("") """ ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines(["*1 skipped*"]) def test_docstring_partial_context_around_error(self, testdir): """Test that we show some context before the actual line of a failing doctest. """ testdir.makepyfile( ''' def foo(): """ text-line-1 text-line-2 text-line-3 text-line-4 text-line-5 text-line-6 text-line-7 text-line-8 text-line-9 text-line-10 text-line-11 >>> 1 + 1 3 text-line-after """ ''' ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ "*docstring_partial_context_around_error*", "005*text-line-3", "006*text-line-4", "013*text-line-11", "014*>>> 1 + 1", "Expected:", " 3", "Got:", " 2", ] ) # lines below should be trimmed out assert "text-line-2" not in result.stdout.str() assert "text-line-after" not in result.stdout.str() def test_docstring_full_context_around_error(self, testdir): """Test that we show the whole context before the actual line of a failing doctest, provided that the context is up to 10 lines long. """ testdir.makepyfile( ''' def foo(): """ text-line-1 text-line-2 >>> 1 + 1 3 """ ''' ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ "*docstring_full_context_around_error*", "003*text-line-1", "004*text-line-2", "006*>>> 1 + 1", "Expected:", " 3", "Got:", " 2", ] ) def test_doctest_linedata_missing(self, testdir): testdir.tmpdir.join("hello.py").write( textwrap.dedent( """\ class Fun(object): @property def test(self): ''' >>> a = 1 >>> 1/0 ''' """ ) ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ "*hello*", "*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*", "*1/0*", "*UNEXPECTED*ZeroDivision*", "*1 failed*", ] ) def test_doctest_unex_importerror_only_txt(self, testdir): testdir.maketxtfile( """ >>> import asdalsdkjaslkdjasd >>> """ ) result = testdir.runpytest() # doctest is never executed because of error during hello.py collection result.stdout.fnmatch_lines( [ "*>>> import asdals*", "*UNEXPECTED*{e}*".format(e=MODULE_NOT_FOUND_ERROR), "{e}: No module named *asdal*".format(e=MODULE_NOT_FOUND_ERROR), ] ) def test_doctest_unex_importerror_with_module(self, testdir): testdir.tmpdir.join("hello.py").write( textwrap.dedent( """\ import asdalsdkjaslkdjasd """ ) ) testdir.maketxtfile( """ >>> import hello >>> """ ) result = testdir.runpytest("--doctest-modules") # doctest is never executed because of error during hello.py collection result.stdout.fnmatch_lines( [ "*ERROR collecting hello.py*", "*{e}: No module named *asdals*".format(e=MODULE_NOT_FOUND_ERROR), "*Interrupted: 1 errors during collection*", ] ) def test_doctestmodule(self, testdir): p = testdir.makepyfile( """ ''' >>> x = 1 >>> x == 1 False ''' """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1) def test_doctestmodule_external_and_issue116(self, testdir): p = testdir.mkpydir("hello") p.join("__init__.py").write( textwrap.dedent( """\ def somefunc(): ''' >>> i = 0 >>> i + 1 2 ''' """ ) ) result = testdir.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines( [ "003 *>>> i = 0", "004 *>>> i + 1", "*Expected:", "* 2", "*Got:", "* 1", "*:4: DocTestFailure", ] ) def test_txtfile_failing(self, testdir): p = testdir.maketxtfile( """ >>> i = 0 >>> i + 1 2 """ ) result = testdir.runpytest(p, "-s") result.stdout.fnmatch_lines( [ "001 >>> i = 0", "002 >>> i + 1", "Expected:", " 2", "Got:", " 1", "*test_txtfile_failing.txt:2: DocTestFailure", ] ) def test_txtfile_with_fixtures(self, testdir): p = testdir.maketxtfile( """ >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) def test_txtfile_with_usefixtures_in_ini(self, testdir): testdir.makeini( """ [pytest] usefixtures = myfixture """ ) testdir.makeconftest( """ import pytest @pytest.fixture def myfixture(monkeypatch): monkeypatch.setenv("HELLO", "WORLD") """ ) p = testdir.maketxtfile( """ >>> import os >>> os.environ["HELLO"] 'WORLD' """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) def test_doctestmodule_with_fixtures(self, testdir): p = testdir.makepyfile( """ ''' >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' ''' """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) def test_doctestmodule_three_tests(self, testdir): p = testdir.makepyfile( """ ''' >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' ''' def my_func(): ''' >>> magic = 42 >>> magic - 42 0 ''' def unuseful(): pass def another(): ''' >>> import os >>> os is os True ''' """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=3) def test_doctestmodule_two_tests_one_fail(self, testdir): p = testdir.makepyfile( """ class MyClass(object): def bad_meth(self): ''' >>> magic = 42 >>> magic 0 ''' def nice_meth(self): ''' >>> magic = 42 >>> magic - 42 0 ''' """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1, passed=1) def test_ignored_whitespace(self, testdir): testdir.makeini( """ [pytest] doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE """ ) p = testdir.makepyfile( """ class MyClass(object): ''' >>> a = "foo " >>> print(a) foo ''' pass """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) def test_non_ignored_whitespace(self, testdir): testdir.makeini( """ [pytest] doctest_optionflags = ELLIPSIS """ ) p = testdir.makepyfile( """ class MyClass(object): ''' >>> a = "foo " >>> print(a) foo ''' pass """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1, passed=0) def test_ignored_whitespace_glob(self, testdir): testdir.makeini( """ [pytest] doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE """ ) p = testdir.maketxtfile( xdoc=""" >>> a = "foo " >>> print(a) foo """ ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(passed=1) def test_non_ignored_whitespace_glob(self, testdir): testdir.makeini( """ [pytest] doctest_optionflags = ELLIPSIS """ ) p = testdir.maketxtfile( xdoc=""" >>> a = "foo " >>> print(a) foo """ ) reprec = testdir.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1, passed=0) def test_contains_unicode(self, testdir): """Fix internal error with docstrings containing non-ascii characters. """ testdir.makepyfile( u''' # -*- coding: utf-8 -*- def foo(): """ >>> name = 'с' # not letter 'c' but instead Cyrillic 's'. 'anything' """ ''' ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines(["Got nothing", "* 1 failed in*"]) def test_ignore_import_errors_on_doctest(self, testdir): p = testdir.makepyfile( """ import asdf def add_one(x): ''' >>> add_one(1) 2 ''' return x + 1 """ ) reprec = testdir.inline_run( p, "--doctest-modules", "--doctest-ignore-import-errors" ) reprec.assertoutcome(skipped=1, failed=1, passed=0) def test_junit_report_for_doctest(self, testdir): """ #713: Fix --junit-xml option when used with --doctest-modules. """ p = testdir.makepyfile( """ def foo(): ''' >>> 1 + 1 3 ''' pass """ ) reprec = testdir.inline_run(p, "--doctest-modules", "--junit-xml=junit.xml") reprec.assertoutcome(failed=1) def test_unicode_doctest(self, testdir): """ Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii characters. """ p = testdir.maketxtfile( test_unicode_doctest=""" .. doctest:: >>> print( ... "Hi\\n\\nByé") Hi ... Byé >>> 1/0 # Byé 1 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( ["*UNEXPECTED EXCEPTION: ZeroDivisionError*", "*1 failed*"] ) def test_unicode_doctest_module(self, testdir): """ Test case for issue 2434: DecodeError on Python 2 when doctest docstring contains non-ascii characters. """ p = testdir.makepyfile( test_unicode_doctest_module=""" # -*- coding: utf-8 -*- from __future__ import unicode_literals def fix_bad_unicode(text): ''' >>> print(fix_bad_unicode('único')) único ''' return "único" """ ) result = testdir.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines(["* 1 passed *"]) def test_print_unicode_value(self, testdir): """ Test case for issue 3583: Printing Unicode in doctest under Python 2.7 doesn't work """ p = testdir.maketxtfile( test_print_unicode_value=r""" Here is a doctest:: >>> print(u'\xE5\xE9\xEE\xF8\xFC') åéîøü """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["* 1 passed *"]) def test_reportinfo(self, testdir): """ Test case to make sure that DoctestItem.reportinfo() returns lineno. """ p = testdir.makepyfile( test_reportinfo=""" def foo(x): ''' >>> foo('a') 'b' ''' return 'c' """ ) items, reprec = testdir.inline_genitems(p, "--doctest-modules") reportinfo = items[0].reportinfo() assert reportinfo[1] == 1 def test_valid_setup_py(self, testdir): """ Test to make sure that pytest ignores valid setup.py files when ran with --doctest-modules """ p = testdir.makepyfile( setup=""" from setuptools import setup, find_packages setup(name='sample', version='0.0', description='description', packages=find_packages() ) """ ) result = testdir.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines(["*collected 0 items*"]) def test_invalid_setup_py(self, testdir): """ Test to make sure that pytest reads setup.py files that are not used for python packages when ran with --doctest-modules """ p = testdir.makepyfile( setup=""" def test_foo(): return 'bar' """ ) result = testdir.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines(["*collected 1 item*"]) class TestLiterals(object): @pytest.mark.parametrize("config_mode", ["ini", "comment"]) def test_allow_unicode(self, testdir, config_mode): """Test that doctests which output unicode work in all python versions tested by pytest when the ALLOW_UNICODE option is used (either in the ini file or by an inline comment). """ if config_mode == "ini": testdir.makeini( """ [pytest] doctest_optionflags = ALLOW_UNICODE """ ) comment = "" else: comment = "#doctest: +ALLOW_UNICODE" testdir.maketxtfile( test_doc=""" >>> b'12'.decode('ascii') {comment} '12' """.format( comment=comment ) ) testdir.makepyfile( foo=""" def foo(): ''' >>> b'12'.decode('ascii') {comment} '12' ''' """.format( comment=comment ) ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=2) @pytest.mark.parametrize("config_mode", ["ini", "comment"]) def test_allow_bytes(self, testdir, config_mode): """Test that doctests which output bytes work in all python versions tested by pytest when the ALLOW_BYTES option is used (either in the ini file or by an inline comment)(#1287). """ if config_mode == "ini": testdir.makeini( """ [pytest] doctest_optionflags = ALLOW_BYTES """ ) comment = "" else: comment = "#doctest: +ALLOW_BYTES" testdir.maketxtfile( test_doc=""" >>> b'foo' {comment} 'foo' """.format( comment=comment ) ) testdir.makepyfile( foo=""" def foo(): ''' >>> b'foo' {comment} 'foo' ''' """.format( comment=comment ) ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=2) def test_unicode_string(self, testdir): """Test that doctests which output unicode fail in Python 2 when the ALLOW_UNICODE option is not used. The same test should pass in Python 3. """ testdir.maketxtfile( test_doc=""" >>> b'12'.decode('ascii') '12' """ ) reprec = testdir.inline_run() passed = int(sys.version_info[0] >= 3) reprec.assertoutcome(passed=passed, failed=int(not passed)) def test_bytes_literal(self, testdir): """Test that doctests which output bytes fail in Python 3 when the ALLOW_BYTES option is not used. The same test should pass in Python 2 (#1287). """ testdir.maketxtfile( test_doc=""" >>> b'foo' 'foo' """ ) reprec = testdir.inline_run() passed = int(sys.version_info[0] == 2) reprec.assertoutcome(passed=passed, failed=int(not passed)) class TestDoctestSkips(object): """ If all examples in a doctest are skipped due to the SKIP option, then the tests should be SKIPPED rather than PASSED. (#957) """ @pytest.fixture(params=["text", "module"]) def makedoctest(self, testdir, request): def makeit(doctest): mode = request.param if mode == "text": testdir.maketxtfile(doctest) else: assert mode == "module" testdir.makepyfile('"""\n%s"""' % doctest) return makeit def test_one_skipped(self, testdir, makedoctest): makedoctest( """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 4 """ ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=1) def test_one_skipped_failed(self, testdir, makedoctest): makedoctest( """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 200 """ ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(failed=1) def test_all_skipped(self, testdir, makedoctest): makedoctest( """ >>> 1 + 1 # doctest: +SKIP 2 >>> 2 + 2 # doctest: +SKIP 200 """ ) reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(skipped=1) def test_vacuous_all_skipped(self, testdir, makedoctest): makedoctest("") reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=0, skipped=0) def test_continue_on_failure(self, testdir): testdir.maketxtfile( test_something=""" >>> i = 5 >>> def foo(): ... raise ValueError('error1') >>> foo() >>> i >>> i + 2 7 >>> i + 1 """ ) result = testdir.runpytest("--doctest-modules", "--doctest-continue-on-failure") result.assert_outcomes(passed=0, failed=1) # The lines that contains the failure are 4, 5, and 8. The first one # is a stack trace and the other two are mismatches. result.stdout.fnmatch_lines( ["*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*"] ) class TestDoctestAutoUseFixtures(object): SCOPES = ["module", "session", "class", "function"] def test_doctest_module_session_fixture(self, testdir): """Test that session fixtures are initialized for doctest modules (#768) """ # session fixture which changes some global data, which will # be accessed by doctests in a module testdir.makeconftest( """ import pytest import sys @pytest.yield_fixture(autouse=True, scope='session') def myfixture(): assert not hasattr(sys, 'pytest_session_data') sys.pytest_session_data = 1 yield del sys.pytest_session_data """ ) testdir.makepyfile( foo=""" import sys def foo(): ''' >>> assert sys.pytest_session_data == 1 ''' def bar(): ''' >>> assert sys.pytest_session_data == 1 ''' """ ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines(["*2 passed*"]) @pytest.mark.parametrize("scope", SCOPES) @pytest.mark.parametrize("enable_doctest", [True, False]) def test_fixture_scopes(self, testdir, scope, enable_doctest): """Test that auto-use fixtures work properly with doctest modules. See #1057 and #1100. """ testdir.makeconftest( """ import pytest @pytest.fixture(autouse=True, scope="{scope}") def auto(request): return 99 """.format( scope=scope ) ) testdir.makepyfile( test_1=''' def test_foo(): """ >>> getfixture('auto') + 1 100 """ def test_bar(): assert 1 ''' ) params = ("--doctest-modules",) if enable_doctest else () passes = 3 if enable_doctest else 2 result = testdir.runpytest(*params) result.stdout.fnmatch_lines(["*=== %d passed in *" % passes]) @pytest.mark.parametrize("scope", SCOPES) @pytest.mark.parametrize("autouse", [True, False]) @pytest.mark.parametrize("use_fixture_in_doctest", [True, False]) def test_fixture_module_doctest_scopes( self, testdir, scope, autouse, use_fixture_in_doctest ): """Test that auto-use fixtures work properly with doctest files. See #1057 and #1100. """ testdir.makeconftest( """ import pytest @pytest.fixture(autouse={autouse}, scope="{scope}") def auto(request): return 99 """.format( scope=scope, autouse=autouse ) ) if use_fixture_in_doctest: testdir.maketxtfile( test_doc=""" >>> getfixture('auto') 99 """ ) else: testdir.maketxtfile( test_doc=""" >>> 1 + 1 2 """ ) result = testdir.runpytest("--doctest-modules") assert "FAILURES" not in str(result.stdout.str()) result.stdout.fnmatch_lines(["*=== 1 passed in *"]) @pytest.mark.parametrize("scope", SCOPES) def test_auto_use_request_attributes(self, testdir, scope): """Check that all attributes of a request in an autouse fixture behave as expected when requested for a doctest item. """ testdir.makeconftest( """ import pytest @pytest.fixture(autouse=True, scope="{scope}") def auto(request): if "{scope}" == 'module': assert request.module is None if "{scope}" == 'class': assert request.cls is None if "{scope}" == 'function': assert request.function is None return 99 """.format( scope=scope ) ) testdir.maketxtfile( test_doc=""" >>> 1 + 1 2 """ ) result = testdir.runpytest("--doctest-modules") assert "FAILURES" not in str(result.stdout.str()) result.stdout.fnmatch_lines(["*=== 1 passed in *"]) class TestDoctestNamespaceFixture(object): SCOPES = ["module", "session", "class", "function"] @pytest.mark.parametrize("scope", SCOPES) def test_namespace_doctestfile(self, testdir, scope): """ Check that inserting something into the namespace works in a simple text file doctest """ testdir.makeconftest( """ import pytest import contextlib @pytest.fixture(autouse=True, scope="{scope}") def add_contextlib(doctest_namespace): doctest_namespace['cl'] = contextlib """.format( scope=scope ) ) p = testdir.maketxtfile( """ >>> print(cl.__name__) contextlib """ ) reprec = testdir.inline_run(p) reprec.assertoutcome(passed=1) @pytest.mark.parametrize("scope", SCOPES) def test_namespace_pyfile(self, testdir, scope): """ Check that inserting something into the namespace works in a simple Python file docstring doctest """ testdir.makeconftest( """ import pytest import contextlib @pytest.fixture(autouse=True, scope="{scope}") def add_contextlib(doctest_namespace): doctest_namespace['cl'] = contextlib """.format( scope=scope ) ) p = testdir.makepyfile( """ def foo(): ''' >>> print(cl.__name__) contextlib ''' """ ) reprec = testdir.inline_run(p, "--doctest-modules") reprec.assertoutcome(passed=1) class TestDoctestReportingOption(object): def _run_doctest_report(self, testdir, format): testdir.makepyfile( """ def foo(): ''' >>> foo() a b 0 1 4 1 2 4 2 3 6 ''' print(' a b\\n' '0 1 4\\n' '1 2 5\\n' '2 3 6') """ ) return testdir.runpytest("--doctest-modules", "--doctest-report", format) @pytest.mark.parametrize("format", ["udiff", "UDIFF", "uDiFf"]) def test_doctest_report_udiff(self, testdir, format): result = self._run_doctest_report(testdir, format) result.stdout.fnmatch_lines( [" 0 1 4", " -1 2 4", " +1 2 5", " 2 3 6"] ) def test_doctest_report_cdiff(self, testdir): result = self._run_doctest_report(testdir, "cdiff") result.stdout.fnmatch_lines( [ " a b", " 0 1 4", " ! 1 2 4", " 2 3 6", " --- 1,4 ----", " a b", " 0 1 4", " ! 1 2 5", " 2 3 6", ] ) def test_doctest_report_ndiff(self, testdir): result = self._run_doctest_report(testdir, "ndiff") result.stdout.fnmatch_lines( [ " a b", " 0 1 4", " - 1 2 4", " ? ^", " + 1 2 5", " ? ^", " 2 3 6", ] ) @pytest.mark.parametrize("format", ["none", "only_first_failure"]) def test_doctest_report_none_or_only_first_failure(self, testdir, format): result = self._run_doctest_report(testdir, format) result.stdout.fnmatch_lines( [ "Expected:", " a b", " 0 1 4", " 1 2 4", " 2 3 6", "Got:", " a b", " 0 1 4", " 1 2 5", " 2 3 6", ] ) def test_doctest_report_invalid(self, testdir): result = self._run_doctest_report(testdir, "obviously_invalid_format") result.stderr.fnmatch_lines( [ "*error: argument --doctest-report: invalid choice: 'obviously_invalid_format' (choose from*" ] ) @pytest.mark.parametrize("mock_module", ["mock", "unittest.mock"]) def test_doctest_mock_objects_dont_recurse_missbehaved(mock_module, testdir): pytest.importorskip(mock_module) testdir.makepyfile( """ from {mock_module} import call class Example(object): ''' >>> 1 + 1 2 ''' """.format( mock_module=mock_module ) ) result = testdir.runpytest("--doctest-modules") result.stdout.fnmatch_lines(["* 1 passed *"]) class Broken: def __getattr__(self, _): raise KeyError("This should be an AttributeError") @pytest.mark.skipif(not hasattr(inspect, "unwrap"), reason="nothing to patch") @pytest.mark.parametrize( # pragma: no branch (lambdas are not called) "stop", [None, _is_mocked, lambda f: None, lambda f: False, lambda f: True] ) def test_warning_on_unwrap_of_broken_object(stop): bad_instance = Broken() assert inspect.unwrap.__module__ == "inspect" with _patch_unwrap_mock_aware(): assert inspect.unwrap.__module__ != "inspect" with pytest.warns( pytest.PytestWarning, match="^Got KeyError.* when unwrapping" ): with pytest.raises(KeyError): inspect.unwrap(bad_instance, stop=stop) assert inspect.unwrap.__module__ == "inspect" pytest-4.6.9/testing/logging/0000775000372000037200000000000013604173421017030 5ustar travistravis00000000000000pytest-4.6.9/testing/logging/test_formatter.py0000664000372000037200000000334313604173400022444 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import logging import py.io import six import pytest from _pytest.logging import ColoredLevelFormatter def test_coloredlogformatter(): logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" record = logging.LogRecord( name="dummy", level=logging.INFO, pathname="dummypath", lineno=10, msg="Test Message", args=(), exc_info=False, ) class ColorConfig(object): class option(object): pass tw = py.io.TerminalWriter() tw.hasmarkup = True formatter = ColoredLevelFormatter(tw, logfmt) output = formatter.format(record) assert output == ( "dummypath 10 \x1b[32mINFO \x1b[0m Test Message" ) tw.hasmarkup = False formatter = ColoredLevelFormatter(tw, logfmt) output = formatter.format(record) assert output == ("dummypath 10 INFO Test Message") @pytest.mark.skipif( six.PY2, reason="Formatter classes don't support format styles in PY2" ) def test_multiline_message(): from _pytest.logging import PercentStyleMultiline logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s" record = logging.LogRecord( name="dummy", level=logging.INFO, pathname="dummypath", lineno=10, msg="Test Message line1\nline2", args=(), exc_info=False, ) # this is called by logging.Formatter.format record.message = record.getMessage() style = PercentStyleMultiline(logfmt) output = style.format(record) assert output == ( "dummypath 10 INFO Test Message line1\n" " line2" ) pytest-4.6.9/testing/logging/test_fixture.py0000664000372000037200000001027313604173400022127 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import logging import pytest logger = logging.getLogger(__name__) sublogger = logging.getLogger(__name__ + ".baz") def test_fixture_help(testdir): result = testdir.runpytest("--fixtures") result.stdout.fnmatch_lines(["*caplog*"]) def test_change_level(caplog): caplog.set_level(logging.INFO) logger.debug("handler DEBUG level") logger.info("handler INFO level") caplog.set_level(logging.CRITICAL, logger=sublogger.name) sublogger.warning("logger WARNING level") sublogger.critical("logger CRITICAL level") assert "DEBUG" not in caplog.text assert "INFO" in caplog.text assert "WARNING" not in caplog.text assert "CRITICAL" in caplog.text def test_change_level_undo(testdir): """Ensure that 'set_level' is undone after the end of the test""" testdir.makepyfile( """ import logging def test1(caplog): caplog.set_level(logging.INFO) # using + operator here so fnmatch_lines doesn't match the code in the traceback logging.info('log from ' + 'test1') assert 0 def test2(caplog): # using + operator here so fnmatch_lines doesn't match the code in the traceback logging.info('log from ' + 'test2') assert 0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"]) assert "log from test2" not in result.stdout.str() def test_with_statement(caplog): with caplog.at_level(logging.INFO): logger.debug("handler DEBUG level") logger.info("handler INFO level") with caplog.at_level(logging.CRITICAL, logger=sublogger.name): sublogger.warning("logger WARNING level") sublogger.critical("logger CRITICAL level") assert "DEBUG" not in caplog.text assert "INFO" in caplog.text assert "WARNING" not in caplog.text assert "CRITICAL" in caplog.text def test_log_access(caplog): caplog.set_level(logging.INFO) logger.info("boo %s", "arg") assert caplog.records[0].levelname == "INFO" assert caplog.records[0].msg == "boo %s" assert "boo arg" in caplog.text def test_messages(caplog): caplog.set_level(logging.INFO) logger.info("boo %s", "arg") logger.info("bar %s\nbaz %s", "arg1", "arg2") assert "boo arg" == caplog.messages[0] assert "bar arg1\nbaz arg2" == caplog.messages[1] assert caplog.text.count("\n") > len(caplog.messages) assert len(caplog.text.splitlines()) > len(caplog.messages) try: raise Exception("test") except Exception: logger.exception("oops") assert "oops" in caplog.text assert "oops" in caplog.messages[-1] # Tracebacks are stored in the record and not added until the formatter or handler. assert "Exception" in caplog.text assert "Exception" not in caplog.messages[-1] def test_record_tuples(caplog): caplog.set_level(logging.INFO) logger.info("boo %s", "arg") assert caplog.record_tuples == [(__name__, logging.INFO, "boo arg")] def test_unicode(caplog): caplog.set_level(logging.INFO) logger.info(u"bū") assert caplog.records[0].levelname == "INFO" assert caplog.records[0].msg == u"bū" assert u"bū" in caplog.text def test_clear(caplog): caplog.set_level(logging.INFO) logger.info(u"bū") assert len(caplog.records) assert caplog.text caplog.clear() assert not len(caplog.records) assert not caplog.text @pytest.fixture def logging_during_setup_and_teardown(caplog): caplog.set_level("INFO") logger.info("a_setup_log") yield logger.info("a_teardown_log") assert [x.message for x in caplog.get_records("teardown")] == ["a_teardown_log"] def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardown): assert not caplog.records assert not caplog.get_records("call") logger.info("a_call_log") assert [x.message for x in caplog.get_records("call")] == ["a_call_log"] assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"] # This reaches into private API, don't use this type of thing in real tests! assert set(caplog._item.catch_log_handlers.keys()) == {"setup", "call"} pytest-4.6.9/testing/logging/test_reporting.py0000664000372000037200000010233313604173400022451 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import os import re from io import open import six import pytest def test_nothing_logged(testdir): testdir.makepyfile( """ import sys def test_foo(): sys.stdout.write('text going to stdout') sys.stderr.write('text going to stderr') assert False """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) with pytest.raises(pytest.fail.Exception): result.stdout.fnmatch_lines(["*- Captured *log call -*"]) def test_messages_logged(testdir): testdir.makepyfile( """ import sys import logging logger = logging.getLogger(__name__) def test_foo(): sys.stdout.write('text going to stdout') sys.stderr.write('text going to stderr') logger.info('text going to logger') assert False """ ) result = testdir.runpytest("--log-level=INFO") assert result.ret == 1 result.stdout.fnmatch_lines(["*- Captured *log call -*", "*text going to logger*"]) result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) def test_root_logger_affected(testdir): testdir.makepyfile( """ import logging logger = logging.getLogger() def test_foo(): logger.info('info text ' + 'going to logger') logger.warning('warning text ' + 'going to logger') logger.error('error text ' + 'going to logger') assert 0 """ ) log_file = testdir.tmpdir.join("pytest.log").strpath result = testdir.runpytest("--log-level=ERROR", "--log-file=pytest.log") assert result.ret == 1 # The capture log calls in the stdout section only contain the # logger.error msg, because of --log-level=ERROR. result.stdout.fnmatch_lines(["*error text going to logger*"]) stdout = result.stdout.str() assert "warning text going to logger" not in stdout assert "info text going to logger" not in stdout # The log file should contain the warning and the error log messages and # not the info one, because the default level of the root logger is # WARNING. assert os.path.isfile(log_file) with open(log_file) as rfh: contents = rfh.read() assert "info text going to logger" not in contents assert "warning text going to logger" in contents assert "error text going to logger" in contents def test_log_cli_level_log_level_interaction(testdir): testdir.makepyfile( """ import logging logger = logging.getLogger() def test_foo(): logger.debug('debug text ' + 'going to logger') logger.info('info text ' + 'going to logger') logger.warning('warning text ' + 'going to logger') logger.error('error text ' + 'going to logger') assert 0 """ ) result = testdir.runpytest("--log-cli-level=INFO", "--log-level=ERROR") assert result.ret == 1 result.stdout.fnmatch_lines( [ "*-- live log call --*", "*INFO*info text going to logger", "*WARNING*warning text going to logger", "*ERROR*error text going to logger", "=* 1 failed in *=", ] ) assert "DEBUG" not in result.stdout.str() def test_setup_logging(testdir): testdir.makepyfile( """ import logging logger = logging.getLogger(__name__) def setup_function(function): logger.info('text going to logger from setup') def test_foo(): logger.info('text going to logger from call') assert False """ ) result = testdir.runpytest("--log-level=INFO") assert result.ret == 1 result.stdout.fnmatch_lines( [ "*- Captured *log setup -*", "*text going to logger from setup*", "*- Captured *log call -*", "*text going to logger from call*", ] ) def test_teardown_logging(testdir): testdir.makepyfile( """ import logging logger = logging.getLogger(__name__) def test_foo(): logger.info('text going to logger from call') def teardown_function(function): logger.info('text going to logger from teardown') assert False """ ) result = testdir.runpytest("--log-level=INFO") assert result.ret == 1 result.stdout.fnmatch_lines( [ "*- Captured *log call -*", "*text going to logger from call*", "*- Captured *log teardown -*", "*text going to logger from teardown*", ] ) def test_disable_log_capturing(testdir): testdir.makepyfile( """ import sys import logging logger = logging.getLogger(__name__) def test_foo(): sys.stdout.write('text going to stdout') logger.warning('catch me if you can!') sys.stderr.write('text going to stderr') assert False """ ) result = testdir.runpytest("--no-print-logs") print(result.stdout) assert result.ret == 1 result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) with pytest.raises(pytest.fail.Exception): result.stdout.fnmatch_lines(["*- Captured *log call -*"]) def test_disable_log_capturing_ini(testdir): testdir.makeini( """ [pytest] log_print=False """ ) testdir.makepyfile( """ import sys import logging logger = logging.getLogger(__name__) def test_foo(): sys.stdout.write('text going to stdout') logger.warning('catch me if you can!') sys.stderr.write('text going to stderr') assert False """ ) result = testdir.runpytest() print(result.stdout) assert result.ret == 1 result.stdout.fnmatch_lines(["*- Captured stdout call -*", "text going to stdout"]) result.stdout.fnmatch_lines(["*- Captured stderr call -*", "text going to stderr"]) with pytest.raises(pytest.fail.Exception): result.stdout.fnmatch_lines(["*- Captured *log call -*"]) @pytest.mark.parametrize("enabled", [True, False]) def test_log_cli_enabled_disabled(testdir, enabled): msg = "critical message logged by test" testdir.makepyfile( """ import logging def test_log_cli(): logging.critical("{}") """.format( msg ) ) if enabled: testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() if enabled: result.stdout.fnmatch_lines( [ "test_log_cli_enabled_disabled.py::test_log_cli ", "*-- live log call --*", "CRITICAL *test_log_cli_enabled_disabled.py* critical message logged by test", "PASSED*", ] ) else: assert msg not in result.stdout.str() def test_log_cli_default_level(testdir): # Default log file level testdir.makepyfile( """ import pytest import logging def test_log_cli(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_cli_handler.level == logging.NOTSET logging.getLogger('catchlog').info("INFO message won't be shown") logging.getLogger('catchlog').warning("WARNING message will be shown") """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines( [ "test_log_cli_default_level.py::test_log_cli ", "WARNING*test_log_cli_default_level.py* message will be shown*", ] ) assert "INFO message won't be shown" not in result.stdout.str() # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 def test_log_cli_default_level_multiple_tests(testdir, request): """Ensure we reset the first newline added by the live logger between tests""" filename = request.node.name + ".py" testdir.makepyfile( """ import logging def test_log_1(): logging.warning("log message from test_log_1") def test_log_2(): logging.warning("log message from test_log_2") """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "{}::test_log_1 ".format(filename), "*WARNING*log message from test_log_1*", "PASSED *50%*", "{}::test_log_2 ".format(filename), "*WARNING*log message from test_log_2*", "PASSED *100%*", "=* 2 passed in *=", ] ) def test_log_cli_default_level_sections(testdir, request): """Check that with live logging enable we are printing the correct headers during start/setup/call/teardown/finish.""" filename = request.node.name + ".py" testdir.makeconftest( """ import pytest import logging def pytest_runtest_logstart(): logging.warning('>>>>> START >>>>>') def pytest_runtest_logfinish(): logging.warning('<<<<< END <<<<<<<') """ ) testdir.makepyfile( """ import pytest import logging @pytest.fixture def fix(request): logging.warning("log message from setup of {}".format(request.node.name)) yield logging.warning("log message from teardown of {}".format(request.node.name)) def test_log_1(fix): logging.warning("log message from test_log_1") def test_log_2(fix): logging.warning("log message from test_log_2") """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "{}::test_log_1 ".format(filename), "*-- live log start --*", "*WARNING* >>>>> START >>>>>*", "*-- live log setup --*", "*WARNING*log message from setup of test_log_1*", "*-- live log call --*", "*WARNING*log message from test_log_1*", "PASSED *50%*", "*-- live log teardown --*", "*WARNING*log message from teardown of test_log_1*", "*-- live log finish --*", "*WARNING* <<<<< END <<<<<<<*", "{}::test_log_2 ".format(filename), "*-- live log start --*", "*WARNING* >>>>> START >>>>>*", "*-- live log setup --*", "*WARNING*log message from setup of test_log_2*", "*-- live log call --*", "*WARNING*log message from test_log_2*", "PASSED *100%*", "*-- live log teardown --*", "*WARNING*log message from teardown of test_log_2*", "*-- live log finish --*", "*WARNING* <<<<< END <<<<<<<*", "=* 2 passed in *=", ] ) def test_live_logs_unknown_sections(testdir, request): """Check that with live logging enable we are printing the correct headers during start/setup/call/teardown/finish.""" filename = request.node.name + ".py" testdir.makeconftest( """ import pytest import logging def pytest_runtest_protocol(item, nextitem): logging.warning('Unknown Section!') def pytest_runtest_logstart(): logging.warning('>>>>> START >>>>>') def pytest_runtest_logfinish(): logging.warning('<<<<< END <<<<<<<') """ ) testdir.makepyfile( """ import pytest import logging @pytest.fixture def fix(request): logging.warning("log message from setup of {}".format(request.node.name)) yield logging.warning("log message from teardown of {}".format(request.node.name)) def test_log_1(fix): logging.warning("log message from test_log_1") """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*WARNING*Unknown Section*", "{}::test_log_1 ".format(filename), "*WARNING* >>>>> START >>>>>*", "*-- live log setup --*", "*WARNING*log message from setup of test_log_1*", "*-- live log call --*", "*WARNING*log message from test_log_1*", "PASSED *100%*", "*-- live log teardown --*", "*WARNING*log message from teardown of test_log_1*", "*WARNING* <<<<< END <<<<<<<*", "=* 1 passed in *=", ] ) def test_sections_single_new_line_after_test_outcome(testdir, request): """Check that only a single new line is written between log messages during teardown/finish.""" filename = request.node.name + ".py" testdir.makeconftest( """ import pytest import logging def pytest_runtest_logstart(): logging.warning('>>>>> START >>>>>') def pytest_runtest_logfinish(): logging.warning('<<<<< END <<<<<<<') logging.warning('<<<<< END <<<<<<<') """ ) testdir.makepyfile( """ import pytest import logging @pytest.fixture def fix(request): logging.warning("log message from setup of {}".format(request.node.name)) yield logging.warning("log message from teardown of {}".format(request.node.name)) logging.warning("log message from teardown of {}".format(request.node.name)) def test_log_1(fix): logging.warning("log message from test_log_1") """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "{}::test_log_1 ".format(filename), "*-- live log start --*", "*WARNING* >>>>> START >>>>>*", "*-- live log setup --*", "*WARNING*log message from setup of test_log_1*", "*-- live log call --*", "*WARNING*log message from test_log_1*", "PASSED *100%*", "*-- live log teardown --*", "*WARNING*log message from teardown of test_log_1*", "*-- live log finish --*", "*WARNING* <<<<< END <<<<<<<*", "*WARNING* <<<<< END <<<<<<<*", "=* 1 passed in *=", ] ) assert ( re.search( r"(.+)live log teardown(.+)\nWARNING(.+)\nWARNING(.+)", result.stdout.str(), re.MULTILINE, ) is not None ) assert ( re.search( r"(.+)live log finish(.+)\nWARNING(.+)\nWARNING(.+)", result.stdout.str(), re.MULTILINE, ) is not None ) def test_log_cli_level(testdir): # Default log file level testdir.makepyfile( """ import pytest import logging def test_log_cli(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_cli_handler.level == logging.INFO logging.getLogger('catchlog').debug("This log message won't be shown") logging.getLogger('catchlog').info("This log message will be shown") print('PASSED') """ ) testdir.makeini( """ [pytest] log_cli=true """ ) result = testdir.runpytest("-s", "--log-cli-level=INFO") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines( [ "*test_log_cli_level.py*This log message will be shown", "PASSED", # 'PASSED' on its own line because the log message prints a new line ] ) assert "This log message won't be shown" not in result.stdout.str() # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 result = testdir.runpytest("-s", "--log-level=INFO") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines( [ "*test_log_cli_level.py* This log message will be shown", "PASSED", # 'PASSED' on its own line because the log message prints a new line ] ) assert "This log message won't be shown" not in result.stdout.str() # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 def test_log_cli_ini_level(testdir): testdir.makeini( """ [pytest] log_cli=true log_cli_level = INFO """ ) testdir.makepyfile( """ import pytest import logging def test_log_cli(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_cli_handler.level == logging.INFO logging.getLogger('catchlog').debug("This log message won't be shown") logging.getLogger('catchlog').info("This log message will be shown") print('PASSED') """ ) result = testdir.runpytest("-s") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines( [ "*test_log_cli_ini_level.py* This log message will be shown", "PASSED", # 'PASSED' on its own line because the log message prints a new line ] ) assert "This log message won't be shown" not in result.stdout.str() # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 @pytest.mark.parametrize( "cli_args", ["", "--log-level=WARNING", "--log-file-level=WARNING", "--log-cli-level=WARNING"], ) def test_log_cli_auto_enable(testdir, request, cli_args): """Check that live logs are enabled if --log-level or --log-cli-level is passed on the CLI. It should not be auto enabled if the same configs are set on the INI file. """ testdir.makepyfile( """ import logging def test_log_1(): logging.info("log message from test_log_1 not to be shown") logging.warning("log message from test_log_1") """ ) testdir.makeini( """ [pytest] log_level=INFO log_cli_level=INFO """ ) result = testdir.runpytest(cli_args) stdout = result.stdout.str() if cli_args == "--log-cli-level=WARNING": result.stdout.fnmatch_lines( [ "*::test_log_1 ", "*-- live log call --*", "*WARNING*log message from test_log_1*", "PASSED *100%*", "=* 1 passed in *=", ] ) assert "INFO" not in stdout else: result.stdout.fnmatch_lines( ["*test_log_cli_auto_enable*100%*", "=* 1 passed in *="] ) assert "INFO" not in stdout assert "WARNING" not in stdout def test_log_file_cli(testdir): # Default log file level testdir.makepyfile( """ import pytest import logging def test_log_file(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_file_handler.level == logging.WARNING logging.getLogger('catchlog').info("This log message won't be shown") logging.getLogger('catchlog').warning("This log message will be shown") print('PASSED') """ ) log_file = testdir.tmpdir.join("pytest.log").strpath result = testdir.runpytest( "-s", "--log-file={}".format(log_file), "--log-file-level=WARNING" ) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines(["test_log_file_cli.py PASSED"]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file) as rfh: contents = rfh.read() assert "This log message will be shown" in contents assert "This log message won't be shown" not in contents def test_log_file_cli_level(testdir): # Default log file level testdir.makepyfile( """ import pytest import logging def test_log_file(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_file_handler.level == logging.INFO logging.getLogger('catchlog').debug("This log message won't be shown") logging.getLogger('catchlog').info("This log message will be shown") print('PASSED') """ ) log_file = testdir.tmpdir.join("pytest.log").strpath result = testdir.runpytest( "-s", "--log-file={}".format(log_file), "--log-file-level=INFO" ) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines(["test_log_file_cli_level.py PASSED"]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file) as rfh: contents = rfh.read() assert "This log message will be shown" in contents assert "This log message won't be shown" not in contents def test_log_level_not_changed_by_default(testdir): testdir.makepyfile( """ import logging def test_log_file(): assert logging.getLogger().level == logging.WARNING """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines(["* 1 passed in *"]) def test_log_file_ini(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level=WARNING """.format( log_file ) ) testdir.makepyfile( """ import pytest import logging def test_log_file(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_file_handler.level == logging.WARNING logging.getLogger('catchlog').info("This log message won't be shown") logging.getLogger('catchlog').warning("This log message will be shown") print('PASSED') """ ) result = testdir.runpytest("-s") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines(["test_log_file_ini.py PASSED"]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file) as rfh: contents = rfh.read() assert "This log message will be shown" in contents assert "This log message won't be shown" not in contents def test_log_file_ini_level(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level = INFO """.format( log_file ) ) testdir.makepyfile( """ import pytest import logging def test_log_file(request): plugin = request.config.pluginmanager.getplugin('logging-plugin') assert plugin.log_file_handler.level == logging.INFO logging.getLogger('catchlog').debug("This log message won't be shown") logging.getLogger('catchlog').info("This log message will be shown") print('PASSED') """ ) result = testdir.runpytest("-s") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines(["test_log_file_ini_level.py PASSED"]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file) as rfh: contents = rfh.read() assert "This log message will be shown" in contents assert "This log message won't be shown" not in contents def test_log_file_unicode(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level = INFO """.format( log_file ) ) testdir.makepyfile( """ # -*- coding: utf-8 -*- from __future__ import unicode_literals import logging def test_log_file(): logging.getLogger('catchlog').info("Normal message") logging.getLogger('catchlog').info("├") logging.getLogger('catchlog').info("Another normal message") """ ) result = testdir.runpytest() # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file, encoding="utf-8") as rfh: contents = rfh.read() assert "Normal message" in contents assert u"├" in contents assert "Another normal message" in contents @pytest.mark.parametrize("has_capture_manager", [True, False]) def test_live_logging_suspends_capture(has_capture_manager, request): """Test that capture manager is suspended when we emitting messages for live logging. This tests the implementation calls instead of behavior because it is difficult/impossible to do it using ``testdir`` facilities because they do their own capturing. We parametrize the test to also make sure _LiveLoggingStreamHandler works correctly if no capture manager plugin is installed. """ import logging import contextlib from functools import partial from _pytest.logging import _LiveLoggingStreamHandler class MockCaptureManager: calls = [] @contextlib.contextmanager def global_and_fixture_disabled(self): self.calls.append("enter disabled") yield self.calls.append("exit disabled") class DummyTerminal(six.StringIO): def section(self, *args, **kwargs): pass out_file = DummyTerminal() capture_manager = MockCaptureManager() if has_capture_manager else None handler = _LiveLoggingStreamHandler(out_file, capture_manager) handler.set_when("call") logger = logging.getLogger(__name__ + ".test_live_logging_suspends_capture") logger.addHandler(handler) request.addfinalizer(partial(logger.removeHandler, handler)) logger.critical("some message") if has_capture_manager: assert MockCaptureManager.calls == ["enter disabled", "exit disabled"] else: assert MockCaptureManager.calls == [] assert out_file.getvalue() == "\nsome message\n" def test_collection_live_logging(testdir): testdir.makepyfile( """ import logging logging.getLogger().info("Normal message") """ ) result = testdir.runpytest("--log-cli-level=INFO") result.stdout.fnmatch_lines( ["*--- live log collection ---*", "*Normal message*", "collected 0 items"] ) @pytest.mark.parametrize("verbose", ["", "-q", "-qq"]) def test_collection_collect_only_live_logging(testdir, verbose): testdir.makepyfile( """ def test_simple(): pass """ ) result = testdir.runpytest("--collect-only", "--log-cli-level=INFO", verbose) expected_lines = [] if not verbose: expected_lines.extend( [ "*collected 1 item*", "**", "*no tests ran*", ] ) elif verbose == "-q": assert "collected 1 item*" not in result.stdout.str() expected_lines.extend( [ "*test_collection_collect_only_live_logging.py::test_simple*", "no tests ran in * seconds", ] ) elif verbose == "-qq": assert "collected 1 item*" not in result.stdout.str() expected_lines.extend(["*test_collection_collect_only_live_logging.py: 1*"]) result.stdout.fnmatch_lines(expected_lines) def test_collection_logging_to_file(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level = INFO """.format( log_file ) ) testdir.makepyfile( """ import logging logging.getLogger().info("Normal message") def test_simple(): logging.getLogger().debug("debug message in test_simple") logging.getLogger().info("info message in test_simple") """ ) result = testdir.runpytest() assert "--- live log collection ---" not in result.stdout.str() assert result.ret == 0 assert os.path.isfile(log_file) with open(log_file, encoding="utf-8") as rfh: contents = rfh.read() assert "Normal message" in contents assert "debug message in test_simple" not in contents assert "info message in test_simple" in contents def test_log_in_hooks(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level = INFO log_cli=true """.format( log_file ) ) testdir.makeconftest( """ import logging def pytest_runtestloop(session): logging.info('runtestloop') def pytest_sessionstart(session): logging.info('sessionstart') def pytest_sessionfinish(session, exitstatus): logging.info('sessionfinish') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*sessionstart*", "*runtestloop*", "*sessionfinish*"]) with open(log_file) as rfh: contents = rfh.read() assert "sessionstart" in contents assert "runtestloop" in contents assert "sessionfinish" in contents def test_log_in_runtest_logreport(testdir): log_file = testdir.tmpdir.join("pytest.log").strpath testdir.makeini( """ [pytest] log_file={} log_file_level = INFO log_cli=true """.format( log_file ) ) testdir.makeconftest( """ import logging logger = logging.getLogger(__name__) def pytest_runtest_logreport(report): logger.info("logreport") """ ) testdir.makepyfile( """ def test_first(): assert True """ ) testdir.runpytest() with open(log_file) as rfh: contents = rfh.read() assert contents.count("logreport") == 3 def test_log_set_path(testdir): report_dir_base = testdir.tmpdir.strpath testdir.makeini( """ [pytest] log_file_level = DEBUG log_cli=true """ ) testdir.makeconftest( """ import os import pytest @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_setup(item): config = item.config logging_plugin = config.pluginmanager.get_plugin("logging-plugin") report_file = os.path.join({}, item._request.node.name) logging_plugin.set_log_path(report_file) yield """.format( repr(report_dir_base) ) ) testdir.makepyfile( """ import logging logger = logging.getLogger("testcase-logger") def test_first(): logger.info("message from test 1") assert True def test_second(): logger.debug("message from test 2") assert True """ ) testdir.runpytest() with open(os.path.join(report_dir_base, "test_first"), "r") as rfh: content = rfh.read() assert "message from test 1" in content with open(os.path.join(report_dir_base, "test_second"), "r") as rfh: content = rfh.read() assert "message from test 2" in content def test_colored_captured_log(testdir): """ Test that the level names of captured log messages of a failing test are colored. """ testdir.makepyfile( """ import logging logger = logging.getLogger(__name__) def test_foo(): logger.info('text going to logger from call') assert False """ ) result = testdir.runpytest("--log-level=INFO", "--color=yes") assert result.ret == 1 result.stdout.fnmatch_lines( [ "*-- Captured log call --*", "\x1b[32mINFO \x1b[0m*text going to logger from call", ] ) def test_colored_ansi_esc_caplogtext(testdir): """ Make sure that caplog.text does not contain ANSI escape sequences. """ testdir.makepyfile( """ import logging logger = logging.getLogger(__name__) def test_foo(caplog): logger.info('text going to logger from call') assert '\x1b' not in caplog.text """ ) result = testdir.runpytest("--log-level=INFO", "--color=yes") assert result.ret == 0 pytest-4.6.9/testing/python/0000775000372000037200000000000013604173421016723 5ustar travistravis00000000000000pytest-4.6.9/testing/python/approx.py0000664000372000037200000004430213604173400020606 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import doctest import operator import sys from decimal import Decimal from fractions import Fraction from operator import eq from operator import ne import pytest from pytest import approx inf, nan = float("inf"), float("nan") class MyDocTestRunner(doctest.DocTestRunner): def __init__(self): doctest.DocTestRunner.__init__(self) def report_failure(self, out, test, example, got): raise AssertionError( "'{}' evaluates to '{}', not '{}'".format( example.source.strip(), got.strip(), example.want.strip() ) ) class TestApprox(object): @pytest.fixture def plus_minus(self): return u"\u00b1" if sys.version_info[0] > 2 else u"+-" def test_repr_string(self, plus_minus): tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf" assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1) assert repr( approx([1.0, 2.0]) ) == "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])".format( pm=plus_minus, tol1=tol1, tol2=tol2 ) assert repr( approx((1.0, 2.0)) ) == "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))".format( pm=plus_minus, tol1=tol1, tol2=tol2 ) assert repr(approx(inf)) == "inf" assert repr(approx(1.0, rel=nan)) == "1.0 {pm} ???".format(pm=plus_minus) assert repr(approx(1.0, rel=inf)) == "1.0 {pm} {infr}".format( pm=plus_minus, infr=infr ) assert repr(approx(1.0j, rel=inf)) == "1j" # Dictionaries aren't ordered, so we need to check both orders. assert repr(approx({"a": 1.0, "b": 2.0})) in ( "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format( pm=plus_minus, tol1=tol1, tol2=tol2 ), "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format( pm=plus_minus, tol1=tol1, tol2=tol2 ), ) @pytest.mark.parametrize( "value, repr_string", [ (5.0, "approx(5.0 {pm} 5.0e-06)"), ([5.0], "approx([5.0 {pm} 5.0e-06])"), ([[5.0]], "approx([[5.0 {pm} 5.0e-06]])"), ([[5.0, 6.0]], "approx([[5.0 {pm} 5.0e-06, 6.0 {pm} 6.0e-06]])"), ([[5.0], [6.0]], "approx([[5.0 {pm} 5.0e-06], [6.0 {pm} 6.0e-06]])"), ], ) def test_repr_nd_array(self, plus_minus, value, repr_string): """Make sure that arrays of all different dimensions are repr'd correctly.""" np = pytest.importorskip("numpy") np_array = np.array(value) assert repr(approx(np_array)) == repr_string.format(pm=plus_minus) def test_operator_overloading(self): assert 1 == approx(1, rel=1e-6, abs=1e-12) assert not (1 != approx(1, rel=1e-6, abs=1e-12)) assert 10 != approx(1, rel=1e-6, abs=1e-12) assert not (10 == approx(1, rel=1e-6, abs=1e-12)) def test_exactly_equal(self): examples = [ (2.0, 2.0), (0.1e200, 0.1e200), (1.123e-300, 1.123e-300), (12345, 12345.0), (0.0, -0.0), (345678, 345678), (Decimal("1.0001"), Decimal("1.0001")), (Fraction(1, 3), Fraction(-1, -3)), ] for a, x in examples: assert a == approx(x) def test_opposite_sign(self): examples = [(eq, 1e-100, -1e-100), (ne, 1e100, -1e100)] for op, a, x in examples: assert op(a, approx(x)) def test_zero_tolerance(self): within_1e10 = [(1.1e-100, 1e-100), (-1.1e-100, -1e-100)] for a, x in within_1e10: assert x == approx(x, rel=0.0, abs=0.0) assert a != approx(x, rel=0.0, abs=0.0) assert a == approx(x, rel=0.0, abs=5e-101) assert a != approx(x, rel=0.0, abs=5e-102) assert a == approx(x, rel=5e-1, abs=0.0) assert a != approx(x, rel=5e-2, abs=0.0) def test_negative_tolerance(self): # Negative tolerances are not allowed. illegal_kwargs = [ dict(rel=-1e100), dict(abs=-1e100), dict(rel=1e100, abs=-1e100), dict(rel=-1e100, abs=1e100), dict(rel=-1e100, abs=-1e100), ] for kwargs in illegal_kwargs: with pytest.raises(ValueError): 1.1 == approx(1, **kwargs) def test_inf_tolerance(self): # Everything should be equal if the tolerance is infinite. large_diffs = [(1, 1000), (1e-50, 1e50), (-1.0, -1e300), (0.0, 10)] for a, x in large_diffs: assert a != approx(x, rel=0.0, abs=0.0) assert a == approx(x, rel=inf, abs=0.0) assert a == approx(x, rel=0.0, abs=inf) assert a == approx(x, rel=inf, abs=inf) def test_inf_tolerance_expecting_zero(self): # If the relative tolerance is zero but the expected value is infinite, # the actual tolerance is a NaN, which should be an error. illegal_kwargs = [dict(rel=inf, abs=0.0), dict(rel=inf, abs=inf)] for kwargs in illegal_kwargs: with pytest.raises(ValueError): 1 == approx(0, **kwargs) def test_nan_tolerance(self): illegal_kwargs = [dict(rel=nan), dict(abs=nan), dict(rel=nan, abs=nan)] for kwargs in illegal_kwargs: with pytest.raises(ValueError): 1.1 == approx(1, **kwargs) def test_reasonable_defaults(self): # Whatever the defaults are, they should work for numbers close to 1 # than have a small amount of floating-point error. assert 0.1 + 0.2 == approx(0.3) def test_default_tolerances(self): # This tests the defaults as they are currently set. If you change the # defaults, this test will fail but you should feel free to change it. # None of the other tests (except the doctests) should be affected by # the choice of defaults. examples = [ # Relative tolerance used. (eq, 1e100 + 1e94, 1e100), (ne, 1e100 + 2e94, 1e100), (eq, 1e0 + 1e-6, 1e0), (ne, 1e0 + 2e-6, 1e0), # Absolute tolerance used. (eq, 1e-100, +1e-106), (eq, 1e-100, +2e-106), (eq, 1e-100, 0), ] for op, a, x in examples: assert op(a, approx(x)) def test_custom_tolerances(self): assert 1e8 + 1e0 == approx(1e8, rel=5e-8, abs=5e0) assert 1e8 + 1e0 == approx(1e8, rel=5e-9, abs=5e0) assert 1e8 + 1e0 == approx(1e8, rel=5e-8, abs=5e-1) assert 1e8 + 1e0 != approx(1e8, rel=5e-9, abs=5e-1) assert 1e0 + 1e-8 == approx(1e0, rel=5e-8, abs=5e-8) assert 1e0 + 1e-8 == approx(1e0, rel=5e-9, abs=5e-8) assert 1e0 + 1e-8 == approx(1e0, rel=5e-8, abs=5e-9) assert 1e0 + 1e-8 != approx(1e0, rel=5e-9, abs=5e-9) assert 1e-8 + 1e-16 == approx(1e-8, rel=5e-8, abs=5e-16) assert 1e-8 + 1e-16 == approx(1e-8, rel=5e-9, abs=5e-16) assert 1e-8 + 1e-16 == approx(1e-8, rel=5e-8, abs=5e-17) assert 1e-8 + 1e-16 != approx(1e-8, rel=5e-9, abs=5e-17) def test_relative_tolerance(self): within_1e8_rel = [(1e8 + 1e0, 1e8), (1e0 + 1e-8, 1e0), (1e-8 + 1e-16, 1e-8)] for a, x in within_1e8_rel: assert a == approx(x, rel=5e-8, abs=0.0) assert a != approx(x, rel=5e-9, abs=0.0) def test_absolute_tolerance(self): within_1e8_abs = [(1e8 + 9e-9, 1e8), (1e0 + 9e-9, 1e0), (1e-8 + 9e-9, 1e-8)] for a, x in within_1e8_abs: assert a == approx(x, rel=0, abs=5e-8) assert a != approx(x, rel=0, abs=5e-9) def test_expecting_zero(self): examples = [ (ne, 1e-6, 0.0), (ne, -1e-6, 0.0), (eq, 1e-12, 0.0), (eq, -1e-12, 0.0), (ne, 2e-12, 0.0), (ne, -2e-12, 0.0), (ne, inf, 0.0), (ne, nan, 0.0), ] for op, a, x in examples: assert op(a, approx(x, rel=0.0, abs=1e-12)) assert op(a, approx(x, rel=1e-6, abs=1e-12)) def test_expecting_inf(self): examples = [ (eq, inf, inf), (eq, -inf, -inf), (ne, inf, -inf), (ne, 0.0, inf), (ne, nan, inf), ] for op, a, x in examples: assert op(a, approx(x)) def test_expecting_nan(self): examples = [ (eq, nan, nan), (eq, -nan, -nan), (eq, nan, -nan), (ne, 0.0, nan), (ne, inf, nan), ] for op, a, x in examples: # Nothing is equal to NaN by default. assert a != approx(x) # If ``nan_ok=True``, then NaN is equal to NaN. assert op(a, approx(x, nan_ok=True)) def test_int(self): within_1e6 = [(1000001, 1000000), (-1000001, -1000000)] for a, x in within_1e6: assert a == approx(x, rel=5e-6, abs=0) assert a != approx(x, rel=5e-7, abs=0) assert approx(x, rel=5e-6, abs=0) == a assert approx(x, rel=5e-7, abs=0) != a def test_decimal(self): within_1e6 = [ (Decimal("1.000001"), Decimal("1.0")), (Decimal("-1.000001"), Decimal("-1.0")), ] for a, x in within_1e6: assert a == approx(x) assert a == approx(x, rel=Decimal("5e-6"), abs=0) assert a != approx(x, rel=Decimal("5e-7"), abs=0) assert approx(x, rel=Decimal("5e-6"), abs=0) == a assert approx(x, rel=Decimal("5e-7"), abs=0) != a def test_fraction(self): within_1e6 = [ (1 + Fraction(1, 1000000), Fraction(1)), (-1 - Fraction(-1, 1000000), Fraction(-1)), ] for a, x in within_1e6: assert a == approx(x, rel=5e-6, abs=0) assert a != approx(x, rel=5e-7, abs=0) assert approx(x, rel=5e-6, abs=0) == a assert approx(x, rel=5e-7, abs=0) != a def test_complex(self): within_1e6 = [ (1.000001 + 1.0j, 1.0 + 1.0j), (1.0 + 1.000001j, 1.0 + 1.0j), (-1.000001 + 1.0j, -1.0 + 1.0j), (1.0 - 1.000001j, 1.0 - 1.0j), ] for a, x in within_1e6: assert a == approx(x, rel=5e-6, abs=0) assert a != approx(x, rel=5e-7, abs=0) assert approx(x, rel=5e-6, abs=0) == a assert approx(x, rel=5e-7, abs=0) != a def test_list(self): actual = [1 + 1e-7, 2 + 1e-8] expected = [1, 2] # Return false if any element is outside the tolerance. assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == actual assert approx(expected, rel=5e-8, abs=0) != actual def test_list_wrong_len(self): assert [1, 2] != approx([1]) assert [1, 2] != approx([1, 2, 3]) def test_tuple(self): actual = (1 + 1e-7, 2 + 1e-8) expected = (1, 2) # Return false if any element is outside the tolerance. assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == actual assert approx(expected, rel=5e-8, abs=0) != actual def test_tuple_wrong_len(self): assert (1, 2) != approx((1,)) assert (1, 2) != approx((1, 2, 3)) def test_dict(self): actual = {"a": 1 + 1e-7, "b": 2 + 1e-8} # Dictionaries became ordered in python3.6, so switch up the order here # to make sure it doesn't matter. expected = {"b": 2, "a": 1} # Return false if any element is outside the tolerance. assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == actual assert approx(expected, rel=5e-8, abs=0) != actual def test_dict_wrong_len(self): assert {"a": 1, "b": 2} != approx({"a": 1}) assert {"a": 1, "b": 2} != approx({"a": 1, "c": 2}) assert {"a": 1, "b": 2} != approx({"a": 1, "b": 2, "c": 3}) def test_numpy_array(self): np = pytest.importorskip("numpy") actual = np.array([1 + 1e-7, 2 + 1e-8]) expected = np.array([1, 2]) # Return false if any element is outside the tolerance. assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == expected assert approx(expected, rel=5e-8, abs=0) != actual # Should be able to compare lists with numpy arrays. assert list(actual) == approx(expected, rel=5e-7, abs=0) assert list(actual) != approx(expected, rel=5e-8, abs=0) assert actual == approx(list(expected), rel=5e-7, abs=0) assert actual != approx(list(expected), rel=5e-8, abs=0) def test_numpy_tolerance_args(self): """ Check that numpy rel/abs args are handled correctly for comparison against an np.array Check both sides of the operator, hopefully it doesn't impact things. Test all permutations of where the approx and np.array() can show up """ np = pytest.importorskip("numpy") expected = 100.0 actual = 99.0 abs_diff = expected - actual rel_diff = (expected - actual) / expected tests = [ (eq, abs_diff, 0), (eq, 0, rel_diff), (ne, 0, rel_diff / 2.0), # rel diff fail (ne, abs_diff / 2.0, 0), # abs diff fail ] for op, _abs, _rel in tests: assert op(np.array(actual), approx(expected, abs=_abs, rel=_rel)) # a, b assert op(approx(expected, abs=_abs, rel=_rel), np.array(actual)) # b, a assert op(actual, approx(np.array(expected), abs=_abs, rel=_rel)) # a, b assert op(approx(np.array(expected), abs=_abs, rel=_rel), actual) # b, a assert op(np.array(actual), approx(np.array(expected), abs=_abs, rel=_rel)) assert op(approx(np.array(expected), abs=_abs, rel=_rel), np.array(actual)) def test_numpy_expecting_nan(self): np = pytest.importorskip("numpy") examples = [ (eq, nan, nan), (eq, -nan, -nan), (eq, nan, -nan), (ne, 0.0, nan), (ne, inf, nan), ] for op, a, x in examples: # Nothing is equal to NaN by default. assert np.array(a) != approx(x) assert a != approx(np.array(x)) # If ``nan_ok=True``, then NaN is equal to NaN. assert op(np.array(a), approx(x, nan_ok=True)) assert op(a, approx(np.array(x), nan_ok=True)) def test_numpy_expecting_inf(self): np = pytest.importorskip("numpy") examples = [ (eq, inf, inf), (eq, -inf, -inf), (ne, inf, -inf), (ne, 0.0, inf), (ne, nan, inf), ] for op, a, x in examples: assert op(np.array(a), approx(x)) assert op(a, approx(np.array(x))) assert op(np.array(a), approx(np.array(x))) def test_numpy_array_wrong_shape(self): np = pytest.importorskip("numpy") a12 = np.array([[1, 2]]) a21 = np.array([[1], [2]]) assert a12 != approx(a21) assert a21 != approx(a12) def test_doctests(self): parser = doctest.DocTestParser() test = parser.get_doctest( approx.__doc__, {"approx": approx}, approx.__name__, None, None ) runner = MyDocTestRunner() runner.run(test) def test_unicode_plus_minus(self, testdir): """ Comparing approx instances inside lists should not produce an error in the detailed diff. Integration test for issue #2111. """ testdir.makepyfile( """ import pytest def test_foo(): assert [3] == [pytest.approx(4)] """ ) expected = "4.0e-06" result = testdir.runpytest() result.stdout.fnmatch_lines( ["*At index 0 diff: 3 != 4 * {}".format(expected), "=* 1 failed in *="] ) @pytest.mark.parametrize( "x", [ pytest.param(None), pytest.param("string"), pytest.param(["string"], id="nested-str"), pytest.param([[1]], id="nested-list"), pytest.param({"key": "string"}, id="dict-with-string"), pytest.param({"key": {"key": 1}}, id="nested-dict"), ], ) def test_expected_value_type_error(self, x): with pytest.raises(TypeError): approx(x) @pytest.mark.parametrize( "op", [ pytest.param(operator.le, id="<="), pytest.param(operator.lt, id="<"), pytest.param(operator.ge, id=">="), pytest.param(operator.gt, id=">"), ], ) def test_comparison_operator_type_error(self, op): """ pytest.approx should raise TypeError for operators other than == and != (#2003). """ with pytest.raises(TypeError): op(1, approx(1, rel=1e-6, abs=1e-12)) def test_numpy_array_with_scalar(self): np = pytest.importorskip("numpy") actual = np.array([1 + 1e-7, 1 - 1e-8]) expected = 1.0 assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == actual assert approx(expected, rel=5e-8, abs=0) != actual def test_numpy_scalar_with_array(self): np = pytest.importorskip("numpy") actual = 1.0 expected = np.array([1 + 1e-7, 1 - 1e-8]) assert actual == approx(expected, rel=5e-7, abs=0) assert actual != approx(expected, rel=5e-8, abs=0) assert approx(expected, rel=5e-7, abs=0) == actual assert approx(expected, rel=5e-8, abs=0) != actual def test_generic_sized_iterable_object(self): class MySizedIterable(object): def __iter__(self): return iter([1, 2, 3, 4]) def __len__(self): return 4 expected = MySizedIterable() assert [1, 2, 3, 4] == approx(expected) pytest-4.6.9/testing/python/collect.py0000664000372000037200000012473513604173400020733 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import os import sys import textwrap import _pytest._code import pytest from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.nodes import Collector class TestModule(object): def test_failing_import(self, testdir): modcol = testdir.getmodulecol("import alksdjalskdjalkjals") pytest.raises(Collector.CollectError, modcol.collect) def test_import_duplicate(self, testdir): a = testdir.mkdir("a") b = testdir.mkdir("b") p = a.ensure("test_whatever.py") p.pyimport() del sys.modules["test_whatever"] b.ensure("test_whatever.py") result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*import*mismatch*", "*imported*test_whatever*", "*%s*" % a.join("test_whatever.py"), "*not the same*", "*%s*" % b.join("test_whatever.py"), "*HINT*", ] ) def test_import_prepend_append(self, testdir, monkeypatch): root1 = testdir.mkdir("root1") root2 = testdir.mkdir("root2") root1.ensure("x456.py") root2.ensure("x456.py") p = root2.join("test_x456.py") monkeypatch.syspath_prepend(str(root1)) p.write( textwrap.dedent( """\ import x456 def test(): assert x456.__file__.startswith({!r}) """.format( str(root2) ) ) ) with root2.as_cwd(): reprec = testdir.inline_run("--import-mode=append") reprec.assertoutcome(passed=0, failed=1) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_syntax_error_in_module(self, testdir): modcol = testdir.getmodulecol("this is a syntax error") pytest.raises(modcol.CollectError, modcol.collect) pytest.raises(modcol.CollectError, modcol.collect) def test_module_considers_pluginmanager_at_import(self, testdir): modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',") pytest.raises(ImportError, lambda: modcol.obj) def test_invalid_test_module_name(self, testdir): a = testdir.mkdir("a") a.ensure("test_one.part1.py") result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( [ "ImportError while importing test module*test_one.part1*", "Hint: make sure your test modules/packages have valid Python names.", ] ) @pytest.mark.parametrize("verbose", [0, 1, 2]) def test_show_traceback_import_error(self, testdir, verbose): """Import errors when collecting modules should display the traceback (#1976). With low verbosity we omit pytest and internal modules, otherwise show all traceback entries. """ testdir.makepyfile( foo_traceback_import_error=""" from bar_traceback_import_error import NOT_AVAILABLE """, bar_traceback_import_error="", ) testdir.makepyfile( """ import foo_traceback_import_error """ ) args = ("-v",) * verbose result = testdir.runpytest(*args) result.stdout.fnmatch_lines( [ "ImportError while importing test module*", "Traceback:", "*from bar_traceback_import_error import NOT_AVAILABLE", "*cannot import name *NOT_AVAILABLE*", ] ) assert result.ret == 2 stdout = result.stdout.str() for name in ("_pytest", os.path.join("py", "_path")): if verbose == 2: assert name in stdout else: assert name not in stdout def test_show_traceback_import_error_unicode(self, testdir): """Check test modules collected which raise ImportError with unicode messages are handled properly (#2336). """ testdir.makepyfile( u""" # -*- coding: utf-8 -*- raise ImportError(u'Something bad happened ☺') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "ImportError while importing test module*", "Traceback:", "*raise ImportError*Something bad happened*", ] ) assert result.ret == 2 class TestClass(object): def test_class_with_init_warning(self, testdir): testdir.makepyfile( """ class TestClass1(object): def __init__(self): pass """ ) result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( [ "*cannot collect test class 'TestClass1' because it has " "a __init__ constructor (from: test_class_with_init_warning.py)" ] ) def test_class_with_new_warning(self, testdir): testdir.makepyfile( """ class TestClass1(object): def __new__(self): pass """ ) result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( [ "*cannot collect test class 'TestClass1' because it has " "a __new__ constructor (from: test_class_with_new_warning.py)" ] ) def test_class_subclassobject(self, testdir): testdir.getmodulecol( """ class test(object): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*collected 0*"]) def test_static_method(self, testdir): """Support for collecting staticmethod tests (#2528, #2699)""" testdir.getmodulecol( """ import pytest class Test(object): @staticmethod def test_something(): pass @pytest.fixture def fix(self): return 1 @staticmethod def test_fix(fix): assert fix == 1 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*collected 2 items*", "*2 passed in*"]) def test_setup_teardown_class_as_classmethod(self, testdir): testdir.makepyfile( test_mod1=""" class TestClassMethod(object): @classmethod def setup_class(cls): pass def test_1(self): pass @classmethod def teardown_class(cls): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_issue1035_obj_has_getattr(self, testdir): modcol = testdir.getmodulecol( """ class Chameleon(object): def __getattr__(self, name): return True chameleon = Chameleon() """ ) colitems = modcol.collect() assert len(colitems) == 0 def test_issue1579_namedtuple(self, testdir): testdir.makepyfile( """ import collections TestCase = collections.namedtuple('TestCase', ['a']) """ ) result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( "*cannot collect test class 'TestCase' " "because it has a __new__ constructor*" ) def test_issue2234_property(self, testdir): testdir.makepyfile( """ class TestCase(object): @property def prop(self): raise NotImplementedError() """ ) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED class TestFunction(object): def test_getmodulecollector(self, testdir): item = testdir.getitem("def test_func(): pass") modcol = item.getparent(pytest.Module) assert isinstance(modcol, pytest.Module) assert hasattr(modcol.obj, "test_func") @pytest.mark.filterwarnings("default") def test_function_as_object_instance_ignored(self, testdir): testdir.makepyfile( """ class A(object): def __call__(self, tmpdir): 0/0 test_a = A() """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "collected 0 items", "*test_function_as_object_instance_ignored.py:2: " "*cannot collect 'test_a' because it is not a function.", ] ) @staticmethod def make_function(testdir, **kwargs): from _pytest.fixtures import FixtureManager config = testdir.parseconfigure() session = testdir.Session(config) session._fixturemanager = FixtureManager(session) return pytest.Function(config=config, parent=session, **kwargs) def test_function_equality(self, testdir, tmpdir): def func1(): pass def func2(): pass f1 = self.make_function(testdir, name="name", args=(1,), callobj=func1) assert f1 == f1 f2 = self.make_function(testdir, name="name", callobj=func2) assert f1 != f2 def test_repr_produces_actual_test_id(self, testdir): f = self.make_function( testdir, name=r"test[\xe5]", callobj=self.test_repr_produces_actual_test_id ) assert repr(f) == r"" def test_issue197_parametrize_emptyset(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize('arg', []) def test_function(arg): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) def test_single_tuple_unwraps_values(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize(('arg',), [(1,)]) def test_function(arg): assert arg == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_issue213_parametrize_value_no_equal(self, testdir): testdir.makepyfile( """ import pytest class A(object): def __eq__(self, other): raise ValueError("not possible") @pytest.mark.parametrize('arg', [A()]) def test_function(arg): assert arg.__class__.__name__ == "A" """ ) reprec = testdir.inline_run("--fulltrace") reprec.assertoutcome(passed=1) def test_parametrize_with_non_hashable_values(self, testdir): """Test parametrization with non-hashable values.""" testdir.makepyfile( """ archival_mapping = { '1.0': {'tag': '1.0'}, '1.2.2a1': {'tag': 'release-1.2.2a1'}, } import pytest @pytest.mark.parametrize('key value'.split(), archival_mapping.items()) def test_archival_to_version(key, value): assert key in archival_mapping assert value == archival_mapping[key] """ ) rec = testdir.inline_run() rec.assertoutcome(passed=2) def test_parametrize_with_non_hashable_values_indirect(self, testdir): """Test parametrization with non-hashable values with indirect parametrization.""" testdir.makepyfile( """ archival_mapping = { '1.0': {'tag': '1.0'}, '1.2.2a1': {'tag': 'release-1.2.2a1'}, } import pytest @pytest.fixture def key(request): return request.param @pytest.fixture def value(request): return request.param @pytest.mark.parametrize('key value'.split(), archival_mapping.items(), indirect=True) def test_archival_to_version(key, value): assert key in archival_mapping assert value == archival_mapping[key] """ ) rec = testdir.inline_run() rec.assertoutcome(passed=2) def test_parametrize_overrides_fixture(self, testdir): """Test parametrization when parameter overrides existing fixture with same name.""" testdir.makepyfile( """ import pytest @pytest.fixture def value(): return 'value' @pytest.mark.parametrize('value', ['overridden']) def test_overridden_via_param(value): assert value == 'overridden' @pytest.mark.parametrize('somevalue', ['overridden']) def test_not_overridden(value, somevalue): assert value == 'value' assert somevalue == 'overridden' @pytest.mark.parametrize('other,value', [('foo', 'overridden')]) def test_overridden_via_multiparam(other, value): assert other == 'foo' assert value == 'overridden' """ ) rec = testdir.inline_run() rec.assertoutcome(passed=3) def test_parametrize_overrides_parametrized_fixture(self, testdir): """Test parametrization when parameter overrides existing parametrized fixture with same name.""" testdir.makepyfile( """ import pytest @pytest.fixture(params=[1, 2]) def value(request): return request.param @pytest.mark.parametrize('value', ['overridden']) def test_overridden_via_param(value): assert value == 'overridden' """ ) rec = testdir.inline_run() rec.assertoutcome(passed=1) def test_parametrize_overrides_indirect_dependency_fixture(self, testdir): """Test parametrization when parameter overrides a fixture that a test indirectly depends on""" testdir.makepyfile( """ import pytest fix3_instantiated = False @pytest.fixture def fix1(fix2): return fix2 + '1' @pytest.fixture def fix2(fix3): return fix3 + '2' @pytest.fixture def fix3(): global fix3_instantiated fix3_instantiated = True return '3' @pytest.mark.parametrize('fix2', ['2']) def test_it(fix1): assert fix1 == '21' assert not fix3_instantiated """ ) rec = testdir.inline_run() rec.assertoutcome(passed=1) def test_parametrize_with_mark(self, testdir): items = testdir.getitems( """ import pytest @pytest.mark.foo @pytest.mark.parametrize('arg', [ 1, pytest.param(2, marks=[pytest.mark.baz, pytest.mark.bar]) ]) def test_function(arg): pass """ ) keywords = [item.keywords for item in items] assert ( "foo" in keywords[0] and "bar" not in keywords[0] and "baz" not in keywords[0] ) assert "foo" in keywords[1] and "bar" in keywords[1] and "baz" in keywords[1] def test_function_equality_with_callspec(self, testdir, tmpdir): items = testdir.getitems( """ import pytest @pytest.mark.parametrize('arg', [1,2]) def test_function(arg): pass """ ) assert items[0] != items[1] assert not (items[0] == items[1]) def test_pyfunc_call(self, testdir): item = testdir.getitem("def test_func(): raise ValueError") config = item.config class MyPlugin1(object): def pytest_pyfunc_call(self, pyfuncitem): raise ValueError class MyPlugin2(object): def pytest_pyfunc_call(self, pyfuncitem): return True config.pluginmanager.register(MyPlugin1()) config.pluginmanager.register(MyPlugin2()) config.hook.pytest_runtest_setup(item=item) config.hook.pytest_pyfunc_call(pyfuncitem=item) def test_multiple_parametrize(self, testdir): modcol = testdir.getmodulecol( """ import pytest @pytest.mark.parametrize('x', [0, 1]) @pytest.mark.parametrize('y', [2, 3]) def test1(x, y): pass """ ) colitems = modcol.collect() assert colitems[0].name == "test1[2-0]" assert colitems[1].name == "test1[2-1]" assert colitems[2].name == "test1[3-0]" assert colitems[3].name == "test1[3-1]" def test_issue751_multiple_parametrize_with_ids(self, testdir): modcol = testdir.getmodulecol( """ import pytest @pytest.mark.parametrize('x', [0], ids=['c']) @pytest.mark.parametrize('y', [0, 1], ids=['a', 'b']) class Test(object): def test1(self, x, y): pass def test2(self, x, y): pass """ ) colitems = modcol.collect()[0].collect()[0].collect() assert colitems[0].name == "test1[a-c]" assert colitems[1].name == "test1[b-c]" assert colitems[2].name == "test2[a-c]" assert colitems[3].name == "test2[b-c]" def test_parametrize_skipif(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.skipif('True') @pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)]) def test_skip_if(x): assert x < 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed, 1 skipped in *"]) def test_parametrize_skip(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.skip('') @pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)]) def test_skip(x): assert x < 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed, 1 skipped in *"]) def test_parametrize_skipif_no_skip(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.skipif('False') @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_skipif_no_skip(x): assert x < 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 failed, 2 passed in *"]) def test_parametrize_xfail(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.xfail('True') @pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)]) def test_xfail(x): assert x < 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed, 1 xfailed in *"]) def test_parametrize_passed(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.xfail('True') @pytest.mark.parametrize('x', [0, 1, pytest.param(2, marks=m)]) def test_xfail(x): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed, 1 xpassed in *"]) def test_parametrize_xfail_passed(self, testdir): testdir.makepyfile( """ import pytest m = pytest.mark.xfail('False') @pytest.mark.parametrize('x', [0, 1, m(2)]) def test_passed(x): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 3 passed in *"]) def test_function_original_name(self, testdir): items = testdir.getitems( """ import pytest @pytest.mark.parametrize('arg', [1,2]) def test_func(arg): pass """ ) assert [x.originalname for x in items] == ["test_func", "test_func"] class TestSorting(object): def test_check_equality(self, testdir): modcol = testdir.getmodulecol( """ def test_pass(): pass def test_fail(): assert 0 """ ) fn1 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn1, pytest.Function) fn2 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn2, pytest.Function) assert fn1 == fn2 assert fn1 != modcol if sys.version_info < (3, 0): assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail") assert isinstance(fn3, pytest.Function) assert not (fn1 == fn3) assert fn1 != fn3 for fn in fn1, fn2, fn3: assert fn != 3 assert fn != modcol assert fn != [1, 2, 3] assert [1, 2, 3] != fn assert modcol != fn def test_allow_sane_sorting_for_decorators(self, testdir): modcol = testdir.getmodulecol( """ def dec(f): g = lambda: f(2) g.place_as = f return g def test_b(y): pass test_b = dec(test_b) def test_a(y): pass test_a = dec(test_a) """ ) colitems = modcol.collect() assert len(colitems) == 2 assert [item.name for item in colitems] == ["test_b", "test_a"] class TestConftestCustomization(object): def test_pytest_pycollect_module(self, testdir): testdir.makeconftest( """ import pytest class MyModule(pytest.Module): pass def pytest_pycollect_makemodule(path, parent): if path.basename == "test_xyz.py": return MyModule(path, parent) """ ) testdir.makepyfile("def test_some(): pass") testdir.makepyfile(test_xyz="def test_func(): pass") result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["* 3 def test_traceback_error_during_import(self, testdir): testdir.makepyfile( """ x = 1 x = 2 x = 17 asd """ ) result = testdir.runpytest() assert result.ret != 0 out = result.stdout.str() assert "x = 1" not in out assert "x = 2" not in out result.stdout.fnmatch_lines([" *asd*", "E*NameError*"]) result = testdir.runpytest("--fulltrace") out = result.stdout.str() assert "x = 1" in out assert "x = 2" in out result.stdout.fnmatch_lines([">*asd*", "E*NameError*"]) def test_traceback_filter_error_during_fixture_collection(self, testdir): """integration test for issue #995. """ testdir.makepyfile( """ import pytest def fail_me(func): ns = {} exec('def w(): raise ValueError("fail me")', ns) return ns['w'] @pytest.fixture(scope='class') @fail_me def fail_fixture(): pass def test_failing_fixture(fail_fixture): pass """ ) result = testdir.runpytest() assert result.ret != 0 out = result.stdout.str() assert "INTERNALERROR>" not in out result.stdout.fnmatch_lines(["*ValueError: fail me*", "* 1 error in *"]) def test_filter_traceback_generated_code(self): """test that filter_traceback() works with the fact that _pytest._code.code.Code.path attribute might return an str object. In this case, one of the entries on the traceback was produced by dynamically generated code. See: https://bitbucket.org/pytest-dev/py/issues/71 This fixes #995. """ from _pytest.python import filter_traceback try: ns = {} exec("def foo(): raise ValueError", ns) ns["foo"]() except ValueError: _, _, tb = sys.exc_info() tb = _pytest._code.Traceback(tb) assert isinstance(tb[-1].path, str) assert not filter_traceback(tb[-1]) def test_filter_traceback_path_no_longer_valid(self, testdir): """test that filter_traceback() works with the fact that _pytest._code.code.Code.path attribute might return an str object. In this case, one of the files in the traceback no longer exists. This fixes #1133. """ from _pytest.python import filter_traceback testdir.syspathinsert() testdir.makepyfile( filter_traceback_entry_as_str=""" def foo(): raise ValueError """ ) try: import filter_traceback_entry_as_str filter_traceback_entry_as_str.foo() except ValueError: _, _, tb = sys.exc_info() testdir.tmpdir.join("filter_traceback_entry_as_str.py").remove() tb = _pytest._code.Traceback(tb) assert isinstance(tb[-1].path, str) assert filter_traceback(tb[-1]) class TestReportInfo(object): def test_itemreport_reportinfo(self, testdir, linecomp): testdir.makeconftest( """ import pytest class MyFunction(pytest.Function): def reportinfo(self): return "ABCDE", 42, "custom" def pytest_pycollect_makeitem(collector, name, obj): if name == "test_func": return MyFunction(name, parent=collector) """ ) item = testdir.getitem("def test_func(): pass") item.config.pluginmanager.getplugin("runner") assert item.location == ("ABCDE", 42, "custom") def test_func_reportinfo(self, testdir): item = testdir.getitem("def test_func(): pass") fspath, lineno, modpath = item.reportinfo() assert fspath == item.fspath assert lineno == 0 assert modpath == "test_func" def test_class_reportinfo(self, testdir): modcol = testdir.getmodulecol( """ # lineno 0 class TestClass(object): def test_hello(self): pass """ ) classcol = testdir.collect_by_name(modcol, "TestClass") fspath, lineno, msg = classcol.reportinfo() assert fspath == modcol.fspath assert lineno == 1 assert msg == "TestClass" @pytest.mark.filterwarnings( "ignore:usage of Generator.Function is deprecated, please use pytest.Function instead" ) def test_reportinfo_with_nasty_getattr(self, testdir): # https://github.com/pytest-dev/pytest/issues/1204 modcol = testdir.getmodulecol( """ # lineno 0 class TestClass(object): def __getattr__(self, name): return "this is not an int" def test_foo(self): pass """ ) classcol = testdir.collect_by_name(modcol, "TestClass") instance = classcol.collect()[0] fspath, lineno, msg = instance.reportinfo() def test_customized_python_discovery(testdir): testdir.makeini( """ [pytest] python_files=check_*.py python_classes=Check python_functions=check """ ) p = testdir.makepyfile( """ def check_simple(): pass class CheckMyApp(object): def check_meth(self): pass """ ) p2 = p.new(basename=p.basename.replace("test", "check")) p.move(p2) result = testdir.runpytest("--collect-only", "-s") result.stdout.fnmatch_lines( ["*check_customized*", "*check_simple*", "*CheckMyApp*", "*check_meth*"] ) result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) def test_customized_python_discovery_functions(testdir): testdir.makeini( """ [pytest] python_functions=_test """ ) testdir.makepyfile( """ def _test_underscore(): pass """ ) result = testdir.runpytest("--collect-only", "-s") result.stdout.fnmatch_lines(["*_test_underscore*"]) result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_unorderable_types(testdir): testdir.makepyfile( """ class TestJoinEmpty(object): pass def make_test(): class Test(object): pass Test.__name__ = "TestFoo" return Test TestFoo = make_test() """ ) result = testdir.runpytest() assert "TypeError" not in result.stdout.str() assert result.ret == EXIT_NOTESTSCOLLECTED def test_collect_functools_partial(testdir): """ Test that collection of functools.partial object works, and arguments to the wrapped functions are dealt correctly (see #811). """ testdir.makepyfile( """ import functools import pytest @pytest.fixture def fix1(): return 'fix1' @pytest.fixture def fix2(): return 'fix2' def check1(i, fix1): assert i == 2 assert fix1 == 'fix1' def check2(fix1, i): assert i == 2 assert fix1 == 'fix1' def check3(fix1, i, fix2): assert i == 2 assert fix1 == 'fix1' assert fix2 == 'fix2' test_ok_1 = functools.partial(check1, i=2) test_ok_2 = functools.partial(check1, i=2, fix1='fix1') test_ok_3 = functools.partial(check1, 2) test_ok_4 = functools.partial(check2, i=2) test_ok_5 = functools.partial(check3, i=2) test_ok_6 = functools.partial(check3, i=2, fix1='fix1') test_fail_1 = functools.partial(check2, 2) test_fail_2 = functools.partial(check3, 2) """ ) result = testdir.inline_run() result.assertoutcome(passed=6, failed=2) @pytest.mark.filterwarnings("default") def test_dont_collect_non_function_callable(testdir): """Test for issue https://github.com/pytest-dev/pytest/issues/331 In this case an INTERNALERROR occurred trying to report the failure of a test like this one because py test failed to get the source lines. """ testdir.makepyfile( """ class Oh(object): def __call__(self): pass test_a = Oh() def test_real(): pass """ ) result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( [ "*collected 1 item*", "*test_dont_collect_non_function_callable.py:2: *cannot collect 'test_a' because it is not a function*", "*1 passed, 1 warnings in *", ] ) def test_class_injection_does_not_break_collection(testdir): """Tests whether injection during collection time will terminate testing. In this case the error should not occur if the TestClass itself is modified during collection time, and the original method list is still used for collection. """ testdir.makeconftest( """ from test_inject import TestClass def pytest_generate_tests(metafunc): TestClass.changed_var = {} """ ) testdir.makepyfile( test_inject=''' class TestClass(object): def test_injection(self): """Test being parametrized.""" pass ''' ) result = testdir.runpytest() assert ( "RuntimeError: dictionary changed size during iteration" not in result.stdout.str() ) result.stdout.fnmatch_lines(["*1 passed*"]) def test_syntax_error_with_non_ascii_chars(testdir): """Fix decoding issue while formatting SyntaxErrors during collection (#578) """ testdir.makepyfile( u""" # -*- coding: utf-8 -*- ☃ """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"]) def test_skip_duplicates_by_default(testdir): """Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609) Ignore duplicate directories. """ a = testdir.mkdir("a") fh = a.join("test_a.py") fh.write( textwrap.dedent( """\ import pytest def test_real(): pass """ ) ) result = testdir.runpytest(a.strpath, a.strpath) result.stdout.fnmatch_lines(["*collected 1 item*"]) def test_keep_duplicates(testdir): """Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609) Use --keep-duplicates to collect tests from duplicate directories. """ a = testdir.mkdir("a") fh = a.join("test_a.py") fh.write( textwrap.dedent( """\ import pytest def test_real(): pass """ ) ) result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath) result.stdout.fnmatch_lines(["*collected 2 item*"]) def test_package_collection_infinite_recursion(testdir): testdir.copy_example("collect/package_infinite_recursion") result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_package_collection_init_given_as_argument(testdir): """Regression test for #3749""" p = testdir.copy_example("collect/package_init_given_as_arg") result = testdir.runpytest(p / "pkg" / "__init__.py") result.stdout.fnmatch_lines(["*1 passed*"]) def test_package_with_modules(testdir): """ . └── root ├── __init__.py ├── sub1 │ ├── __init__.py │ └── sub1_1 │ ├── __init__.py │ └── test_in_sub1.py └── sub2 └── test └── test_in_sub2.py """ root = testdir.mkpydir("root") sub1 = root.mkdir("sub1") sub1.ensure("__init__.py") sub1_test = sub1.mkdir("sub1_1") sub1_test.ensure("__init__.py") sub2 = root.mkdir("sub2") sub2_test = sub2.mkdir("sub2") sub1_test.join("test_in_sub1.py").write("def test_1(): pass") sub2_test.join("test_in_sub2.py").write("def test_2(): pass") # Execute from . result = testdir.runpytest("-v", "-s") result.assert_outcomes(passed=2) # Execute from . with one argument "root" result = testdir.runpytest("-v", "-s", "root") result.assert_outcomes(passed=2) # Chdir into package's root and execute with no args root.chdir() result = testdir.runpytest("-v", "-s") result.assert_outcomes(passed=2) def test_package_ordering(testdir): """ . └── root ├── Test_root.py ├── __init__.py ├── sub1 │ ├── Test_sub1.py │ └── __init__.py └── sub2 └── test └── test_sub2.py """ testdir.makeini( """ [pytest] python_files=*.py """ ) root = testdir.mkpydir("root") sub1 = root.mkdir("sub1") sub1.ensure("__init__.py") sub2 = root.mkdir("sub2") sub2_test = sub2.mkdir("sub2") root.join("Test_root.py").write("def test_1(): pass") sub1.join("Test_sub1.py").write("def test_2(): pass") sub2_test.join("test_sub2.py").write("def test_3(): pass") # Execute from . result = testdir.runpytest("-v", "-s") result.assert_outcomes(passed=3) pytest-4.6.9/testing/python/setup_only.py0000664000372000037200000001506513604173400021502 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest @pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module") def mode(request): return request.param def test_show_only_active_fixtures(testdir, mode): p = testdir.makepyfile( ''' import pytest @pytest.fixture def _arg0(): """hidden arg0 fixture""" @pytest.fixture def arg1(): """arg1 docstring""" def test_arg1(arg1): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( ["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"] ) assert "_arg0" not in result.stdout.str() def test_show_different_scopes(testdir, mode): p = testdir.makepyfile( ''' import pytest @pytest.fixture def arg_function(): """function scoped fixture""" @pytest.fixture(scope='session') def arg_session(): """session scoped fixture""" def test_arg1(arg_session, arg_function): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "SETUP S arg_session*", "*SETUP F arg_function*", "*test_arg1 (fixtures used: arg_function, arg_session)*", "*TEARDOWN F arg_function*", "TEARDOWN S arg_session*", ] ) def test_show_nested_fixtures(testdir, mode): testdir.makeconftest( ''' import pytest @pytest.fixture(scope='session') def arg_same(): """session scoped fixture""" ''' ) p = testdir.makepyfile( ''' import pytest @pytest.fixture(scope='function') def arg_same(arg_same): """function scoped fixture""" def test_arg1(arg_same): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "SETUP S arg_same*", "*SETUP F arg_same (fixtures used: arg_same)*", "*test_arg1 (fixtures used: arg_same)*", "*TEARDOWN F arg_same*", "TEARDOWN S arg_same*", ] ) def test_show_fixtures_with_autouse(testdir, mode): p = testdir.makepyfile( ''' import pytest @pytest.fixture def arg_function(): """function scoped fixture""" @pytest.fixture(scope='session', autouse=True) def arg_session(): """session scoped fixture""" def test_arg1(arg_function): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "SETUP S arg_session*", "*SETUP F arg_function*", "*test_arg1 (fixtures used: arg_function, arg_session)*", ] ) def test_show_fixtures_with_parameters(testdir, mode): testdir.makeconftest( ''' import pytest @pytest.fixture(scope='session', params=['foo', 'bar']) def arg_same(): """session scoped fixture""" ''' ) p = testdir.makepyfile( ''' import pytest @pytest.fixture(scope='function') def arg_other(arg_same): """function scoped fixture""" def test_arg1(arg_other): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "SETUP S arg_same?foo?", "TEARDOWN S arg_same?foo?", "SETUP S arg_same?bar?", "TEARDOWN S arg_same?bar?", ] ) def test_show_fixtures_with_parameter_ids(testdir, mode): testdir.makeconftest( ''' import pytest @pytest.fixture( scope='session', params=['foo', 'bar'], ids=['spam', 'ham']) def arg_same(): """session scoped fixture""" ''' ) p = testdir.makepyfile( ''' import pytest @pytest.fixture(scope='function') def arg_other(arg_same): """function scoped fixture""" def test_arg1(arg_other): pass ''' ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines( ["SETUP S arg_same?spam?", "SETUP S arg_same?ham?"] ) def test_show_fixtures_with_parameter_ids_function(testdir, mode): p = testdir.makepyfile( """ import pytest @pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper()) def foobar(): pass def test_foobar(foobar): pass """ ) result = testdir.runpytest(mode, p) assert result.ret == 0 result.stdout.fnmatch_lines(["*SETUP F foobar?FOO?", "*SETUP F foobar?BAR?"]) def test_dynamic_fixture_request(testdir): p = testdir.makepyfile( """ import pytest @pytest.fixture() def dynamically_requested_fixture(): pass @pytest.fixture() def dependent_fixture(request): request.getfixturevalue('dynamically_requested_fixture') def test_dyn(dependent_fixture): pass """ ) result = testdir.runpytest("--setup-only", p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "*SETUP F dynamically_requested_fixture", "*TEARDOWN F dynamically_requested_fixture", ] ) def test_capturing(testdir): p = testdir.makepyfile( """ import pytest, sys @pytest.fixture() def one(): sys.stdout.write('this should be captured') sys.stderr.write('this should also be captured') @pytest.fixture() def two(one): assert 0 def test_capturing(two): pass """ ) result = testdir.runpytest("--setup-only", p) result.stdout.fnmatch_lines( ["this should be captured", "this should also be captured"] ) def test_show_fixtures_and_execute_test(testdir): """ Verifies that setups are shown and tests are executed. """ p = testdir.makepyfile( """ import pytest @pytest.fixture def arg(): assert True def test_arg(arg): assert False """ ) result = testdir.runpytest("--setup-show", p) assert result.ret == 1 result.stdout.fnmatch_lines( ["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"] ) pytest-4.6.9/testing/python/setup_plan.py0000664000372000037200000000100413604173400021437 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_show_fixtures_and_test(testdir): """ Verifies that fixtures are not executed. """ p = testdir.makepyfile( """ import pytest @pytest.fixture def arg(): assert False def test_arg(arg): assert False """ ) result = testdir.runpytest("--setup-plan", p) assert result.ret == 0 result.stdout.fnmatch_lines( ["*SETUP F arg*", "*test_arg (fixtures used: arg)", "*TEARDOWN F arg*"] ) pytest-4.6.9/testing/python/integration.py0000664000372000037200000003153213604173400021621 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest from _pytest import python from _pytest import runner class TestOEJSKITSpecials(object): def test_funcarg_non_pycollectobj(self, testdir): # rough jstests usage testdir.makeconftest( """ import pytest def pytest_pycollect_makeitem(collector, name, obj): if name == "MyClass": return MyCollector(name, parent=collector) class MyCollector(pytest.Collector): def reportinfo(self): return self.fspath, 3, "xyz" """ ) modcol = testdir.getmodulecol( """ import pytest @pytest.fixture def arg1(request): return 42 class MyClass(object): pass """ ) # this hook finds funcarg factories rep = runner.collect_one_node(collector=modcol) clscol = rep.result[0] clscol.obj = lambda arg1: None clscol.funcargs = {} pytest._fillfuncargs(clscol) assert clscol.funcargs["arg1"] == 42 def test_autouse_fixture(self, testdir): # rough jstests usage testdir.makeconftest( """ import pytest def pytest_pycollect_makeitem(collector, name, obj): if name == "MyClass": return MyCollector(name, parent=collector) class MyCollector(pytest.Collector): def reportinfo(self): return self.fspath, 3, "xyz" """ ) modcol = testdir.getmodulecol( """ import pytest @pytest.fixture(autouse=True) def hello(): pass @pytest.fixture def arg1(request): return 42 class MyClass(object): pass """ ) # this hook finds funcarg factories rep = runner.collect_one_node(modcol) clscol = rep.result[0] clscol.obj = lambda: None clscol.funcargs = {} pytest._fillfuncargs(clscol) assert not clscol.funcargs def test_wrapped_getfslineno(): def func(): pass def wrap(f): func.__wrapped__ = f func.patchings = ["qwe"] return func @wrap def wrapped_func(x, y, z): pass fs, lineno = python.getfslineno(wrapped_func) fs2, lineno2 = python.getfslineno(wrap) assert lineno > lineno2, "getfslineno does not unwrap correctly" class TestMockDecoration(object): def test_wrapped_getfuncargnames(self): from _pytest.compat import getfuncargnames def wrap(f): def func(): pass func.__wrapped__ = f return func @wrap def f(x): pass values = getfuncargnames(f) assert values == ("x",) @pytest.mark.xfail( strict=False, reason="getfuncargnames breaks if mock is imported" ) def test_wrapped_getfuncargnames_patching(self): from _pytest.compat import getfuncargnames def wrap(f): def func(): pass func.__wrapped__ = f func.patchings = ["qwe"] return func @wrap def f(x, y, z): pass values = getfuncargnames(f) assert values == ("y", "z") def test_unittest_mock(self, testdir): pytest.importorskip("unittest.mock") testdir.makepyfile( """ import unittest.mock class T(unittest.TestCase): @unittest.mock.patch("os.path.abspath") def test_hello(self, abspath): import os os.path.abspath("hello") abspath.assert_any_call("hello") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_unittest_mock_and_fixture(self, testdir): pytest.importorskip("unittest.mock") testdir.makepyfile( """ import os.path import unittest.mock import pytest @pytest.fixture def inject_me(): pass @unittest.mock.patch.object(os.path, "abspath", new=unittest.mock.MagicMock) def test_hello(inject_me): import os os.path.abspath("hello") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_unittest_mock_and_pypi_mock(self, testdir): pytest.importorskip("unittest.mock") pytest.importorskip("mock", "1.0.1") testdir.makepyfile( """ import mock import unittest.mock class TestBoth(object): @unittest.mock.patch("os.path.abspath") def test_hello(self, abspath): import os os.path.abspath("hello") abspath.assert_any_call("hello") @mock.patch("os.path.abspath") def test_hello_mock(self, abspath): import os os.path.abspath("hello") abspath.assert_any_call("hello") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_mock(self, testdir): pytest.importorskip("mock", "1.0.1") testdir.makepyfile( """ import os import unittest import mock class T(unittest.TestCase): @mock.patch("os.path.abspath") def test_hello(self, abspath): os.path.abspath("hello") abspath.assert_any_call("hello") def mock_basename(path): return "mock_basename" @mock.patch("os.path.abspath") @mock.patch("os.path.normpath") @mock.patch("os.path.basename", new=mock_basename) def test_someting(normpath, abspath, tmpdir): abspath.return_value = "this" os.path.normpath(os.path.abspath("hello")) normpath.assert_any_call("this") assert os.path.basename("123") == "mock_basename" """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) calls = reprec.getcalls("pytest_runtest_logreport") funcnames = [ call.report.location[2] for call in calls if call.report.when == "call" ] assert funcnames == ["T.test_hello", "test_someting"] def test_mock_sorting(self, testdir): pytest.importorskip("mock", "1.0.1") testdir.makepyfile( """ import os import mock @mock.patch("os.path.abspath") def test_one(abspath): pass @mock.patch("os.path.abspath") def test_two(abspath): pass @mock.patch("os.path.abspath") def test_three(abspath): pass """ ) reprec = testdir.inline_run() calls = reprec.getreports("pytest_runtest_logreport") calls = [x for x in calls if x.when == "call"] names = [x.nodeid.split("::")[-1] for x in calls] assert names == ["test_one", "test_two", "test_three"] def test_mock_double_patch_issue473(self, testdir): pytest.importorskip("mock", "1.0.1") testdir.makepyfile( """ from mock import patch from pytest import mark @patch('os.getcwd') @patch('os.path') @mark.slow class TestSimple(object): def test_simple_thing(self, mock_path, mock_getcwd): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) class TestReRunTests(object): def test_rerun(self, testdir): testdir.makeconftest( """ from _pytest.runner import runtestprotocol def pytest_runtest_protocol(item, nextitem): runtestprotocol(item, log=False, nextitem=nextitem) runtestprotocol(item, log=True, nextitem=nextitem) """ ) testdir.makepyfile( """ import pytest count = 0 req = None @pytest.fixture def fix(request): global count, req assert request != req req = request print("fix count %s" % count) count += 1 def test_fix(fix): pass """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *fix count 0* *fix count 1* """ ) result.stdout.fnmatch_lines( """ *2 passed* """ ) def test_pytestconfig_is_session_scoped(): from _pytest.fixtures import pytestconfig assert pytestconfig._pytestfixturefunction.scope == "session" class TestNoselikeTestAttribute(object): def test_module_with_global_test(self, testdir): testdir.makepyfile( """ __test__ = False def test_hello(): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_class_and_method(self, testdir): testdir.makepyfile( """ __test__ = True def test_func(): pass test_func.__test__ = False class TestSome(object): __test__ = False def test_method(self): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_unittest_class(self, testdir): testdir.makepyfile( """ import unittest class TC(unittest.TestCase): def test_1(self): pass class TC2(unittest.TestCase): __test__ = False def test_2(self): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() call = reprec.getcalls("pytest_collection_modifyitems")[0] assert len(call.items) == 1 assert call.items[0].cls.__name__ == "TC" def test_class_with_nasty_getattr(self, testdir): """Make sure we handle classes with a custom nasty __getattr__ right. With a custom __getattr__ which e.g. returns a function (like with a RPC wrapper), we shouldn't assume this meant "__test__ = True". """ # https://github.com/pytest-dev/pytest/issues/1204 testdir.makepyfile( """ class MetaModel(type): def __getattr__(cls, key): return lambda: None BaseModel = MetaModel('Model', (), {}) class Model(BaseModel): __metaclass__ = MetaModel def test_blah(self): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() call = reprec.getcalls("pytest_collection_modifyitems")[0] assert not call.items class TestParameterize(object): """#351""" def test_idfn_marker(self, testdir): testdir.makepyfile( """ import pytest def idfn(param): if param == 0: return 'spam' elif param == 1: return 'ham' else: return None @pytest.mark.parametrize('a,b', [(0, 2), (1, 2)], ids=idfn) def test_params(a, b): pass """ ) res = testdir.runpytest("--collect-only") res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) def test_idfn_fixture(self, testdir): testdir.makepyfile( """ import pytest def idfn(param): if param == 0: return 'spam' elif param == 1: return 'ham' else: return None @pytest.fixture(params=[0, 1], ids=idfn) def a(request): return request.param @pytest.fixture(params=[1, 2], ids=idfn) def b(request): return request.param def test_params(a, b): pass """ ) res = testdir.runpytest("--collect-only") res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) pytest-4.6.9/testing/python/metafunc.py0000664000372000037200000016062413604173400021105 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import re import sys import textwrap import attr import hypothesis import six from hypothesis import strategies import pytest from _pytest import fixtures from _pytest import python from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG PY3 = sys.version_info >= (3, 0) class TestMetafunc(object): def Metafunc(self, func, config=None): # the unit tests of this class check if things work correctly # on the funcarg level, so we don't need a full blown # initiliazation class FixtureInfo(object): name2fixturedefs = None def __init__(self, names): self.names_closure = names @attr.s class DefinitionMock(object): obj = attr.ib() names = fixtures.getfuncargnames(func) fixtureinfo = FixtureInfo(names) definition = DefinitionMock(func) return python.Metafunc(definition, fixtureinfo, config) def test_no_funcargs(self, testdir): def function(): pass metafunc = self.Metafunc(function) assert not metafunc.fixturenames repr(metafunc._calls) def test_function_basic(self): def func(arg1, arg2="qwe"): pass metafunc = self.Metafunc(func) assert len(metafunc.fixturenames) == 1 assert "arg1" in metafunc.fixturenames assert metafunc.function is func assert metafunc.cls is None def test_parametrize_error(self): def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x", [1, 2]) pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6])) pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6])) metafunc.parametrize("y", [1, 2]) pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6])) pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6])) def test_parametrize_bad_scope(self, testdir): def func(x): pass metafunc = self.Metafunc(func) with pytest.raises( pytest.fail.Exception, match=r"parametrize\(\) call in func got an unexpected scope value 'doggy'", ): metafunc.parametrize("x", [1], scope="doggy") def test_find_parametrized_scope(self): """unittest for _find_parametrized_scope (#3941)""" from _pytest.python import _find_parametrized_scope @attr.s class DummyFixtureDef(object): scope = attr.ib() fixtures_defs = dict( session_fix=[DummyFixtureDef("session")], package_fix=[DummyFixtureDef("package")], module_fix=[DummyFixtureDef("module")], class_fix=[DummyFixtureDef("class")], func_fix=[DummyFixtureDef("function")], ) # use arguments to determine narrow scope; the cause of the bug is that it would look on all # fixture defs given to the method def find_scope(argnames, indirect): return _find_parametrized_scope(argnames, fixtures_defs, indirect=indirect) assert find_scope(["func_fix"], indirect=True) == "function" assert find_scope(["class_fix"], indirect=True) == "class" assert find_scope(["module_fix"], indirect=True) == "module" assert find_scope(["package_fix"], indirect=True) == "package" assert find_scope(["session_fix"], indirect=True) == "session" assert find_scope(["class_fix", "func_fix"], indirect=True) == "function" assert find_scope(["func_fix", "session_fix"], indirect=True) == "function" assert find_scope(["session_fix", "class_fix"], indirect=True) == "class" assert find_scope(["package_fix", "session_fix"], indirect=True) == "package" assert find_scope(["module_fix", "session_fix"], indirect=True) == "module" # when indirect is False or is not for all scopes, always use function assert find_scope(["session_fix", "module_fix"], indirect=False) == "function" assert ( find_scope(["session_fix", "module_fix"], indirect=["module_fix"]) == "function" ) assert ( find_scope( ["session_fix", "module_fix"], indirect=["session_fix", "module_fix"] ) == "module" ) def test_parametrize_and_id(self): def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x", [1, 2], ids=["basic", "advanced"]) metafunc.parametrize("y", ["abc", "def"]) ids = [x.id for x in metafunc._calls] assert ids == ["basic-abc", "basic-def", "advanced-abc", "advanced-def"] def test_parametrize_and_id_unicode(self): """Allow unicode strings for "ids" parameter in Python 2 (##1905)""" def func(x): pass metafunc = self.Metafunc(func) metafunc.parametrize("x", [1, 2], ids=[u"basic", u"advanced"]) ids = [x.id for x in metafunc._calls] assert ids == [u"basic", u"advanced"] def test_parametrize_with_wrong_number_of_ids(self, testdir): def func(x, y): pass metafunc = self.Metafunc(func) with pytest.raises(pytest.fail.Exception): metafunc.parametrize("x", [1, 2], ids=["basic"]) with pytest.raises(pytest.fail.Exception): metafunc.parametrize( ("x", "y"), [("abc", "def"), ("ghi", "jkl")], ids=["one"] ) def test_parametrize_empty_list(self): """#510""" def func(y): pass class MockConfig(object): def getini(self, name): return "" @property def hook(self): return self def pytest_make_parametrize_id(self, **kw): pass metafunc = self.Metafunc(func, MockConfig()) metafunc.parametrize("y", []) assert "skip" == metafunc._calls[0].marks[0].name def test_parametrize_with_userobjects(self): def func(x, y): pass metafunc = self.Metafunc(func) class A(object): pass metafunc.parametrize("x", [A(), A()]) metafunc.parametrize("y", list("ab")) assert metafunc._calls[0].id == "x0-a" assert metafunc._calls[1].id == "x0-b" assert metafunc._calls[2].id == "x1-a" assert metafunc._calls[3].id == "x1-b" @hypothesis.given(strategies.text() | strategies.binary()) @hypothesis.settings( deadline=400.0 ) # very close to std deadline and CI boxes are not reliable in CPU power def test_idval_hypothesis(self, value): from _pytest.python import _idval escaped = _idval(value, "a", 6, None, item=None, config=None) assert isinstance(escaped, six.text_type) escaped.encode("ascii") def test_unicode_idval(self): """This tests that Unicode strings outside the ASCII character set get escaped, using byte escapes if they're in that range or unicode escapes if they're not. """ from _pytest.python import _idval values = [ (u"", ""), (u"ascii", "ascii"), (u"ação", "a\\xe7\\xe3o"), (u"josé@blah.com", "jos\\xe9@blah.com"), ( u"δοκ.ιμή@παράδειγμα.δοκιμή", "\\u03b4\\u03bf\\u03ba.\\u03b9\\u03bc\\u03ae@\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3" "\\u03bc\\u03b1.\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae", ), ] for val, expected in values: assert _idval(val, "a", 6, None, item=None, config=None) == expected def test_unicode_idval_with_config(self): """unittest for expected behavior to obtain ids with disable_test_id_escaping_and_forfeit_all_rights_to_community_support option. (#5294) """ from _pytest.python import _idval class MockConfig(object): def __init__(self, config): self.config = config @property def hook(self): return self def pytest_make_parametrize_id(self, **kw): pass def getini(self, name): return self.config[name] option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support" values = [ (u"ação", MockConfig({option: True}), u"ação"), (u"ação", MockConfig({option: False}), "a\\xe7\\xe3o"), ] for val, config, expected in values: assert _idval(val, "a", 6, None, item=None, config=config) == expected def test_bytes_idval(self): """unittest for the expected behavior to obtain ids for parametrized bytes values: - python2: non-ascii strings are considered bytes and formatted using "binary escape", where any byte < 127 is escaped into its hex form. - python3: bytes objects are always escaped using "binary escape". """ from _pytest.python import _idval values = [ (b"", ""), (b"\xc3\xb4\xff\xe4", "\\xc3\\xb4\\xff\\xe4"), (b"ascii", "ascii"), (u"αρά".encode("utf-8"), "\\xce\\xb1\\xcf\\x81\\xce\\xac"), ] for val, expected in values: assert _idval(val, "a", 6, idfn=None, item=None, config=None) == expected def test_class_or_function_idval(self): """unittest for the expected behavior to obtain ids for parametrized values that are classes or functions: their __name__. """ from _pytest.python import _idval class TestClass(object): pass def test_function(): pass values = [(TestClass, "TestClass"), (test_function, "test_function")] for val, expected in values: assert _idval(val, "a", 6, None, item=None, config=None) == expected def test_idmaker_autoname(self): """#250""" from _pytest.python import idmaker result = idmaker( ("a", "b"), [pytest.param("string", 1.0), pytest.param("st-ring", 2.0)] ) assert result == ["string-1.0", "st-ring-2.0"] result = idmaker( ("a", "b"), [pytest.param(object(), 1.0), pytest.param(object(), object())] ) assert result == ["a0-1.0", "a1-b1"] # unicode mixing, issue250 result = idmaker((u"a", "b"), [pytest.param({}, b"\xc3\xb4")]) assert result == ["a0-\\xc3\\xb4"] def test_idmaker_with_bytes_regex(self): from _pytest.python import idmaker result = idmaker(("a"), [pytest.param(re.compile(b"foo"), 1.0)]) assert result == ["foo"] def test_idmaker_native_strings(self): from _pytest.python import idmaker result = idmaker( ("a", "b"), [ pytest.param(1.0, -1.1), pytest.param(2, -202), pytest.param("three", "three hundred"), pytest.param(True, False), pytest.param(None, None), pytest.param(re.compile("foo"), re.compile("bar")), pytest.param(str, int), pytest.param(list("six"), [66, 66]), pytest.param({7}, set("seven")), pytest.param(tuple("eight"), (8, -8, 8)), pytest.param(b"\xc3\xb4", b"name"), pytest.param(b"\xc3\xb4", u"other"), ], ) assert result == [ "1.0--1.1", "2--202", "three-three hundred", "True-False", "None-None", "foo-bar", "str-int", "a7-b7", "a8-b8", "a9-b9", "\\xc3\\xb4-name", "\\xc3\\xb4-other", ] def test_idmaker_non_printable_characters(self): from _pytest.python import idmaker result = idmaker( ("s", "n"), [ pytest.param("\x00", 1), pytest.param("\x05", 2), pytest.param(b"\x00", 3), pytest.param(b"\x05", 4), pytest.param("\t", 5), pytest.param(b"\t", 6), ], ) assert result == ["\\x00-1", "\\x05-2", "\\x00-3", "\\x05-4", "\\t-5", "\\t-6"] def test_idmaker_manual_ids_must_be_printable(self): from _pytest.python import idmaker result = idmaker( ("s",), [ pytest.param("x00", id="hello \x00"), pytest.param("x05", id="hello \x05"), ], ) assert result == ["hello \\x00", "hello \\x05"] def test_idmaker_enum(self): from _pytest.python import idmaker enum = pytest.importorskip("enum") e = enum.Enum("Foo", "one, two") result = idmaker(("a", "b"), [pytest.param(e.one, e.two)]) assert result == ["Foo.one-Foo.two"] def test_idmaker_idfn(self): """#351""" from _pytest.python import idmaker def ids(val): if isinstance(val, Exception): return repr(val) result = idmaker( ("a", "b"), [ pytest.param(10.0, IndexError()), pytest.param(20, KeyError()), pytest.param("three", [1, 2, 3]), ], idfn=ids, ) assert result == ["10.0-IndexError()", "20-KeyError()", "three-b2"] def test_idmaker_idfn_unique_names(self): """#351""" from _pytest.python import idmaker def ids(val): return "a" result = idmaker( ("a", "b"), [ pytest.param(10.0, IndexError()), pytest.param(20, KeyError()), pytest.param("three", [1, 2, 3]), ], idfn=ids, ) assert result == ["a-a0", "a-a1", "a-a2"] def test_idmaker_with_idfn_and_config(self): """unittest for expected behavior to create ids with idfn and disable_test_id_escaping_and_forfeit_all_rights_to_community_support option. (#5294) """ from _pytest.python import idmaker class MockConfig(object): def __init__(self, config): self.config = config @property def hook(self): return self def pytest_make_parametrize_id(self, **kw): pass def getini(self, name): return self.config[name] option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support" values = [ (MockConfig({option: True}), u"ação"), (MockConfig({option: False}), "a\\xe7\\xe3o"), ] for config, expected in values: result = idmaker( ("a",), [pytest.param("string")], idfn=lambda _: u"ação", config=config ) assert result == [expected] def test_idmaker_with_ids_and_config(self): """unittest for expected behavior to create ids with ids and disable_test_id_escaping_and_forfeit_all_rights_to_community_support option. (#5294) """ from _pytest.python import idmaker class MockConfig(object): def __init__(self, config): self.config = config @property def hook(self): return self def pytest_make_parametrize_id(self, **kw): pass def getini(self, name): return self.config[name] option = "disable_test_id_escaping_and_forfeit_all_rights_to_community_support" values = [ (MockConfig({option: True}), u"ação"), (MockConfig({option: False}), "a\\xe7\\xe3o"), ] for config, expected in values: result = idmaker( ("a",), [pytest.param("string")], ids=[u"ação"], config=config ) assert result == [expected] def test_parametrize_ids_exception(self, testdir): """ :param testdir: the instance of Testdir class, a temporary test directory. """ testdir.makepyfile( """ import pytest def ids(arg): raise Exception("bad ids") @pytest.mark.parametrize("arg", ["a", "b"], ids=ids) def test_foo(arg): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_foo: error raised while trying to determine id of parameter 'arg' at position 0", "*Exception: bad ids", ] ) def test_parametrize_ids_returns_non_string(self, testdir): testdir.makepyfile( """\ import pytest def ids(d): return d @pytest.mark.parametrize("arg", ({1: 2}, {3, 4}), ids=ids) def test(arg): assert arg """ ) assert testdir.runpytest().ret == 0 def test_idmaker_with_ids(self): from _pytest.python import idmaker result = idmaker( ("a", "b"), [pytest.param(1, 2), pytest.param(3, 4)], ids=["a", None] ) assert result == ["a", "3-4"] def test_idmaker_with_paramset_id(self): from _pytest.python import idmaker result = idmaker( ("a", "b"), [pytest.param(1, 2, id="me"), pytest.param(3, 4, id="you")], ids=["a", None], ) assert result == ["me", "you"] def test_idmaker_with_ids_unique_names(self): from _pytest.python import idmaker result = idmaker( ("a"), map(pytest.param, [1, 2, 3, 4, 5]), ids=["a", "a", "b", "c", "b"] ) assert result == ["a0", "a1", "b0", "c", "b1"] def test_parametrize_indirect(self): """#714""" def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x", [1], indirect=True) metafunc.parametrize("y", [2, 3], indirect=True) assert len(metafunc._calls) == 2 assert metafunc._calls[0].funcargs == {} assert metafunc._calls[1].funcargs == {} assert metafunc._calls[0].params == dict(x=1, y=2) assert metafunc._calls[1].params == dict(x=1, y=3) def test_parametrize_indirect_list(self): """#714""" def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x, y", [("a", "b")], indirect=["x"]) assert metafunc._calls[0].funcargs == dict(y="b") assert metafunc._calls[0].params == dict(x="a") def test_parametrize_indirect_list_all(self): """#714""" def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "y"]) assert metafunc._calls[0].funcargs == {} assert metafunc._calls[0].params == dict(x="a", y="b") def test_parametrize_indirect_list_empty(self): """#714""" def func(x, y): pass metafunc = self.Metafunc(func) metafunc.parametrize("x, y", [("a", "b")], indirect=[]) assert metafunc._calls[0].funcargs == dict(x="a", y="b") assert metafunc._calls[0].params == {} def test_parametrize_indirect_list_functional(self, testdir): """ #714 Test parametrization with 'indirect' parameter applied on particular arguments. As y is is direct, its value should be used directly rather than being passed to the fixture y. :param testdir: the instance of Testdir class, a temporary test directory. """ testdir.makepyfile( """ import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.fixture(scope='function') def y(request): return request.param * 2 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x']) def test_simple(x,y): assert len(x) == 3 assert len(y) == 1 """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines(["*test_simple*a-b*", "*1 passed*"]) def test_parametrize_indirect_list_error(self, testdir): """#714""" def func(x, y): pass metafunc = self.Metafunc(func) with pytest.raises(pytest.fail.Exception): metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "z"]) def test_parametrize_uses_no_fixture_error_indirect_false(self, testdir): """The 'uses no fixture' error tells the user at collection time that the parametrize data they've set up doesn't correspond to the fixtures in their test function, rather than silently ignoring this and letting the test potentially pass. #714 """ testdir.makepyfile( """ import pytest @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=False) def test_simple(x): assert len(x) == 3 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) def test_parametrize_uses_no_fixture_error_indirect_true(self, testdir): """#714""" testdir.makepyfile( """ import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.fixture(scope='function') def y(request): return request.param * 2 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=True) def test_simple(x): assert len(x) == 3 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) def test_parametrize_indirect_uses_no_fixture_error_indirect_string(self, testdir): """#714""" testdir.makepyfile( """ import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect='y') def test_simple(x): assert len(x) == 3 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir): """#714""" testdir.makepyfile( """ import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['y']) def test_simple(x): assert len(x) == 3 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) def test_parametrize_argument_not_in_indirect_list(self, testdir): """#714""" testdir.makepyfile( """ import pytest @pytest.fixture(scope='function') def x(request): return request.param * 3 @pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x']) def test_simple(x): assert len(x) == 3 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) def test_parametrize_gives_indicative_error_on_function_with_default_argument( self, testdir ): testdir.makepyfile( """ import pytest @pytest.mark.parametrize('x, y', [('a', 'b')]) def test_simple(x, y=1): assert len(x) == 1 """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines( ["*already takes an argument 'y' with a default value"] ) def test_parametrize_functional(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize('x', [1,2], indirect=True) metafunc.parametrize('y', [2]) @pytest.fixture def x(request): return request.param * 10 def test_simple(x,y): assert x in (10,20) assert y == 2 """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( ["*test_simple*1-2*", "*test_simple*2-2*", "*2 passed*"] ) def test_parametrize_onearg(self): metafunc = self.Metafunc(lambda x: None) metafunc.parametrize("x", [1, 2]) assert len(metafunc._calls) == 2 assert metafunc._calls[0].funcargs == dict(x=1) assert metafunc._calls[0].id == "1" assert metafunc._calls[1].funcargs == dict(x=2) assert metafunc._calls[1].id == "2" def test_parametrize_onearg_indirect(self): metafunc = self.Metafunc(lambda x: None) metafunc.parametrize("x", [1, 2], indirect=True) assert metafunc._calls[0].params == dict(x=1) assert metafunc._calls[0].id == "1" assert metafunc._calls[1].params == dict(x=2) assert metafunc._calls[1].id == "2" def test_parametrize_twoargs(self): metafunc = self.Metafunc(lambda x, y: None) metafunc.parametrize(("x", "y"), [(1, 2), (3, 4)]) assert len(metafunc._calls) == 2 assert metafunc._calls[0].funcargs == dict(x=1, y=2) assert metafunc._calls[0].id == "1-2" assert metafunc._calls[1].funcargs == dict(x=3, y=4) assert metafunc._calls[1].id == "3-4" def test_parametrize_multiple_times(self, testdir): testdir.makepyfile( """ import pytest pytestmark = pytest.mark.parametrize("x", [1,2]) def test_func(x): assert 0, x class TestClass(object): pytestmark = pytest.mark.parametrize("y", [3,4]) def test_meth(self, x, y): assert 0, x """ ) result = testdir.runpytest() assert result.ret == 1 result.assert_outcomes(failed=6) def test_parametrize_CSV(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize("x, y,", [(1,2), (2,3)]) def test_func(x, y): assert x+1 == y """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_parametrize_class_scenarios(self, testdir): testdir.makepyfile( """ # same as doc/en/example/parametrize scenario example def pytest_generate_tests(metafunc): idlist = [] argvalues = [] for scenario in metafunc.cls.scenarios: idlist.append(scenario[0]) items = scenario[1].items() argnames = [x[0] for x in items] argvalues.append(([x[1] for x in items])) metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class") class Test(object): scenarios = [['1', {'arg': {1: 2}, "arg2": "value2"}], ['2', {'arg':'value2', "arg2": "value2"}]] def test_1(self, arg, arg2): pass def test_2(self, arg2, arg): pass def test_3(self, arg, arg2): pass """ ) result = testdir.runpytest("-v") assert result.ret == 0 result.stdout.fnmatch_lines( """ *test_1*1* *test_2*1* *test_3*1* *test_1*2* *test_2*2* *test_3*2* *6 passed* """ ) def test_format_args(self): def function1(): pass assert fixtures._format_args(function1) == "()" def function2(arg1): pass assert fixtures._format_args(function2) == "(arg1)" def function3(arg1, arg2="qwe"): pass assert fixtures._format_args(function3) == "(arg1, arg2='qwe')" def function4(arg1, *args, **kwargs): pass assert fixtures._format_args(function4) == "(arg1, *args, **kwargs)" class TestMetafuncFunctional(object): def test_attributes(self, testdir): p = testdir.makepyfile( """ # assumes that generate/provide runs in the same process import sys, pytest, six def pytest_generate_tests(metafunc): metafunc.parametrize('metafunc', [metafunc]) @pytest.fixture def metafunc(request): return request.param def test_function(metafunc, pytestconfig): assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ assert metafunc.function == test_function assert metafunc.cls is None class TestClass(object): def test_method(self, metafunc, pytestconfig): assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ unbound = six.get_unbound_function(TestClass.test_method) assert metafunc.function == unbound assert metafunc.cls == TestClass """ ) result = testdir.runpytest(p, "-v", SHOW_PYTEST_WARNINGS_ARG) result.assert_outcomes(passed=2) def test_two_functions(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize('arg1', [10, 20], ids=['0', '1']) def test_func1(arg1): assert arg1 == 10 def test_func2(arg1): assert arg1 in (10, 20) """ ) result = testdir.runpytest("-v", p, SHOW_PYTEST_WARNINGS_ARG) result.stdout.fnmatch_lines( [ "*test_func1*0*PASS*", "*test_func1*1*FAIL*", "*test_func2*PASS*", "*test_func2*PASS*", "*1 failed, 3 passed*", ] ) def test_noself_in_method(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): assert 'xyz' not in metafunc.fixturenames class TestHello(object): def test_hello(xyz): pass """ ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_generate_tests_in_class(self, testdir): p = testdir.makepyfile( """ class TestClass(object): def pytest_generate_tests(self, metafunc): metafunc.parametrize('hello', ['world'], ids=['hellow']) def test_myfunc(self, hello): assert hello == "world" """ ) result = testdir.runpytest("-v", p, SHOW_PYTEST_WARNINGS_ARG) result.stdout.fnmatch_lines(["*test_myfunc*hello*PASS*", "*1 passed*"]) def test_two_functions_not_same_instance(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize('arg1', [10, 20], ids=["0", "1"]) class TestClass(object): def test_func(self, arg1): assert not hasattr(self, 'x') self.x = 1 """ ) result = testdir.runpytest("-v", p, SHOW_PYTEST_WARNINGS_ARG) result.stdout.fnmatch_lines( ["*test_func*0*PASS*", "*test_func*1*PASS*", "*2 pass*"] ) def test_issue28_setup_method_in_generate_tests(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize('arg1', [1]) class TestClass(object): def test_method(self, arg1): assert arg1 == self.val def setup_method(self, func): self.val = 1 """ ) result = testdir.runpytest(p, SHOW_PYTEST_WARNINGS_ARG) result.assert_outcomes(passed=1) def test_parametrize_functional2(self, testdir): testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize("arg1", [1,2]) metafunc.parametrize("arg2", [4,5]) def test_hello(arg1, arg2): assert 0, (arg1, arg2) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*(1, 4)*", "*(1, 5)*", "*(2, 4)*", "*(2, 5)*", "*4 failed*"] ) def test_parametrize_and_inner_getfixturevalue(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg2", [10], indirect=True) import pytest @pytest.fixture def arg1(request): x = request.getfixturevalue("arg2") return x + request.param @pytest.fixture def arg2(request): return request.param def test_func1(arg1, arg2): assert arg1 == 11 """ ) result = testdir.runpytest("-v", p) result.stdout.fnmatch_lines(["*test_func1*1*PASS*", "*1 passed*"]) def test_parametrize_on_setup_arg(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): assert "arg1" in metafunc.fixturenames metafunc.parametrize("arg1", [1], indirect=True) import pytest @pytest.fixture def arg1(request): return request.param @pytest.fixture def arg2(request, arg1): return 10 * arg1 def test_func(arg2): assert arg2 == 10 """ ) result = testdir.runpytest("-v", p) result.stdout.fnmatch_lines(["*test_func*1*PASS*", "*1 passed*"]) def test_parametrize_with_ids(self, testdir): testdir.makeini( """ [pytest] console_output_style=classic """ ) testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,2)], ids=["basic", "advanced"]) def test_function(a, b): assert a == b """ ) result = testdir.runpytest("-v") assert result.ret == 1 result.stdout.fnmatch_lines_random( ["*test_function*basic*PASSED", "*test_function*advanced*FAILED"] ) def test_parametrize_without_ids(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,object()), (1.3,object())]) def test_function(a, b): assert 1 """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( """ *test_function*1-b0* *test_function*1.3-b1* """ ) def test_parametrize_with_None_in_ids(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,1), (1,2)], ids=["basic", None, "advanced"]) def test_function(a, b): assert a == b """ ) result = testdir.runpytest("-v") assert result.ret == 1 result.stdout.fnmatch_lines_random( [ "*test_function*basic*PASSED*", "*test_function*1-1*PASSED*", "*test_function*advanced*FAILED*", ] ) def test_fixture_parametrized_empty_ids(self, testdir): """Fixtures parametrized with empty ids cause an internal error (#1849).""" testdir.makepyfile( """ import pytest @pytest.fixture(scope="module", ids=[], params=[]) def temp(request): return request.param def test_temp(temp): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 skipped *"]) def test_parametrized_empty_ids(self, testdir): """Tests parametrized with empty ids cause an internal error (#1849).""" testdir.makepyfile( """ import pytest @pytest.mark.parametrize('temp', [], ids=list()) def test_temp(temp): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 skipped *"]) def test_parametrized_ids_invalid_type(self, testdir): """Tests parametrized with ids as non-strings (#1857).""" testdir.makepyfile( """ import pytest @pytest.mark.parametrize("x, expected", [(10, 20), (40, 80)], ids=(None, 2)) def test_ids_numbers(x,expected): assert x * 2 == expected """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*In test_ids_numbers: ids must be list of strings, found: 2 (type: *'int'>)*" ] ) def test_parametrize_with_identical_ids_get_unique_names(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): metafunc.parametrize(("a", "b"), [(1,1), (1,2)], ids=["a", "a"]) def test_function(a, b): assert a == b """ ) result = testdir.runpytest("-v") assert result.ret == 1 result.stdout.fnmatch_lines_random( ["*test_function*a0*PASSED*", "*test_function*a1*FAILED*"] ) @pytest.mark.parametrize(("scope", "length"), [("module", 2), ("function", 4)]) def test_parametrize_scope_overrides(self, testdir, scope, length): testdir.makepyfile( """ import pytest values = [] def pytest_generate_tests(metafunc): if "arg" in metafunc.funcargnames: metafunc.parametrize("arg", [1,2], indirect=True, scope=%r) @pytest.fixture def arg(request): values.append(request.param) return request.param def test_hello(arg): assert arg in (1,2) def test_world(arg): assert arg in (1,2) def test_checklength(): assert len(values) == %d """ % (scope, length) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=5) def test_parametrize_issue323(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='module', params=range(966)) def foo(request): return request.param def test_it(foo): pass def test_it2(foo): pass """ ) reprec = testdir.inline_run("--collect-only") assert not reprec.getcalls("pytest_internalerror") def test_usefixtures_seen_in_generate_tests(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): assert "abc" in metafunc.fixturenames metafunc.parametrize("abc", [1]) @pytest.mark.usefixtures("abc") def test_function(): pass """ ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=1) def test_generate_tests_only_done_in_subdir(self, testdir): sub1 = testdir.mkpydir("sub1") sub2 = testdir.mkpydir("sub2") sub1.join("conftest.py").write( textwrap.dedent( """\ def pytest_generate_tests(metafunc): assert metafunc.function.__name__ == "test_1" """ ) ) sub2.join("conftest.py").write( textwrap.dedent( """\ def pytest_generate_tests(metafunc): assert metafunc.function.__name__ == "test_2" """ ) ) sub1.join("test_in_sub1.py").write("def test_1(): pass") sub2.join("test_in_sub2.py").write("def test_2(): pass") result = testdir.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1) result.assert_outcomes(passed=3) def test_generate_same_function_names_issue403(self, testdir): testdir.makepyfile( """ import pytest def make_tests(): @pytest.mark.parametrize("x", range(2)) def test_foo(x): pass return test_foo test_x = make_tests() test_y = make_tests() """ ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=4) @pytest.mark.parametrize("attr", ["parametrise", "parameterize", "parameterise"]) def test_parametrize_misspelling(self, testdir, attr): """#463""" testdir.makepyfile( """ import pytest @pytest.mark.{}("x", range(2)) def test_foo(x): pass """.format( attr ) ) result = testdir.runpytest("--collectonly") result.stdout.fnmatch_lines( [ "test_foo has '{}' mark, spelling should be 'parametrize'".format(attr), "*1 error in*", ] ) class TestMetafuncFunctionalAuto(object): """ Tests related to automatically find out the correct scope for parametrized tests (#1832). """ def test_parametrize_auto_scope(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='session', autouse=True) def fixture(): return 1 @pytest.mark.parametrize('animal', ["dog", "cat"]) def test_1(animal): assert animal in ('dog', 'cat') @pytest.mark.parametrize('animal', ['fish']) def test_2(animal): assert animal == 'fish' """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_auto_scope_indirect(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='session') def echo(request): return request.param @pytest.mark.parametrize('animal, echo', [("dog", 1), ("cat", 2)], indirect=['echo']) def test_1(animal, echo): assert animal in ('dog', 'cat') assert echo in (1, 2, 3) @pytest.mark.parametrize('animal, echo', [('fish', 3)], indirect=['echo']) def test_2(animal, echo): assert animal == 'fish' assert echo in (1, 2, 3) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_auto_scope_override_fixture(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='session', autouse=True) def animal(): return 'fox' @pytest.mark.parametrize('animal', ["dog", "cat"]) def test_1(animal): assert animal in ('dog', 'cat') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed *"]) def test_parametrize_all_indirects(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def animal(request): return request.param @pytest.fixture(scope='session') def echo(request): return request.param @pytest.mark.parametrize('animal, echo', [("dog", 1), ("cat", 2)], indirect=True) def test_1(animal, echo): assert animal in ('dog', 'cat') assert echo in (1, 2, 3) @pytest.mark.parametrize('animal, echo', [("fish", 3)], indirect=True) def test_2(animal, echo): assert animal == 'fish' assert echo in (1, 2, 3) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 3 passed *"]) def test_parametrize_some_arguments_auto_scope(self, testdir, monkeypatch): """Integration test for (#3941)""" class_fix_setup = [] monkeypatch.setattr(sys, "class_fix_setup", class_fix_setup, raising=False) func_fix_setup = [] monkeypatch.setattr(sys, "func_fix_setup", func_fix_setup, raising=False) testdir.makepyfile( """ import pytest import sys @pytest.fixture(scope='class', autouse=True) def class_fix(request): sys.class_fix_setup.append(request.param) @pytest.fixture(autouse=True) def func_fix(): sys.func_fix_setup.append(True) @pytest.mark.parametrize('class_fix', [10, 20], indirect=True) class Test: def test_foo(self): pass def test_bar(self): pass """ ) result = testdir.runpytest_inprocess() result.stdout.fnmatch_lines(["* 4 passed in *"]) assert func_fix_setup == [True] * 4 assert class_fix_setup == [10, 20] def test_parametrize_issue634(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='module') def foo(request): print('preparing foo-%d' % request.param) return 'foo-%d' % request.param def test_one(foo): pass def test_two(foo): pass test_two.test_with = (2, 3) def pytest_generate_tests(metafunc): params = (1, 2, 3, 4) if not 'foo' in metafunc.fixturenames: return test_with = getattr(metafunc.function, 'test_with', None) if test_with: params = test_with metafunc.parametrize('foo', params, indirect=True) """ ) result = testdir.runpytest("-s") output = result.stdout.str() assert output.count("preparing foo-2") == 1 assert output.count("preparing foo-3") == 1 class TestMarkersWithParametrization(object): """#308""" def test_simple_mark(self, testdir): s = """ import pytest @pytest.mark.foo @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(1, 3, marks=pytest.mark.bar), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ items = testdir.getitems(s) assert len(items) == 3 for item in items: assert "foo" in item.keywords assert "bar" not in items[0].keywords assert "bar" in items[1].keywords assert "bar" not in items[2].keywords def test_select_based_on_mark(self, testdir): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(2, 3, marks=pytest.mark.foo), (3, 4), ]) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) rec = testdir.inline_run("-m", "foo", SHOW_PYTEST_WARNINGS_ARG) passed, skipped, fail = rec.listoutcomes() assert len(passed) == 1 assert len(skipped) == 0 assert len(fail) == 0 @pytest.mark.xfail(reason="is this important to support??") def test_nested_marks(self, testdir): s = """ import pytest mastermark = pytest.mark.foo(pytest.mark.bar) @pytest.mark.parametrize(("n", "expected"), [ (1, 2), mastermark((1, 3)), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ items = testdir.getitems(s) assert len(items) == 3 for mark in ["foo", "bar"]: assert mark not in items[0].keywords assert mark in items[1].keywords assert mark not in items[2].keywords def test_simple_xfail(self, testdir): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(1, 3, marks=pytest.mark.xfail), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) # xfail is skip?? reprec.assertoutcome(passed=2, skipped=1) def test_simple_xfail_single_argname(self, testdir): s = """ import pytest @pytest.mark.parametrize("n", [ 2, pytest.param(3, marks=pytest.mark.xfail), 4, ]) def test_isEven(n): assert n % 2 == 0 """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=2, skipped=1) def test_xfail_with_arg(self, testdir): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(1, 3, marks=pytest.mark.xfail("True")), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=2, skipped=1) def test_xfail_with_kwarg(self, testdir): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=2, skipped=1) def test_xfail_with_arg_and_kwarg(self, testdir): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(1, 3, marks=pytest.mark.xfail("True", reason="some bug")), (2, 3), ]) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=2, skipped=1) @pytest.mark.parametrize("strict", [True, False]) def test_xfail_passing_is_xpass(self, testdir, strict): s = """ import pytest m = pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict}) @pytest.mark.parametrize(("n", "expected"), [ (1, 2), pytest.param(2, 3, marks=m), (3, 4), ]) def test_increment(n, expected): assert n + 1 == expected """.format( strict=strict ) testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) passed, failed = (2, 1) if strict else (3, 0) reprec.assertoutcome(passed=passed, failed=failed) def test_parametrize_called_in_generate_tests(self, testdir): s = """ import pytest def pytest_generate_tests(metafunc): passingTestData = [(1, 2), (2, 3)] failingTestData = [(1, 3), (2, 2)] testData = passingTestData + [pytest.param(*d, marks=pytest.mark.xfail) for d in failingTestData] metafunc.parametrize(("n", "expected"), testData) def test_increment(n, expected): assert n + 1 == expected """ testdir.makepyfile(s) reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG) reprec.assertoutcome(passed=2, skipped=2) def test_parametrize_ID_generation_string_int_works(self, testdir): """#290""" testdir.makepyfile( """ import pytest @pytest.fixture def myfixture(): return 'example' @pytest.mark.parametrize( 'limit', (0, '0')) def test_limit(limit, myfixture): return """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) @pytest.mark.parametrize("strict", [True, False]) def test_parametrize_marked_value(self, testdir, strict): s = """ import pytest @pytest.mark.parametrize(("n", "expected"), [ pytest.param( 2,3, marks=pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict}), ), pytest.param( 2,3, marks=[pytest.mark.xfail("sys.version_info > (0, 0, 0)", reason="some bug", strict={strict})], ), ]) def test_increment(n, expected): assert n + 1 == expected """.format( strict=strict ) testdir.makepyfile(s) reprec = testdir.inline_run() passed, failed = (0, 2) if strict else (2, 0) reprec.assertoutcome(passed=passed, failed=failed) def test_pytest_make_parametrize_id(self, testdir): testdir.makeconftest( """ def pytest_make_parametrize_id(config, val): return str(val * 2) """ ) testdir.makepyfile( """ import pytest @pytest.mark.parametrize("x", range(2)) def test_func(x): pass """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines(["*test_func*0*PASS*", "*test_func*2*PASS*"]) def test_pytest_make_parametrize_id_with_argname(self, testdir): testdir.makeconftest( """ def pytest_make_parametrize_id(config, val, argname): return str(val * 2 if argname == 'x' else val * 10) """ ) testdir.makepyfile( """ import pytest @pytest.mark.parametrize("x", range(2)) def test_func_a(x): pass @pytest.mark.parametrize("y", [1]) def test_func_b(y): pass """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( ["*test_func_a*0*PASS*", "*test_func_a*2*PASS*", "*test_func_b*10*PASS*"] ) def test_parametrize_positional_args(self, testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize("a", [1], False) def test_foo(a): pass """ ) result = testdir.runpytest() result.assert_outcomes(passed=1) pytest-4.6.9/testing/python/show_fixtures_per_test.py0000664000372000037200000001036513604173400024115 0ustar travistravis00000000000000# -*- coding: utf-8 -*- def test_no_items_should_not_show_output(testdir): result = testdir.runpytest("--fixtures-per-test") assert "fixtures used by" not in result.stdout.str() assert result.ret == 0 def test_fixtures_in_module(testdir): p = testdir.makepyfile( ''' import pytest @pytest.fixture def _arg0(): """hidden arg0 fixture""" @pytest.fixture def arg1(): """arg1 docstring""" def test_arg1(arg1): pass ''' ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*", "*(test_fixtures_in_module.py:9)*", "arg1", " arg1 docstring", ] ) assert "_arg0" not in result.stdout.str() def test_fixtures_in_conftest(testdir): testdir.makeconftest( ''' import pytest @pytest.fixture def arg1(): """arg1 docstring""" @pytest.fixture def arg2(): """arg2 docstring""" @pytest.fixture def arg3(arg1, arg2): """arg3 docstring """ ''' ) p = testdir.makepyfile( """ def test_arg2(arg2): pass def test_arg3(arg3): pass """ ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "*fixtures used by test_arg2*", "*(test_fixtures_in_conftest.py:2)*", "arg2", " arg2 docstring", "*fixtures used by test_arg3*", "*(test_fixtures_in_conftest.py:4)*", "arg1", " arg1 docstring", "arg2", " arg2 docstring", "arg3", " arg3", " docstring", ] ) def test_should_show_fixtures_used_by_test(testdir): testdir.makeconftest( ''' import pytest @pytest.fixture def arg1(): """arg1 from conftest""" @pytest.fixture def arg2(): """arg2 from conftest""" ''' ) p = testdir.makepyfile( ''' import pytest @pytest.fixture def arg1(): """arg1 from testmodule""" def test_args(arg1, arg2): pass ''' ) result = testdir.runpytest("--fixtures-per-test", p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "*fixtures used by test_args*", "*(test_should_show_fixtures_used_by_test.py:6)*", "arg1", " arg1 from testmodule", "arg2", " arg2 from conftest", ] ) def test_verbose_include_private_fixtures_and_loc(testdir): testdir.makeconftest( ''' import pytest @pytest.fixture def _arg1(): """_arg1 from conftest""" @pytest.fixture def arg2(_arg1): """arg2 from conftest""" ''' ) p = testdir.makepyfile( ''' import pytest @pytest.fixture def arg3(): """arg3 from testmodule""" def test_args(arg2, arg3): pass ''' ) result = testdir.runpytest("--fixtures-per-test", "-v", p) assert result.ret == 0 result.stdout.fnmatch_lines( [ "*fixtures used by test_args*", "*(test_verbose_include_private_fixtures_and_loc.py:6)*", "_arg1 -- conftest.py:3", " _arg1 from conftest", "arg2 -- conftest.py:6", " arg2 from conftest", "arg3 -- test_verbose_include_private_fixtures_and_loc.py:3", " arg3 from testmodule", ] ) def test_doctest_items(testdir): testdir.makepyfile( ''' def foo(): """ >>> 1 + 1 2 """ ''' ) testdir.maketxtfile( """ >>> 1 + 1 2 """ ) result = testdir.runpytest( "--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v" ) assert result.ret == 0 result.stdout.fnmatch_lines(["*collected 2 items*"]) pytest-4.6.9/testing/python/fixtures.py0000664000372000037200000035520413604173400021154 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import textwrap import pytest from _pytest import fixtures from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureRequest from _pytest.pathlib import Path from _pytest.pytester import get_public_names from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG def test_getfuncargnames(): def f(): pass assert not fixtures.getfuncargnames(f) def g(arg): pass assert fixtures.getfuncargnames(g) == ("arg",) def h(arg1, arg2="hello"): pass assert fixtures.getfuncargnames(h) == ("arg1",) def h(arg1, arg2, arg3="hello"): pass assert fixtures.getfuncargnames(h) == ("arg1", "arg2") class A(object): def f(self, arg1, arg2="hello"): pass @staticmethod def static(arg1, arg2): pass assert fixtures.getfuncargnames(A().f) == ("arg1",) assert fixtures.getfuncargnames(A.static, cls=A) == ("arg1", "arg2") @pytest.mark.pytester_example_path("fixtures/fill_fixtures") class TestFillFixtures(object): def test_fillfuncargs_exposed(self): # used by oejskit, kept for compatibility assert pytest._fillfuncargs == fixtures.fillfixtures def test_funcarg_lookupfails(self, testdir): testdir.copy_example() result = testdir.runpytest() # "--collect-only") assert result.ret != 0 result.stdout.fnmatch_lines( """ *def test_func(some)* *fixture*some*not found* *xyzsomething* """ ) def test_detect_recursive_dependency_error(self, testdir): testdir.copy_example() result = testdir.runpytest() result.stdout.fnmatch_lines( ["*recursive dependency involving fixture 'fix1' detected*"] ) def test_funcarg_basic(self, testdir): testdir.copy_example() item = testdir.getitem(Path("test_funcarg_basic.py")) fixtures.fillfixtures(item) del item.funcargs["request"] assert len(get_public_names(item.funcargs)) == 2 assert item.funcargs["some"] == "test_func" assert item.funcargs["other"] == 42 def test_funcarg_lookup_modulelevel(self, testdir): testdir.copy_example() reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_funcarg_lookup_classlevel(self, testdir): p = testdir.copy_example() result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*1 passed*"]) def test_conftest_funcargs_only_available_in_subdir(self, testdir): testdir.copy_example() result = testdir.runpytest("-v") result.assert_outcomes(passed=2) def test_extend_fixture_module_class(self, testdir): testfile = testdir.copy_example() result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_module(self, testdir): p = testdir.copy_example() result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(next(p.visit("test_*.py"))) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_conftest(self, testdir): p = testdir.copy_example() result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(next(p.visit("test_*.py"))) result.stdout.fnmatch_lines(["*1 passed*"]) def test_extend_fixture_conftest_plugin(self, testdir): testdir.makepyfile( testplugin=""" import pytest @pytest.fixture def foo(): return 7 """ ) testdir.syspathinsert() testdir.makeconftest( """ import pytest pytest_plugins = 'testplugin' @pytest.fixture def foo(foo): return foo + 7 """ ) testdir.makepyfile( """ def test_foo(foo): assert foo == 14 """ ) result = testdir.runpytest("-s") assert result.ret == 0 def test_extend_fixture_plugin_plugin(self, testdir): # Two plugins should extend each order in loading order testdir.makepyfile( testplugin0=""" import pytest @pytest.fixture def foo(): return 7 """ ) testdir.makepyfile( testplugin1=""" import pytest @pytest.fixture def foo(foo): return foo + 7 """ ) testdir.syspathinsert() testdir.makepyfile( """ pytest_plugins = ['testplugin0', 'testplugin1'] def test_foo(foo): assert foo == 14 """ ) result = testdir.runpytest() assert result.ret == 0 def test_override_parametrized_fixture_conftest_module(self, testdir): """Test override of the parametrized fixture with non-parametrized one on the test module level.""" testdir.makeconftest( """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param """ ) testfile = testdir.makepyfile( """ import pytest @pytest.fixture def spam(): return 'spam' def test_spam(spam): assert spam == 'spam' """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_override_parametrized_fixture_conftest_conftest(self, testdir): """Test override of the parametrized fixture with non-parametrized one on the conftest level.""" testdir.makeconftest( """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param """ ) subdir = testdir.mkpydir("subdir") subdir.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def spam(): return 'spam' """ ) ) testfile = subdir.join("test_spam.py") testfile.write( textwrap.dedent( """\ def test_spam(spam): assert spam == "spam" """ ) ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*1 passed*"]) def test_override_non_parametrized_fixture_conftest_module(self, testdir): """Test override of the non-parametrized fixture with parametrized one on the test module level.""" testdir.makeconftest( """ import pytest @pytest.fixture def spam(): return 'spam' """ ) testfile = testdir.makepyfile( """ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param params = {'spam': 1} def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*3 passed*"]) def test_override_non_parametrized_fixture_conftest_conftest(self, testdir): """Test override of the non-parametrized fixture with parametrized one on the conftest level.""" testdir.makeconftest( """ import pytest @pytest.fixture def spam(): return 'spam' """ ) subdir = testdir.mkpydir("subdir") subdir.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param """ ) ) testfile = subdir.join("test_spam.py") testfile.write( textwrap.dedent( """\ params = {'spam': 1} def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 """ ) ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*3 passed*"]) def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest( self, testdir ): """Test override of the autouse fixture with parametrized one on the conftest level. This test covers the issue explained in issue 1601 """ testdir.makeconftest( """ import pytest @pytest.fixture(autouse=True) def spam(): return 'spam' """ ) subdir = testdir.mkpydir("subdir") subdir.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture(params=[1, 2, 3]) def spam(request): return request.param """ ) ) testfile = subdir.join("test_spam.py") testfile.write( textwrap.dedent( """\ params = {'spam': 1} def test_spam(spam): assert spam == params['spam'] params['spam'] += 1 """ ) ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) result = testdir.runpytest(testfile) result.stdout.fnmatch_lines(["*3 passed*"]) def test_autouse_fixture_plugin(self, testdir): # A fixture from a plugin has no baseid set, which screwed up # the autouse fixture handling. testdir.makepyfile( testplugin=""" import pytest @pytest.fixture(autouse=True) def foo(request): request.function.foo = 7 """ ) testdir.syspathinsert() testdir.makepyfile( """ pytest_plugins = 'testplugin' def test_foo(request): assert request.function.foo == 7 """ ) result = testdir.runpytest() assert result.ret == 0 def test_funcarg_lookup_error(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture def a_fixture(): pass @pytest.fixture def b_fixture(): pass @pytest.fixture def c_fixture(): pass @pytest.fixture def d_fixture(): pass """ ) testdir.makepyfile( """ def test_lookup_error(unknown): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ERROR at setup of test_lookup_error*", " def test_lookup_error(unknown):*", "E fixture 'unknown' not found", "> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", # sorted "> use 'py*test --fixtures *' for help on them.", "*1 error*", ] ) assert "INTERNAL" not in result.stdout.str() def test_fixture_excinfo_leak(self, testdir): # on python2 sys.excinfo would leak into fixture executions testdir.makepyfile( """ import sys import traceback import pytest @pytest.fixture def leak(): if sys.exc_info()[0]: # python3 bug :) traceback.print_exc() #fails assert sys.exc_info() == (None, None, None) def test_leak(leak): if sys.exc_info()[0]: # python3 bug :) traceback.print_exc() assert sys.exc_info() == (None, None, None) """ ) result = testdir.runpytest() assert result.ret == 0 class TestRequestBasic(object): def test_request_attributes(self, testdir): item = testdir.getitem( """ import pytest @pytest.fixture def something(request): pass def test_func(something): pass """ ) req = fixtures.FixtureRequest(item) assert req.function == item.obj assert req.keywords == item.keywords assert hasattr(req.module, "test_func") assert req.cls is None assert req.function.__name__ == "test_func" assert req.config == item.config assert repr(req).find(req.function.__name__) != -1 def test_request_attributes_method(self, testdir): (item,) = testdir.getitems( """ import pytest class TestB(object): @pytest.fixture def something(self, request): return 1 def test_func(self, something): pass """ ) req = item._request assert req.cls.__name__ == "TestB" assert req.instance.__class__ == req.cls def test_request_contains_funcarg_arg2fixturedefs(self, testdir): modcol = testdir.getmodulecol( """ import pytest @pytest.fixture def something(request): pass class TestClass(object): def test_method(self, something): pass """ ) (item1,) = testdir.genitems([modcol]) assert item1.name == "test_method" arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs assert len(arg2fixturedefs) == 1 assert arg2fixturedefs["something"][0].argname == "something" @pytest.mark.skipif( hasattr(sys, "pypy_version_info"), reason="this method of test doesn't work on pypy", ) def test_request_garbage(self, testdir): try: import xdist # noqa except ImportError: pass else: pytest.xfail("this test is flaky when executed with xdist") testdir.makepyfile( """ import sys import pytest from _pytest.fixtures import PseudoFixtureDef import gc @pytest.fixture(autouse=True) def something(request): original = gc.get_debug() gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() yield try: gc.collect() leaked = [x for _ in gc.garbage if isinstance(_, PseudoFixtureDef)] assert leaked == [] finally: gc.set_debug(original) def test_func(): pass """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["* 1 passed in *"]) def test_getfixturevalue_recursive(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture def something(request): return 1 """ ) testdir.makepyfile( """ import pytest @pytest.fixture def something(request): return request.getfixturevalue("something") + 1 def test_func(something): assert something == 2 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_getfixturevalue_teardown(self, testdir): """ Issue #1895 `test_inner` requests `inner` fixture, which in turn requests `resource` using `getfixturevalue`. `test_func` then requests `resource`. `resource` is teardown before `inner` because the fixture mechanism won't consider `inner` dependent on `resource` when it is used via `getfixturevalue`: `test_func` will then cause the `resource`'s finalizer to be called first because of this. """ testdir.makepyfile( """ import pytest @pytest.fixture(scope='session') def resource(): r = ['value'] yield r r.pop() @pytest.fixture(scope='session') def inner(request): resource = request.getfixturevalue('resource') assert resource == ['value'] yield assert resource == ['value'] def test_inner(inner): pass def test_func(resource): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed in *"]) @pytest.mark.parametrize("getfixmethod", ("getfixturevalue", "getfuncargvalue")) def test_getfixturevalue(self, testdir, getfixmethod): item = testdir.getitem( """ import pytest values = [2] @pytest.fixture def something(request): return 1 @pytest.fixture def other(request): return values.pop() def test_func(something): pass """ ) import contextlib if getfixmethod == "getfuncargvalue": warning_expectation = pytest.warns(DeprecationWarning) else: # see #1830 for a cleaner way to accomplish this @contextlib.contextmanager def expecting_no_warning(): yield warning_expectation = expecting_no_warning() req = item._request with warning_expectation: fixture_fetcher = getattr(req, getfixmethod) with pytest.raises(FixtureLookupError): fixture_fetcher("notexists") val = fixture_fetcher("something") assert val == 1 val = fixture_fetcher("something") assert val == 1 val2 = fixture_fetcher("other") assert val2 == 2 val2 = fixture_fetcher("other") # see about caching assert val2 == 2 pytest._fillfuncargs(item) assert item.funcargs["something"] == 1 assert len(get_public_names(item.funcargs)) == 2 assert "request" in item.funcargs def test_request_addfinalizer(self, testdir): item = testdir.getitem( """ import pytest teardownlist = [] @pytest.fixture def something(request): request.addfinalizer(lambda: teardownlist.append(1)) def test_func(something): pass """ ) item.session._setupstate.prepare(item) pytest._fillfuncargs(item) # successively check finalization calls teardownlist = item.getparent(pytest.Module).obj.teardownlist ss = item.session._setupstate assert not teardownlist ss.teardown_exact(item, None) print(ss.stack) assert teardownlist == [1] def test_request_addfinalizer_failing_setup(self, testdir): testdir.makepyfile( """ import pytest values = [1] @pytest.fixture def myfix(request): request.addfinalizer(values.pop) assert 0 def test_fix(myfix): pass def test_finalizer_ran(): assert not values """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(failed=1, passed=1) def test_request_addfinalizer_failing_setup_module(self, testdir): testdir.makepyfile( """ import pytest values = [1, 2] @pytest.fixture(scope="module") def myfix(request): request.addfinalizer(values.pop) request.addfinalizer(values.pop) assert 0 def test_fix(myfix): pass """ ) reprec = testdir.inline_run("-s") mod = reprec.getcalls("pytest_runtest_setup")[0].item.module assert not mod.values def test_request_addfinalizer_partial_setup_failure(self, testdir): p = testdir.makepyfile( """ import pytest values = [] @pytest.fixture def something(request): request.addfinalizer(lambda: values.append(None)) def test_func(something, missingarg): pass def test_second(): assert len(values) == 1 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( ["*1 error*"] # XXX the whole module collection fails ) def test_request_subrequest_addfinalizer_exceptions(self, testdir): """ Ensure exceptions raised during teardown by a finalizer are suppressed until all finalizers are called, re-raising the first exception (#2440) """ testdir.makepyfile( """ import pytest values = [] def _excepts(where): raise Exception('Error in %s fixture' % where) @pytest.fixture def subrequest(request): return request @pytest.fixture def something(subrequest): subrequest.addfinalizer(lambda: values.append(1)) subrequest.addfinalizer(lambda: values.append(2)) subrequest.addfinalizer(lambda: _excepts('something')) @pytest.fixture def excepts(subrequest): subrequest.addfinalizer(lambda: _excepts('excepts')) subrequest.addfinalizer(lambda: values.append(3)) def test_first(something, excepts): pass def test_second(): assert values == [3, 2, 1] """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*Exception: Error in excepts fixture", "* 2 passed, 1 error in *"] ) def test_request_getmodulepath(self, testdir): modcol = testdir.getmodulecol("def test_somefunc(): pass") (item,) = testdir.genitems([modcol]) req = fixtures.FixtureRequest(item) assert req.fspath == modcol.fspath def test_request_fixturenames(self, testdir): testdir.makepyfile( """ import pytest from _pytest.pytester import get_public_names @pytest.fixture() def arg1(): pass @pytest.fixture() def farg(arg1): pass @pytest.fixture(autouse=True) def sarg(tmpdir): pass def test_function(request, farg): assert set(get_public_names(request.fixturenames)) == \ set(["tmpdir", "sarg", "arg1", "request", "farg", "tmp_path", "tmp_path_factory"]) """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_request_fixturenames_dynamic_fixture(self, testdir): """Regression test for #3057""" testdir.copy_example("fixtures/test_getfixturevalue_dynamic.py") result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_funcargnames_compatattr(self, testdir): testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): assert metafunc.funcargnames == metafunc.fixturenames @pytest.fixture def fn(request): assert request._pyfuncitem.funcargnames == \ request._pyfuncitem.fixturenames return request.funcargnames, request.fixturenames def test_hello(fn): assert fn[0] == fn[1] """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setupdecorator_and_xunit(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope='module', autouse=True) def setup_module(): values.append("module") @pytest.fixture(autouse=True) def setup_function(): values.append("function") def test_func(): pass class TestClass(object): @pytest.fixture(scope="class", autouse=True) def setup_class(self): values.append("class") @pytest.fixture(autouse=True) def setup_method(self): values.append("method") def test_method(self): pass def test_all(): assert values == ["module", "function", "class", "function", "method", "function"] """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=3) def test_fixtures_sub_subdir_normalize_sep(self, testdir): # this tests that normalization of nodeids takes place b = testdir.mkdir("tests").mkdir("unit") b.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def arg1(): pass """ ) ) p = b.join("test_module.py") p.write("def test_func(arg1): pass") result = testdir.runpytest(p, "--fixtures") assert result.ret == 0 result.stdout.fnmatch_lines( """ *fixtures defined*conftest* *arg1* """ ) def test_show_fixtures_color_yes(self, testdir): testdir.makepyfile("def test_this(): assert 1") result = testdir.runpytest("--color=yes", "--fixtures") assert "\x1b[32mtmpdir" in result.stdout.str() def test_newstyle_with_request(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def arg(request): pass def test_1(arg): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setupcontext_no_param(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(params=[1,2]) def arg(request): return request.param @pytest.fixture(autouse=True) def mysetup(request, arg): assert not hasattr(request, "param") def test_1(arg): assert arg in (1,2) """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) class TestRequestMarking(object): def test_applymarker(self, testdir): item1, item2 = testdir.getitems( """ import pytest @pytest.fixture def something(request): pass class TestClass(object): def test_func1(self, something): pass def test_func2(self, something): pass """ ) req1 = fixtures.FixtureRequest(item1) assert "xfail" not in item1.keywords req1.applymarker(pytest.mark.xfail) assert "xfail" in item1.keywords assert "skipif" not in item1.keywords req1.applymarker(pytest.mark.skipif) assert "skipif" in item1.keywords with pytest.raises(ValueError): req1.applymarker(42) def test_accesskeywords(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def keywords(request): return request.keywords @pytest.mark.XYZ def test_function(keywords): assert keywords["XYZ"] assert "abc" not in keywords """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_accessmarker_dynamic(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture() def keywords(request): return request.keywords @pytest.fixture(scope="class", autouse=True) def marking(request): request.applymarker(pytest.mark.XYZ("hello")) """ ) testdir.makepyfile( """ import pytest def test_fun1(keywords): assert keywords["XYZ"] is not None assert "abc" not in keywords def test_fun2(keywords): assert keywords["XYZ"] is not None assert "abc" not in keywords """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) class TestFixtureUsages(object): def test_noargfixturedec(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def arg1(): return 1 def test_func(arg1): assert arg1 == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_receives_funcargs(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def arg1(): return 1 @pytest.fixture() def arg2(arg1): return arg1 + 1 def test_add(arg2): assert arg2 == 2 def test_all(arg1, arg2): assert arg1 == 1 assert arg2 == 2 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_receives_funcargs_scope_mismatch(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="function") def arg1(): return 1 @pytest.fixture(scope="module") def arg2(arg1): return arg1 + 1 def test_add(arg2): assert arg2 == 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*ScopeMismatch*involved factories*", "test_receives_funcargs_scope_mismatch.py:6: def arg2(arg1)", "test_receives_funcargs_scope_mismatch.py:2: def arg1()", "*1 error*", ] ) def test_receives_funcargs_scope_mismatch_issue660(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="function") def arg1(): return 1 @pytest.fixture(scope="module") def arg2(arg1): return arg1 + 1 def test_add(arg1, arg2): assert arg2 == 2 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*ScopeMismatch*involved factories*", "* def arg2*", "*1 error*"] ) def test_invalid_scope(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="functions") def badscope(): pass def test_nothing(badscope): pass """ ) result = testdir.runpytest_inprocess() result.stdout.fnmatch_lines( "*Fixture 'badscope' from test_invalid_scope.py got an unexpected scope value 'functions'" ) def test_funcarg_parametrized_and_used_twice(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(params=[1,2]) def arg1(request): values.append(1) return request.param @pytest.fixture() def arg2(arg1): return arg1 + 1 def test_add(arg1, arg2): assert arg2 == arg1 + 1 assert len(values) == arg1 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 passed*"]) def test_factory_uses_unknown_funcarg_as_dependency_error(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def fail(missing): return @pytest.fixture() def call_fail(fail): return def test_missing(call_fail): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *pytest.fixture()* *def call_fail(fail)* *pytest.fixture()* *def fail* *fixture*'missing'*not found* """ ) def test_factory_setup_as_classes_fails(self, testdir): testdir.makepyfile( """ import pytest class arg1(object): def __init__(self, request): self.x = 1 arg1 = pytest.fixture()(arg1) """ ) reprec = testdir.inline_run() values = reprec.getfailedcollections() assert len(values) == 1 @pytest.mark.filterwarnings("ignore::pytest.PytestDeprecationWarning") def test_request_can_be_overridden(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def request(request): request.a = 1 return request def test_request(request): assert request.a == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_usefixtures_marker(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="class") def myfix(request): request.cls.hello = "world" values.append(1) class TestClass(object): def test_one(self): assert self.hello == "world" assert len(values) == 1 def test_two(self): assert self.hello == "world" assert len(values) == 1 pytest.mark.usefixtures("myfix")(TestClass) """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_usefixtures_ini(self, testdir): testdir.makeini( """ [pytest] usefixtures = myfix """ ) testdir.makeconftest( """ import pytest @pytest.fixture(scope="class") def myfix(request): request.cls.hello = "world" """ ) testdir.makepyfile( """ class TestClass(object): def test_one(self): assert self.hello == "world" def test_two(self): assert self.hello == "world" """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_usefixtures_seen_in_showmarkers(self, testdir): result = testdir.runpytest("--markers") result.stdout.fnmatch_lines( """ *usefixtures(fixturename1*mark tests*fixtures* """ ) def test_request_instance_issue203(self, testdir): testdir.makepyfile( """ import pytest class TestClass(object): @pytest.fixture def setup1(self, request): assert self == request.instance self.arg1 = 1 def test_hello(self, setup1): assert self.arg1 == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_fixture_parametrized_with_iterator(self, testdir): testdir.makepyfile( """ import pytest values = [] def f(): yield 1 yield 2 dec = pytest.fixture(scope="module", params=f()) @dec def arg(request): return request.param @dec def arg2(request): return request.param def test_1(arg): values.append(arg) def test_2(arg2): values.append(arg2*10) """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [1, 2, 10, 20] def test_setup_functions_as_fixtures(self, testdir): """Ensure setup_* methods obey fixture scope rules (#517, #3094).""" testdir.makepyfile( """ import pytest DB_INITIALIZED = None @pytest.yield_fixture(scope="session", autouse=True) def db(): global DB_INITIALIZED DB_INITIALIZED = True yield DB_INITIALIZED = False def setup_module(): assert DB_INITIALIZED def teardown_module(): assert DB_INITIALIZED class TestClass(object): def setup_method(self, method): assert DB_INITIALIZED def teardown_method(self, method): assert DB_INITIALIZED def test_printer_1(self): pass def test_printer_2(self): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 2 passed in *"]) class TestFixtureManagerParseFactories(object): @pytest.fixture def testdir(self, request): testdir = request.getfixturevalue("testdir") testdir.makeconftest( """ import pytest @pytest.fixture def hello(request): return "conftest" @pytest.fixture def fm(request): return request._fixturemanager @pytest.fixture def item(request): return request._pyfuncitem """ ) return testdir def test_parsefactories_evil_objects_issue214(self, testdir): testdir.makepyfile( """ class A(object): def __call__(self): pass def __getattr__(self, name): raise RuntimeError() a = A() def test_hello(): pass """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1, failed=0) def test_parsefactories_conftest(self, testdir): testdir.makepyfile( """ def test_hello(item, fm): for name in ("fm", "hello", "item"): faclist = fm.getfixturedefs(name, item.nodeid) assert len(faclist) == 1 fac = faclist[0] assert fac.func.__name__ == name """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_parsefactories_conftest_and_module_and_class(self, testdir): testdir.makepyfile( """ import pytest import six @pytest.fixture def hello(request): return "module" class TestClass(object): @pytest.fixture def hello(self, request): return "class" def test_hello(self, item, fm): faclist = fm.getfixturedefs("hello", item.nodeid) print(faclist) assert len(faclist) == 3 assert faclist[0].func(item._request) == "conftest" assert faclist[1].func(item._request) == "module" assert faclist[2].func(item._request) == "class" """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_parsefactories_relative_node_ids(self, testdir): # example mostly taken from: # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html runner = testdir.mkdir("runner") package = testdir.mkdir("package") package.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def one(): return 1 """ ) ) package.join("test_x.py").write( textwrap.dedent( """\ def test_x(one): assert one == 1 """ ) ) sub = package.mkdir("sub") sub.join("__init__.py").ensure() sub.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def one(): return 2 """ ) ) sub.join("test_y.py").write( textwrap.dedent( """\ def test_x(one): assert one == 2 """ ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) with runner.as_cwd(): reprec = testdir.inline_run("..") reprec.assertoutcome(passed=2) def test_package_xunit_fixture(self, testdir): testdir.makepyfile( __init__="""\ values = [] """ ) package = testdir.mkdir("package") package.join("__init__.py").write( textwrap.dedent( """\ from .. import values def setup_module(): values.append("package") def teardown_module(): values[:] = [] """ ) ) package.join("test_x.py").write( textwrap.dedent( """\ from .. import values def test_x(): assert values == ["package"] """ ) ) package = testdir.mkdir("package2") package.join("__init__.py").write( textwrap.dedent( """\ from .. import values def setup_module(): values.append("package2") def teardown_module(): values[:] = [] """ ) ) package.join("test_x.py").write( textwrap.dedent( """\ from .. import values def test_x(): assert values == ["package2"] """ ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_package_fixture_complex(self, testdir): testdir.makepyfile( __init__="""\ values = [] """ ) testdir.syspathinsert(testdir.tmpdir.dirname) package = testdir.mkdir("package") package.join("__init__.py").write("") package.join("conftest.py").write( textwrap.dedent( """\ import pytest from .. import values @pytest.fixture(scope="package") def one(): values.append("package") yield values values.pop() @pytest.fixture(scope="package", autouse=True) def two(): values.append("package-auto") yield values values.pop() """ ) ) package.join("test_x.py").write( textwrap.dedent( """\ from .. import values def test_package_autouse(): assert values == ["package-auto"] def test_package(one): assert values == ["package-auto", "package"] """ ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_collect_custom_items(self, testdir): testdir.copy_example("fixtures/custom_item") result = testdir.runpytest("foo") result.stdout.fnmatch_lines(["*passed*"]) class TestAutouseDiscovery(object): @pytest.fixture def testdir(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture(autouse=True) def perfunction(request, tmpdir): pass @pytest.fixture() def arg1(tmpdir): pass @pytest.fixture(autouse=True) def perfunction2(arg1): pass @pytest.fixture def fm(request): return request._fixturemanager @pytest.fixture def item(request): return request._pyfuncitem """ ) return testdir def test_parsefactories_conftest(self, testdir): testdir.makepyfile( """ from _pytest.pytester import get_public_names def test_check_setup(item, fm): autousenames = fm._getautousenames(item.nodeid) assert len(get_public_names(autousenames)) == 2 assert "perfunction2" in autousenames assert "perfunction" in autousenames """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_two_classes_separated_autouse(self, testdir): testdir.makepyfile( """ import pytest class TestA(object): values = [] @pytest.fixture(autouse=True) def setup1(self): self.values.append(1) def test_setup1(self): assert self.values == [1] class TestB(object): values = [] @pytest.fixture(autouse=True) def setup2(self): self.values.append(1) def test_setup2(self): assert self.values == [1] """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_setup_at_classlevel(self, testdir): testdir.makepyfile( """ import pytest class TestClass(object): @pytest.fixture(autouse=True) def permethod(self, request): request.instance.funcname = request.function.__name__ def test_method1(self): assert self.funcname == "test_method1" def test_method2(self): assert self.funcname == "test_method2" """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) @pytest.mark.xfail(reason="'enabled' feature not implemented") def test_setup_enabled_functionnode(self, testdir): testdir.makepyfile( """ import pytest def enabled(parentnode, markers): return "needsdb" in markers @pytest.fixture(params=[1,2]) def db(request): return request.param @pytest.fixture(enabled=enabled, autouse=True) def createdb(db): pass def test_func1(request): assert "db" not in request.fixturenames @pytest.mark.needsdb def test_func2(request): assert "db" in request.fixturenames """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) def test_callables_nocode(self, testdir): """ an imported mock.call would break setup/factory discovery due to it being callable and __code__ not being a code object """ testdir.makepyfile( """ class _call(tuple): def __call__(self, *k, **kw): pass def __getattr__(self, k): return self call = _call() """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(failed=0, passed=0) def test_autouse_in_conftests(self, testdir): a = testdir.mkdir("a") b = testdir.mkdir("a1") conftest = testdir.makeconftest( """ import pytest @pytest.fixture(autouse=True) def hello(): xxx """ ) conftest.move(a.join(conftest.basename)) a.join("test_something.py").write("def test_func(): pass") b.join("test_otherthing.py").write("def test_func(): pass") result = testdir.runpytest() result.stdout.fnmatch_lines( """ *1 passed*1 error* """ ) def test_autouse_in_module_and_two_classes(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(autouse=True) def append1(): values.append("module") def test_x(): assert values == ["module"] class TestA(object): @pytest.fixture(autouse=True) def append2(self): values.append("A") def test_hello(self): assert values == ["module", "module", "A"], values class TestA2(object): def test_world(self): assert values == ["module", "module", "A", "module"], values """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) class TestAutouseManagement(object): def test_autouse_conftest_mid_directory(self, testdir): pkgdir = testdir.mkpydir("xyz123") pkgdir.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture(autouse=True) def app(): import sys sys._myapp = "hello" """ ) ) t = pkgdir.ensure("tests", "test_app.py") t.write( textwrap.dedent( """\ import sys def test_app(): assert sys._myapp == "hello" """ ) ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) def test_funcarg_and_setup(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="module") def arg(): values.append(1) return 0 @pytest.fixture(scope="module", autouse=True) def something(arg): values.append(2) def test_hello(arg): assert len(values) == 2 assert values == [1,2] assert arg == 0 def test_hello2(arg): assert len(values) == 2 assert values == [1,2] assert arg == 0 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_uses_parametrized_resource(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(params=[1,2]) def arg(request): return request.param @pytest.fixture(autouse=True) def something(arg): values.append(arg) def test_hello(): if len(values) == 1: assert values == [1] elif len(values) == 2: assert values == [1, 2] else: 0/0 """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) def test_session_parametrized_function(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="session", params=[1,2]) def arg(request): return request.param @pytest.fixture(scope="function", autouse=True) def append(request, arg): if request.function.__name__ == "test_some": values.append(arg) def test_some(): pass def test_result(arg): assert len(values) == arg assert values[:arg] == [1,2][:arg] """ ) reprec = testdir.inline_run("-v", "-s") reprec.assertoutcome(passed=4) def test_class_function_parametrization_finalization(self, testdir): p = testdir.makeconftest( """ import pytest import pprint values = [] @pytest.fixture(scope="function", params=[1,2]) def farg(request): return request.param @pytest.fixture(scope="class", params=list("ab")) def carg(request): return request.param @pytest.fixture(scope="function", autouse=True) def append(request, farg, carg): def fin(): values.append("fin_%s%s" % (carg, farg)) request.addfinalizer(fin) """ ) testdir.makepyfile( """ import pytest class TestClass(object): def test_1(self): pass class TestClass2(object): def test_2(self): pass """ ) confcut = "--confcutdir={}".format(testdir.tmpdir) reprec = testdir.inline_run("-v", "-s", confcut) reprec.assertoutcome(passed=8) config = reprec.getcalls("pytest_unconfigure")[0].config values = config.pluginmanager._getconftestmodules(p)[0].values assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2 def test_scope_ordering(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="function", autouse=True) def fappend2(): values.append(2) @pytest.fixture(scope="class", autouse=True) def classappend3(): values.append(3) @pytest.fixture(scope="module", autouse=True) def mappend(): values.append(1) class TestHallo(object): def test_method(self): assert values == [1,3,2] """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_parametrization_setup_teardown_ordering(self, testdir): testdir.makepyfile( """ import pytest values = [] def pytest_generate_tests(metafunc): if metafunc.cls is None: assert metafunc.function is test_finish if metafunc.cls is not None: metafunc.parametrize("item", [1,2], scope="class") class TestClass(object): @pytest.fixture(scope="class", autouse=True) def addteardown(self, item, request): values.append("setup-%d" % item) request.addfinalizer(lambda: values.append("teardown-%d" % item)) def test_step1(self, item): values.append("step1-%d" % item) def test_step2(self, item): values.append("step2-%d" % item) def test_finish(): print(values) assert values == ["setup-1", "step1-1", "step2-1", "teardown-1", "setup-2", "step1-2", "step2-2", "teardown-2",] """ ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=5) def test_ordering_autouse_before_explicit(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(autouse=True) def fix1(): values.append(1) @pytest.fixture() def arg1(): values.append(2) def test_hello(arg1): assert values == [1,2] """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"]) @pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"]) def test_ordering_dependencies_torndown_first(self, testdir, param1, param2): """#226""" testdir.makepyfile( """ import pytest values = [] @pytest.fixture(%(param1)s) def arg1(request): request.addfinalizer(lambda: values.append("fin1")) values.append("new1") @pytest.fixture(%(param2)s) def arg2(request, arg1): request.addfinalizer(lambda: values.append("fin2")) values.append("new2") def test_arg(arg2): pass def test_check(): assert values == ["new1", "new2", "fin2", "fin1"] """ % locals() ) reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=2) class TestFixtureMarker(object): def test_parametrize(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(params=["a", "b", "c"]) def arg(request): return request.param values = [] def test_param(arg): values.append(arg) def test_result(): assert values == list("abc") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=4) def test_multiple_parametrization_issue_736(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(params=[1,2,3]) def foo(request): return request.param @pytest.mark.parametrize('foobar', [4,5,6]) def test_issue(foo, foobar): assert foo in [1,2,3] assert foobar in [4,5,6] """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=9) @pytest.mark.parametrize( "param_args", ["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"], ) def test_override_parametrized_fixture_issue_979(self, testdir, param_args): """Make sure a parametrized argument can override a parametrized fixture. This was a regression introduced in the fix for #736. """ testdir.makepyfile( """ import pytest @pytest.fixture(params=[1, 2]) def fixt(request): return request.param @pytest.mark.parametrize(%s, [(3, 'x'), (4, 'x')]) def test_foo(fixt, val): pass """ % param_args ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_scope_session(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="module") def arg(): values.append(1) return 1 def test_1(arg): assert arg == 1 def test_2(arg): assert arg == 1 assert len(values) == 1 class TestClass(object): def test3(self, arg): assert arg == 1 assert len(values) == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) def test_scope_session_exc(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="session") def fix(): values.append(1) pytest.skip('skipping') def test_1(fix): pass def test_2(fix): pass def test_last(): assert values == [1] """ ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) def test_scope_session_exc_two_fix(self, testdir): testdir.makepyfile( """ import pytest values = [] m = [] @pytest.fixture(scope="session") def a(): values.append(1) pytest.skip('skipping') @pytest.fixture(scope="session") def b(a): m.append(1) def test_1(b): pass def test_2(b): pass def test_last(): assert values == [1] assert m == [] """ ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) def test_scope_exc(self, testdir): testdir.makepyfile( test_foo=""" def test_foo(fix): pass """, test_bar=""" def test_bar(fix): pass """, conftest=""" import pytest reqs = [] @pytest.fixture(scope="session") def fix(request): reqs.append(1) pytest.skip() @pytest.fixture def req_list(): return reqs """, test_real=""" def test_last(req_list): assert req_list == [1] """, ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=2, passed=1) def test_scope_module_uses_session(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="module") def arg(): values.append(1) return 1 def test_1(arg): assert arg == 1 def test_2(arg): assert arg == 1 assert len(values) == 1 class TestClass(object): def test3(self, arg): assert arg == 1 assert len(values) == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=3) def test_scope_module_and_finalizer(self, testdir): testdir.makeconftest( """ import pytest finalized_list = [] created_list = [] @pytest.fixture(scope="module") def arg(request): created_list.append(1) assert request.scope == "module" request.addfinalizer(lambda: finalized_list.append(1)) @pytest.fixture def created(request): return len(created_list) @pytest.fixture def finalized(request): return len(finalized_list) """ ) testdir.makepyfile( test_mod1=""" def test_1(arg, created, finalized): assert created == 1 assert finalized == 0 def test_2(arg, created, finalized): assert created == 1 assert finalized == 0""", test_mod2=""" def test_3(arg, created, finalized): assert created == 2 assert finalized == 1""", test_mode3=""" def test_4(arg, created, finalized): assert created == 3 assert finalized == 2 """, ) reprec = testdir.inline_run() reprec.assertoutcome(passed=4) def test_scope_mismatch_various(self, testdir): testdir.makeconftest( """ import pytest finalized = [] created = [] @pytest.fixture(scope="function") def arg(request): pass """ ) testdir.makepyfile( test_mod1=""" import pytest @pytest.fixture(scope="session") def arg(request): request.getfixturevalue("arg") def test_1(arg): pass """ ) result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG) assert result.ret != 0 result.stdout.fnmatch_lines( ["*ScopeMismatch*You tried*function*session*request*"] ) def test_register_only_with_mark(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture() def arg(): return 1 """ ) testdir.makepyfile( test_mod1=""" import pytest @pytest.fixture() def arg(arg): return arg + 1 def test_1(arg): assert arg == 2 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_parametrize_and_scope(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="module", params=["a", "b", "c"]) def arg(request): return request.param values = [] def test_param(arg): values.append(arg) """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=3) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert len(values) == 3 assert "a" in values assert "b" in values assert "c" in values def test_scope_mismatch(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture(scope="function") def arg(request): pass """ ) testdir.makepyfile( """ import pytest @pytest.fixture(scope="session") def arg(arg): pass def test_mismatch(arg): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*ScopeMismatch*", "*1 error*"]) def test_parametrize_separated_order(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="module", params=[1, 2]) def arg(request): return request.param values = [] def test_1(arg): values.append(arg) def test_2(arg): values.append(arg) """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [1, 1, 2, 2] def test_module_parametrized_ordering(self, testdir): testdir.makeini( """ [pytest] console_output_style=classic """ ) testdir.makeconftest( """ import pytest @pytest.fixture(scope="session", params="s1 s2".split()) def sarg(): pass @pytest.fixture(scope="module", params="m1 m2".split()) def marg(): pass """ ) testdir.makepyfile( test_mod1=""" def test_func(sarg): pass def test_func1(marg): pass """, test_mod2=""" def test_func2(sarg): pass def test_func3(sarg, marg): pass def test_func3b(sarg, marg): pass def test_func4(marg): pass """, ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( """ test_mod1.py::test_func[s1] PASSED test_mod2.py::test_func2[s1] PASSED test_mod2.py::test_func3[s1-m1] PASSED test_mod2.py::test_func3b[s1-m1] PASSED test_mod2.py::test_func3[s1-m2] PASSED test_mod2.py::test_func3b[s1-m2] PASSED test_mod1.py::test_func[s2] PASSED test_mod2.py::test_func2[s2] PASSED test_mod2.py::test_func3[s2-m1] PASSED test_mod2.py::test_func3b[s2-m1] PASSED test_mod2.py::test_func4[m1] PASSED test_mod2.py::test_func3[s2-m2] PASSED test_mod2.py::test_func3b[s2-m2] PASSED test_mod2.py::test_func4[m2] PASSED test_mod1.py::test_func1[m1] PASSED test_mod1.py::test_func1[m2] PASSED """ ) def test_dynamic_parametrized_ordering(self, testdir): testdir.makeini( """ [pytest] console_output_style=classic """ ) testdir.makeconftest( """ import pytest def pytest_configure(config): class DynamicFixturePlugin(object): @pytest.fixture(scope='session', params=['flavor1', 'flavor2']) def flavor(self, request): return request.param config.pluginmanager.register(DynamicFixturePlugin(), 'flavor-fixture') @pytest.fixture(scope='session', params=['vxlan', 'vlan']) def encap(request): return request.param @pytest.fixture(scope='session', autouse='True') def reprovision(request, flavor, encap): pass """ ) testdir.makepyfile( """ def test(reprovision): pass def test2(reprovision): pass """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( """ test_dynamic_parametrized_ordering.py::test[flavor1-vxlan] PASSED test_dynamic_parametrized_ordering.py::test2[flavor1-vxlan] PASSED test_dynamic_parametrized_ordering.py::test[flavor2-vxlan] PASSED test_dynamic_parametrized_ordering.py::test2[flavor2-vxlan] PASSED test_dynamic_parametrized_ordering.py::test[flavor2-vlan] PASSED test_dynamic_parametrized_ordering.py::test2[flavor2-vlan] PASSED test_dynamic_parametrized_ordering.py::test[flavor1-vlan] PASSED test_dynamic_parametrized_ordering.py::test2[flavor1-vlan] PASSED """ ) def test_class_ordering(self, testdir): testdir.makeini( """ [pytest] console_output_style=classic """ ) testdir.makeconftest( """ import pytest values = [] @pytest.fixture(scope="function", params=[1,2]) def farg(request): return request.param @pytest.fixture(scope="class", params=list("ab")) def carg(request): return request.param @pytest.fixture(scope="function", autouse=True) def append(request, farg, carg): def fin(): values.append("fin_%s%s" % (carg, farg)) request.addfinalizer(fin) """ ) testdir.makepyfile( """ import pytest class TestClass2(object): def test_1(self): pass def test_2(self): pass class TestClass(object): def test_3(self): pass """ ) result = testdir.runpytest("-vs") result.stdout.re_match_lines( r""" test_class_ordering.py::TestClass2::test_1\[a-1\] PASSED test_class_ordering.py::TestClass2::test_1\[a-2\] PASSED test_class_ordering.py::TestClass2::test_2\[a-1\] PASSED test_class_ordering.py::TestClass2::test_2\[a-2\] PASSED test_class_ordering.py::TestClass2::test_1\[b-1\] PASSED test_class_ordering.py::TestClass2::test_1\[b-2\] PASSED test_class_ordering.py::TestClass2::test_2\[b-1\] PASSED test_class_ordering.py::TestClass2::test_2\[b-2\] PASSED test_class_ordering.py::TestClass::test_3\[a-1\] PASSED test_class_ordering.py::TestClass::test_3\[a-2\] PASSED test_class_ordering.py::TestClass::test_3\[b-1\] PASSED test_class_ordering.py::TestClass::test_3\[b-2\] PASSED """ ) def test_parametrize_separated_order_higher_scope_first(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="function", params=[1, 2]) def arg(request): param = request.param request.addfinalizer(lambda: values.append("fin:%s" % param)) values.append("create:%s" % param) return request.param @pytest.fixture(scope="module", params=["mod1", "mod2"]) def modarg(request): param = request.param request.addfinalizer(lambda: values.append("fin:%s" % param)) values.append("create:%s" % param) return request.param values = [] def test_1(arg): values.append("test1") def test_2(modarg): values.append("test2") def test_3(arg, modarg): values.append("test3") def test_4(modarg, arg): values.append("test4") """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=12) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values expected = [ "create:1", "test1", "fin:1", "create:2", "test1", "fin:2", "create:mod1", "test2", "create:1", "test3", "fin:1", "create:2", "test3", "fin:2", "create:1", "test4", "fin:1", "create:2", "test4", "fin:2", "fin:mod1", "create:mod2", "test2", "create:1", "test3", "fin:1", "create:2", "test3", "fin:2", "create:1", "test4", "fin:1", "create:2", "test4", "fin:2", "fin:mod2", ] import pprint pprint.pprint(list(zip(values, expected))) assert values == expected def test_parametrized_fixture_teardown_order(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(params=[1,2], scope="class") def param1(request): return request.param values = [] class TestClass(object): @classmethod @pytest.fixture(scope="class", autouse=True) def setup1(self, request, param1): values.append(1) request.addfinalizer(self.teardown1) @classmethod def teardown1(self): assert values.pop() == 1 @pytest.fixture(scope="class", autouse=True) def setup2(self, request, param1): values.append(2) request.addfinalizer(self.teardown2) @classmethod def teardown2(self): assert values.pop() == 2 def test(self): pass def test_finish(): assert not values """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines( """ *3 passed* """ ) assert "error" not in result.stdout.str() def test_fixture_finalizer(self, testdir): testdir.makeconftest( """ import pytest import sys @pytest.fixture def browser(request): def finalize(): sys.stdout.write('Finalized') request.addfinalizer(finalize) return {} """ ) b = testdir.mkdir("subdir") b.join("test_overridden_fixture_finalizer.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def browser(browser): browser['visited'] = True return browser def test_browser(browser): assert browser['visited'] is True """ ) ) reprec = testdir.runpytest("-s") for test in ["test_browser"]: reprec.stdout.fnmatch_lines(["*Finalized*"]) def test_class_scope_with_normal_tests(self, testdir): testpath = testdir.makepyfile( """ import pytest class Box(object): value = 0 @pytest.fixture(scope='class') def a(request): Box.value += 1 return Box.value def test_a(a): assert a == 1 class Test1(object): def test_b(self, a): assert a == 2 class Test2(object): def test_c(self, a): assert a == 3""" ) reprec = testdir.inline_run(testpath) for test in ["test_a", "test_b", "test_c"]: assert reprec.matchreport(test).passed def test_request_is_clean(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(params=[1, 2]) def fix(request): request.addfinalizer(lambda: values.append(request.param)) def test_fix(fix): pass """ ) reprec = testdir.inline_run("-s") values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [1, 2] def test_parametrize_separated_lifecycle(self, testdir): testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope="module", params=[1, 2]) def arg(request): x = request.param request.addfinalizer(lambda: values.append("fin%s" % x)) return request.param def test_1(arg): values.append(arg) def test_2(arg): values.append(arg) """ ) reprec = testdir.inline_run("-vs") reprec.assertoutcome(passed=4) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values import pprint pprint.pprint(values) # assert len(values) == 6 assert values[0] == values[1] == 1 assert values[2] == "fin1" assert values[3] == values[4] == 2 assert values[5] == "fin2" def test_parametrize_function_scoped_finalizers_called(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="function", params=[1, 2]) def arg(request): x = request.param request.addfinalizer(lambda: values.append("fin%s" % x)) return request.param values = [] def test_1(arg): values.append(arg) def test_2(arg): values.append(arg) def test_3(): assert len(values) == 8 assert values == [1, "fin1", 2, "fin2", 1, "fin1", 2, "fin2"] """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=5) @pytest.mark.parametrize("scope", ["session", "function", "module"]) def test_finalizer_order_on_parametrization(self, scope, testdir): """#246""" testdir.makepyfile( """ import pytest values = [] @pytest.fixture(scope=%(scope)r, params=["1"]) def fix1(request): return request.param @pytest.fixture(scope=%(scope)r) def fix2(request, base): def cleanup_fix2(): assert not values, "base should not have been finalized" request.addfinalizer(cleanup_fix2) @pytest.fixture(scope=%(scope)r) def base(request, fix1): def cleanup_base(): values.append("fin_base") print("finalizing base") request.addfinalizer(cleanup_base) def test_begin(): pass def test_baz(base, fix2): pass def test_other(): pass """ % {"scope": scope} ) reprec = testdir.inline_run("-lvs") reprec.assertoutcome(passed=3) def test_class_scope_parametrization_ordering(self, testdir): """#396""" testdir.makepyfile( """ import pytest values = [] @pytest.fixture(params=["John", "Doe"], scope="class") def human(request): request.addfinalizer(lambda: values.append("fin %s" % request.param)) return request.param class TestGreetings(object): def test_hello(self, human): values.append("test_hello") class TestMetrics(object): def test_name(self, human): values.append("test_name") def test_population(self, human): values.append("test_population") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=6) values = reprec.getcalls("pytest_runtest_call")[0].item.module.values assert values == [ "test_hello", "fin John", "test_hello", "fin Doe", "test_name", "test_population", "fin John", "test_name", "test_population", "fin Doe", ] def test_parametrize_setup_function(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope="module", params=[1, 2]) def arg(request): return request.param @pytest.fixture(scope="module", autouse=True) def mysetup(request, arg): request.addfinalizer(lambda: values.append("fin%s" % arg)) values.append("setup%s" % arg) values = [] def test_1(arg): values.append(arg) def test_2(arg): values.append(arg) def test_3(): import pprint pprint.pprint(values) if arg == 1: assert values == ["setup1", 1, 1, ] elif arg == 2: assert values == ["setup1", 1, 1, "fin1", "setup2", 2, 2, ] """ ) reprec = testdir.inline_run("-v") reprec.assertoutcome(passed=6) def test_fixture_marked_function_not_collected_as_test(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def test_app(): return 1 def test_something(test_app): assert test_app == 1 """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_params_and_ids(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(params=[object(), object()], ids=['alpha', 'beta']) def fix(request): return request.param def test_foo(fix): assert 1 """ ) res = testdir.runpytest("-v") res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"]) def test_params_and_ids_yieldfixture(self, testdir): testdir.makepyfile( """ import pytest @pytest.yield_fixture(params=[object(), object()], ids=['alpha', 'beta']) def fix(request): yield request.param def test_foo(fix): assert 1 """ ) res = testdir.runpytest("-v") res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"]) def test_deterministic_fixture_collection(self, testdir, monkeypatch): """#920""" testdir.makepyfile( """ import pytest @pytest.fixture(scope="module", params=["A", "B", "C"]) def A(request): return request.param @pytest.fixture(scope="module", params=["DDDDDDDDD", "EEEEEEEEEEEE", "FFFFFFFFFFF", "banansda"]) def B(request, A): return request.param def test_foo(B): # Something funky is going on here. # Despite specified seeds, on what is collected, # sometimes we get unexpected passes. hashing B seems # to help? assert hash(B) or True """ ) monkeypatch.setenv("PYTHONHASHSEED", "1") out1 = testdir.runpytest_subprocess("-v") monkeypatch.setenv("PYTHONHASHSEED", "2") out2 = testdir.runpytest_subprocess("-v") out1 = [ line for line in out1.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo") ] out2 = [ line for line in out2.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo") ] assert len(out1) == 12 assert out1 == out2 class TestRequestScopeAccess(object): pytestmark = pytest.mark.parametrize( ("scope", "ok", "error"), [ ["session", "", "fspath class function module"], ["module", "module fspath", "cls function"], ["class", "module fspath cls", "function"], ["function", "module fspath cls function", ""], ], ) def test_setup(self, testdir, scope, ok, error): testdir.makepyfile( """ import pytest @pytest.fixture(scope=%r, autouse=True) def myscoped(request): for x in %r: assert hasattr(request, x) for x in %r: pytest.raises(AttributeError, lambda: getattr(request, x)) assert request.session assert request.config def test_func(): pass """ % (scope, ok.split(), error.split()) ) reprec = testdir.inline_run("-l") reprec.assertoutcome(passed=1) def test_funcarg(self, testdir, scope, ok, error): testdir.makepyfile( """ import pytest @pytest.fixture(scope=%r) def arg(request): for x in %r: assert hasattr(request, x) for x in %r: pytest.raises(AttributeError, lambda: getattr(request, x)) assert request.session assert request.config def test_func(arg): pass """ % (scope, ok.split(), error.split()) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) class TestErrors(object): def test_subfactory_missing_funcarg(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture() def gen(qwe123): return 1 def test_something(gen): pass """ ) result = testdir.runpytest() assert result.ret != 0 result.stdout.fnmatch_lines( ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"] ) def test_issue498_fixture_finalizer_failing(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture def fix1(request): def f(): raise KeyError request.addfinalizer(f) return object() values = [] def test_1(fix1): values.append(fix1) def test_2(fix1): values.append(fix1) def test_3(): assert values[0] != values[1] """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *ERROR*teardown*test_1* *KeyError* *ERROR*teardown*test_2* *KeyError* *3 pass*2 error* """ ) def test_setupfunc_missing_funcarg(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(autouse=True) def gen(qwe123): return 1 def test_something(): pass """ ) result = testdir.runpytest() assert result.ret != 0 result.stdout.fnmatch_lines( ["*def gen(qwe123):*", "*fixture*qwe123*not found*", "*1 error*"] ) class TestShowFixtures(object): def test_funcarg_compat(self, testdir): config = testdir.parseconfigure("--funcargs") assert config.option.showfixtures def test_show_fixtures(self, testdir): result = testdir.runpytest("--fixtures") result.stdout.fnmatch_lines( [ "tmpdir_factory [[]session scope[]]", "*for the test session*", "tmpdir", "*temporary directory*", ] ) def test_show_fixtures_verbose(self, testdir): result = testdir.runpytest("--fixtures", "-v") result.stdout.fnmatch_lines( [ "tmpdir_factory [[]session scope[]] -- *tmpdir.py*", "*for the test session*", "tmpdir -- *tmpdir.py*", "*temporary directory*", ] ) def test_show_fixtures_testmodule(self, testdir): p = testdir.makepyfile( ''' import pytest @pytest.fixture def _arg0(): """ hidden """ @pytest.fixture def arg1(): """ hello world """ ''' ) result = testdir.runpytest("--fixtures", p) result.stdout.fnmatch_lines( """ *tmpdir *fixtures defined from* *arg1* *hello world* """ ) assert "arg0" not in result.stdout.str() @pytest.mark.parametrize("testmod", [True, False]) def test_show_fixtures_conftest(self, testdir, testmod): testdir.makeconftest( ''' import pytest @pytest.fixture def arg1(): """ hello world """ ''' ) if testmod: testdir.makepyfile( """ def test_hello(): pass """ ) result = testdir.runpytest("--fixtures") result.stdout.fnmatch_lines( """ *tmpdir* *fixtures defined from*conftest* *arg1* *hello world* """ ) def test_show_fixtures_trimmed_doc(self, testdir): p = testdir.makepyfile( textwrap.dedent( '''\ import pytest @pytest.fixture def arg1(): """ line1 line2 """ @pytest.fixture def arg2(): """ line1 line2 """ ''' ) ) result = testdir.runpytest("--fixtures", p) result.stdout.fnmatch_lines( textwrap.dedent( """\ * fixtures defined from test_show_fixtures_trimmed_doc * arg2 line1 line2 arg1 line1 line2 """ ) ) def test_show_fixtures_indented_doc(self, testdir): p = testdir.makepyfile( textwrap.dedent( '''\ import pytest @pytest.fixture def fixture1(): """ line1 indented line """ ''' ) ) result = testdir.runpytest("--fixtures", p) result.stdout.fnmatch_lines( textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_doc * fixture1 line1 indented line """ ) ) def test_show_fixtures_indented_doc_first_line_unindented(self, testdir): p = testdir.makepyfile( textwrap.dedent( '''\ import pytest @pytest.fixture def fixture1(): """line1 line2 indented line """ ''' ) ) result = testdir.runpytest("--fixtures", p) result.stdout.fnmatch_lines( textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_doc_first_line_unindented * fixture1 line1 line2 indented line """ ) ) def test_show_fixtures_indented_in_class(self, testdir): p = testdir.makepyfile( textwrap.dedent( '''\ import pytest class TestClass(object): @pytest.fixture def fixture1(self): """line1 line2 indented line """ ''' ) ) result = testdir.runpytest("--fixtures", p) result.stdout.fnmatch_lines( textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_in_class * fixture1 line1 line2 indented line """ ) ) def test_show_fixtures_different_files(self, testdir): """ #833: --fixtures only shows fixtures from first file """ testdir.makepyfile( test_a=''' import pytest @pytest.fixture def fix_a(): """Fixture A""" pass def test_a(fix_a): pass ''' ) testdir.makepyfile( test_b=''' import pytest @pytest.fixture def fix_b(): """Fixture B""" pass def test_b(fix_b): pass ''' ) result = testdir.runpytest("--fixtures") result.stdout.fnmatch_lines( """ * fixtures defined from test_a * fix_a Fixture A * fixtures defined from test_b * fix_b Fixture B """ ) def test_show_fixtures_with_same_name(self, testdir): testdir.makeconftest( ''' import pytest @pytest.fixture def arg1(): """Hello World in conftest.py""" return "Hello World" ''' ) testdir.makepyfile( """ def test_foo(arg1): assert arg1 == "Hello World" """ ) testdir.makepyfile( ''' import pytest @pytest.fixture def arg1(): """Hi from test module""" return "Hi" def test_bar(arg1): assert arg1 == "Hi" ''' ) result = testdir.runpytest("--fixtures") result.stdout.fnmatch_lines( """ * fixtures defined from conftest * arg1 Hello World in conftest.py * fixtures defined from test_show_fixtures_with_same_name * arg1 Hi from test module """ ) def test_fixture_disallow_twice(self): """Test that applying @pytest.fixture twice generates an error (#2334).""" with pytest.raises(ValueError): @pytest.fixture @pytest.fixture def foo(): pass class TestContextManagerFixtureFuncs(object): @pytest.fixture(params=["fixture", "yield_fixture"]) def flavor(self, request, testdir, monkeypatch): monkeypatch.setenv("PYTEST_FIXTURE_FLAVOR", request.param) testdir.makepyfile( test_context=""" import os import pytest import warnings VAR = "PYTEST_FIXTURE_FLAVOR" if VAR not in os.environ: warnings.warn("PYTEST_FIXTURE_FLAVOR was not set, assuming fixture") fixture = pytest.fixture else: fixture = getattr(pytest, os.environ[VAR]) """ ) def test_simple(self, testdir, flavor): testdir.makepyfile( """ from __future__ import print_function from test_context import fixture @fixture def arg1(): print("setup") yield 1 print("teardown") def test_1(arg1): print("test1", arg1) def test_2(arg1): print("test2", arg1) assert 0 """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *setup* *test1 1* *teardown* *setup* *test2 1* *teardown* """ ) def test_scoped(self, testdir, flavor): testdir.makepyfile( """ from __future__ import print_function from test_context import fixture @fixture(scope="module") def arg1(): print("setup") yield 1 print("teardown") def test_1(arg1): print("test1", arg1) def test_2(arg1): print("test2", arg1) """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *setup* *test1 1* *test2 1* *teardown* """ ) def test_setup_exception(self, testdir, flavor): testdir.makepyfile( """ from test_context import fixture @fixture(scope="module") def arg1(): pytest.fail("setup") yield 1 def test_1(arg1): pass """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *pytest.fail*setup* *1 error* """ ) def test_teardown_exception(self, testdir, flavor): testdir.makepyfile( """ from test_context import fixture @fixture(scope="module") def arg1(): yield 1 pytest.fail("teardown") def test_1(arg1): pass """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *pytest.fail*teardown* *1 passed*1 error* """ ) def test_yields_more_than_one(self, testdir, flavor): testdir.makepyfile( """ from test_context import fixture @fixture(scope="module") def arg1(): yield 1 yield 2 def test_1(arg1): pass """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines( """ *fixture function* *test_yields*:2* """ ) def test_custom_name(self, testdir, flavor): testdir.makepyfile( """ from test_context import fixture @fixture(name='meow') def arg1(): return 'mew' def test_1(meow): print(meow) """ ) result = testdir.runpytest("-s") result.stdout.fnmatch_lines(["*mew*"]) class TestParameterizedSubRequest(object): def test_call_from_fixture(self, testdir): testdir.makepyfile( test_call_from_fixture=""" import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param @pytest.fixture def get_named_fixture(request): return request.getfixturevalue('fix_with_param') def test_foo(request, get_named_fixture): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "The requested fixture has no parameter defined for test:", " test_call_from_fixture.py::test_foo", "Requested fixture 'fix_with_param' defined in:", "test_call_from_fixture.py:4", "Requested here:", "test_call_from_fixture.py:9", "*1 error in*", ] ) def test_call_from_test(self, testdir): testdir.makepyfile( test_call_from_test=""" import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param def test_foo(request): request.getfixturevalue('fix_with_param') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "The requested fixture has no parameter defined for test:", " test_call_from_test.py::test_foo", "Requested fixture 'fix_with_param' defined in:", "test_call_from_test.py:4", "Requested here:", "test_call_from_test.py:8", "*1 failed*", ] ) def test_external_fixture(self, testdir): testdir.makeconftest( """ import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param """ ) testdir.makepyfile( test_external_fixture=""" def test_foo(request): request.getfixturevalue('fix_with_param') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "The requested fixture has no parameter defined for test:", " test_external_fixture.py::test_foo", "", "Requested fixture 'fix_with_param' defined in:", "conftest.py:4", "Requested here:", "test_external_fixture.py:2", "*1 failed*", ] ) def test_non_relative_path(self, testdir): tests_dir = testdir.mkdir("tests") fixdir = testdir.mkdir("fixtures") fixfile = fixdir.join("fix.py") fixfile.write( textwrap.dedent( """\ import pytest @pytest.fixture(params=[0, 1, 2]) def fix_with_param(request): return request.param """ ) ) testfile = tests_dir.join("test_foos.py") testfile.write( textwrap.dedent( """\ from fix import fix_with_param def test_foo(request): request.getfixturevalue('fix_with_param') """ ) ) tests_dir.chdir() testdir.syspathinsert(fixdir) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "The requested fixture has no parameter defined for test:", " test_foos.py::test_foo", "", "Requested fixture 'fix_with_param' defined in:", "*fix.py:4", "Requested here:", "test_foos.py:4", "*1 failed*", ] ) def test_pytest_fixture_setup_and_post_finalizer_hook(testdir): testdir.makeconftest( """ from __future__ import print_function def pytest_fixture_setup(fixturedef, request): print('ROOT setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) def pytest_fixture_post_finalizer(fixturedef, request): print('ROOT finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) """ ) testdir.makepyfile( **{ "tests/conftest.py": """ from __future__ import print_function def pytest_fixture_setup(fixturedef, request): print('TESTS setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) def pytest_fixture_post_finalizer(fixturedef, request): print('TESTS finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name)) """, "tests/test_hooks.py": """ from __future__ import print_function import pytest @pytest.fixture() def my_fixture(): return 'some' def test_func(my_fixture): print('TEST test_func') assert my_fixture == 'some' """, } ) result = testdir.runpytest("-s") assert result.ret == 0 result.stdout.fnmatch_lines( [ "*TESTS setup hook called for my_fixture from test_func*", "*ROOT setup hook called for my_fixture from test_func*", "*TEST test_func*", "*TESTS finalizer hook called for my_fixture from test_func*", "*ROOT finalizer hook called for my_fixture from test_func*", ] ) class TestScopeOrdering(object): """Class of tests that ensure fixtures are ordered based on their scopes (#2405)""" @pytest.mark.parametrize("variant", ["mark", "autouse"]) def test_func_closure_module_auto(self, testdir, variant, monkeypatch): """Semantically identical to the example posted in #2405 when ``use_mark=True``""" monkeypatch.setenv("FIXTURE_ACTIVATION_VARIANT", variant) testdir.makepyfile( """ import warnings import os import pytest VAR = 'FIXTURE_ACTIVATION_VARIANT' VALID_VARS = ('autouse', 'mark') VARIANT = os.environ.get(VAR) if VARIANT is None or VARIANT not in VALID_VARS: warnings.warn("{!r} is not in {}, assuming autouse".format(VARIANT, VALID_VARS) ) variant = 'mark' @pytest.fixture(scope='module', autouse=VARIANT == 'autouse') def m1(): pass if VARIANT=='mark': pytestmark = pytest.mark.usefixtures('m1') @pytest.fixture(scope='function', autouse=True) def f1(): pass def test_func(m1): pass """ ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "m1 f1".split() def test_func_closure_with_native_fixtures(self, testdir, monkeypatch): """Sanity check that verifies the order returned by the closures and the actual fixture execution order: The execution order may differ because of fixture inter-dependencies. """ monkeypatch.setattr(pytest, "FIXTURE_ORDER", [], raising=False) testdir.makepyfile( """ import pytest FIXTURE_ORDER = pytest.FIXTURE_ORDER @pytest.fixture(scope="session") def s1(): FIXTURE_ORDER.append('s1') @pytest.fixture(scope="package") def p1(): FIXTURE_ORDER.append('p1') @pytest.fixture(scope="module") def m1(): FIXTURE_ORDER.append('m1') @pytest.fixture(scope='session') def my_tmpdir_factory(): FIXTURE_ORDER.append('my_tmpdir_factory') @pytest.fixture def my_tmpdir(my_tmpdir_factory): FIXTURE_ORDER.append('my_tmpdir') @pytest.fixture def f1(my_tmpdir): FIXTURE_ORDER.append('f1') @pytest.fixture def f2(): FIXTURE_ORDER.append('f2') def test_foo(f1, p1, m1, f2, s1): pass """ ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) # order of fixtures based on their scope and position in the parameter list assert ( request.fixturenames == "s1 my_tmpdir_factory p1 m1 f1 f2 my_tmpdir".split() ) testdir.runpytest() # actual fixture execution differs: dependent fixtures must be created first ("my_tmpdir") assert ( pytest.FIXTURE_ORDER == "s1 my_tmpdir_factory p1 m1 my_tmpdir f1 f2".split() ) def test_func_closure_module(self, testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='module') def m1(): pass @pytest.fixture(scope='function') def f1(): pass def test_func(f1, m1): pass """ ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "m1 f1".split() def test_func_closure_scopes_reordered(self, testdir): """Test ensures that fixtures are ordered by scope regardless of the order of the parameters, although fixtures of same scope keep the declared order """ testdir.makepyfile( """ import pytest @pytest.fixture(scope='session') def s1(): pass @pytest.fixture(scope='module') def m1(): pass @pytest.fixture(scope='function') def f1(): pass @pytest.fixture(scope='function') def f2(): pass class Test: @pytest.fixture(scope='class') def c1(cls): pass def test_func(self, f2, f1, c1, m1, s1): pass """ ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "s1 m1 c1 f2 f1".split() def test_func_closure_same_scope_closer_root_first(self, testdir): """Auto-use fixtures of same scope are ordered by closer-to-root first""" testdir.makeconftest( """ import pytest @pytest.fixture(scope='module', autouse=True) def m_conf(): pass """ ) testdir.makepyfile( **{ "sub/conftest.py": """ import pytest @pytest.fixture(scope='package', autouse=True) def p_sub(): pass @pytest.fixture(scope='module', autouse=True) def m_sub(): pass """, "sub/__init__.py": "", "sub/test_func.py": """ import pytest @pytest.fixture(scope='module', autouse=True) def m_test(): pass @pytest.fixture(scope='function') def f1(): pass def test_func(m_test, f1): pass """, } ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "p_sub m_conf m_sub m_test f1".split() def test_func_closure_all_scopes_complex(self, testdir): """Complex test involving all scopes and mixing autouse with normal fixtures""" testdir.makeconftest( """ import pytest @pytest.fixture(scope='session') def s1(): pass @pytest.fixture(scope='package', autouse=True) def p1(): pass """ ) testdir.makepyfile(**{"__init__.py": ""}) testdir.makepyfile( """ import pytest @pytest.fixture(scope='module', autouse=True) def m1(): pass @pytest.fixture(scope='module') def m2(s1): pass @pytest.fixture(scope='function') def f1(): pass @pytest.fixture(scope='function') def f2(): pass class Test: @pytest.fixture(scope='class', autouse=True) def c1(self): pass def test_func(self, f2, f1, m2): pass """ ) items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split() def test_multiple_packages(self, testdir): """Complex test involving multiple package fixtures. Make sure teardowns are executed in order. . └── root ├── __init__.py ├── sub1 │ ├── __init__.py │ ├── conftest.py │ └── test_1.py └── sub2 ├── __init__.py ├── conftest.py └── test_2.py """ root = testdir.mkdir("root") root.join("__init__.py").write("values = []") sub1 = root.mkdir("sub1") sub1.ensure("__init__.py") sub1.join("conftest.py").write( textwrap.dedent( """\ import pytest from .. import values @pytest.fixture(scope="package") def fix(): values.append("pre-sub1") yield values assert values.pop() == "pre-sub1" """ ) ) sub1.join("test_1.py").write( textwrap.dedent( """\ from .. import values def test_1(fix): assert values == ["pre-sub1"] """ ) ) sub2 = root.mkdir("sub2") sub2.ensure("__init__.py") sub2.join("conftest.py").write( textwrap.dedent( """\ import pytest from .. import values @pytest.fixture(scope="package") def fix(): values.append("pre-sub2") yield values assert values.pop() == "pre-sub2" """ ) ) sub2.join("test_2.py").write( textwrap.dedent( """\ from .. import values def test_2(fix): assert values == ["pre-sub2"] """ ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=2) def test_call_fixture_function_error(): """Check if an error is raised if a fixture function is called directly (#4545)""" @pytest.fixture def fix(): return 1 with pytest.raises(pytest.fail.Exception): assert fix() == 1 def test_fixture_param_shadowing(testdir): """Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)""" testdir.makepyfile( """ import pytest @pytest.fixture(params=['a', 'b']) def argroot(request): return request.param @pytest.fixture def arg(argroot): return argroot # This should only be parametrized directly @pytest.mark.parametrize("arg", [1]) def test_direct(arg): assert arg == 1 # This should be parametrized based on the fixtures def test_normal_fixture(arg): assert isinstance(arg, str) # Indirect should still work: @pytest.fixture def arg2(request): return 2*request.param @pytest.mark.parametrize("arg2", [1], indirect=True) def test_indirect(arg2): assert arg2 == 2 """ ) # Only one test should have run result = testdir.runpytest("-v") result.assert_outcomes(passed=4) result.stdout.fnmatch_lines(["*::test_direct[[]1[]]*"]) result.stdout.fnmatch_lines(["*::test_normal_fixture[[]a[]]*"]) result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"]) result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"]) pytest-4.6.9/testing/python/raises.py0000664000372000037200000002477413604173400020576 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import six import pytest from _pytest.compat import dummy_context_manager from _pytest.outcomes import Failed from _pytest.warning_types import PytestDeprecationWarning class TestRaises(object): def test_raises(self): source = "int('qwe')" with pytest.warns(PytestDeprecationWarning): excinfo = pytest.raises(ValueError, source) code = excinfo.traceback[-1].frame.code s = str(code.fullsource) assert s == source def test_raises_exec(self): with pytest.warns(PytestDeprecationWarning) as warninfo: pytest.raises(ValueError, "a,x = []") assert warninfo[0].filename == __file__ def test_raises_exec_correct_filename(self): with pytest.warns(PytestDeprecationWarning): excinfo = pytest.raises(ValueError, 'int("s")') assert __file__ in excinfo.traceback[-1].path def test_raises_syntax_error(self): with pytest.warns(PytestDeprecationWarning) as warninfo: pytest.raises(SyntaxError, "qwe qwe qwe") assert warninfo[0].filename == __file__ def test_raises_function(self): pytest.raises(ValueError, int, "hello") def test_raises_callable_no_exception(self): class A(object): def __call__(self): pass try: pytest.raises(ValueError, A()) except pytest.raises.Exception: pass def test_raises_falsey_type_error(self): with pytest.raises(TypeError): with pytest.raises(AssertionError, match=0): raise AssertionError("ohai") def test_raises_repr_inflight(self): """Ensure repr() on an exception info inside a pytest.raises with block works (#4386)""" class E(Exception): pass with pytest.raises(E) as excinfo: # this test prints the inflight uninitialized object # using repr and str as well as pprint to demonstrate # it works print(str(excinfo)) print(repr(excinfo)) import pprint pprint.pprint(excinfo) raise E() def test_raises_as_contextmanager(self, testdir): testdir.makepyfile( """ import pytest import _pytest._code def test_simple(): with pytest.raises(ZeroDivisionError) as excinfo: assert isinstance(excinfo, _pytest._code.ExceptionInfo) 1/0 print(excinfo) assert excinfo.type == ZeroDivisionError assert isinstance(excinfo.value, ZeroDivisionError) def test_noraise(): with pytest.raises(pytest.raises.Exception): with pytest.raises(ValueError): int() def test_raise_wrong_exception_passes_by(): with pytest.raises(ZeroDivisionError): with pytest.raises(ValueError): 1/0 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*3 passed*"]) def test_does_not_raise(self, testdir): testdir.makepyfile( """ from contextlib import contextmanager import pytest @contextmanager def does_not_raise(): yield @pytest.mark.parametrize('example_input,expectation', [ (3, does_not_raise()), (2, does_not_raise()), (1, does_not_raise()), (0, pytest.raises(ZeroDivisionError)), ]) def test_division(example_input, expectation): '''Test how much I know division.''' with expectation: assert (6 / example_input) is not None """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*4 passed*"]) def test_does_not_raise_does_raise(self, testdir): testdir.makepyfile( """ from contextlib import contextmanager import pytest @contextmanager def does_not_raise(): yield @pytest.mark.parametrize('example_input,expectation', [ (0, does_not_raise()), (1, pytest.raises(ZeroDivisionError)), ]) def test_division(example_input, expectation): '''Test how much I know division.''' with expectation: assert (6 / example_input) is not None """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*2 failed*"]) def test_noclass(self): with pytest.raises(TypeError): pytest.raises("wrong", lambda: None) def test_invalid_arguments_to_raises(self): with pytest.raises(TypeError, match="unknown"): with pytest.raises(TypeError, unknown="bogus"): raise ValueError() def test_tuple(self): with pytest.raises((KeyError, ValueError)): raise KeyError("oops") def test_no_raise_message(self): try: pytest.raises(ValueError, int, "0") except pytest.raises.Exception as e: assert e.msg == "DID NOT RAISE {}".format(repr(ValueError)) else: assert False, "Expected pytest.raises.Exception" try: with pytest.raises(ValueError): pass except pytest.raises.Exception as e: assert e.msg == "DID NOT RAISE {}".format(repr(ValueError)) else: assert False, "Expected pytest.raises.Exception" def test_custom_raise_message(self): message = "TEST_MESSAGE" try: with pytest.warns(PytestDeprecationWarning): with pytest.raises(ValueError, message=message): pass except pytest.raises.Exception as e: assert e.msg == message else: assert False, "Expected pytest.raises.Exception" @pytest.mark.parametrize("method", ["function", "with"]) def test_raises_cyclic_reference(self, method): """ Ensure pytest.raises does not leave a reference cycle (#1965). """ import gc class T(object): def __call__(self): raise ValueError t = T() if method == "function": pytest.raises(ValueError, t) else: with pytest.raises(ValueError): t() # ensure both forms of pytest.raises don't leave exceptions in sys.exc_info() assert sys.exc_info() == (None, None, None) del t # ensure the t instance is not stuck in a cyclic reference for o in gc.get_objects(): assert type(o) is not T def test_raises_match(self): msg = r"with base \d+" with pytest.raises(ValueError, match=msg): int("asdf") msg = "with base 10" with pytest.raises(ValueError, match=msg): int("asdf") msg = "with base 16" expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format( msg ) with pytest.raises(AssertionError, match=expr): with pytest.raises(ValueError, match=msg): int("asdf", base=10) def test_raises_match_wrong_type(self): """Raising an exception with the wrong type and match= given. pytest should throw the unexpected exception - the pattern match is not really relevant if we got a different exception. """ with pytest.raises(ValueError): with pytest.raises(IndexError, match="nomatch"): int("asdf") def test_raises_exception_looks_iterable(self): from six import add_metaclass class Meta(type(object)): def __getitem__(self, item): return 1 / 0 def __len__(self): return 1 @add_metaclass(Meta) class ClassLooksIterableException(Exception): pass with pytest.raises( Failed, match=r"DID NOT RAISE ", ): pytest.raises(ClassLooksIterableException, lambda: None) def test_raises_with_raising_dunder_class(self): """Test current behavior with regard to exceptions via __class__ (#4284).""" class CrappyClass(Exception): @property def __class__(self): assert False, "via __class__" if six.PY2: with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.raises(CrappyClass()): pass assert "DID NOT RAISE" in excinfo.value.args[0] with pytest.raises(CrappyClass) as excinfo: raise CrappyClass() else: with pytest.raises(AssertionError) as excinfo: with pytest.raises(CrappyClass()): pass assert "via __class__" in excinfo.value.args[0] class TestUnicodeHandling: """Test various combinations of bytes and unicode with pytest.raises (#5478) https://github.com/pytest-dev/pytest/pull/5479#discussion_r298852433 """ success = dummy_context_manager py2_only = pytest.mark.skipif( not six.PY2, reason="bytes in raises only supported in Python 2" ) @pytest.mark.parametrize( "message, match, expectation", [ (u"\u2603", u"\u2603", success()), (u"\u2603", u"\u2603foo", pytest.raises(AssertionError)), pytest.param(b"hello", b"hello", success(), marks=py2_only), pytest.param( b"hello", b"world", pytest.raises(AssertionError), marks=py2_only ), pytest.param(u"hello", b"hello", success(), marks=py2_only), pytest.param( u"hello", b"world", pytest.raises(AssertionError), marks=py2_only ), pytest.param( u"😊".encode("UTF-8"), b"world", pytest.raises(AssertionError), marks=py2_only, ), pytest.param( u"world", u"😊".encode("UTF-8"), pytest.raises(AssertionError), marks=py2_only, ), ], ) def test_handling(self, message, match, expectation): with expectation: with pytest.raises(RuntimeError, match=match): raise RuntimeError(message) pytest-4.6.9/testing/test_reports.py0000664000372000037200000002757013604173400020521 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import pytest from _pytest.pathlib import Path from _pytest.reports import CollectReport from _pytest.reports import TestReport class TestReportSerialization(object): def test_xdist_longrepr_to_str_issue_241(self, testdir): """ Regarding issue pytest-xdist#241 This test came originally from test_remote.py in xdist (ca03269). """ testdir.makepyfile( """ def test_a(): assert False def test_b(): pass """ ) reprec = testdir.inline_run() reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 6 test_a_call = reports[1] assert test_a_call.when == "call" assert test_a_call.outcome == "failed" assert test_a_call._to_json()["longrepr"]["reprtraceback"]["style"] == "long" test_b_call = reports[4] assert test_b_call.when == "call" assert test_b_call.outcome == "passed" assert test_b_call._to_json()["longrepr"] is None def test_xdist_report_longrepr_reprcrash_130(self, testdir): """Regarding issue pytest-xdist#130 This test came originally from test_remote.py in xdist (ca03269). """ reprec = testdir.inline_runsource( """ def test_fail(): assert False, 'Expected Message' """ ) reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 rep = reports[1] added_section = ("Failure Metadata", str("metadata metadata"), "*") rep.longrepr.sections.append(added_section) d = rep._to_json() a = TestReport._from_json(d) # Check assembled == rep assert a.__dict__.keys() == rep.__dict__.keys() for key in rep.__dict__.keys(): if key != "longrepr": assert getattr(a, key) == getattr(rep, key) assert rep.longrepr.reprcrash.lineno == a.longrepr.reprcrash.lineno assert rep.longrepr.reprcrash.message == a.longrepr.reprcrash.message assert rep.longrepr.reprcrash.path == a.longrepr.reprcrash.path assert rep.longrepr.reprtraceback.entrysep == a.longrepr.reprtraceback.entrysep assert ( rep.longrepr.reprtraceback.extraline == a.longrepr.reprtraceback.extraline ) assert rep.longrepr.reprtraceback.style == a.longrepr.reprtraceback.style assert rep.longrepr.sections == a.longrepr.sections # Missing section attribute PR171 assert added_section in a.longrepr.sections def test_reprentries_serialization_170(self, testdir): """Regarding issue pytest-xdist#170 This test came originally from test_remote.py in xdist (ca03269). """ from _pytest._code.code import ReprEntry reprec = testdir.inline_runsource( """ def test_repr_entry(): x = 0 assert x """, "--showlocals", ) reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 rep = reports[1] d = rep._to_json() a = TestReport._from_json(d) rep_entries = rep.longrepr.reprtraceback.reprentries a_entries = a.longrepr.reprtraceback.reprentries for i in range(len(a_entries)): assert isinstance(rep_entries[i], ReprEntry) assert rep_entries[i].lines == a_entries[i].lines assert rep_entries[i].reprfileloc.lineno == a_entries[i].reprfileloc.lineno assert ( rep_entries[i].reprfileloc.message == a_entries[i].reprfileloc.message ) assert rep_entries[i].reprfileloc.path == a_entries[i].reprfileloc.path assert rep_entries[i].reprfuncargs.args == a_entries[i].reprfuncargs.args assert rep_entries[i].reprlocals.lines == a_entries[i].reprlocals.lines assert rep_entries[i].style == a_entries[i].style def test_reprentries_serialization_196(self, testdir): """Regarding issue pytest-xdist#196 This test came originally from test_remote.py in xdist (ca03269). """ from _pytest._code.code import ReprEntryNative reprec = testdir.inline_runsource( """ def test_repr_entry_native(): x = 0 assert x """, "--tb=native", ) reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 rep = reports[1] d = rep._to_json() a = TestReport._from_json(d) rep_entries = rep.longrepr.reprtraceback.reprentries a_entries = a.longrepr.reprtraceback.reprentries for i in range(len(a_entries)): assert isinstance(rep_entries[i], ReprEntryNative) assert rep_entries[i].lines == a_entries[i].lines def test_itemreport_outcomes(self, testdir): """ This test came originally from test_remote.py in xdist (ca03269). """ reprec = testdir.inline_runsource( """ import py def test_pass(): pass def test_fail(): 0/0 @py.test.mark.skipif("True") def test_skip(): pass def test_skip_imperative(): py.test.skip("hello") @py.test.mark.xfail("True") def test_xfail(): 0/0 def test_xfail_imperative(): py.test.xfail("hello") """ ) reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 17 # with setup/teardown "passed" reports for rep in reports: d = rep._to_json() newrep = TestReport._from_json(d) assert newrep.passed == rep.passed assert newrep.failed == rep.failed assert newrep.skipped == rep.skipped if newrep.skipped and not hasattr(newrep, "wasxfail"): assert len(newrep.longrepr) == 3 assert newrep.outcome == rep.outcome assert newrep.when == rep.when assert newrep.keywords == rep.keywords if rep.failed: assert newrep.longreprtext == rep.longreprtext def test_collectreport_passed(self, testdir): """This test came originally from test_remote.py in xdist (ca03269).""" reprec = testdir.inline_runsource("def test_func(): pass") reports = reprec.getreports("pytest_collectreport") for rep in reports: d = rep._to_json() newrep = CollectReport._from_json(d) assert newrep.passed == rep.passed assert newrep.failed == rep.failed assert newrep.skipped == rep.skipped def test_collectreport_fail(self, testdir): """This test came originally from test_remote.py in xdist (ca03269).""" reprec = testdir.inline_runsource("qwe abc") reports = reprec.getreports("pytest_collectreport") assert reports for rep in reports: d = rep._to_json() newrep = CollectReport._from_json(d) assert newrep.passed == rep.passed assert newrep.failed == rep.failed assert newrep.skipped == rep.skipped if rep.failed: assert newrep.longrepr == str(rep.longrepr) def test_extended_report_deserialization(self, testdir): """This test came originally from test_remote.py in xdist (ca03269).""" reprec = testdir.inline_runsource("qwe abc") reports = reprec.getreports("pytest_collectreport") assert reports for rep in reports: rep.extra = True d = rep._to_json() newrep = CollectReport._from_json(d) assert newrep.extra assert newrep.passed == rep.passed assert newrep.failed == rep.failed assert newrep.skipped == rep.skipped if rep.failed: assert newrep.longrepr == str(rep.longrepr) def test_paths_support(self, testdir): """Report attributes which are py.path or pathlib objects should become strings.""" testdir.makepyfile( """ def test_a(): assert False """ ) reprec = testdir.inline_run() reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 test_a_call = reports[1] test_a_call.path1 = testdir.tmpdir test_a_call.path2 = Path(testdir.tmpdir) data = test_a_call._to_json() assert data["path1"] == str(testdir.tmpdir) assert data["path2"] == str(testdir.tmpdir) def test_unserialization_failure(self, testdir): """Check handling of failure during unserialization of report types.""" testdir.makepyfile( """ def test_a(): assert False """ ) reprec = testdir.inline_run() reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3 test_a_call = reports[1] data = test_a_call._to_json() entry = data["longrepr"]["reprtraceback"]["reprentries"][0] assert entry["type"] == "ReprEntry" entry["type"] = "Unknown" with pytest.raises( RuntimeError, match="INTERNALERROR: Unknown entry type returned: Unknown" ): TestReport._from_json(data) class TestHooks: """Test that the hooks are working correctly for plugins""" def test_test_report(self, testdir, pytestconfig): testdir.makepyfile( """ def test_a(): assert False def test_b(): pass """ ) reprec = testdir.inline_run() reports = reprec.getreports("pytest_runtest_logreport") assert len(reports) == 6 for rep in reports: data = pytestconfig.hook.pytest_report_to_serializable( config=pytestconfig, report=rep ) assert data["_report_type"] == "TestReport" new_rep = pytestconfig.hook.pytest_report_from_serializable( config=pytestconfig, data=data ) assert new_rep.nodeid == rep.nodeid assert new_rep.when == rep.when assert new_rep.outcome == rep.outcome def test_collect_report(self, testdir, pytestconfig): testdir.makepyfile( """ def test_a(): assert False def test_b(): pass """ ) reprec = testdir.inline_run() reports = reprec.getreports("pytest_collectreport") assert len(reports) == 2 for rep in reports: data = pytestconfig.hook.pytest_report_to_serializable( config=pytestconfig, report=rep ) assert data["_report_type"] == "CollectReport" new_rep = pytestconfig.hook.pytest_report_from_serializable( config=pytestconfig, data=data ) assert new_rep.nodeid == rep.nodeid assert new_rep.when == "collect" assert new_rep.outcome == rep.outcome @pytest.mark.parametrize( "hook_name", ["pytest_runtest_logreport", "pytest_collectreport"] ) def test_invalid_report_types(self, testdir, pytestconfig, hook_name): testdir.makepyfile( """ def test_a(): pass """ ) reprec = testdir.inline_run() reports = reprec.getreports(hook_name) assert reports rep = reports[0] data = pytestconfig.hook.pytest_report_to_serializable( config=pytestconfig, report=rep ) data["_report_type"] = "Unknown" with pytest.raises(AssertionError): _ = pytestconfig.hook.pytest_report_from_serializable( config=pytestconfig, data=data ) pytest-4.6.9/testing/test_nodes.py0000664000372000037200000000257013604173400020124 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import py import pytest from _pytest import nodes @pytest.mark.parametrize( "baseid, nodeid, expected", ( ("", "", True), ("", "foo", True), ("", "foo/bar", True), ("", "foo/bar::TestBaz", True), ("foo", "food", False), ("foo/bar::TestBaz", "foo/bar", False), ("foo/bar::TestBaz", "foo/bar::TestBop", False), ("foo/bar", "foo/bar::TestBop", True), ), ) def test_ischildnode(baseid, nodeid, expected): result = nodes.ischildnode(baseid, nodeid) assert result is expected def test_std_warn_not_pytestwarning(testdir): items = testdir.getitems( """ def test(): pass """ ) with pytest.raises(ValueError, match=".*instance of PytestWarning.*"): items[0].warn(UserWarning("some warning")) def test__check_initialpaths_for_relpath(): """Ensure that it handles dirs, and does not always use dirname.""" cwd = py.path.local() class FakeSession: _initialpaths = [cwd] assert nodes._check_initialpaths_for_relpath(FakeSession, cwd) == "" sub = cwd.join("file") class FakeSession: _initialpaths = [cwd] assert nodes._check_initialpaths_for_relpath(FakeSession, sub) == "file" outside = py.path.local("/outside") assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None pytest-4.6.9/testing/test_pdb.py0000664000372000037200000011403613604173400017562 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys import six import _pytest._code import pytest from _pytest.debugging import _validate_usepdb_cls try: breakpoint except NameError: SUPPORTS_BREAKPOINT_BUILTIN = False else: SUPPORTS_BREAKPOINT_BUILTIN = True _ENVIRON_PYTHONBREAKPOINT = os.environ.get("PYTHONBREAKPOINT", "") def runpdb_and_get_report(testdir, source): p = testdir.makepyfile(source) result = testdir.runpytest_inprocess("--pdb", p) reports = result.reprec.getreports("pytest_runtest_logreport") assert len(reports) == 3, reports # setup/call/teardown return reports[1] @pytest.fixture def custom_pdb_calls(): called = [] # install dummy debugger class and track which methods were called on it class _CustomPdb(object): quitting = False def __init__(self, *args, **kwargs): called.append("init") def reset(self): called.append("reset") def interaction(self, *args): called.append("interaction") _pytest._CustomPdb = _CustomPdb return called @pytest.fixture def custom_debugger_hook(): called = [] # install dummy debugger class and track which methods were called on it class _CustomDebugger(object): def __init__(self, *args, **kwargs): called.append("init") def reset(self): called.append("reset") def interaction(self, *args): called.append("interaction") def set_trace(self, frame): print("**CustomDebugger**") called.append("set_trace") _pytest._CustomDebugger = _CustomDebugger yield called del _pytest._CustomDebugger class TestPDB(object): @pytest.fixture def pdblist(self, request): monkeypatch = request.getfixturevalue("monkeypatch") pdblist = [] def mypdb(*args): pdblist.append(args) plugin = request.config.pluginmanager.getplugin("debugging") monkeypatch.setattr(plugin, "post_mortem", mypdb) return pdblist def test_pdb_on_fail(self, testdir, pdblist): rep = runpdb_and_get_report( testdir, """ def test_func(): assert 0 """, ) assert rep.failed assert len(pdblist) == 1 tb = _pytest._code.Traceback(pdblist[0][0]) assert tb[-1].name == "test_func" def test_pdb_on_xfail(self, testdir, pdblist): rep = runpdb_and_get_report( testdir, """ import pytest @pytest.mark.xfail def test_func(): assert 0 """, ) assert "xfail" in rep.keywords assert not pdblist def test_pdb_on_skip(self, testdir, pdblist): rep = runpdb_and_get_report( testdir, """ import pytest def test_func(): pytest.skip("hello") """, ) assert rep.skipped assert len(pdblist) == 0 def test_pdb_on_BdbQuit(self, testdir, pdblist): rep = runpdb_and_get_report( testdir, """ import bdb def test_func(): raise bdb.BdbQuit """, ) assert rep.failed assert len(pdblist) == 0 def test_pdb_on_KeyboardInterrupt(self, testdir, pdblist): rep = runpdb_and_get_report( testdir, """ def test_func(): raise KeyboardInterrupt """, ) assert rep.failed assert len(pdblist) == 1 @staticmethod def flush(child): if child.isalive(): # Read if the test has not (e.g. test_pdb_unittest_skip). child.read() child.wait() assert not child.isalive() def test_pdb_unittest_postmortem(self, testdir): p1 = testdir.makepyfile( """ import unittest class Blub(unittest.TestCase): def tearDown(self): self.filename = None def test_false(self): self.filename = 'debug' + '.me' assert 0 """ ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect("Pdb") child.sendline("p self.filename") child.sendeof() rest = child.read().decode("utf8") assert "debug.me" in rest self.flush(child) def test_pdb_unittest_skip(self, testdir): """Test for issue #2137""" p1 = testdir.makepyfile( """ import unittest @unittest.skipIf(True, 'Skipping also with pdb active') class MyTestCase(unittest.TestCase): def test_one(self): assert 0 """ ) child = testdir.spawn_pytest("-rs --pdb %s" % p1) child.expect("Skipping also with pdb active") child.expect("1 skipped in") child.sendeof() self.flush(child) def test_pdb_print_captured_stdout_and_stderr(self, testdir): p1 = testdir.makepyfile( """ def test_1(): import sys sys.stderr.write("get\\x20rekt") print("get\\x20rekt") assert False def test_not_called_due_to_quit(): pass """ ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect("captured stdout") child.expect("get rekt") child.expect("captured stderr") child.expect("get rekt") child.expect("traceback") child.expect("def test_1") child.expect("Pdb") child.sendeof() rest = child.read().decode("utf8") assert "Exit: Quitting debugger" in rest assert "= 1 failed in" in rest assert "def test_1" not in rest assert "get rekt" not in rest self.flush(child) def test_pdb_dont_print_empty_captured_stdout_and_stderr(self, testdir): p1 = testdir.makepyfile( """ def test_1(): assert False """ ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect("Pdb") output = child.before.decode("utf8") child.sendeof() assert "captured stdout" not in output assert "captured stderr" not in output self.flush(child) @pytest.mark.parametrize("showcapture", ["all", "no", "log"]) def test_pdb_print_captured_logs(self, testdir, showcapture): p1 = testdir.makepyfile( """ def test_1(): import logging logging.warn("get " + "rekt") assert False """ ) child = testdir.spawn_pytest( "--show-capture={} --pdb {}".format(showcapture, p1) ) if showcapture in ("all", "log"): child.expect("captured log") child.expect("get rekt") child.expect("Pdb") child.sendeof() rest = child.read().decode("utf8") assert "1 failed" in rest self.flush(child) def test_pdb_print_captured_logs_nologging(self, testdir): p1 = testdir.makepyfile( """ def test_1(): import logging logging.warn("get " + "rekt") assert False """ ) child = testdir.spawn_pytest("--show-capture=all --pdb -p no:logging %s" % p1) child.expect("get rekt") output = child.before.decode("utf8") assert "captured log" not in output child.expect("Pdb") child.sendeof() rest = child.read().decode("utf8") assert "1 failed" in rest self.flush(child) def test_pdb_interaction_exception(self, testdir): p1 = testdir.makepyfile( """ import pytest def globalfunc(): pass def test_1(): pytest.raises(ValueError, globalfunc) """ ) child = testdir.spawn_pytest("--pdb %s" % p1) child.expect(".*def test_1") child.expect(".*pytest.raises.*globalfunc") child.expect("Pdb") child.sendline("globalfunc") child.expect(".*function") child.sendeof() child.expect("1 failed") self.flush(child) def test_pdb_interaction_on_collection_issue181(self, testdir): p1 = testdir.makepyfile( """ import pytest xxx """ ) child = testdir.spawn_pytest("--pdb %s" % p1) # child.expect(".*import pytest.*") child.expect("Pdb") child.sendline("c") child.expect("1 error") self.flush(child) def test_pdb_interaction_on_internal_error(self, testdir): testdir.makeconftest( """ def pytest_runtest_protocol(): 0/0 """ ) p1 = testdir.makepyfile("def test_func(): pass") child = testdir.spawn_pytest("--pdb %s" % p1) child.expect("Pdb") # INTERNALERROR is only displayed once via terminal reporter. assert ( len( [ x for x in child.before.decode().splitlines() if x.startswith("INTERNALERROR> Traceback") ] ) == 1 ) child.sendeof() self.flush(child) def test_pdb_interaction_capturing_simple(self, testdir): p1 = testdir.makepyfile( """ import pytest def test_1(): i = 0 print("hello17") pytest.set_trace() i == 1 assert 0 """ ) child = testdir.spawn_pytest(str(p1)) child.expect(r"test_1\(\)") child.expect("i == 1") child.expect("Pdb") child.sendline("c") rest = child.read().decode("utf-8") assert "AssertionError" in rest assert "1 failed" in rest assert "def test_1" in rest assert "hello17" in rest # out is captured self.flush(child) def test_pdb_set_trace_kwargs(self, testdir): p1 = testdir.makepyfile( """ import pytest def test_1(): i = 0 print("hello17") pytest.set_trace(header="== my_header ==") x = 3 assert 0 """ ) child = testdir.spawn_pytest(str(p1)) child.expect("== my_header ==") assert "PDB set_trace" not in child.before.decode() child.expect("Pdb") child.sendline("c") rest = child.read().decode("utf-8") assert "1 failed" in rest assert "def test_1" in rest assert "hello17" in rest # out is captured self.flush(child) def test_pdb_set_trace_interception(self, testdir): p1 = testdir.makepyfile( """ import pdb def test_1(): pdb.set_trace() """ ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("Pdb") child.sendline("q") rest = child.read().decode("utf8") assert "no tests ran" in rest assert "reading from stdin while output" not in rest assert "BdbQuit" not in rest self.flush(child) def test_pdb_and_capsys(self, testdir): p1 = testdir.makepyfile( """ import pytest def test_1(capsys): print("hello1") pytest.set_trace() """ ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.send("capsys.readouterr()\n") child.expect("hello1") child.sendeof() child.read() self.flush(child) def test_pdb_with_caplog_on_pdb_invocation(self, testdir): p1 = testdir.makepyfile( """ def test_1(capsys, caplog): import logging logging.getLogger(__name__).warning("some_warning") assert 0 """ ) child = testdir.spawn_pytest("--pdb %s" % str(p1)) child.send("caplog.record_tuples\n") child.expect_exact( "[('test_pdb_with_caplog_on_pdb_invocation', 30, 'some_warning')]" ) child.sendeof() child.read() self.flush(child) def test_set_trace_capturing_afterwards(self, testdir): p1 = testdir.makepyfile( """ import pdb def test_1(): pdb.set_trace() def test_2(): print("hello") assert 0 """ ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.send("c\n") child.expect("test_2") child.expect("Captured") child.expect("hello") child.sendeof() child.read() self.flush(child) def test_pdb_interaction_doctest(self, testdir, monkeypatch): p1 = testdir.makepyfile( """ import pytest def function_1(): ''' >>> i = 0 >>> assert i == 1 ''' """ ) child = testdir.spawn_pytest("--doctest-modules --pdb %s" % p1) child.expect("Pdb") assert "UNEXPECTED EXCEPTION: AssertionError()" in child.before.decode("utf8") child.sendline("'i=%i.' % i") child.expect("Pdb") assert "\r\n'i=0.'\r\n" in child.before.decode("utf8") child.sendeof() rest = child.read().decode("utf8") assert "1 failed" in rest self.flush(child) def test_pdb_interaction_capturing_twice(self, testdir): p1 = testdir.makepyfile( """ import pytest def test_1(): i = 0 print("hello17") pytest.set_trace() x = 3 print("hello18") pytest.set_trace() x = 4 assert 0 """ ) child = testdir.spawn_pytest(str(p1)) child.expect(r"PDB set_trace \(IO-capturing turned off\)") child.expect("test_1") child.expect("x = 3") child.expect("Pdb") child.sendline("c") child.expect(r"PDB continue \(IO-capturing resumed\)") child.expect(r"PDB set_trace \(IO-capturing turned off\)") child.expect("x = 4") child.expect("Pdb") child.sendline("c") child.expect("_ test_1 _") child.expect("def test_1") rest = child.read().decode("utf8") assert "Captured stdout call" in rest assert "hello17" in rest # out is captured assert "hello18" in rest # out is captured assert "1 failed" in rest self.flush(child) def test_pdb_with_injected_do_debug(self, testdir): """Simulates pdbpp, which injects Pdb into do_debug, and uses self.__class__ in do_continue. """ p1 = testdir.makepyfile( mytest=""" import pdb import pytest count_continue = 0 class CustomPdb(pdb.Pdb, object): def do_debug(self, arg): import sys import types if sys.version_info < (3, ): do_debug_func = pdb.Pdb.do_debug.im_func else: do_debug_func = pdb.Pdb.do_debug newglobals = do_debug_func.__globals__.copy() newglobals['Pdb'] = self.__class__ orig_do_debug = types.FunctionType( do_debug_func.__code__, newglobals, do_debug_func.__name__, do_debug_func.__defaults__, ) return orig_do_debug(self, arg) do_debug.__doc__ = pdb.Pdb.do_debug.__doc__ def do_continue(self, *args, **kwargs): global count_continue count_continue += 1 return super(CustomPdb, self).do_continue(*args, **kwargs) def foo(): print("print_from_foo") def test_1(): i = 0 print("hello17") pytest.set_trace() x = 3 print("hello18") assert count_continue == 2, "unexpected_failure: %d != 2" % count_continue pytest.fail("expected_failure") """ ) child = testdir.spawn_pytest("--pdbcls=mytest:CustomPdb %s" % str(p1)) child.expect(r"PDB set_trace \(IO-capturing turned off\)") child.expect(r"\n\(Pdb") child.sendline("debug foo()") child.expect("ENTERING RECURSIVE DEBUGGER") child.expect(r"\n\(\(Pdb") child.sendline("c") child.expect("LEAVING RECURSIVE DEBUGGER") assert b"PDB continue" not in child.before # No extra newline. assert child.before.endswith(b"c\r\nprint_from_foo\r\n") # set_debug should not raise outcomes.Exit, if used recrursively. child.sendline("debug 42") child.sendline("q") child.expect("LEAVING RECURSIVE DEBUGGER") assert b"ENTERING RECURSIVE DEBUGGER" in child.before assert b"Quitting debugger" not in child.before child.sendline("c") child.expect(r"PDB continue \(IO-capturing resumed\)") rest = child.read().decode("utf8") assert "hello17" in rest # out is captured assert "hello18" in rest # out is captured assert "1 failed" in rest assert "Failed: expected_failure" in rest assert "AssertionError: unexpected_failure" not in rest self.flush(child) def test_pdb_without_capture(self, testdir): p1 = testdir.makepyfile( """ import pytest def test_1(): pytest.set_trace() """ ) child = testdir.spawn_pytest("-s %s" % p1) child.expect(r">>> PDB set_trace >>>") child.expect("Pdb") child.sendline("c") child.expect(r">>> PDB continue >>>") child.expect("1 passed") self.flush(child) @pytest.mark.parametrize("capture_arg", ("", "-s", "-p no:capture")) def test_pdb_continue_with_recursive_debug(self, capture_arg, testdir): """Full coverage for do_debug without capturing. This is very similar to test_pdb_interaction_continue_recursive in general, but mocks out ``pdb.set_trace`` for providing more coverage. """ p1 = testdir.makepyfile( """ try: input = raw_input except NameError: pass def set_trace(): __import__('pdb').set_trace() def test_1(monkeypatch): import _pytest.debugging class pytestPDBTest(_pytest.debugging.pytestPDB): @classmethod def set_trace(cls, *args, **kwargs): # Init PytestPdbWrapper to handle capturing. _pdb = cls._init_pdb("set_trace", *args, **kwargs) # Mock out pdb.Pdb.do_continue. import pdb pdb.Pdb.do_continue = lambda self, arg: None print("===" + " SET_TRACE ===") assert input() == "debug set_trace()" # Simulate PytestPdbWrapper.do_debug cls._recursive_debug += 1 print("ENTERING RECURSIVE DEBUGGER") print("===" + " SET_TRACE_2 ===") assert input() == "c" _pdb.do_continue("") print("===" + " SET_TRACE_3 ===") # Simulate PytestPdbWrapper.do_debug print("LEAVING RECURSIVE DEBUGGER") cls._recursive_debug -= 1 print("===" + " SET_TRACE_4 ===") assert input() == "c" _pdb.do_continue("") def do_continue(self, arg): print("=== do_continue") monkeypatch.setattr(_pytest.debugging, "pytestPDB", pytestPDBTest) import pdb monkeypatch.setattr(pdb, "set_trace", pytestPDBTest.set_trace) set_trace() """ ) child = testdir.spawn_pytest("--tb=short %s %s" % (p1, capture_arg)) child.expect("=== SET_TRACE ===") before = child.before.decode("utf8") if not capture_arg: assert ">>> PDB set_trace (IO-capturing turned off) >>>" in before else: assert ">>> PDB set_trace >>>" in before child.sendline("debug set_trace()") child.expect("=== SET_TRACE_2 ===") before = child.before.decode("utf8") assert "\r\nENTERING RECURSIVE DEBUGGER\r\n" in before child.sendline("c") child.expect("=== SET_TRACE_3 ===") # No continue message with recursive debugging. before = child.before.decode("utf8") assert ">>> PDB continue " not in before child.sendline("c") child.expect("=== SET_TRACE_4 ===") before = child.before.decode("utf8") assert "\r\nLEAVING RECURSIVE DEBUGGER\r\n" in before child.sendline("c") rest = child.read().decode("utf8") if not capture_arg: assert "> PDB continue (IO-capturing resumed) >" in rest else: assert "> PDB continue >" in rest assert "1 passed in" in rest def test_pdb_used_outside_test(self, testdir): p1 = testdir.makepyfile( """ import pytest pytest.set_trace() x = 5 """ ) child = testdir.spawn("{} {}".format(sys.executable, p1)) child.expect("x = 5") child.expect("Pdb") child.sendeof() self.flush(child) def test_pdb_used_in_generate_tests(self, testdir): p1 = testdir.makepyfile( """ import pytest def pytest_generate_tests(metafunc): pytest.set_trace() x = 5 def test_foo(a): pass """ ) child = testdir.spawn_pytest(str(p1)) child.expect("x = 5") child.expect("Pdb") child.sendeof() self.flush(child) def test_pdb_collection_failure_is_shown(self, testdir): p1 = testdir.makepyfile("xxx") result = testdir.runpytest_subprocess("--pdb", p1) result.stdout.fnmatch_lines( ["E NameError: *xxx*", "*! *Exit: Quitting debugger !*"] # due to EOF ) @pytest.mark.parametrize("post_mortem", (False, True)) def test_enter_leave_pdb_hooks_are_called(self, post_mortem, testdir): testdir.makeconftest( """ mypdb = None def pytest_configure(config): config.testing_verification = 'configured' def pytest_enter_pdb(config, pdb): assert config.testing_verification == 'configured' print('enter_pdb_hook') global mypdb mypdb = pdb mypdb.set_attribute = "bar" def pytest_leave_pdb(config, pdb): assert config.testing_verification == 'configured' print('leave_pdb_hook') global mypdb assert mypdb is pdb assert mypdb.set_attribute == "bar" """ ) p1 = testdir.makepyfile( """ import pytest def test_set_trace(): pytest.set_trace() assert 0 def test_post_mortem(): assert 0 """ ) if post_mortem: child = testdir.spawn_pytest(str(p1) + " --pdb -s -k test_post_mortem") else: child = testdir.spawn_pytest(str(p1) + " -k test_set_trace") child.expect("enter_pdb_hook") child.sendline("c") if post_mortem: child.expect(r"PDB continue") else: child.expect(r"PDB continue \(IO-capturing resumed\)") child.expect("Captured stdout call") rest = child.read().decode("utf8") assert "leave_pdb_hook" in rest assert "1 failed" in rest self.flush(child) def test_pdb_custom_cls(self, testdir, custom_pdb_calls): p1 = testdir.makepyfile("""xxx """) result = testdir.runpytest_inprocess("--pdb", "--pdbcls=_pytest:_CustomPdb", p1) result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"]) assert custom_pdb_calls == ["init", "reset", "interaction"] def test_pdb_custom_cls_invalid(self, testdir): result = testdir.runpytest_inprocess("--pdbcls=invalid") result.stderr.fnmatch_lines( [ "*: error: argument --pdbcls: 'invalid' is not in the format 'modname:classname'" ] ) def test_pdb_validate_usepdb_cls(self, testdir): assert _validate_usepdb_cls("os.path:dirname.__name__") == ( "os.path", "dirname.__name__", ) assert _validate_usepdb_cls("pdb:DoesNotExist") == ("pdb", "DoesNotExist") def test_pdb_custom_cls_without_pdb(self, testdir, custom_pdb_calls): p1 = testdir.makepyfile("""xxx """) result = testdir.runpytest_inprocess("--pdbcls=_pytest:_CustomPdb", p1) result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"]) assert custom_pdb_calls == [] def test_pdb_custom_cls_with_set_trace(self, testdir, monkeypatch): testdir.makepyfile( custom_pdb=""" class CustomPdb(object): def __init__(self, *args, **kwargs): skip = kwargs.pop("skip") assert skip == ["foo.*"] print("__init__") super(CustomPdb, self).__init__(*args, **kwargs) def set_trace(*args, **kwargs): print('custom set_trace>') """ ) p1 = testdir.makepyfile( """ import pytest def test_foo(): pytest.set_trace(skip=['foo.*']) """ ) monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir)) child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1)) child.expect("__init__") child.expect("custom set_trace>") self.flush(child) class TestDebuggingBreakpoints(object): def test_supports_breakpoint_module_global(self): """ Test that supports breakpoint global marks on Python 3.7+ and not on CPython 3.5, 2.7 """ if sys.version_info.major == 3 and sys.version_info.minor >= 7: assert SUPPORTS_BREAKPOINT_BUILTIN is True if sys.version_info.major == 3 and sys.version_info.minor == 5: assert SUPPORTS_BREAKPOINT_BUILTIN is False if sys.version_info.major == 2 and sys.version_info.minor == 7: assert SUPPORTS_BREAKPOINT_BUILTIN is False @pytest.mark.skipif( not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" ) @pytest.mark.parametrize("arg", ["--pdb", ""]) def test_sys_breakpointhook_configure_and_unconfigure(self, testdir, arg): """ Test that sys.breakpointhook is set to the custom Pdb class once configured, test that hook is reset to system value once pytest has been unconfigured """ testdir.makeconftest( """ import sys from pytest import hookimpl from _pytest.debugging import pytestPDB def pytest_configure(config): config._cleanup.append(check_restored) def check_restored(): assert sys.breakpointhook == sys.__breakpointhook__ def test_check(): assert sys.breakpointhook == pytestPDB.set_trace """ ) testdir.makepyfile( """ def test_nothing(): pass """ ) args = (arg,) if arg else () result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines(["*1 passed in *"]) @pytest.mark.skipif( not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" ) def test_pdb_custom_cls(self, testdir, custom_debugger_hook): p1 = testdir.makepyfile( """ def test_nothing(): breakpoint() """ ) result = testdir.runpytest_inprocess( "--pdb", "--pdbcls=_pytest:_CustomDebugger", p1 ) result.stdout.fnmatch_lines(["*CustomDebugger*", "*1 passed*"]) assert custom_debugger_hook == ["init", "set_trace"] @pytest.mark.parametrize("arg", ["--pdb", ""]) @pytest.mark.skipif( not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" ) def test_environ_custom_class(self, testdir, custom_debugger_hook, arg): testdir.makeconftest( """ import os import sys os.environ['PYTHONBREAKPOINT'] = '_pytest._CustomDebugger.set_trace' def pytest_configure(config): config._cleanup.append(check_restored) def check_restored(): assert sys.breakpointhook == sys.__breakpointhook__ def test_check(): import _pytest assert sys.breakpointhook is _pytest._CustomDebugger.set_trace """ ) testdir.makepyfile( """ def test_nothing(): pass """ ) args = (arg,) if arg else () result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines(["*1 passed in *"]) @pytest.mark.skipif( not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" ) @pytest.mark.skipif( not _ENVIRON_PYTHONBREAKPOINT == "", reason="Requires breakpoint() default value", ) def test_sys_breakpoint_interception(self, testdir): p1 = testdir.makepyfile( """ def test_1(): breakpoint() """ ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("Pdb") child.sendline("quit") rest = child.read().decode("utf8") assert "Quitting debugger" in rest assert "reading from stdin while output" not in rest TestPDB.flush(child) @pytest.mark.skipif( not SUPPORTS_BREAKPOINT_BUILTIN, reason="Requires breakpoint() builtin" ) def test_pdb_not_altered(self, testdir): p1 = testdir.makepyfile( """ import pdb def test_1(): pdb.set_trace() assert 0 """ ) child = testdir.spawn_pytest(str(p1)) child.expect("test_1") child.expect("Pdb") child.sendline("c") rest = child.read().decode("utf8") assert "1 failed" in rest assert "reading from stdin while output" not in rest TestPDB.flush(child) class TestTraceOption: def test_trace_sets_breakpoint(self, testdir): p1 = testdir.makepyfile( """ def test_1(): assert True def test_2(): pass def test_3(): pass """ ) child = testdir.spawn_pytest("--trace " + str(p1)) child.expect("test_1") child.expect("Pdb") child.sendline("c") child.expect("test_2") child.expect("Pdb") child.sendline("c") child.expect("test_3") child.expect("Pdb") child.sendline("q") child.expect_exact("Exit: Quitting debugger") rest = child.read().decode("utf8") assert "2 passed in" in rest assert "reading from stdin while output" not in rest # Only printed once - not on stderr. assert "Exit: Quitting debugger" not in child.before.decode("utf8") TestPDB.flush(child) def test_trace_after_runpytest(testdir): """Test that debugging's pytest_configure is re-entrant.""" p1 = testdir.makepyfile( """ from _pytest.debugging import pytestPDB def test_outer(testdir): assert len(pytestPDB._saved) == 1 testdir.makepyfile( \""" from _pytest.debugging import pytestPDB def test_inner(): assert len(pytestPDB._saved) == 2 print() print("test_inner_" + "end") \""" ) result = testdir.runpytest("-s", "-k", "test_inner") assert result.ret == 0 assert len(pytestPDB._saved) == 1 """ ) result = testdir.runpytest_subprocess("-s", "-p", "pytester", str(p1)) result.stdout.fnmatch_lines(["test_inner_end"]) assert result.ret == 0 def test_quit_with_swallowed_SystemExit(testdir): """Test that debugging's pytest_configure is re-entrant.""" p1 = testdir.makepyfile( """ def call_pdb_set_trace(): __import__('pdb').set_trace() def test_1(): try: call_pdb_set_trace() except SystemExit: pass def test_2(): pass """ ) child = testdir.spawn_pytest(str(p1)) child.expect("Pdb") child.sendline("q") child.expect_exact("Exit: Quitting debugger") rest = child.read().decode("utf8") assert "no tests ran" in rest TestPDB.flush(child) @pytest.mark.parametrize("fixture", ("capfd", "capsys")) def test_pdb_suspends_fixture_capturing(testdir, fixture): """Using "-s" with pytest should suspend/resume fixture capturing.""" p1 = testdir.makepyfile( """ def test_inner({fixture}): import sys print("out_inner_before") sys.stderr.write("err_inner_before\\n") __import__("pdb").set_trace() print("out_inner_after") sys.stderr.write("err_inner_after\\n") out, err = {fixture}.readouterr() assert out =="out_inner_before\\nout_inner_after\\n" assert err =="err_inner_before\\nerr_inner_after\\n" """.format( fixture=fixture ) ) child = testdir.spawn_pytest(str(p1) + " -s") child.expect("Pdb") before = child.before.decode("utf8") assert ( "> PDB set_trace (IO-capturing turned off for fixture %s) >" % (fixture) in before ) # Test that capturing is really suspended. child.sendline("p 40 + 2") child.expect("Pdb") assert "\r\n42\r\n" in child.before.decode("utf8") child.sendline("c") rest = child.read().decode("utf8") assert "out_inner" not in rest assert "err_inner" not in rest TestPDB.flush(child) assert child.exitstatus == 0 assert "= 1 passed in " in rest assert "> PDB continue (IO-capturing resumed for fixture %s) >" % (fixture) in rest def test_pdbcls_via_local_module(testdir): """It should be imported in pytest_configure or later only.""" p1 = testdir.makepyfile( """ def test(): print("before_set_trace") __import__("pdb").set_trace() """, mypdb=""" class Wrapped: class MyPdb: def set_trace(self, *args): print("set_trace_called", args) def runcall(self, *args, **kwds): print("runcall_called", args, kwds) assert "func" in kwds """, ) result = testdir.runpytest( str(p1), "--pdbcls=really.invalid:Value", syspathinsert=True ) result.stdout.fnmatch_lines( [ "*= FAILURES =*", "E * --pdbcls: could not import 'really.invalid:Value': No module named *really*", ] ) assert result.ret == 1 result = testdir.runpytest( str(p1), "--pdbcls=mypdb:Wrapped.MyPdb", syspathinsert=True ) assert result.ret == 0 result.stdout.fnmatch_lines(["*set_trace_called*", "* 1 passed in *"]) # Ensure that it also works with --trace. result = testdir.runpytest( str(p1), "--pdbcls=mypdb:Wrapped.MyPdb", "--trace", syspathinsert=True ) assert result.ret == 0 result.stdout.fnmatch_lines(["*runcall_called*", "* 1 passed in *"]) def test_raises_bdbquit_with_eoferror(testdir): """It is not guaranteed that DontReadFromInput's read is called.""" if six.PY2: builtin_module = "__builtin__" input_func = "raw_input" else: builtin_module = "builtins" input_func = "input" p1 = testdir.makepyfile( """ def input_without_read(*args, **kwargs): raise EOFError() def test(monkeypatch): import {builtin_module} monkeypatch.setattr({builtin_module}, {input_func!r}, input_without_read) __import__('pdb').set_trace() """.format( builtin_module=builtin_module, input_func=input_func ) ) result = testdir.runpytest(str(p1)) result.stdout.fnmatch_lines(["E *BdbQuit", "*= 1 failed in*"]) assert result.ret == 1 def test_pdb_wrapper_class_is_reused(testdir): p1 = testdir.makepyfile( """ def test(): __import__("pdb").set_trace() __import__("pdb").set_trace() import mypdb instances = mypdb.instances assert len(instances) == 2 assert instances[0].__class__ is instances[1].__class__ """, mypdb=""" instances = [] class MyPdb: def __init__(self, *args, **kwargs): instances.append(self) def set_trace(self, *args): print("set_trace_called", args) """, ) result = testdir.runpytest(str(p1), "--pdbcls=mypdb:MyPdb", syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines( ["*set_trace_called*", "*set_trace_called*", "* 1 passed in *"] ) pytest-4.6.9/testing/deprecated_test.py0000664000372000037200000001507013604173400021113 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import pytest from _pytest.warning_types import PytestDeprecationWarning from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG pytestmark = pytest.mark.pytester_example_path("deprecated") def test_pytest_setup_cfg_unsupported(testdir): testdir.makefile( ".cfg", setup=""" [pytest] addopts = --verbose """, ) with pytest.raises(pytest.fail.Exception): testdir.runpytest() def test_pytest_custom_cfg_unsupported(testdir): testdir.makefile( ".cfg", custom=""" [pytest] addopts = --verbose """, ) with pytest.raises(pytest.fail.Exception): testdir.runpytest("-c", "custom.cfg") def test_getfuncargvalue_is_deprecated(request): pytest.deprecated_call(request.getfuncargvalue, "tmpdir") @pytest.mark.filterwarnings("default") def test_resultlog_is_deprecated(testdir): result = testdir.runpytest("--help") result.stdout.fnmatch_lines(["*DEPRECATED path for machine-readable result log*"]) testdir.makepyfile( """ def test(): pass """ ) result = testdir.runpytest("--result-log=%s" % testdir.tmpdir.join("result.log")) result.stdout.fnmatch_lines( [ "*--result-log is deprecated and scheduled for removal in pytest 5.0*", "*See https://docs.pytest.org/en/latest/deprecations.html#result-log-result-log for more information*", ] ) def test_terminal_reporter_writer_attr(pytestconfig): """Check that TerminalReporter._tw is also available as 'writer' (#2984) This attribute is planned to be deprecated in 3.4. """ try: import xdist # noqa pytest.skip("xdist workers disable the terminal reporter plugin") except ImportError: pass terminal_reporter = pytestconfig.pluginmanager.get_plugin("terminalreporter") assert terminal_reporter.writer is terminal_reporter._tw @pytest.mark.parametrize("plugin", ["catchlog", "capturelog"]) @pytest.mark.filterwarnings("default") def test_pytest_catchlog_deprecated(testdir, plugin): testdir.makepyfile( """ def test_func(pytestconfig): pytestconfig.pluginmanager.register(None, 'pytest_{}') """.format( plugin ) ) res = testdir.runpytest() assert res.ret == 0 res.stdout.fnmatch_lines( ["*pytest-*log plugin has been merged into the core*", "*1 passed, 1 warnings*"] ) def test_raises_message_argument_deprecated(): with pytest.warns(pytest.PytestDeprecationWarning): with pytest.raises(RuntimeError, message="foobar"): raise RuntimeError def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir): from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST testdir.makepyfile( **{ "subdirectory/conftest.py": """ pytest_plugins=['capture'] """ } ) testdir.makepyfile( """ def test_func(): pass """ ) res = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG) assert res.ret == 2 msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] res.stdout.fnmatch_lines( ["*{msg}*".format(msg=msg), "*subdirectory{sep}conftest.py*".format(sep=os.sep)] ) @pytest.mark.parametrize("use_pyargs", [True, False]) def test_pytest_plugins_in_non_top_level_conftest_unsupported_pyargs( testdir, use_pyargs ): """When using --pyargs, do not emit the warning about non-top-level conftest warnings (#4039, #4044)""" from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST files = { "src/pkg/__init__.py": "", "src/pkg/conftest.py": "", "src/pkg/test_root.py": "def test(): pass", "src/pkg/sub/__init__.py": "", "src/pkg/sub/conftest.py": "pytest_plugins=['capture']", "src/pkg/sub/test_bar.py": "def test(): pass", } testdir.makepyfile(**files) testdir.syspathinsert(testdir.tmpdir.join("src")) args = ("--pyargs", "pkg") if use_pyargs else () args += (SHOW_PYTEST_WARNINGS_ARG,) res = testdir.runpytest(*args) assert res.ret == (0 if use_pyargs else 2) msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] if use_pyargs: assert msg not in res.stdout.str() else: res.stdout.fnmatch_lines(["*{msg}*".format(msg=msg)]) def test_pytest_plugins_in_non_top_level_conftest_unsupported_no_top_level_conftest( testdir, ): from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST subdirectory = testdir.tmpdir.join("subdirectory") subdirectory.mkdir() testdir.makeconftest( """ pytest_plugins=['capture'] """ ) testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py")) testdir.makepyfile( """ def test_func(): pass """ ) res = testdir.runpytest_subprocess() assert res.ret == 2 msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] res.stdout.fnmatch_lines( ["*{msg}*".format(msg=msg), "*subdirectory{sep}conftest.py*".format(sep=os.sep)] ) def test_pytest_plugins_in_non_top_level_conftest_unsupported_no_false_positives( testdir, ): from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST subdirectory = testdir.tmpdir.join("subdirectory") subdirectory.mkdir() testdir.makeconftest( """ pass """ ) testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py")) testdir.makeconftest( """ import warnings warnings.filterwarnings('always', category=DeprecationWarning) pytest_plugins=['capture'] """ ) testdir.makepyfile( """ def test_func(): pass """ ) res = testdir.runpytest_subprocess() assert res.ret == 0 msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] assert msg not in res.stdout.str() def test_fixture_named_request(testdir): testdir.copy_example() result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*'request' is a reserved name for fixtures and will raise an error in future versions" ] ) def test_pytest_warns_unknown_kwargs(): with pytest.warns( PytestDeprecationWarning, match=r"pytest.warns\(\) got unexpected keyword arguments: \['foo'\]", ): pytest.warns(UserWarning, foo="hello") pytest-4.6.9/testing/test_parseopt.py0000664000372000037200000003205313604173400020650 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import distutils.spawn import os import sys import py import pytest from _pytest.config import argparsing as parseopt from _pytest.config.exceptions import UsageError @pytest.fixture def parser(): return parseopt.Parser() class TestParser(object): def test_no_help_by_default(self): parser = parseopt.Parser(usage="xyz") pytest.raises(UsageError, lambda: parser.parse(["-h"])) def test_custom_prog(self, parser): """Custom prog can be set for `argparse.ArgumentParser`.""" assert parser._getparser().prog == os.path.basename(sys.argv[0]) parser.prog = "custom-prog" assert parser._getparser().prog == "custom-prog" def test_argument(self): with pytest.raises(parseopt.ArgumentError): # need a short or long option argument = parseopt.Argument() argument = parseopt.Argument("-t") assert argument._short_opts == ["-t"] assert argument._long_opts == [] assert argument.dest == "t" argument = parseopt.Argument("-t", "--test") assert argument._short_opts == ["-t"] assert argument._long_opts == ["--test"] assert argument.dest == "test" argument = parseopt.Argument("-t", "--test", dest="abc") assert argument.dest == "abc" assert str(argument) == ( "Argument(_short_opts: ['-t'], _long_opts: ['--test'], dest: 'abc')" ) def test_argument_type(self): argument = parseopt.Argument("-t", dest="abc", type=int) assert argument.type is int argument = parseopt.Argument("-t", dest="abc", type=str) assert argument.type is str argument = parseopt.Argument("-t", dest="abc", type=float) assert argument.type is float with pytest.warns(DeprecationWarning): with pytest.raises(KeyError): argument = parseopt.Argument("-t", dest="abc", type="choice") argument = parseopt.Argument( "-t", dest="abc", type=str, choices=["red", "blue"] ) assert argument.type is str def test_argument_processopt(self): argument = parseopt.Argument("-t", type=int) argument.default = 42 argument.dest = "abc" res = argument.attrs() assert res["default"] == 42 assert res["dest"] == "abc" def test_group_add_and_get(self, parser): group = parser.getgroup("hello", description="desc") assert group.name == "hello" assert group.description == "desc" def test_getgroup_simple(self, parser): group = parser.getgroup("hello", description="desc") assert group.name == "hello" assert group.description == "desc" group2 = parser.getgroup("hello") assert group2 is group def test_group_ordering(self, parser): parser.getgroup("1") parser.getgroup("2") parser.getgroup("3", after="1") groups = parser._groups groups_names = [x.name for x in groups] assert groups_names == list("132") def test_group_addoption(self): group = parseopt.OptionGroup("hello") group.addoption("--option1", action="store_true") assert len(group.options) == 1 assert isinstance(group.options[0], parseopt.Argument) def test_group_addoption_conflict(self): group = parseopt.OptionGroup("hello again") group.addoption("--option1", "--option-1", action="store_true") with pytest.raises(ValueError) as err: group.addoption("--option1", "--option-one", action="store_true") assert str({"--option1"}) in str(err.value) def test_group_shortopt_lowercase(self, parser): group = parser.getgroup("hello") with pytest.raises(ValueError): group.addoption("-x", action="store_true") assert len(group.options) == 0 group._addoption("-x", action="store_true") assert len(group.options) == 1 def test_parser_addoption(self, parser): group = parser.getgroup("custom options") assert len(group.options) == 0 group.addoption("--option1", action="store_true") assert len(group.options) == 1 def test_parse(self, parser): parser.addoption("--hello", dest="hello", action="store") args = parser.parse(["--hello", "world"]) assert args.hello == "world" assert not getattr(args, parseopt.FILE_OR_DIR) def test_parse2(self, parser): args = parser.parse([py.path.local()]) assert getattr(args, parseopt.FILE_OR_DIR)[0] == py.path.local() def test_parse_known_args(self, parser): parser.parse_known_args([py.path.local()]) parser.addoption("--hello", action="store_true") ns = parser.parse_known_args(["x", "--y", "--hello", "this"]) assert ns.hello assert ns.file_or_dir == ["x"] def test_parse_known_and_unknown_args(self, parser): parser.addoption("--hello", action="store_true") ns, unknown = parser.parse_known_and_unknown_args( ["x", "--y", "--hello", "this"] ) assert ns.hello assert ns.file_or_dir == ["x"] assert unknown == ["--y", "this"] def test_parse_will_set_default(self, parser): parser.addoption("--hello", dest="hello", default="x", action="store") option = parser.parse([]) assert option.hello == "x" del option.hello parser.parse_setoption([], option) assert option.hello == "x" def test_parse_setoption(self, parser): parser.addoption("--hello", dest="hello", action="store") parser.addoption("--world", dest="world", default=42) class A(object): pass option = A() args = parser.parse_setoption(["--hello", "world"], option) assert option.hello == "world" assert option.world == 42 assert not args def test_parse_special_destination(self, parser): parser.addoption("--ultimate-answer", type=int) args = parser.parse(["--ultimate-answer", "42"]) assert args.ultimate_answer == 42 def test_parse_split_positional_arguments(self, parser): parser.addoption("-R", action="store_true") parser.addoption("-S", action="store_false") args = parser.parse(["-R", "4", "2", "-S"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] args = parser.parse(["-R", "-S", "4", "2", "-R"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False args = parser.parse(["-R", "4", "-S", "2"]) assert getattr(args, parseopt.FILE_OR_DIR) == ["4", "2"] assert args.R is True assert args.S is False def test_parse_defaultgetter(self): def defaultget(option): if not hasattr(option, "type"): return if option.type is int: option.default = 42 elif option.type is str: option.default = "world" parser = parseopt.Parser(processopt=defaultget) parser.addoption("--this", dest="this", type=int, action="store") parser.addoption("--hello", dest="hello", type=str, action="store") parser.addoption("--no", dest="no", action="store_true") option = parser.parse([]) assert option.hello == "world" assert option.this == 42 assert option.no is False def test_drop_short_helper(self): parser = argparse.ArgumentParser( formatter_class=parseopt.DropShorterLongHelpFormatter ) parser.add_argument( "-t", "--twoword", "--duo", "--two-word", "--two", help="foo" ).map_long_option = {"two": "two-word"} # throws error on --deux only! parser.add_argument( "-d", "--deuxmots", "--deux-mots", action="store_true", help="foo" ).map_long_option = {"deux": "deux-mots"} parser.add_argument("-s", action="store_true", help="single short") parser.add_argument("--abc", "-a", action="store_true", help="bar") parser.add_argument("--klm", "-k", "--kl-m", action="store_true", help="bar") parser.add_argument( "-P", "--pq-r", "-p", "--pqr", action="store_true", help="bar" ) parser.add_argument( "--zwei-wort", "--zweiwort", "--zweiwort", action="store_true", help="bar" ) parser.add_argument( "-x", "--exit-on-first", "--exitfirst", action="store_true", help="spam" ).map_long_option = {"exitfirst": "exit-on-first"} parser.add_argument("files_and_dirs", nargs="*") args = parser.parse_args(["-k", "--duo", "hallo", "--exitfirst"]) assert args.twoword == "hallo" assert args.klm is True assert args.zwei_wort is False assert args.exit_on_first is True assert args.s is False args = parser.parse_args(["--deux-mots"]) with pytest.raises(AttributeError): assert args.deux_mots is True assert args.deuxmots is True args = parser.parse_args(["file", "dir"]) assert "|".join(args.files_and_dirs) == "file|dir" def test_drop_short_0(self, parser): parser.addoption("--funcarg", "--func-arg", action="store_true") parser.addoption("--abc-def", "--abc-def", action="store_true") parser.addoption("--klm-hij", action="store_true") args = parser.parse(["--funcarg", "--k"]) assert args.funcarg is True assert args.abc_def is False assert args.klm_hij is True def test_drop_short_2(self, parser): parser.addoption("--func-arg", "--doit", action="store_true") args = parser.parse(["--doit"]) assert args.func_arg is True def test_drop_short_3(self, parser): parser.addoption("--func-arg", "--funcarg", "--doit", action="store_true") args = parser.parse(["abcd"]) assert args.func_arg is False assert args.file_or_dir == ["abcd"] def test_drop_short_help0(self, parser, capsys): parser.addoption("--func-args", "--doit", help="foo", action="store_true") parser.parse([]) help = parser.optparser.format_help() assert "--func-args, --doit foo" in help # testing would be more helpful with all help generated def test_drop_short_help1(self, parser, capsys): group = parser.getgroup("general") group.addoption("--doit", "--func-args", action="store_true", help="foo") group._addoption( "-h", "--help", action="store_true", dest="help", help="show help message and configuration info", ) parser.parse(["-h"]) help = parser.optparser.format_help() assert "-doit, --func-args foo" in help def test_multiple_metavar_help(self, parser): """ Help text for options with a metavar tuple should display help in the form "--preferences=value1 value2 value3" (#2004). """ group = parser.getgroup("general") group.addoption( "--preferences", metavar=("value1", "value2", "value3"), nargs=3 ) group._addoption("-h", "--help", action="store_true", dest="help") parser.parse(["-h"]) help = parser.optparser.format_help() assert "--preferences=value1 value2 value3" in help def test_argcomplete(testdir, monkeypatch): if not distutils.spawn.find_executable("bash"): pytest.skip("bash not available") script = str(testdir.tmpdir.join("test_argcomplete")) with open(str(script), "w") as fp: # redirect output from argcomplete to stdin and stderr is not trivial # http://stackoverflow.com/q/12589419/1307905 # so we use bash fp.write('COMP_WORDBREAKS="$COMP_WORDBREAKS" python -m pytest 8>&1 9>&2') # alternative would be exteneded Testdir.{run(),_run(),popen()} to be able # to handle a keyword argument env that replaces os.environ in popen or # extends the copy, advantage: could not forget to restore monkeypatch.setenv("_ARGCOMPLETE", "1") monkeypatch.setenv("_ARGCOMPLETE_IFS", "\x0b") monkeypatch.setenv("COMP_WORDBREAKS", " \\t\\n\"\\'><=;|&(:") arg = "--fu" monkeypatch.setenv("COMP_LINE", "pytest " + arg) monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) result = testdir.run("bash", str(script), arg) if result.ret == 255: # argcomplete not found pytest.skip("argcomplete not available") elif not result.stdout.str(): pytest.skip( "bash provided no output on stdout, argcomplete not available? (stderr={!r})".format( result.stderr.str() ) ) else: result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) os.mkdir("test_argcomplete.d") arg = "test_argc" monkeypatch.setenv("COMP_LINE", "pytest " + arg) monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) result = testdir.run("bash", str(script), arg) result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"]) pytest-4.6.9/testing/test_monkeypatch.py0000664000372000037200000003000613604173400021331 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import re import sys import textwrap import six import pytest from _pytest.monkeypatch import MonkeyPatch @pytest.fixture def mp(): cwd = os.getcwd() sys_path = list(sys.path) yield MonkeyPatch() sys.path[:] = sys_path os.chdir(cwd) def test_setattr(): class A(object): x = 1 monkeypatch = MonkeyPatch() pytest.raises(AttributeError, monkeypatch.setattr, A, "notexists", 2) monkeypatch.setattr(A, "y", 2, raising=False) assert A.y == 2 monkeypatch.undo() assert not hasattr(A, "y") monkeypatch = MonkeyPatch() monkeypatch.setattr(A, "x", 2) assert A.x == 2 monkeypatch.setattr(A, "x", 3) assert A.x == 3 monkeypatch.undo() assert A.x == 1 A.x = 5 monkeypatch.undo() # double-undo makes no modification assert A.x == 5 class TestSetattrWithImportPath(object): def test_string_expression(self, monkeypatch): monkeypatch.setattr("os.path.abspath", lambda x: "hello2") assert os.path.abspath("123") == "hello2" def test_string_expression_class(self, monkeypatch): monkeypatch.setattr("_pytest.config.Config", 42) import _pytest assert _pytest.config.Config == 42 def test_unicode_string(self, monkeypatch): monkeypatch.setattr("_pytest.config.Config", 42) import _pytest assert _pytest.config.Config == 42 monkeypatch.delattr("_pytest.config.Config") def test_wrong_target(self, monkeypatch): pytest.raises(TypeError, lambda: monkeypatch.setattr(None, None)) def test_unknown_import(self, monkeypatch): pytest.raises(ImportError, lambda: monkeypatch.setattr("unkn123.classx", None)) def test_unknown_attr(self, monkeypatch): pytest.raises( AttributeError, lambda: monkeypatch.setattr("os.path.qweqwe", None) ) def test_unknown_attr_non_raising(self, monkeypatch): # https://github.com/pytest-dev/pytest/issues/746 monkeypatch.setattr("os.path.qweqwe", 42, raising=False) assert os.path.qweqwe == 42 def test_delattr(self, monkeypatch): monkeypatch.delattr("os.path.abspath") assert not hasattr(os.path, "abspath") monkeypatch.undo() assert os.path.abspath def test_delattr(): class A(object): x = 1 monkeypatch = MonkeyPatch() monkeypatch.delattr(A, "x") assert not hasattr(A, "x") monkeypatch.undo() assert A.x == 1 monkeypatch = MonkeyPatch() monkeypatch.delattr(A, "x") pytest.raises(AttributeError, monkeypatch.delattr, A, "y") monkeypatch.delattr(A, "y", raising=False) monkeypatch.setattr(A, "x", 5, raising=False) assert A.x == 5 monkeypatch.undo() assert A.x == 1 def test_setitem(): d = {"x": 1} monkeypatch = MonkeyPatch() monkeypatch.setitem(d, "x", 2) monkeypatch.setitem(d, "y", 1700) monkeypatch.setitem(d, "y", 1700) assert d["x"] == 2 assert d["y"] == 1700 monkeypatch.setitem(d, "x", 3) assert d["x"] == 3 monkeypatch.undo() assert d["x"] == 1 assert "y" not in d d["x"] = 5 monkeypatch.undo() assert d["x"] == 5 def test_setitem_deleted_meanwhile(): d = {} monkeypatch = MonkeyPatch() monkeypatch.setitem(d, "x", 2) del d["x"] monkeypatch.undo() assert not d @pytest.mark.parametrize("before", [True, False]) def test_setenv_deleted_meanwhile(before): key = "qwpeoip123" if before: os.environ[key] = "world" monkeypatch = MonkeyPatch() monkeypatch.setenv(key, "hello") del os.environ[key] monkeypatch.undo() if before: assert os.environ[key] == "world" del os.environ[key] else: assert key not in os.environ def test_delitem(): d = {"x": 1} monkeypatch = MonkeyPatch() monkeypatch.delitem(d, "x") assert "x" not in d monkeypatch.delitem(d, "y", raising=False) pytest.raises(KeyError, monkeypatch.delitem, d, "y") assert not d monkeypatch.setitem(d, "y", 1700) assert d["y"] == 1700 d["hello"] = "world" monkeypatch.setitem(d, "x", 1500) assert d["x"] == 1500 monkeypatch.undo() assert d == {"hello": "world", "x": 1} def test_setenv(): monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 2) import os assert os.environ["XYZ123"] == "2" monkeypatch.undo() assert "XYZ123" not in os.environ def test_delenv(): name = "xyz1234" assert name not in os.environ monkeypatch = MonkeyPatch() pytest.raises(KeyError, monkeypatch.delenv, name, raising=True) monkeypatch.delenv(name, raising=False) monkeypatch.undo() os.environ[name] = "1" try: monkeypatch = MonkeyPatch() monkeypatch.delenv(name) assert name not in os.environ monkeypatch.setenv(name, "3") assert os.environ[name] == "3" monkeypatch.undo() assert os.environ[name] == "1" finally: if name in os.environ: del os.environ[name] class TestEnvironWarnings(object): """ os.environ keys and values should be native strings, otherwise it will cause problems with other modules (notably subprocess). On Python 2 os.environ accepts anything without complaining, while Python 3 does the right thing and raises an error. """ VAR_NAME = u"PYTEST_INTERNAL_MY_VAR" @pytest.mark.skipif(not six.PY2, reason="Python 2 only test") def test_setenv_unicode_key(self, monkeypatch): with pytest.warns( pytest.PytestWarning, match="Environment variable name {!r} should be str".format(self.VAR_NAME), ): monkeypatch.setenv(self.VAR_NAME, "2") @pytest.mark.skipif(not six.PY2, reason="Python 2 only test") def test_delenv_unicode_key(self, monkeypatch): with pytest.warns( pytest.PytestWarning, match="Environment variable name {!r} should be str".format(self.VAR_NAME), ): monkeypatch.delenv(self.VAR_NAME, raising=False) def test_setenv_non_str_warning(self, monkeypatch): value = 2 msg = ( "Value of environment variable PYTEST_INTERNAL_MY_VAR type should be str, " "but got 2 (type: int); converted to str implicitly" ) with pytest.warns(pytest.PytestWarning, match=re.escape(msg)): monkeypatch.setenv(str(self.VAR_NAME), value) def test_setenv_prepend(): import os monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 2, prepend="-") assert os.environ["XYZ123"] == "2" with pytest.warns(pytest.PytestWarning): monkeypatch.setenv("XYZ123", 3, prepend="-") assert os.environ["XYZ123"] == "3-2" monkeypatch.undo() assert "XYZ123" not in os.environ def test_monkeypatch_plugin(testdir): reprec = testdir.inline_runsource( """ def test_method(monkeypatch): assert monkeypatch.__class__.__name__ == "MonkeyPatch" """ ) res = reprec.countoutcomes() assert tuple(res) == (1, 0, 0), res def test_syspath_prepend(mp): old = list(sys.path) mp.syspath_prepend("world") mp.syspath_prepend("hello") assert sys.path[0] == "hello" assert sys.path[1] == "world" mp.undo() assert sys.path == old mp.undo() assert sys.path == old def test_syspath_prepend_double_undo(mp): old_syspath = sys.path[:] try: mp.syspath_prepend("hello world") mp.undo() sys.path.append("more hello world") mp.undo() assert sys.path[-1] == "more hello world" finally: sys.path[:] = old_syspath def test_chdir_with_path_local(mp, tmpdir): mp.chdir(tmpdir) assert os.getcwd() == tmpdir.strpath def test_chdir_with_str(mp, tmpdir): mp.chdir(tmpdir.strpath) assert os.getcwd() == tmpdir.strpath def test_chdir_undo(mp, tmpdir): cwd = os.getcwd() mp.chdir(tmpdir) mp.undo() assert os.getcwd() == cwd def test_chdir_double_undo(mp, tmpdir): mp.chdir(tmpdir.strpath) mp.undo() tmpdir.chdir() mp.undo() assert os.getcwd() == tmpdir.strpath def test_issue185_time_breaks(testdir): testdir.makepyfile( """ import time def test_m(monkeypatch): def f(): raise Exception monkeypatch.setattr(time, "time", f) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *1 passed* """ ) def test_importerror(testdir): p = testdir.mkpydir("package") p.join("a.py").write( textwrap.dedent( """\ import doesnotexist x = 1 """ ) ) testdir.tmpdir.join("test_importerror.py").write( textwrap.dedent( """\ def test_importerror(monkeypatch): monkeypatch.setattr('package.a.x', 2) """ ) ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *import error in package.a: No module named {0}doesnotexist{0}* """.format( "'" if sys.version_info > (3, 0) else "" ) ) class SampleNew(object): @staticmethod def hello(): return True class SampleNewInherit(SampleNew): pass class SampleOld(object): # oldstyle on python2 @staticmethod def hello(): return True class SampleOldInherit(SampleOld): pass @pytest.mark.parametrize( "Sample", [SampleNew, SampleNewInherit, SampleOld, SampleOldInherit], ids=["new", "new-inherit", "old", "old-inherit"], ) def test_issue156_undo_staticmethod(Sample): monkeypatch = MonkeyPatch() monkeypatch.setattr(Sample, "hello", None) assert Sample.hello is None monkeypatch.undo() assert Sample.hello() def test_undo_class_descriptors_delattr(): class SampleParent(object): @classmethod def hello(_cls): pass @staticmethod def world(): pass class SampleChild(SampleParent): pass monkeypatch = MonkeyPatch() original_hello = SampleChild.hello original_world = SampleChild.world monkeypatch.delattr(SampleParent, "hello") monkeypatch.delattr(SampleParent, "world") assert getattr(SampleParent, "hello", None) is None assert getattr(SampleParent, "world", None) is None monkeypatch.undo() assert original_hello == SampleChild.hello assert original_world == SampleChild.world def test_issue1338_name_resolving(): pytest.importorskip("requests") monkeypatch = MonkeyPatch() try: monkeypatch.delattr("requests.sessions.Session.request") finally: monkeypatch.undo() def test_context(): monkeypatch = MonkeyPatch() import functools import inspect with monkeypatch.context() as m: m.setattr(functools, "partial", 3) assert not inspect.isclass(functools.partial) assert inspect.isclass(functools.partial) def test_syspath_prepend_with_namespace_packages(testdir, monkeypatch): for dirname in "hello", "world": d = testdir.mkdir(dirname) ns = d.mkdir("ns_pkg") ns.join("__init__.py").write( "__import__('pkg_resources').declare_namespace(__name__)" ) lib = ns.mkdir(dirname) lib.join("__init__.py").write("def check(): return %r" % dirname) monkeypatch.syspath_prepend("hello") import ns_pkg.hello assert ns_pkg.hello.check() == "hello" with pytest.raises(ImportError): import ns_pkg.world # Prepending should call fixup_namespace_packages. monkeypatch.syspath_prepend("world") import ns_pkg.world assert ns_pkg.world.check() == "world" # Should invalidate caches via importlib.invalidate_caches. tmpdir = testdir.tmpdir modules_tmpdir = tmpdir.mkdir("modules_tmpdir") monkeypatch.syspath_prepend(str(modules_tmpdir)) modules_tmpdir.join("main_app.py").write("app = True") from main_app import app # noqa: F401 pytest-4.6.9/testing/test_pluginmanager.py0000664000372000037200000003135013604173400021643 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import sys import types import pytest from _pytest.config import PytestPluginManager from _pytest.config.exceptions import UsageError from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import Session @pytest.fixture def pytestpm(): return PytestPluginManager() class TestPytestPluginInteractions(object): def test_addhooks_conftestplugin(self, testdir, _config_for_test): testdir.makepyfile( newhooks=""" def pytest_myhook(xyz): "new hook" """ ) conf = testdir.makeconftest( """ import newhooks def pytest_addhooks(pluginmanager): pluginmanager.add_hookspecs(newhooks) def pytest_myhook(xyz): return xyz + 1 """ ) config = _config_for_test pm = config.pluginmanager pm.hook.pytest_addhooks.call_historic( kwargs=dict(pluginmanager=config.pluginmanager) ) config.pluginmanager._importconftest(conf) # print(config.pluginmanager.get_plugins()) res = config.hook.pytest_myhook(xyz=10) assert res == [11] def test_addhooks_nohooks(self, testdir): testdir.makeconftest( """ import sys def pytest_addhooks(pluginmanager): pluginmanager.add_hookspecs(sys) """ ) res = testdir.runpytest() assert res.ret != 0 res.stderr.fnmatch_lines(["*did not find*sys*"]) def test_do_option_postinitialize(self, testdir): config = testdir.parseconfigure() assert not hasattr(config.option, "test123") p = testdir.makepyfile( """ def pytest_addoption(parser): parser.addoption('--test123', action="store_true", default=True) """ ) config.pluginmanager._importconftest(p) assert config.option.test123 def test_configure(self, testdir): config = testdir.parseconfig() values = [] class A(object): def pytest_configure(self, config): values.append(self) config.pluginmanager.register(A()) assert len(values) == 0 config._do_configure() assert len(values) == 1 config.pluginmanager.register(A()) # leads to a configured() plugin assert len(values) == 2 assert values[0] != values[1] config._ensure_unconfigure() config.pluginmanager.register(A()) assert len(values) == 2 def test_hook_tracing(self, _config_for_test): pytestpm = _config_for_test.pluginmanager # fully initialized with plugins saveindent = [] class api1(object): def pytest_plugin_registered(self): saveindent.append(pytestpm.trace.root.indent) class api2(object): def pytest_plugin_registered(self): saveindent.append(pytestpm.trace.root.indent) raise ValueError() values = [] pytestpm.trace.root.setwriter(values.append) undo = pytestpm.enable_tracing() try: indent = pytestpm.trace.root.indent p = api1() pytestpm.register(p) assert pytestpm.trace.root.indent == indent assert len(values) >= 2 assert "pytest_plugin_registered" in values[0] assert "finish" in values[1] values[:] = [] with pytest.raises(ValueError): pytestpm.register(api2()) assert pytestpm.trace.root.indent == indent assert saveindent[0] > indent finally: undo() def test_hook_proxy(self, testdir): """Test the gethookproxy function(#2016)""" config = testdir.parseconfig() session = Session(config) testdir.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""}) conftest1 = testdir.tmpdir.join("tests/conftest.py") conftest2 = testdir.tmpdir.join("tests/subdir/conftest.py") config.pluginmanager._importconftest(conftest1) ihook_a = session.gethookproxy(testdir.tmpdir.join("tests")) assert ihook_a is not None config.pluginmanager._importconftest(conftest2) ihook_b = session.gethookproxy(testdir.tmpdir.join("tests")) assert ihook_a is not ihook_b def test_default_markers(testdir): result = testdir.runpytest("--markers") result.stdout.fnmatch_lines(["*tryfirst*first*", "*trylast*last*"]) def test_importplugin_error_message(testdir, pytestpm): """Don't hide import errors when importing plugins and provide an easy to debug message. See #375 and #1998. """ testdir.syspathinsert(testdir.tmpdir) testdir.makepyfile( qwe=""" # -*- coding: utf-8 -*- def test_traceback(): raise ImportError(u'Not possible to import: ☺') test_traceback() """ ) with pytest.raises(ImportError) as excinfo: pytestpm.import_plugin("qwe") assert str(excinfo.value).endswith( 'Error importing plugin "qwe": Not possible to import: ☺' ) assert "in test_traceback" in str(excinfo.traceback[-1]) class TestPytestPluginManager(object): def test_register_imported_modules(self): pm = PytestPluginManager() mod = types.ModuleType("x.y.pytest_hello") pm.register(mod) assert pm.is_registered(mod) values = pm.get_plugins() assert mod in values pytest.raises(ValueError, pm.register, mod) pytest.raises(ValueError, lambda: pm.register(mod)) # assert not pm.is_registered(mod2) assert pm.get_plugins() == values def test_canonical_import(self, monkeypatch): mod = types.ModuleType("pytest_xyz") monkeypatch.setitem(sys.modules, "pytest_xyz", mod) pm = PytestPluginManager() pm.import_plugin("pytest_xyz") assert pm.get_plugin("pytest_xyz") == mod assert pm.is_registered(mod) def test_consider_module(self, testdir, pytestpm): testdir.syspathinsert() testdir.makepyfile(pytest_p1="#") testdir.makepyfile(pytest_p2="#") mod = types.ModuleType("temp") mod.pytest_plugins = ["pytest_p1", "pytest_p2"] pytestpm.consider_module(mod) assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1" assert pytestpm.get_plugin("pytest_p2").__name__ == "pytest_p2" def test_consider_module_import_module(self, testdir, _config_for_test): pytestpm = _config_for_test.pluginmanager mod = types.ModuleType("x") mod.pytest_plugins = "pytest_a" aplugin = testdir.makepyfile(pytest_a="#") reprec = testdir.make_hook_recorder(pytestpm) testdir.syspathinsert(aplugin.dirpath()) pytestpm.consider_module(mod) call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name) assert call.plugin.__name__ == "pytest_a" # check that it is not registered twice pytestpm.consider_module(mod) values = reprec.getcalls("pytest_plugin_registered") assert len(values) == 1 def test_consider_env_fails_to_import(self, monkeypatch, pytestpm): monkeypatch.setenv("PYTEST_PLUGINS", "nonexisting", prepend=",") with pytest.raises(ImportError): pytestpm.consider_env() @pytest.mark.filterwarnings("always") def test_plugin_skip(self, testdir, monkeypatch): p = testdir.makepyfile( skipping1=""" import pytest pytest.skip("hello", allow_module_level=True) """ ) p.copy(p.dirpath("skipping2.py")) monkeypatch.setenv("PYTEST_PLUGINS", "skipping2") result = testdir.runpytest("-rw", "-p", "skipping1", syspathinsert=True) assert result.ret == EXIT_NOTESTSCOLLECTED result.stdout.fnmatch_lines( ["*skipped plugin*skipping1*hello*", "*skipped plugin*skipping2*hello*"] ) def test_consider_env_plugin_instantiation(self, testdir, monkeypatch, pytestpm): testdir.syspathinsert() testdir.makepyfile(xy123="#") monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123") l1 = len(pytestpm.get_plugins()) pytestpm.consider_env() l2 = len(pytestpm.get_plugins()) assert l2 == l1 + 1 assert pytestpm.get_plugin("xy123") pytestpm.consider_env() l3 = len(pytestpm.get_plugins()) assert l2 == l3 def test_pluginmanager_ENV_startup(self, testdir, monkeypatch): testdir.makepyfile(pytest_x500="#") p = testdir.makepyfile( """ import pytest def test_hello(pytestconfig): plugin = pytestconfig.pluginmanager.get_plugin('pytest_x500') assert plugin is not None """ ) monkeypatch.setenv("PYTEST_PLUGINS", "pytest_x500", prepend=",") result = testdir.runpytest(p, syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_import_plugin_importname(self, testdir, pytestpm): pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwx.y") testdir.syspathinsert() pluginname = "pytest_hello" testdir.makepyfile(**{pluginname: ""}) pytestpm.import_plugin("pytest_hello") len1 = len(pytestpm.get_plugins()) pytestpm.import_plugin("pytest_hello") len2 = len(pytestpm.get_plugins()) assert len1 == len2 plugin1 = pytestpm.get_plugin("pytest_hello") assert plugin1.__name__.endswith("pytest_hello") plugin2 = pytestpm.get_plugin("pytest_hello") assert plugin2 is plugin1 def test_import_plugin_dotted_name(self, testdir, pytestpm): pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y") testdir.syspathinsert() testdir.mkpydir("pkg").join("plug.py").write("x=3") pluginname = "pkg.plug" pytestpm.import_plugin(pluginname) mod = pytestpm.get_plugin("pkg.plug") assert mod.x == 3 def test_consider_conftest_deps(self, testdir, pytestpm): mod = testdir.makepyfile("pytest_plugins='xyz'").pyimport() with pytest.raises(ImportError): pytestpm.consider_conftest(mod) class TestPytestPluginManagerBootstrapming(object): def test_preparse_args(self, pytestpm): pytest.raises( ImportError, lambda: pytestpm.consider_preparse(["xyz", "-p", "hello123"]) ) # Handles -p without space (#3532). with pytest.raises(ImportError) as excinfo: pytestpm.consider_preparse(["-phello123"]) assert '"hello123"' in excinfo.value.args[0] pytestpm.consider_preparse(["-pno:hello123"]) # Handles -p without following arg (when used without argparse). pytestpm.consider_preparse(["-p"]) with pytest.raises(UsageError, match="^plugin main cannot be disabled$"): pytestpm.consider_preparse(["-p", "no:main"]) def test_plugin_prevent_register(self, pytestpm): pytestpm.consider_preparse(["xyz", "-p", "no:abc"]) l1 = pytestpm.get_plugins() pytestpm.register(42, name="abc") l2 = pytestpm.get_plugins() assert len(l2) == len(l1) assert 42 not in l2 def test_plugin_prevent_register_unregistered_alredy_registered(self, pytestpm): pytestpm.register(42, name="abc") l1 = pytestpm.get_plugins() assert 42 in l1 pytestpm.consider_preparse(["xyz", "-p", "no:abc"]) l2 = pytestpm.get_plugins() assert 42 not in l2 def test_plugin_prevent_register_stepwise_on_cacheprovider_unregister( self, pytestpm ): """ From PR #4304 : The only way to unregister a module is documented at the end of https://docs.pytest.org/en/latest/plugins.html. When unregister cacheprovider, then unregister stepwise too """ pytestpm.register(42, name="cacheprovider") pytestpm.register(43, name="stepwise") l1 = pytestpm.get_plugins() assert 42 in l1 assert 43 in l1 pytestpm.consider_preparse(["xyz", "-p", "no:cacheprovider"]) l2 = pytestpm.get_plugins() assert 42 not in l2 assert 43 not in l2 def test_blocked_plugin_can_be_used(self, pytestpm): pytestpm.consider_preparse(["xyz", "-p", "no:abc", "-p", "abc"]) assert pytestpm.has_plugin("abc") assert not pytestpm.is_blocked("abc") assert not pytestpm.is_blocked("pytest_abc") pytest-4.6.9/testing/test_warnings.py0000664000372000037200000005111013604173400020636 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import unicode_literals import sys import warnings import six import pytest WARNINGS_SUMMARY_HEADER = "warnings summary" @pytest.fixture def pyfile_with_warnings(testdir, request): """ Create a test file which calls a function in a module which generates warnings. """ testdir.syspathinsert() test_name = request.function.__name__ module_name = test_name.lstrip("test_") + "_module" testdir.makepyfile( **{ module_name: """ import warnings def foo(): warnings.warn(UserWarning("user warning")) warnings.warn(RuntimeWarning("runtime warning")) return 1 """, test_name: """ import {module_name} def test_func(): assert {module_name}.foo() == 1 """.format( module_name=module_name ), } ) @pytest.mark.filterwarnings("default") def test_normal_flow(testdir, pyfile_with_warnings): """ Check that the warnings section is displayed. """ result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "test_normal_flow.py::test_func", "*normal_flow_module.py:3: UserWarning: user warning", '* warnings.warn(UserWarning("user warning"))', "*normal_flow_module.py:4: RuntimeWarning: runtime warning", '* warnings.warn(RuntimeWarning("runtime warning"))', "* 1 passed, 2 warnings*", ] ) @pytest.mark.filterwarnings("always") def test_setup_teardown_warnings(testdir, pyfile_with_warnings): testdir.makepyfile( """ import warnings import pytest @pytest.fixture def fix(): warnings.warn(UserWarning("warning during setup")) yield warnings.warn(UserWarning("warning during teardown")) def test_func(fix): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_setup_teardown_warnings.py:6: UserWarning: warning during setup", '*warnings.warn(UserWarning("warning during setup"))', "*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown", '*warnings.warn(UserWarning("warning during teardown"))', "* 1 passed, 2 warnings*", ] ) @pytest.mark.parametrize("method", ["cmdline", "ini"]) def test_as_errors(testdir, pyfile_with_warnings, method): args = ("-W", "error") if method == "cmdline" else () if method == "ini": testdir.makeini( """ [pytest] filterwarnings=error """ ) # Use a subprocess, since changing logging level affects other threads # (xdist). result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines( [ "E UserWarning: user warning", "as_errors_module.py:3: UserWarning", "* 1 failed in *", ] ) @pytest.mark.parametrize("method", ["cmdline", "ini"]) def test_ignore(testdir, pyfile_with_warnings, method): args = ("-W", "ignore") if method == "cmdline" else () if method == "ini": testdir.makeini( """ [pytest] filterwarnings= ignore """ ) result = testdir.runpytest(*args) result.stdout.fnmatch_lines(["* 1 passed in *"]) assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() @pytest.mark.skipif( sys.version_info < (3, 0), reason="warnings message is unicode is ok in python3" ) @pytest.mark.filterwarnings("always") def test_unicode(testdir, pyfile_with_warnings): testdir.makepyfile( """ # -*- coding: utf-8 -*- import warnings import pytest @pytest.fixture def fix(): warnings.warn(u"测试") yield def test_func(fix): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_unicode.py:8: UserWarning: \u6d4b\u8bd5*", "* 1 passed, 1 warnings*", ] ) @pytest.mark.skipif( sys.version_info >= (3, 0), reason="warnings message is broken as it is not str instance", ) def test_py2_unicode(testdir, pyfile_with_warnings): if getattr(sys, "pypy_version_info", ())[:2] == (5, 9) and sys.platform.startswith( "win" ): pytest.xfail("fails with unicode error on PyPy2 5.9 and Windows (#2905)") testdir.makepyfile( """ # -*- coding: utf-8 -*- import warnings import pytest @pytest.fixture def fix(): warnings.warn(u"测试") yield @pytest.mark.filterwarnings('always') def test_func(fix): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_py2_unicode.py:8: UserWarning: \\u6d4b\\u8bd5", '*warnings.warn(u"\u6d4b\u8bd5")', "*warnings.py:*: UnicodeWarning: Warning is using unicode non*", "* 1 passed, 2 warnings*", ] ) def test_py2_unicode_ascii(testdir): """Ensure that our warning about 'unicode warnings containing non-ascii messages' does not trigger with ascii-convertible messages""" testdir.makeini("[pytest]") testdir.makepyfile( """ import pytest import warnings @pytest.mark.filterwarnings('always') def test_func(): warnings.warn(u"hello") """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, '*warnings.warn(u"hello")', "* 1 passed, 1 warnings in*", ] ) def test_works_with_filterwarnings(testdir): """Ensure our warnings capture does not mess with pre-installed filters (#2430).""" testdir.makepyfile( """ import warnings class MyWarning(Warning): pass warnings.filterwarnings("error", category=MyWarning) class TestWarnings(object): def test_my_warning(self): try: warnings.warn(MyWarning("warn!")) assert False except MyWarning: assert True """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*== 1 passed in *"]) @pytest.mark.parametrize("default_config", ["ini", "cmdline"]) def test_filterwarnings_mark(testdir, default_config): """ Test ``filterwarnings`` mark works and takes precedence over command line and ini options. """ if default_config == "ini": testdir.makeini( """ [pytest] filterwarnings = always """ ) testdir.makepyfile( """ import warnings import pytest @pytest.mark.filterwarnings('ignore::RuntimeWarning') def test_ignore_runtime_warning(): warnings.warn(RuntimeWarning()) @pytest.mark.filterwarnings('error') def test_warning_error(): warnings.warn(RuntimeWarning()) def test_show_warning(): warnings.warn(RuntimeWarning()) """ ) result = testdir.runpytest("-W always" if default_config == "cmdline" else "") result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warnings in *"]) def test_non_string_warning_argument(testdir): """Non-str argument passed to warning breaks pytest (#2956)""" testdir.makepyfile( """ import warnings import pytest def test(): warnings.warn(UserWarning(1, u'foo')) """ ) result = testdir.runpytest("-W", "always") result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"]) def test_filterwarnings_mark_registration(testdir): """Ensure filterwarnings mark is registered""" testdir.makepyfile( """ import pytest @pytest.mark.filterwarnings('error') def test_func(): pass """ ) result = testdir.runpytest("--strict-markers") assert result.ret == 0 @pytest.mark.filterwarnings("always") def test_warning_captured_hook(testdir): testdir.makeconftest( """ from _pytest.warnings import _issue_warning_captured def pytest_configure(config): _issue_warning_captured(UserWarning("config warning"), config.hook, stacklevel=2) """ ) testdir.makepyfile( """ import pytest, warnings warnings.warn(UserWarning("collect warning")) @pytest.fixture def fix(): warnings.warn(UserWarning("setup warning")) yield 1 warnings.warn(UserWarning("teardown warning")) def test_func(fix): warnings.warn(UserWarning("call warning")) assert fix == 1 """ ) collected = [] class WarningCollector: def pytest_warning_captured(self, warning_message, when, item): imge_name = item.name if item is not None else "" collected.append((str(warning_message.message), when, imge_name)) result = testdir.runpytest(plugins=[WarningCollector()]) result.stdout.fnmatch_lines(["*1 passed*"]) expected = [ ("config warning", "config", ""), ("collect warning", "collect", ""), ("setup warning", "runtest", "test_func"), ("call warning", "runtest", "test_func"), ("teardown warning", "runtest", "test_func"), ] assert collected == expected @pytest.mark.filterwarnings("always") def test_collection_warnings(testdir): """ Check that we also capture warnings issued during test collection (#3251). """ testdir.makepyfile( """ import warnings warnings.warn(UserWarning("collection warning")) def test_foo(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, " *collection_warnings.py:3: UserWarning: collection warning", ' warnings.warn(UserWarning("collection warning"))', "* 1 passed, 1 warnings*", ] ) @pytest.mark.filterwarnings("always") def test_mark_regex_escape(testdir): """@pytest.mark.filterwarnings should not try to escape regex characters (#3936)""" testdir.makepyfile( r""" import pytest, warnings @pytest.mark.filterwarnings(r"ignore:some \(warning\)") def test_foo(): warnings.warn(UserWarning("some (warning)")) """ ) result = testdir.runpytest() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() @pytest.mark.filterwarnings("default") @pytest.mark.parametrize("ignore_pytest_warnings", ["no", "ini", "cmdline"]) def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings): """Make sure we can ignore internal pytest warnings using a warnings filter.""" testdir.makepyfile( """ import pytest import warnings warnings.warn(pytest.PytestWarning("some internal warning")) def test_bar(): pass """ ) if ignore_pytest_warnings == "ini": testdir.makeini( """ [pytest] filterwarnings = ignore::pytest.PytestWarning """ ) args = ( ["-W", "ignore::pytest.PytestWarning"] if ignore_pytest_warnings == "cmdline" else [] ) result = testdir.runpytest(*args) if ignore_pytest_warnings != "no": assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() else: result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_hide_pytest_internal_warnings.py:4: PytestWarning: some internal warning", "* 1 passed, 1 warnings *", ] ) @pytest.mark.parametrize("ignore_on_cmdline", [True, False]) def test_option_precedence_cmdline_over_ini(testdir, ignore_on_cmdline): """filters defined in the command-line should take precedence over filters in ini files (#3946).""" testdir.makeini( """ [pytest] filterwarnings = error """ ) testdir.makepyfile( """ import warnings def test(): warnings.warn(UserWarning('hello')) """ ) args = ["-W", "ignore"] if ignore_on_cmdline else [] result = testdir.runpytest(*args) if ignore_on_cmdline: result.stdout.fnmatch_lines(["* 1 passed in*"]) else: result.stdout.fnmatch_lines(["* 1 failed in*"]) def test_option_precedence_mark(testdir): """Filters defined by marks should always take precedence (#3946).""" testdir.makeini( """ [pytest] filterwarnings = ignore """ ) testdir.makepyfile( """ import pytest, warnings @pytest.mark.filterwarnings('error') def test(): warnings.warn(UserWarning('hello')) """ ) result = testdir.runpytest("-W", "ignore") result.stdout.fnmatch_lines(["* 1 failed in*"]) class TestDeprecationWarningsByDefault: """ Note: all pytest runs are executed in a subprocess so we don't inherit warning filters from pytest's own test suite """ def create_file(self, testdir, mark=""): testdir.makepyfile( """ import pytest, warnings warnings.warn(DeprecationWarning("collection")) {mark} def test_foo(): warnings.warn(PendingDeprecationWarning("test run")) """.format( mark=mark ) ) @pytest.mark.parametrize("customize_filters", [True, False]) def test_shown_by_default(self, testdir, customize_filters): """Show deprecation warnings by default, even if user has customized the warnings filters (#4013).""" self.create_file(testdir) if customize_filters: testdir.makeini( """ [pytest] filterwarnings = once::UserWarning """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_shown_by_default.py:3: DeprecationWarning: collection", "*test_shown_by_default.py:7: PendingDeprecationWarning: test run", "* 1 passed, 2 warnings*", ] ) def test_hidden_by_ini(self, testdir): self.create_file(testdir) testdir.makeini( """ [pytest] filterwarnings = ignore::DeprecationWarning ignore::PendingDeprecationWarning """ ) result = testdir.runpytest_subprocess() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() def test_hidden_by_mark(self, testdir): """Should hide the deprecation warning from the function, but the warning during collection should be displayed normally. """ self.create_file( testdir, mark='@pytest.mark.filterwarnings("ignore::PendingDeprecationWarning")', ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( [ "*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*test_hidden_by_mark.py:3: DeprecationWarning: collection", "* 1 passed, 1 warnings*", ] ) def test_hidden_by_cmdline(self, testdir): self.create_file(testdir) result = testdir.runpytest_subprocess( "-W", "ignore::DeprecationWarning", "-W", "ignore::PendingDeprecationWarning", ) assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() def test_hidden_by_system(self, testdir, monkeypatch): self.create_file(testdir) monkeypatch.setenv(str("PYTHONWARNINGS"), str("once::UserWarning")) result = testdir.runpytest_subprocess() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() @pytest.mark.skipif(not six.PY2, reason="Python 2 only issue") def test_infinite_loop_warning_against_unicode_usage_py2(testdir): """ We need to be careful when raising the warning about unicode usage with "warnings.warn" because it might be overwritten by users and this itself causes another warning (#3691). """ testdir.makepyfile( """ # -*- coding: utf-8 -*- from __future__ import unicode_literals import warnings import pytest def _custom_showwarning(message, *a, **b): return "WARNING: {}".format(message) warnings.formatwarning = _custom_showwarning @pytest.mark.filterwarnings("default") def test_custom_warning_formatter(): warnings.warn("¥") """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed, * warnings in*"]) @pytest.mark.parametrize("change_default", [None, "ini", "cmdline"]) def test_removed_in_pytest4_warning_as_error(testdir, change_default): testdir.makepyfile( """ import warnings, pytest def test(): warnings.warn(pytest.RemovedInPytest4Warning("some warning")) """ ) if change_default == "ini": testdir.makeini( """ [pytest] filterwarnings = ignore::pytest.RemovedInPytest4Warning """ ) args = ( ("-Wignore::pytest.RemovedInPytest4Warning",) if change_default == "cmdline" else () ) result = testdir.runpytest(*args) if change_default is None: result.stdout.fnmatch_lines(["* 1 failed in *"]) else: assert change_default in ("ini", "cmdline") result.stdout.fnmatch_lines(["* 1 passed in *"]) class TestAssertionWarnings: @staticmethod def assert_result_warns(result, msg): result.stdout.fnmatch_lines(["*PytestAssertRewriteWarning: %s*" % msg]) def test_tuple_warning(self, testdir): testdir.makepyfile( """ def test_foo(): assert (1,2) """ ) result = testdir.runpytest() self.assert_result_warns( result, "assertion is always true, perhaps remove parentheses?" ) @staticmethod def create_file(testdir, return_none): testdir.makepyfile( """ def foo(return_none): if return_none: return None else: return False def test_foo(): assert foo({return_none}) """.format( return_none=return_none ) ) def test_none_function_warns(self, testdir): self.create_file(testdir, True) result = testdir.runpytest() self.assert_result_warns( result, 'asserting the value None, please use "assert is None"' ) def test_assert_is_none_no_warn(self, testdir): testdir.makepyfile( """ def foo(): return None def test_foo(): assert foo() is None """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed in*"]) def test_false_function_no_warn(self, testdir): self.create_file(testdir, False) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 failed in*"]) def test_warnings_checker_twice(): """Issue #4617""" expectation = pytest.warns(UserWarning) with expectation: warnings.warn("Message A", UserWarning) with expectation: warnings.warn("Message B", UserWarning) @pytest.mark.filterwarnings("always") def test_group_warnings_by_message(testdir): testdir.copy_example("warnings/test_group_warnings_by_message.py") result = testdir.runpytest() result.stdout.fnmatch_lines( [ "test_group_warnings_by_message.py::test_foo[0]", "test_group_warnings_by_message.py::test_foo[1]", "test_group_warnings_by_message.py::test_foo[2]", "test_group_warnings_by_message.py::test_foo[3]", "test_group_warnings_by_message.py::test_foo[4]", "test_group_warnings_by_message.py::test_bar", ] ) warning_code = 'warnings.warn(UserWarning("foo"))' assert warning_code in result.stdout.str() assert result.stdout.str().count(warning_code) == 1 pytest-4.6.9/testing/test_nose.py0000664000372000037200000002150713604173400017761 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import pytest def setup_module(mod): mod.nose = pytest.importorskip("nose") def test_nose_setup(testdir): p = testdir.makepyfile( """ values = [] from nose.tools import with_setup @with_setup(lambda: values.append(1), lambda: values.append(2)) def test_hello(): assert values == [1] def test_world(): assert values == [1,2] test_hello.setup = lambda: values.append(1) test_hello.teardown = lambda: values.append(2) """ ) result = testdir.runpytest(p, "-p", "nose") result.assert_outcomes(passed=2) def test_setup_func_with_setup_decorator(): from _pytest.nose import call_optional values = [] class A(object): @pytest.fixture(autouse=True) def f(self): values.append(1) call_optional(A(), "f") assert not values def test_setup_func_not_callable(): from _pytest.nose import call_optional class A(object): f = 1 call_optional(A(), "f") def test_nose_setup_func(testdir): p = testdir.makepyfile( """ from nose.tools import with_setup values = [] def my_setup(): a = 1 values.append(a) def my_teardown(): b = 2 values.append(b) @with_setup(my_setup, my_teardown) def test_hello(): print(values) assert values == [1] def test_world(): print(values) assert values == [1,2] """ ) result = testdir.runpytest(p, "-p", "nose") result.assert_outcomes(passed=2) def test_nose_setup_func_failure(testdir): p = testdir.makepyfile( """ from nose.tools import with_setup values = [] my_setup = lambda x: 1 my_teardown = lambda x: 2 @with_setup(my_setup, my_teardown) def test_hello(): print(values) assert values == [1] def test_world(): print(values) assert values == [1,2] """ ) result = testdir.runpytest(p, "-p", "nose") result.stdout.fnmatch_lines(["*TypeError: ()*"]) def test_nose_setup_func_failure_2(testdir): testdir.makepyfile( """ values = [] my_setup = 1 my_teardown = 2 def test_hello(): assert values == [] test_hello.setup = my_setup test_hello.teardown = my_teardown """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_nose_setup_partial(testdir): pytest.importorskip("functools") p = testdir.makepyfile( """ from functools import partial values = [] def my_setup(x): a = x values.append(a) def my_teardown(x): b = x values.append(b) my_setup_partial = partial(my_setup, 1) my_teardown_partial = partial(my_teardown, 2) def test_hello(): print(values) assert values == [1] def test_world(): print(values) assert values == [1,2] test_hello.setup = my_setup_partial test_hello.teardown = my_teardown_partial """ ) result = testdir.runpytest(p, "-p", "nose") result.stdout.fnmatch_lines(["*2 passed*"]) def test_module_level_setup(testdir): testdir.makepyfile( """ from nose.tools import with_setup items = {} def setup(): items[1]=1 def teardown(): del items[1] def setup2(): items[2] = 2 def teardown2(): del items[2] def test_setup_module_setup(): assert items[1] == 1 @with_setup(setup2, teardown2) def test_local_setup(): assert items[2] == 2 assert 1 not in items """ ) result = testdir.runpytest("-p", "nose") result.stdout.fnmatch_lines(["*2 passed*"]) def test_nose_style_setup_teardown(testdir): testdir.makepyfile( """ values = [] def setup_module(): values.append(1) def teardown_module(): del values[0] def test_hello(): assert values == [1] def test_world(): assert values == [1] """ ) result = testdir.runpytest("-p", "nose") result.stdout.fnmatch_lines(["*2 passed*"]) def test_nose_setup_ordering(testdir): testdir.makepyfile( """ def setup_module(mod): mod.visited = True class TestClass(object): def setup(self): assert visited def test_first(self): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) def test_apiwrapper_problem_issue260(testdir): # this would end up trying a call an optional teardown on the class # for plain unittests we dont want nose behaviour testdir.makepyfile( """ import unittest class TestCase(unittest.TestCase): def setup(self): #should not be called in unittest testcases assert 0, 'setup' def teardown(self): #should not be called in unittest testcases assert 0, 'teardown' def setUp(self): print('setup') def tearDown(self): print('teardown') def test_fun(self): pass """ ) result = testdir.runpytest() result.assert_outcomes(passed=1) def test_setup_teardown_linking_issue265(testdir): # we accidentally didnt integrate nose setupstate with normal setupstate # this test ensures that won't happen again testdir.makepyfile( ''' import pytest class TestGeneric(object): def test_nothing(self): """Tests the API of the implementation (for generic and specialized).""" @pytest.mark.skipif("True", reason= "Skip tests to check if teardown is skipped as well.") class TestSkipTeardown(TestGeneric): def setup(self): """Sets up my specialized implementation for $COOL_PLATFORM.""" raise Exception("should not call setup for skipped tests") def teardown(self): """Undoes the setup.""" raise Exception("should not call teardown for skipped tests") ''' ) reprec = testdir.runpytest() reprec.assert_outcomes(passed=1, skipped=1) def test_SkipTest_during_collection(testdir): p = testdir.makepyfile( """ import nose raise nose.SkipTest("during collection") def test_failing(): assert False """ ) result = testdir.runpytest(p) result.assert_outcomes(skipped=1) def test_SkipTest_in_test(testdir): testdir.makepyfile( """ import nose def test_skipping(): raise nose.SkipTest("in test") """ ) reprec = testdir.inline_run() reprec.assertoutcome(skipped=1) def test_istest_function_decorator(testdir): p = testdir.makepyfile( """ import nose.tools @nose.tools.istest def not_test_prefix(): pass """ ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_nottest_function_decorator(testdir): testdir.makepyfile( """ import nose.tools @nose.tools.nottest def test_prefix(): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_istest_class_decorator(testdir): p = testdir.makepyfile( """ import nose.tools @nose.tools.istest class NotTestPrefix(object): def test_method(self): pass """ ) result = testdir.runpytest(p) result.assert_outcomes(passed=1) def test_nottest_class_decorator(testdir): testdir.makepyfile( """ import nose.tools @nose.tools.nottest class TestPrefix(object): def test_method(self): pass """ ) reprec = testdir.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls def test_skip_test_with_unicode(testdir): testdir.makepyfile( """ # -*- coding: utf-8 -*- import unittest class TestClass(): def test_io(self): raise unittest.SkipTest(u'😊') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["* 1 skipped *"]) pytest-4.6.9/testing/test_conftest.py0000664000372000037200000004632213604173400020644 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import textwrap import py import pytest from _pytest.config import PytestPluginManager from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_OK from _pytest.main import EXIT_USAGEERROR def ConftestWithSetinitial(path): conftest = PytestPluginManager() conftest_setinitial(conftest, [path]) return conftest def conftest_setinitial(conftest, args, confcutdir=None): class Namespace(object): def __init__(self): self.file_or_dir = args self.confcutdir = str(confcutdir) self.noconftest = False self.pyargs = False conftest._set_initial_conftests(Namespace()) @pytest.mark.usefixtures("_sys_snapshot") class TestConftestValueAccessGlobal(object): @pytest.fixture(scope="module", params=["global", "inpackage"]) def basedir(self, request, tmpdir_factory): tmpdir = tmpdir_factory.mktemp("basedir", numbered=True) tmpdir.ensure("adir/conftest.py").write("a=1 ; Directory = 3") tmpdir.ensure("adir/b/conftest.py").write("b=2 ; a = 1.5") if request.param == "inpackage": tmpdir.ensure("adir/__init__.py") tmpdir.ensure("adir/b/__init__.py") yield tmpdir def test_basic_init(self, basedir): conftest = PytestPluginManager() p = basedir.join("adir") assert conftest._rget_with_confmod("a", p)[1] == 1 def test_immediate_initialiation_and_incremental_are_the_same(self, basedir): conftest = PytestPluginManager() assert not len(conftest._dirpath2confmods) conftest._getconftestmodules(basedir) snap1 = len(conftest._dirpath2confmods) assert snap1 == 1 conftest._getconftestmodules(basedir.join("adir")) assert len(conftest._dirpath2confmods) == snap1 + 1 conftest._getconftestmodules(basedir.join("b")) assert len(conftest._dirpath2confmods) == snap1 + 2 def test_value_access_not_existing(self, basedir): conftest = ConftestWithSetinitial(basedir) with pytest.raises(KeyError): conftest._rget_with_confmod("a", basedir) def test_value_access_by_path(self, basedir): conftest = ConftestWithSetinitial(basedir) adir = basedir.join("adir") assert conftest._rget_with_confmod("a", adir)[1] == 1 assert conftest._rget_with_confmod("a", adir.join("b"))[1] == 1.5 def test_value_access_with_confmod(self, basedir): startdir = basedir.join("adir", "b") startdir.ensure("xx", dir=True) conftest = ConftestWithSetinitial(startdir) mod, value = conftest._rget_with_confmod("a", startdir) assert value == 1.5 path = py.path.local(mod.__file__) assert path.dirpath() == basedir.join("adir", "b") assert path.purebasename.startswith("conftest") def test_conftest_in_nonpkg_with_init(tmpdir, _sys_snapshot): tmpdir.ensure("adir-1.0/conftest.py").write("a=1 ; Directory = 3") tmpdir.ensure("adir-1.0/b/conftest.py").write("b=2 ; a = 1.5") tmpdir.ensure("adir-1.0/b/__init__.py") tmpdir.ensure("adir-1.0/__init__.py") ConftestWithSetinitial(tmpdir.join("adir-1.0", "b")) def test_doubledash_considered(testdir): conf = testdir.mkdir("--option") conf.ensure("conftest.py") conftest = PytestPluginManager() conftest_setinitial(conftest, [conf.basename, conf.basename]) values = conftest._getconftestmodules(conf) assert len(values) == 1 def test_issue151_load_all_conftests(testdir): names = "code proj src".split() for name in names: p = testdir.mkdir(name) p.ensure("conftest.py") conftest = PytestPluginManager() conftest_setinitial(conftest, names) d = list(conftest._conftestpath2mod.values()) assert len(d) == len(names) def test_conftest_global_import(testdir): testdir.makeconftest("x=3") p = testdir.makepyfile( """ import py, pytest from _pytest.config import PytestPluginManager conf = PytestPluginManager() mod = conf._importconftest(py.path.local("conftest.py")) assert mod.x == 3 import conftest assert conftest is mod, (conftest, mod) subconf = py.path.local().ensure("sub", "conftest.py") subconf.write("y=4") mod2 = conf._importconftest(subconf) assert mod != mod2 assert mod2.y == 4 import conftest assert conftest is mod2, (conftest, mod) """ ) res = testdir.runpython(p) assert res.ret == 0 def test_conftestcutdir(testdir): conf = testdir.makeconftest("") p = testdir.mkdir("x") conftest = PytestPluginManager() conftest_setinitial(conftest, [testdir.tmpdir], confcutdir=p) values = conftest._getconftestmodules(p) assert len(values) == 0 values = conftest._getconftestmodules(conf.dirpath()) assert len(values) == 0 assert conf not in conftest._conftestpath2mod # but we can still import a conftest directly conftest._importconftest(conf) values = conftest._getconftestmodules(conf.dirpath()) assert values[0].__file__.startswith(str(conf)) # and all sub paths get updated properly values = conftest._getconftestmodules(p) assert len(values) == 1 assert values[0].__file__.startswith(str(conf)) def test_conftestcutdir_inplace_considered(testdir): conf = testdir.makeconftest("") conftest = PytestPluginManager() conftest_setinitial(conftest, [conf.dirpath()], confcutdir=conf.dirpath()) values = conftest._getconftestmodules(conf.dirpath()) assert len(values) == 1 assert values[0].__file__.startswith(str(conf)) @pytest.mark.parametrize("name", "test tests whatever .dotdir".split()) def test_setinitial_conftest_subdirs(testdir, name): sub = testdir.mkdir(name) subconftest = sub.ensure("conftest.py") conftest = PytestPluginManager() conftest_setinitial(conftest, [sub.dirpath()], confcutdir=testdir.tmpdir) if name not in ("whatever", ".dotdir"): assert subconftest in conftest._conftestpath2mod assert len(conftest._conftestpath2mod) == 1 else: assert subconftest not in conftest._conftestpath2mod assert len(conftest._conftestpath2mod) == 0 def test_conftest_confcutdir(testdir): testdir.makeconftest("assert 0") x = testdir.mkdir("x") x.join("conftest.py").write( textwrap.dedent( """\ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") """ ) ) result = testdir.runpytest("-h", "--confcutdir=%s" % x, x) result.stdout.fnmatch_lines(["*--xyz*"]) assert "warning: could not load initial" not in result.stdout.str() @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) def test_conftest_symlink(testdir): """Ensure that conftest.py is used for resolved symlinks.""" real = testdir.tmpdir.mkdir("real") realtests = real.mkdir("app").mkdir("tests") testdir.tmpdir.join("symlinktests").mksymlinkto(realtests) testdir.tmpdir.join("symlink").mksymlinkto(real) testdir.makepyfile( **{ "real/app/tests/test_foo.py": "def test1(fixture): pass", "real/conftest.py": textwrap.dedent( """ import pytest print("conftest_loaded") @pytest.fixture def fixture(): print("fixture_used") """ ), } ) result = testdir.runpytest("-vs", "symlinktests") result.stdout.fnmatch_lines( [ "*conftest_loaded*", "real/app/tests/test_foo.py::test1 fixture_used", "PASSED", ] ) assert result.ret == EXIT_OK # Should not cause "ValueError: Plugin already registered" (#4174). result = testdir.runpytest("-vs", "symlink") assert result.ret == EXIT_OK realtests.ensure("__init__.py") result = testdir.runpytest("-vs", "symlinktests/test_foo.py::test1") result.stdout.fnmatch_lines( [ "*conftest_loaded*", "real/app/tests/test_foo.py::test1 fixture_used", "PASSED", ] ) assert result.ret == EXIT_OK @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) def test_conftest_symlink_files(testdir): """Check conftest.py loading when running in directory with symlinks.""" real = testdir.tmpdir.mkdir("real") source = { "app/test_foo.py": "def test1(fixture): pass", "app/__init__.py": "", "app/conftest.py": textwrap.dedent( """ import pytest print("conftest_loaded") @pytest.fixture def fixture(): print("fixture_used") """ ), } testdir.makepyfile(**{"real/%s" % k: v for k, v in source.items()}) # Create a build directory that contains symlinks to actual files # but doesn't symlink actual directories. build = testdir.tmpdir.mkdir("build") build.mkdir("app") for f in source: build.join(f).mksymlinkto(real.join(f)) build.chdir() result = testdir.runpytest("-vs", "app/test_foo.py") result.stdout.fnmatch_lines(["*conftest_loaded*", "PASSED"]) assert result.ret == EXIT_OK def test_no_conftest(testdir): testdir.makeconftest("assert 0") result = testdir.runpytest("--noconftest") assert result.ret == EXIT_NOTESTSCOLLECTED result = testdir.runpytest() assert result.ret == EXIT_USAGEERROR def test_conftest_existing_resultlog(testdir): x = testdir.mkdir("tests") x.join("conftest.py").write( textwrap.dedent( """\ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") """ ) ) testdir.makefile(ext=".log", result="") # Writes result.log result = testdir.runpytest("-h", "--resultlog", "result.log") result.stdout.fnmatch_lines(["*--xyz*"]) def test_conftest_existing_junitxml(testdir): x = testdir.mkdir("tests") x.join("conftest.py").write( textwrap.dedent( """\ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true") """ ) ) testdir.makefile(ext=".xml", junit="") # Writes junit.xml result = testdir.runpytest("-h", "--junitxml", "junit.xml") result.stdout.fnmatch_lines(["*--xyz*"]) def test_conftest_import_order(testdir, monkeypatch): ct1 = testdir.makeconftest("") sub = testdir.mkdir("sub") ct2 = sub.join("conftest.py") ct2.write("") def impct(p): return p conftest = PytestPluginManager() conftest._confcutdir = testdir.tmpdir monkeypatch.setattr(conftest, "_importconftest", impct) assert conftest._getconftestmodules(sub) == [ct1, ct2] def test_fixture_dependency(testdir, monkeypatch): ct1 = testdir.makeconftest("") ct1 = testdir.makepyfile("__init__.py") ct1.write("") sub = testdir.mkdir("sub") sub.join("__init__.py").write("") sub.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def not_needed(): assert False, "Should not be called!" @pytest.fixture def foo(): assert False, "Should not be called!" @pytest.fixture def bar(foo): return 'bar' """ ) ) subsub = sub.mkdir("subsub") subsub.join("__init__.py").write("") subsub.join("test_bar.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def bar(): return 'sub bar' def test_event_fixture(bar): assert bar == 'sub bar' """ ) ) result = testdir.runpytest("sub") result.stdout.fnmatch_lines(["*1 passed*"]) def test_conftest_found_with_double_dash(testdir): sub = testdir.mkdir("sub") sub.join("conftest.py").write( textwrap.dedent( """\ def pytest_addoption(parser): parser.addoption("--hello-world", action="store_true") """ ) ) p = sub.join("test_hello.py") p.write("def test_hello(): pass") result = testdir.runpytest(str(p) + "::test_hello", "-h") result.stdout.fnmatch_lines( """ *--hello-world* """ ) class TestConftestVisibility(object): def _setup_tree(self, testdir): # for issue616 # example mostly taken from: # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html runner = testdir.mkdir("empty") package = testdir.mkdir("package") package.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def fxtr(): return "from-package" """ ) ) package.join("test_pkgroot.py").write( textwrap.dedent( """\ def test_pkgroot(fxtr): assert fxtr == "from-package" """ ) ) swc = package.mkdir("swc") swc.join("__init__.py").ensure() swc.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def fxtr(): return "from-swc" """ ) ) swc.join("test_with_conftest.py").write( textwrap.dedent( """\ def test_with_conftest(fxtr): assert fxtr == "from-swc" """ ) ) snc = package.mkdir("snc") snc.join("__init__.py").ensure() snc.join("test_no_conftest.py").write( textwrap.dedent( """\ def test_no_conftest(fxtr): assert fxtr == "from-package" # No local conftest.py, so should # use value from parent dir's """ ) ) print("created directory structure:") for x in testdir.tmpdir.visit(): print(" " + x.relto(testdir.tmpdir)) return {"runner": runner, "package": package, "swc": swc, "snc": snc} # N.B.: "swc" stands for "subdir with conftest.py" # "snc" stands for "subdir no [i.e. without] conftest.py" @pytest.mark.parametrize( "chdir,testarg,expect_ntests_passed", [ # Effective target: package/.. ("runner", "..", 3), ("package", "..", 3), ("swc", "../..", 3), ("snc", "../..", 3), # Effective target: package ("runner", "../package", 3), ("package", ".", 3), ("swc", "..", 3), ("snc", "..", 3), # Effective target: package/swc ("runner", "../package/swc", 1), ("package", "./swc", 1), ("swc", ".", 1), ("snc", "../swc", 1), # Effective target: package/snc ("runner", "../package/snc", 1), ("package", "./snc", 1), ("swc", "../snc", 1), ("snc", ".", 1), ], ) def test_parsefactories_relative_node_ids( self, testdir, chdir, testarg, expect_ntests_passed ): """#616""" dirs = self._setup_tree(testdir) print("pytest run in cwd: %s" % (dirs[chdir].relto(testdir.tmpdir))) print("pytestarg : %s" % (testarg)) print("expected pass : %s" % (expect_ntests_passed)) with dirs[chdir].as_cwd(): reprec = testdir.inline_run(testarg, "-q", "--traceconfig") reprec.assertoutcome(passed=expect_ntests_passed) @pytest.mark.parametrize( "confcutdir,passed,error", [(".", 2, 0), ("src", 1, 1), (None, 1, 1)] ) def test_search_conftest_up_to_inifile(testdir, confcutdir, passed, error): """Test that conftest files are detected only up to an ini file, unless an explicit --confcutdir option is given. """ root = testdir.tmpdir src = root.join("src").ensure(dir=1) src.join("pytest.ini").write("[pytest]") src.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def fix1(): pass """ ) ) src.join("test_foo.py").write( textwrap.dedent( """\ def test_1(fix1): pass def test_2(out_of_reach): pass """ ) ) root.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def out_of_reach(): pass """ ) ) args = [str(src)] if confcutdir: args = ["--confcutdir=%s" % root.join(confcutdir)] result = testdir.runpytest(*args) match = "" if passed: match += "*%d passed*" % passed if error: match += "*%d error*" % error result.stdout.fnmatch_lines(match) def test_issue1073_conftest_special_objects(testdir): testdir.makeconftest( """\ class DontTouchMe(object): def __getattr__(self, x): raise Exception('cant touch me') x = DontTouchMe() """ ) testdir.makepyfile( """\ def test_some(): pass """ ) res = testdir.runpytest() assert res.ret == 0 def test_conftest_exception_handling(testdir): testdir.makeconftest( """\ raise ValueError() """ ) testdir.makepyfile( """\ def test_some(): pass """ ) res = testdir.runpytest() assert res.ret == 4 assert "raise ValueError()" in [line.strip() for line in res.errlines] def test_hook_proxy(testdir): """Session's gethookproxy() would cache conftests incorrectly (#2016). It was decided to remove the cache altogether. """ testdir.makepyfile( **{ "root/demo-0/test_foo1.py": "def test1(): pass", "root/demo-a/test_foo2.py": "def test1(): pass", "root/demo-a/conftest.py": """\ def pytest_ignore_collect(path, config): return True """, "root/demo-b/test_foo3.py": "def test1(): pass", "root/demo-c/test_foo4.py": "def test1(): pass", } ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*test_foo1.py*", "*test_foo3.py*", "*test_foo4.py*", "*3 passed*"] ) def test_required_option_help(testdir): testdir.makeconftest("assert 0") x = testdir.mkdir("x") x.join("conftest.py").write( textwrap.dedent( """\ def pytest_addoption(parser): parser.addoption("--xyz", action="store_true", required=True) """ ) ) result = testdir.runpytest("-h", x) assert "argument --xyz is required" not in result.stdout.str() assert "general:" in result.stdout.str() pytest-4.6.9/testing/test_tmpdir.py0000664000372000037200000003153413604173400020315 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import errno import os import stat import sys import attr import six import pytest from _pytest import pathlib from _pytest.pathlib import Path from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG def test_tmpdir_fixture(testdir): p = testdir.copy_example("tmpdir/tmpdir_fixture.py") results = testdir.runpytest(p) results.stdout.fnmatch_lines(["*1 passed*"]) def test_ensuretemp(recwarn): d1 = pytest.ensuretemp("hello") d2 = pytest.ensuretemp("hello") assert d1 == d2 assert d1.check(dir=1) @attr.s class FakeConfig(object): basetemp = attr.ib() trace = attr.ib(default=None) @property def trace(self): return self def get(self, key): return lambda *k: None @property def option(self): return self class TestTempdirHandler(object): def test_mktemp(self, tmp_path): from _pytest.tmpdir import TempdirFactory, TempPathFactory config = FakeConfig(tmp_path) t = TempdirFactory(TempPathFactory.from_config(config)) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp def test_tmppath_relative_basetemp_absolute(self, tmp_path, monkeypatch): """#4425""" from _pytest.tmpdir import TempPathFactory monkeypatch.chdir(tmp_path) config = FakeConfig("hello") t = TempPathFactory.from_config(config) assert t.getbasetemp().resolve() == (tmp_path / "hello").resolve() class TestConfigTmpdir(object): def test_getbasetemp_custom_removes_old(self, testdir): mytemp = testdir.tmpdir.join("xyz") p = testdir.makepyfile( """ def test_1(tmpdir): pass """ ) testdir.runpytest(p, "--basetemp=%s" % mytemp) mytemp.check() mytemp.ensure("hello") testdir.runpytest(p, "--basetemp=%s" % mytemp) mytemp.check() assert not mytemp.join("hello").check() def test_basetemp(testdir): mytemp = testdir.tmpdir.mkdir("mytemp") p = testdir.makepyfile( """ import pytest def test_1(): pytest.ensuretemp("hello") """ ) result = testdir.runpytest(p, "--basetemp=%s" % mytemp, SHOW_PYTEST_WARNINGS_ARG) assert result.ret == 0 assert mytemp.join("hello").check() def test_tmpdir_always_is_realpath(testdir): # the reason why tmpdir should be a realpath is that # when you cd to it and do "os.getcwd()" you will anyway # get the realpath. Using the symlinked path can thus # easily result in path-inequality # XXX if that proves to be a problem, consider using # os.environ["PWD"] realtemp = testdir.tmpdir.mkdir("myrealtemp") linktemp = testdir.tmpdir.join("symlinktemp") attempt_symlink_to(linktemp, str(realtemp)) p = testdir.makepyfile( """ def test_1(tmpdir): import os assert os.path.realpath(str(tmpdir)) == str(tmpdir) """ ) result = testdir.runpytest("-s", p, "--basetemp=%s/bt" % linktemp) assert not result.ret def test_tmp_path_always_is_realpath(testdir, monkeypatch): # for reasoning see: test_tmpdir_always_is_realpath test-case realtemp = testdir.tmpdir.mkdir("myrealtemp") linktemp = testdir.tmpdir.join("symlinktemp") attempt_symlink_to(linktemp, str(realtemp)) monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(linktemp)) testdir.makepyfile( """ def test_1(tmp_path): assert tmp_path.resolve() == tmp_path """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_tmpdir_too_long_on_parametrization(testdir): testdir.makepyfile( """ import pytest @pytest.mark.parametrize("arg", ["1"*1000]) def test_some(arg, tmpdir): tmpdir.ensure("hello") """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_tmpdir_factory(testdir): testdir.makepyfile( """ import pytest @pytest.fixture(scope='session') def session_dir(tmpdir_factory): return tmpdir_factory.mktemp('data', numbered=False) def test_some(session_dir): assert session_dir.isdir() """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_tmpdir_fallback_tox_env(testdir, monkeypatch): """Test that tmpdir works even if environment variables required by getpass module are missing (#1010). """ monkeypatch.delenv("USER", raising=False) monkeypatch.delenv("USERNAME", raising=False) testdir.makepyfile( """ import pytest def test_some(tmpdir): assert tmpdir.isdir() """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.fixture def break_getuser(monkeypatch): monkeypatch.setattr("os.getuid", lambda: -1) # taken from python 2.7/3.4 for envvar in ("LOGNAME", "USER", "LNAME", "USERNAME"): monkeypatch.delenv(envvar, raising=False) @pytest.mark.usefixtures("break_getuser") @pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows") def test_tmpdir_fallback_uid_not_found(testdir): """Test that tmpdir works even if the current process's user id does not correspond to a valid user. """ testdir.makepyfile( """ import pytest def test_some(tmpdir): assert tmpdir.isdir() """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.usefixtures("break_getuser") @pytest.mark.skipif(sys.platform.startswith("win"), reason="no os.getuid on windows") def test_get_user_uid_not_found(): """Test that get_user() function works even if the current process's user id does not correspond to a valid user (e.g. running pytest in a Docker container with 'docker run -u'. """ from _pytest.tmpdir import get_user assert get_user() is None @pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only") def test_get_user(monkeypatch): """Test that get_user() function works even if environment variables required by getpass module are missing from the environment on Windows (#1010). """ from _pytest.tmpdir import get_user monkeypatch.delenv("USER", raising=False) monkeypatch.delenv("USERNAME", raising=False) assert get_user() is None class TestNumberedDir(object): PREFIX = "fun-" def test_make(self, tmp_path): from _pytest.pathlib import make_numbered_dir for i in range(10): d = make_numbered_dir(root=tmp_path, prefix=self.PREFIX) assert d.name.startswith(self.PREFIX) assert d.name.endswith(str(i)) symlink = tmp_path.joinpath(self.PREFIX + "current") if symlink.exists(): # unix assert symlink.is_symlink() assert symlink.resolve() == d.resolve() def test_cleanup_lock_create(self, tmp_path): d = tmp_path.joinpath("test") d.mkdir() from _pytest.pathlib import create_cleanup_lock lockfile = create_cleanup_lock(d) with pytest.raises(EnvironmentError, match="cannot create lockfile in .*"): create_cleanup_lock(d) lockfile.unlink() def test_lock_register_cleanup_removal(self, tmp_path): from _pytest.pathlib import create_cleanup_lock, register_cleanup_lock_removal lock = create_cleanup_lock(tmp_path) registry = [] register_cleanup_lock_removal(lock, register=registry.append) (cleanup_func,) = registry assert lock.is_file() cleanup_func(original_pid="intentionally_different") assert lock.is_file() cleanup_func() assert not lock.exists() cleanup_func() assert not lock.exists() def _do_cleanup(self, tmp_path): self.test_make(tmp_path) from _pytest.pathlib import cleanup_numbered_dir cleanup_numbered_dir( root=tmp_path, prefix=self.PREFIX, keep=2, consider_lock_dead_if_created_before=0, ) def test_cleanup_keep(self, tmp_path): self._do_cleanup(tmp_path) a, b = (x for x in tmp_path.iterdir() if not x.is_symlink()) print(a, b) def test_cleanup_locked(self, tmp_path): from _pytest import pathlib p = pathlib.make_numbered_dir(root=tmp_path, prefix=self.PREFIX) pathlib.create_cleanup_lock(p) assert not pathlib.ensure_deletable( p, consider_lock_dead_if_created_before=p.stat().st_mtime - 1 ) assert pathlib.ensure_deletable( p, consider_lock_dead_if_created_before=p.stat().st_mtime + 1 ) def test_cleanup_ignores_symlink(self, tmp_path): the_symlink = tmp_path / (self.PREFIX + "current") attempt_symlink_to(the_symlink, tmp_path / (self.PREFIX + "5")) self._do_cleanup(tmp_path) def test_removal_accepts_lock(self, tmp_path): folder = pathlib.make_numbered_dir(root=tmp_path, prefix=self.PREFIX) pathlib.create_cleanup_lock(folder) pathlib.maybe_delete_a_numbered_dir(folder) assert folder.is_dir() class TestRmRf: def test_rm_rf(self, tmp_path): from _pytest.pathlib import rm_rf adir = tmp_path / "adir" adir.mkdir() rm_rf(adir) assert not adir.exists() adir.mkdir() afile = adir / "afile" afile.write_bytes(b"aa") rm_rf(adir) assert not adir.exists() def test_rm_rf_with_read_only_file(self, tmp_path): """Ensure rm_rf can remove directories with read-only files in them (#5524)""" from _pytest.pathlib import rm_rf fn = tmp_path / "dir/foo.txt" fn.parent.mkdir() fn.touch() self.chmod_r(fn) rm_rf(fn.parent) assert not fn.parent.is_dir() def chmod_r(self, path): mode = os.stat(str(path)).st_mode os.chmod(str(path), mode & ~stat.S_IWRITE) def test_rm_rf_with_read_only_directory(self, tmp_path): """Ensure rm_rf can remove read-only directories (#5524)""" from _pytest.pathlib import rm_rf adir = tmp_path / "dir" adir.mkdir() (adir / "foo.txt").touch() self.chmod_r(adir) rm_rf(adir) assert not adir.is_dir() def test_on_rm_rf_error(self, tmp_path): from _pytest.pathlib import on_rm_rf_error adir = tmp_path / "dir" adir.mkdir() fn = adir / "foo.txt" fn.touch() self.chmod_r(fn) # unknown exception with pytest.warns(pytest.PytestWarning): exc_info = (None, RuntimeError(), None) on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path) assert fn.is_file() # we ignore FileNotFoundError file_not_found = OSError() file_not_found.errno = errno.ENOENT exc_info = (None, file_not_found, None) assert not on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) permission_error = OSError() permission_error.errno = errno.EACCES # unknown function with pytest.warns(pytest.PytestWarning): exc_info = (None, permission_error, None) on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) assert fn.is_file() exc_info = (None, permission_error, None) on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path) assert not fn.is_file() def attempt_symlink_to(path, to_path): """Try to make a symlink from "path" to "to_path", skipping in case this platform does not support it or we don't have sufficient privileges (common on Windows).""" if sys.platform.startswith("win") and six.PY2: pytest.skip("pathlib for some reason cannot make symlinks on Python 2") try: Path(path).symlink_to(Path(to_path)) except OSError: pytest.skip("could not create symbolic link") def test_tmpdir_equals_tmp_path(tmpdir, tmp_path): assert Path(tmpdir) == tmp_path def test_basetemp_with_read_only_files(testdir): """Integration test for #5524""" testdir.makepyfile( """ import os import stat def test(tmp_path): fn = tmp_path / 'foo.txt' fn.write_text(u'hello') mode = os.stat(str(fn)).st_mode os.chmod(str(fn), mode & ~stat.S_IREAD) """ ) result = testdir.runpytest("--basetemp=tmp") assert result.ret == 0 # running a second time and ensure we don't crash result = testdir.runpytest("--basetemp=tmp") assert result.ret == 0 pytest-4.6.9/testing/test_config.py0000664000372000037200000011774213604173400020271 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import textwrap import _pytest._code import pytest from _pytest.compat import importlib_metadata from _pytest.config import _iter_rewritable_modules from _pytest.config.exceptions import UsageError from _pytest.config.findpaths import determine_setup from _pytest.config.findpaths import get_common_ancestor from _pytest.config.findpaths import getcfg from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_OK from _pytest.main import EXIT_TESTSFAILED from _pytest.main import EXIT_USAGEERROR class TestParseIni(object): @pytest.mark.parametrize( "section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")] ) def test_getcfg_and_config(self, testdir, tmpdir, section, filename): sub = tmpdir.mkdir("sub") sub.chdir() tmpdir.join(filename).write( textwrap.dedent( """\ [{section}] name = value """.format( section=section ) ) ) rootdir, inifile, cfg = getcfg([sub]) assert cfg["name"] == "value" config = testdir.parseconfigure(sub) assert config.inicfg["name"] == "value" def test_getcfg_empty_path(self): """correctly handle zero length arguments (a la pytest '')""" getcfg([""]) def test_setupcfg_uses_toolpytest_with_pytest(self, testdir): p1 = testdir.makepyfile("def test(): pass") testdir.makefile( ".cfg", setup=""" [tool:pytest] testpaths=%s [pytest] testpaths=ignored """ % p1.basename, ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*, inifile: setup.cfg, *", "* 1 passed in *"]) assert result.ret == 0 def test_append_parse_args(self, testdir, tmpdir, monkeypatch): monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"') tmpdir.join("pytest.ini").write( textwrap.dedent( """\ [pytest] addopts = --verbose """ ) ) config = testdir.parseconfig(tmpdir) assert config.option.color == "no" assert config.option.reportchars == "s" assert config.option.tbstyle == "short" assert config.option.verbose def test_tox_ini_wrong_version(self, testdir): testdir.makefile( ".ini", tox=""" [pytest] minversion=9.0 """, ) result = testdir.runpytest() assert result.ret != 0 result.stderr.fnmatch_lines(["*tox.ini:2*requires*9.0*actual*"]) @pytest.mark.parametrize( "section, name", [("tool:pytest", "setup.cfg"), ("pytest", "tox.ini"), ("pytest", "pytest.ini")], ) def test_ini_names(self, testdir, name, section): testdir.tmpdir.join(name).write( textwrap.dedent( """ [{section}] minversion = 1.0 """.format( section=section ) ) ) config = testdir.parseconfig() assert config.getini("minversion") == "1.0" def test_toxini_before_lower_pytestini(self, testdir): sub = testdir.tmpdir.mkdir("sub") sub.join("tox.ini").write( textwrap.dedent( """ [pytest] minversion = 2.0 """ ) ) testdir.tmpdir.join("pytest.ini").write( textwrap.dedent( """ [pytest] minversion = 1.5 """ ) ) config = testdir.parseconfigure(sub) assert config.getini("minversion") == "2.0" def test_ini_parse_error(self, testdir): testdir.tmpdir.join("pytest.ini").write("addopts = -x") result = testdir.runpytest() assert result.ret != 0 result.stderr.fnmatch_lines(["ERROR: *pytest.ini:1: no section header defined"]) @pytest.mark.xfail(reason="probably not needed") def test_confcutdir(self, testdir): sub = testdir.mkdir("sub") sub.chdir() testdir.makeini( """ [pytest] addopts = --qwe """ ) result = testdir.inline_run("--confcutdir=.") assert result.ret == 0 class TestConfigCmdlineParsing(object): def test_parsing_again_fails(self, testdir): config = testdir.parseconfig() pytest.raises(AssertionError, lambda: config.parse([])) def test_explicitly_specified_config_file_is_loaded(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("custom", "") """ ) testdir.makeini( """ [pytest] custom = 0 """ ) testdir.makefile( ".ini", custom=""" [pytest] custom = 1 """, ) config = testdir.parseconfig("-c", "custom.ini") assert config.getini("custom") == "1" testdir.makefile( ".cfg", custom_tool_pytest_section=""" [tool:pytest] custom = 1 """, ) config = testdir.parseconfig("-c", "custom_tool_pytest_section.cfg") assert config.getini("custom") == "1" def test_absolute_win32_path(self, testdir): temp_ini_file = testdir.makefile( ".ini", custom=""" [pytest] addopts = --version """, ) from os.path import normpath temp_ini_file = normpath(str(temp_ini_file)) ret = pytest.main(["-c", temp_ini_file]) assert ret == _pytest.main.EXIT_OK class TestConfigAPI(object): def test_config_trace(self, testdir): config = testdir.parseconfig() values = [] config.trace.root.setwriter(values.append) config.trace("hello") assert len(values) == 1 assert values[0] == "hello [config]\n" def test_config_getoption(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addoption("--hello", "-X", dest="hello") """ ) config = testdir.parseconfig("--hello=this") for x in ("hello", "--hello", "-X"): assert config.getoption(x) == "this" pytest.raises(ValueError, config.getoption, "qweqwe") @pytest.mark.skipif("sys.version_info[0] < 3") def test_config_getoption_unicode(self, testdir): testdir.makeconftest( """ from __future__ import unicode_literals def pytest_addoption(parser): parser.addoption('--hello', type=str) """ ) config = testdir.parseconfig("--hello=this") assert config.getoption("hello") == "this" def test_config_getvalueorskip(self, testdir): config = testdir.parseconfig() pytest.raises(pytest.skip.Exception, config.getvalueorskip, "hello") verbose = config.getvalueorskip("verbose") assert verbose == config.option.verbose def test_config_getvalueorskip_None(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addoption("--hello") """ ) config = testdir.parseconfig() with pytest.raises(pytest.skip.Exception): config.getvalueorskip("hello") def test_getoption(self, testdir): config = testdir.parseconfig() with pytest.raises(ValueError): config.getvalue("x") assert config.getoption("x", 1) == 1 def test_getconftest_pathlist(self, testdir, tmpdir): somepath = tmpdir.join("x", "y", "z") p = tmpdir.join("conftest.py") p.write("pathlist = ['.', %r]" % str(somepath)) config = testdir.parseconfigure(p) assert config._getconftest_pathlist("notexist", path=tmpdir) is None pl = config._getconftest_pathlist("pathlist", path=tmpdir) print(pl) assert len(pl) == 2 assert pl[0] == tmpdir assert pl[1] == somepath def test_addini(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("myname", "my new ini value") """ ) testdir.makeini( """ [pytest] myname=hello """ ) config = testdir.parseconfig() val = config.getini("myname") assert val == "hello" pytest.raises(ValueError, config.getini, "other") def test_addini_pathlist(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("paths", "my new ini value", type="pathlist") parser.addini("abc", "abc value") """ ) p = testdir.makeini( """ [pytest] paths=hello world/sub.py """ ) config = testdir.parseconfig() values = config.getini("paths") assert len(values) == 2 assert values[0] == p.dirpath("hello") assert values[1] == p.dirpath("world/sub.py") pytest.raises(ValueError, config.getini, "other") def test_addini_args(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("args", "new args", type="args") parser.addini("a2", "", "args", default="1 2 3".split()) """ ) testdir.makeini( """ [pytest] args=123 "123 hello" "this" """ ) config = testdir.parseconfig() values = config.getini("args") assert len(values) == 3 assert values == ["123", "123 hello", "this"] values = config.getini("a2") assert values == list("123") def test_addini_linelist(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") parser.addini("a2", "", "linelist") """ ) testdir.makeini( """ [pytest] xy= 123 345 second line """ ) config = testdir.parseconfig() values = config.getini("xy") assert len(values) == 2 assert values == ["123 345", "second line"] values = config.getini("a2") assert values == [] @pytest.mark.parametrize( "str_val, bool_val", [("True", True), ("no", False), ("no-ini", True)] ) def test_addini_bool(self, testdir, str_val, bool_val): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("strip", "", type="bool", default=True) """ ) if str_val != "no-ini": testdir.makeini( """ [pytest] strip=%s """ % str_val ) config = testdir.parseconfig() assert config.getini("strip") is bool_val def test_addinivalue_line_existing(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") """ ) testdir.makeini( """ [pytest] xy= 123 """ ) config = testdir.parseconfig() values = config.getini("xy") assert len(values) == 1 assert values == ["123"] config.addinivalue_line("xy", "456") values = config.getini("xy") assert len(values) == 2 assert values == ["123", "456"] def test_addinivalue_line_new(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("xy", "", type="linelist") """ ) config = testdir.parseconfig() assert not config.getini("xy") config.addinivalue_line("xy", "456") values = config.getini("xy") assert len(values) == 1 assert values == ["456"] config.addinivalue_line("xy", "123") values = config.getini("xy") assert len(values) == 2 assert values == ["456", "123"] def test_confcutdir_check_isdir(self, testdir): """Give an error if --confcutdir is not a valid directory (#2078)""" with pytest.raises(pytest.UsageError): testdir.parseconfig( "--confcutdir", testdir.tmpdir.join("file").ensure(file=1) ) with pytest.raises(pytest.UsageError): testdir.parseconfig("--confcutdir", testdir.tmpdir.join("inexistant")) config = testdir.parseconfig( "--confcutdir", testdir.tmpdir.join("dir").ensure(dir=1) ) assert config.getoption("confcutdir") == str(testdir.tmpdir.join("dir")) @pytest.mark.parametrize( "names, expected", [ # dist-info based distributions root are files as will be put in PYTHONPATH (["bar.py"], ["bar"]), (["foo/bar.py"], ["bar"]), (["foo/bar.pyc"], []), (["foo/__init__.py"], ["foo"]), (["bar/__init__.py", "xz.py"], ["bar", "xz"]), (["setup.py"], []), # egg based distributions root contain the files from the dist root (["src/bar/__init__.py"], ["bar"]), (["src/bar/__init__.py", "setup.py"], ["bar"]), (["source/python/bar/__init__.py", "setup.py"], ["bar"]), ], ) def test_iter_rewritable_modules(self, names, expected): assert list(_iter_rewritable_modules(names)) == expected class TestConfigFromdictargs(object): def test_basic_behavior(self, _sys_snapshot): from _pytest.config import Config option_dict = {"verbose": 444, "foo": "bar", "capture": "no"} args = ["a", "b"] config = Config.fromdictargs(option_dict, args) with pytest.raises(AssertionError): config.parse(["should refuse to parse again"]) assert config.option.verbose == 444 assert config.option.foo == "bar" assert config.option.capture == "no" assert config.args == args def test_origargs(self, _sys_snapshot): """Show that fromdictargs can handle args in their "orig" format""" from _pytest.config import Config option_dict = {} args = ["-vvvv", "-s", "a", "b"] config = Config.fromdictargs(option_dict, args) assert config.args == ["a", "b"] assert config._origargs == args assert config.option.verbose == 4 assert config.option.capture == "no" def test_inifilename(self, tmpdir): tmpdir.join("foo/bar.ini").ensure().write( textwrap.dedent( """\ [pytest] name = value """ ) ) from _pytest.config import Config inifile = "../../foo/bar.ini" option_dict = {"inifilename": inifile, "capture": "no"} cwd = tmpdir.join("a/b") cwd.join("pytest.ini").ensure().write( textwrap.dedent( """\ [pytest] name = wrong-value should_not_be_set = true """ ) ) with cwd.ensure(dir=True).as_cwd(): config = Config.fromdictargs(option_dict, ()) assert config.args == [str(cwd)] assert config.option.inifilename == inifile assert config.option.capture == "no" # this indicates this is the file used for getting configuration values assert config.inifile == inifile assert config.inicfg.get("name") == "value" assert config.inicfg.get("should_not_be_set") is None def test_options_on_small_file_do_not_blow_up(testdir): def runfiletest(opts): reprec = testdir.inline_run(*opts) passed, skipped, failed = reprec.countoutcomes() assert failed == 2 assert skipped == passed == 0 path = testdir.makepyfile( """ def test_f1(): assert 0 def test_f2(): assert 0 """ ) for opts in ( [], ["-l"], ["-s"], ["--tb=no"], ["--tb=short"], ["--tb=long"], ["--fulltrace"], ["--traceconfig"], ["-v"], ["-v", "-v"], ): runfiletest(opts + [path]) def test_preparse_ordering_with_setuptools(testdir, monkeypatch): monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) class EntryPoint(object): name = "mytestplugin" group = "pytest11" def load(self): class PseudoPlugin(object): x = 42 return PseudoPlugin() class Dist(object): files = () entry_points = (EntryPoint(),) def my_dists(): return (Dist,) monkeypatch.setattr(importlib_metadata, "distributions", my_dists) testdir.makeconftest( """ pytest_plugins = "mytestplugin", """ ) monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin") config = testdir.parseconfig() plugin = config.pluginmanager.getplugin("mytestplugin") assert plugin.x == 42 def test_setuptools_importerror_issue1479(testdir, monkeypatch): monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) class DummyEntryPoint(object): name = "mytestplugin" group = "pytest11" def load(self): raise ImportError("Don't hide me!") class Distribution(object): version = "1.0" files = ("foo.txt",) entry_points = (DummyEntryPoint(),) def distributions(): return (Distribution(),) monkeypatch.setattr(importlib_metadata, "distributions", distributions) with pytest.raises(ImportError): testdir.parseconfig() def test_importlib_metadata_broken_distribution(testdir, monkeypatch): """Integration test for broken distributions with 'files' metadata being None (#5389)""" monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) class DummyEntryPoint: name = "mytestplugin" group = "pytest11" def load(self): return object() class Distribution: version = "1.0" files = None entry_points = (DummyEntryPoint(),) def distributions(): return (Distribution(),) monkeypatch.setattr(importlib_metadata, "distributions", distributions) testdir.parseconfig() @pytest.mark.parametrize("block_it", [True, False]) def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch, block_it): monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) plugin_module_placeholder = object() class DummyEntryPoint(object): name = "mytestplugin" group = "pytest11" def load(self): return plugin_module_placeholder class Distribution(object): version = "1.0" files = ("foo.txt",) entry_points = (DummyEntryPoint(),) def distributions(): return (Distribution(),) monkeypatch.setattr(importlib_metadata, "distributions", distributions) args = ("-p", "no:mytestplugin") if block_it else () config = testdir.parseconfig(*args) config.pluginmanager.import_plugin("mytestplugin") if block_it: assert "mytestplugin" not in sys.modules assert config.pluginmanager.get_plugin("mytestplugin") is None else: assert ( config.pluginmanager.get_plugin("mytestplugin") is plugin_module_placeholder ) @pytest.mark.parametrize( "parse_args,should_load", [(("-p", "mytestplugin"), True), ((), False)] ) def test_disable_plugin_autoload(testdir, monkeypatch, parse_args, should_load): class DummyEntryPoint(object): project_name = name = "mytestplugin" group = "pytest11" version = "1.0" def load(self): return sys.modules[self.name] class Distribution(object): entry_points = (DummyEntryPoint(),) files = () class PseudoPlugin(object): x = 42 def distributions(): return (Distribution(),) monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") monkeypatch.setattr(importlib_metadata, "distributions", distributions) monkeypatch.setitem(sys.modules, "mytestplugin", PseudoPlugin()) config = testdir.parseconfig(*parse_args) has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None assert has_loaded == should_load def test_cmdline_processargs_simple(testdir): testdir.makeconftest( """ def pytest_cmdline_preparse(args): args.append("-h") """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*pytest*", "*-h*"]) def test_invalid_options_show_extra_information(testdir): """display extra information when pytest exits due to unrecognized options in the command-line""" testdir.makeini( """ [pytest] addopts = --invalid-option """ ) result = testdir.runpytest() result.stderr.fnmatch_lines( [ "*error: unrecognized arguments: --invalid-option*", "* inifile: %s*" % testdir.tmpdir.join("tox.ini"), "* rootdir: %s*" % testdir.tmpdir, ] ) @pytest.mark.parametrize( "args", [ ["dir1", "dir2", "-v"], ["dir1", "-v", "dir2"], ["dir2", "-v", "dir1"], ["-v", "dir2", "dir1"], ], ) def test_consider_args_after_options_for_rootdir(testdir, args): """ Consider all arguments in the command-line for rootdir discovery, even if they happen to occur after an option. #949 """ # replace "dir1" and "dir2" from "args" into their real directory root = testdir.tmpdir.mkdir("myroot") d1 = root.mkdir("dir1") d2 = root.mkdir("dir2") for i, arg in enumerate(args): if arg == "dir1": args[i] = d1 elif arg == "dir2": args[i] = d2 with root.as_cwd(): result = testdir.runpytest(*args) result.stdout.fnmatch_lines(["*rootdir: *myroot"]) @pytest.mark.skipif("sys.platform == 'win32'") def test_toolongargs_issue224(testdir): result = testdir.runpytest("-m", "hello" * 500) assert result.ret == EXIT_NOTESTSCOLLECTED def test_config_in_subdirectory_colon_command_line_issue2148(testdir): conftest_source = """ def pytest_addoption(parser): parser.addini('foo', 'foo') """ testdir.makefile( ".ini", **{"pytest": "[pytest]\nfoo = root", "subdir/pytest": "[pytest]\nfoo = subdir"} ) testdir.makepyfile( **{ "conftest": conftest_source, "subdir/conftest": conftest_source, "subdir/test_foo": """\ def test_foo(pytestconfig): assert pytestconfig.getini('foo') == 'subdir' """, } ) result = testdir.runpytest("subdir/test_foo.py::test_foo") assert result.ret == 0 def test_notify_exception(testdir, capfd): config = testdir.parseconfig() with pytest.raises(ValueError) as excinfo: raise ValueError(1) config.notify_exception(excinfo, config.option) out, err = capfd.readouterr() assert "ValueError" in err class A(object): def pytest_internalerror(self, excrepr): return True config.pluginmanager.register(A()) config.notify_exception(excinfo, config.option) out, err = capfd.readouterr() assert not err config = testdir.parseconfig("-p", "no:terminal") with pytest.raises(ValueError) as excinfo: raise ValueError(1) config.notify_exception(excinfo, config.option) out, err = capfd.readouterr() assert "ValueError" in err def test_no_terminal_discovery_error(testdir): testdir.makepyfile("raise TypeError('oops!')") result = testdir.runpytest("-p", "no:terminal", "--collect-only") assert result.ret == EXIT_INTERRUPTED def test_load_initial_conftest_last_ordering(testdir, _config_for_test): pm = _config_for_test.pluginmanager class My(object): def pytest_load_initial_conftests(self): pass m = My() pm.register(m) hc = pm.hook.pytest_load_initial_conftests values = hc._nonwrappers + hc._wrappers expected = ["_pytest.config", "test_config", "_pytest.capture"] assert [x.function.__module__ for x in values] == expected def test_get_plugin_specs_as_list(): from _pytest.config import _get_plugin_specs_as_list with pytest.raises(pytest.UsageError): _get_plugin_specs_as_list({"foo"}) with pytest.raises(pytest.UsageError): _get_plugin_specs_as_list(dict()) assert _get_plugin_specs_as_list(None) == [] assert _get_plugin_specs_as_list("") == [] assert _get_plugin_specs_as_list("foo") == ["foo"] assert _get_plugin_specs_as_list("foo,bar") == ["foo", "bar"] assert _get_plugin_specs_as_list(["foo", "bar"]) == ["foo", "bar"] assert _get_plugin_specs_as_list(("foo", "bar")) == ["foo", "bar"] def test_collect_pytest_prefix_bug_integration(testdir): """Integration test for issue #3775""" p = testdir.copy_example("config/collect_pytest_prefix") result = testdir.runpytest(p) result.stdout.fnmatch_lines(["* 1 passed *"]) def test_collect_pytest_prefix_bug(pytestconfig): """Ensure we collect only actual functions from conftest files (#3775)""" class Dummy(object): class pytest_something(object): pass pm = pytestconfig.pluginmanager assert pm.parse_hookimpl_opts(Dummy(), "pytest_something") is None class TestRootdir(object): def test_simple_noini(self, tmpdir): assert get_common_ancestor([tmpdir]) == tmpdir a = tmpdir.mkdir("a") assert get_common_ancestor([a, tmpdir]) == tmpdir assert get_common_ancestor([tmpdir, a]) == tmpdir with tmpdir.as_cwd(): assert get_common_ancestor([]) == tmpdir no_path = tmpdir.join("does-not-exist") assert get_common_ancestor([no_path]) == tmpdir assert get_common_ancestor([no_path.join("a")]) == tmpdir @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) def test_with_ini(self, tmpdir, name): inifile = tmpdir.join(name) inifile.write("[pytest]\n" if name != "setup.cfg" else "[tool:pytest]\n") a = tmpdir.mkdir("a") b = a.mkdir("b") for args in ([tmpdir], [a], [b]): rootdir, inifile, inicfg = determine_setup(None, args) assert rootdir == tmpdir assert inifile == inifile rootdir, inifile, inicfg = determine_setup(None, [b, a]) assert rootdir == tmpdir assert inifile == inifile @pytest.mark.parametrize("name", "setup.cfg tox.ini".split()) def test_pytestini_overrides_empty_other(self, tmpdir, name): inifile = tmpdir.ensure("pytest.ini") a = tmpdir.mkdir("a") a.ensure(name) rootdir, inifile, inicfg = determine_setup(None, [a]) assert rootdir == tmpdir assert inifile == inifile def test_setuppy_fallback(self, tmpdir): a = tmpdir.mkdir("a") a.ensure("setup.cfg") tmpdir.ensure("setup.py") rootdir, inifile, inicfg = determine_setup(None, [a]) assert rootdir == tmpdir assert inifile is None assert inicfg == {} def test_nothing(self, tmpdir, monkeypatch): monkeypatch.chdir(str(tmpdir)) rootdir, inifile, inicfg = determine_setup(None, [tmpdir]) assert rootdir == tmpdir assert inifile is None assert inicfg == {} def test_with_specific_inifile(self, tmpdir): inifile = tmpdir.ensure("pytest.ini") rootdir, inifile, inicfg = determine_setup(inifile, [tmpdir]) assert rootdir == tmpdir class TestOverrideIniArgs(object): @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) def test_override_ini_names(self, testdir, name): section = "[pytest]" if name != "setup.cfg" else "[tool:pytest]" testdir.tmpdir.join(name).write( textwrap.dedent( """ {section} custom = 1.0""".format( section=section ) ) ) testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("custom", "")""" ) testdir.makepyfile( """ def test_pass(pytestconfig): ini_val = pytestconfig.getini("custom") print('\\ncustom_option:%s\\n' % ini_val)""" ) result = testdir.runpytest("--override-ini", "custom=2.0", "-s") assert result.ret == 0 result.stdout.fnmatch_lines(["custom_option:2.0"]) result = testdir.runpytest( "--override-ini", "custom=2.0", "--override-ini=custom=3.0", "-s" ) assert result.ret == 0 result.stdout.fnmatch_lines(["custom_option:3.0"]) def test_override_ini_pathlist(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): parser.addini("paths", "my new ini value", type="pathlist")""" ) testdir.makeini( """ [pytest] paths=blah.py""" ) testdir.makepyfile( """ import py.path def test_pathlist(pytestconfig): config_paths = pytestconfig.getini("paths") print(config_paths) for cpf in config_paths: print('\\nuser_path:%s' % cpf.basename)""" ) result = testdir.runpytest( "--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s" ) result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"]) def test_override_multiple_and_default(self, testdir): testdir.makeconftest( """ def pytest_addoption(parser): addini = parser.addini addini("custom_option_1", "", default="o1") addini("custom_option_2", "", default="o2") addini("custom_option_3", "", default=False, type="bool") addini("custom_option_4", "", default=True, type="bool")""" ) testdir.makeini( """ [pytest] custom_option_1=custom_option_1 custom_option_2=custom_option_2 """ ) testdir.makepyfile( """ def test_multiple_options(pytestconfig): prefix = "custom_option" for x in range(1, 5): ini_value=pytestconfig.getini("%s_%d" % (prefix, x)) print('\\nini%d:%s' % (x, ini_value)) """ ) result = testdir.runpytest( "--override-ini", "custom_option_1=fulldir=/tmp/user1", "-o", "custom_option_2=url=/tmp/user2?a=b&d=e", "-o", "custom_option_3=True", "-o", "custom_option_4=no", "-s", ) result.stdout.fnmatch_lines( [ "ini1:fulldir=/tmp/user1", "ini2:url=/tmp/user2?a=b&d=e", "ini3:True", "ini4:False", ] ) def test_override_ini_usage_error_bad_style(self, testdir): testdir.makeini( """ [pytest] xdist_strict=False """ ) result = testdir.runpytest("--override-ini", "xdist_strict True", "-s") result.stderr.fnmatch_lines(["*ERROR* *expects option=value*"]) @pytest.mark.parametrize("with_ini", [True, False]) def test_override_ini_handled_asap(self, testdir, with_ini): """-o should be handled as soon as possible and always override what's in ini files (#2238)""" if with_ini: testdir.makeini( """ [pytest] python_files=test_*.py """ ) testdir.makepyfile( unittest_ini_handle=""" def test(): pass """ ) result = testdir.runpytest("--override-ini", "python_files=unittest_*.py") result.stdout.fnmatch_lines(["*1 passed in*"]) def test_with_arg_outside_cwd_without_inifile(self, tmpdir, monkeypatch): monkeypatch.chdir(str(tmpdir)) a = tmpdir.mkdir("a") b = tmpdir.mkdir("b") rootdir, inifile, inicfg = determine_setup(None, [a, b]) assert rootdir == tmpdir assert inifile is None def test_with_arg_outside_cwd_with_inifile(self, tmpdir): a = tmpdir.mkdir("a") b = tmpdir.mkdir("b") inifile = a.ensure("pytest.ini") rootdir, parsed_inifile, inicfg = determine_setup(None, [a, b]) assert rootdir == a assert inifile == parsed_inifile @pytest.mark.parametrize("dirs", ([], ["does-not-exist"], ["a/does-not-exist"])) def test_with_non_dir_arg(self, dirs, tmpdir): with tmpdir.ensure(dir=True).as_cwd(): rootdir, inifile, inicfg = determine_setup(None, dirs) assert rootdir == tmpdir assert inifile is None def test_with_existing_file_in_subdir(self, tmpdir): a = tmpdir.mkdir("a") a.ensure("exist") with tmpdir.as_cwd(): rootdir, inifile, inicfg = determine_setup(None, ["a/exist"]) assert rootdir == tmpdir assert inifile is None def test_addopts_before_initini(self, monkeypatch, _config_for_test, _sys_snapshot): cache_dir = ".custom_cache" monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir) config = _config_for_test config._preparse([], addopts=True) assert config._override_ini == ["cache_dir=%s" % cache_dir] def test_addopts_from_env_not_concatenated(self, monkeypatch, _config_for_test): """PYTEST_ADDOPTS should not take values from normal args (#4265).""" monkeypatch.setenv("PYTEST_ADDOPTS", "-o") config = _config_for_test with pytest.raises(UsageError) as excinfo: config._preparse(["cache_dir=ignored"], addopts=True) assert ( "error: argument -o/--override-ini: expected one argument (via PYTEST_ADDOPTS)" in excinfo.value.args[0] ) def test_addopts_from_ini_not_concatenated(self, testdir): """addopts from ini should not take values from normal args (#4265).""" testdir.makeini( """ [pytest] addopts=-o """ ) result = testdir.runpytest("cache_dir=ignored") result.stderr.fnmatch_lines( [ "%s: error: argument -o/--override-ini: expected one argument (via addopts config)" % (testdir.request.config._parser.optparser.prog,) ] ) assert result.ret == _pytest.main.EXIT_USAGEERROR def test_override_ini_does_not_contain_paths(self, _config_for_test, _sys_snapshot): """Check that -o no longer swallows all options after it (#3103)""" config = _config_for_test config._preparse(["-o", "cache_dir=/cache", "/some/test/path"]) assert config._override_ini == ["cache_dir=/cache"] def test_multiple_override_ini_options(self, testdir, request): """Ensure a file path following a '-o' option does not generate an error (#3103)""" testdir.makepyfile( **{ "conftest.py": """ def pytest_addoption(parser): parser.addini('foo', default=None, help='some option') parser.addini('bar', default=None, help='some option') """, "test_foo.py": """ def test(pytestconfig): assert pytestconfig.getini('foo') == '1' assert pytestconfig.getini('bar') == '0' """, "test_bar.py": """ def test(): assert False """, } ) result = testdir.runpytest("-o", "foo=1", "-o", "bar=0", "test_foo.py") assert "ERROR:" not in result.stderr.str() result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="]) def test_help_via_addopts(testdir): testdir.makeini( """ [pytest] addopts = --unknown-option-should-allow-for-help --help """ ) result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines( [ "usage: *", "positional arguments:", # Displays full/default help. "to see available markers type: pytest --markers", ] ) def test_help_and_version_after_argument_error(testdir): testdir.makeconftest( """ def validate(arg): raise argparse.ArgumentTypeError("argerror") def pytest_addoption(parser): group = parser.getgroup('cov') group.addoption( "--invalid-option-should-allow-for-help", type=validate, ) """ ) testdir.makeini( """ [pytest] addopts = --invalid-option-should-allow-for-help """ ) result = testdir.runpytest("--help") result.stdout.fnmatch_lines( [ "usage: *", "positional arguments:", "NOTE: displaying only minimal help due to UsageError.", ] ) result.stderr.fnmatch_lines( [ "ERROR: usage: *", "%s: error: argument --invalid-option-should-allow-for-help: expected one argument" % (testdir.request.config._parser.optparser.prog,), ] ) # Does not display full/default help. assert "to see available markers type: pytest --markers" not in result.stdout.lines assert result.ret == EXIT_USAGEERROR result = testdir.runpytest("--version") result.stderr.fnmatch_lines( ["*pytest*{}*imported from*".format(pytest.__version__)] ) assert result.ret == EXIT_USAGEERROR def test_config_does_not_load_blocked_plugin_from_args(testdir): """This tests that pytest's config setup handles "-p no:X".""" p = testdir.makepyfile("def test(capfd): pass") result = testdir.runpytest(str(p), "-pno:capture") result.stdout.fnmatch_lines(["E fixture 'capfd' not found"]) assert result.ret == EXIT_TESTSFAILED result = testdir.runpytest(str(p), "-pno:capture", "-s") result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"]) assert result.ret == EXIT_USAGEERROR @pytest.mark.parametrize( "plugin", [ x for x in _pytest.config.default_plugins if x not in _pytest.config.essential_plugins ], ) def test_config_blocked_default_plugins(testdir, plugin): if plugin == "debugging": # Fixed in xdist master (after 1.27.0). # https://github.com/pytest-dev/pytest-xdist/pull/422 try: import xdist # noqa: F401 except ImportError: pass else: pytest.skip("does not work with xdist currently") p = testdir.makepyfile("def test(): pass") result = testdir.runpytest(str(p), "-pno:%s" % plugin) if plugin == "python": assert result.ret == EXIT_USAGEERROR result.stderr.fnmatch_lines( [ "ERROR: not found: */test_config_blocked_default_plugins.py", "(no name '*/test_config_blocked_default_plugins.py' in any of [])", ] ) return assert result.ret == EXIT_OK if plugin != "terminal": result.stdout.fnmatch_lines(["* 1 passed in *"]) p = testdir.makepyfile("def test(): assert 0") result = testdir.runpytest(str(p), "-pno:%s" % plugin) assert result.ret == EXIT_TESTSFAILED if plugin != "terminal": result.stdout.fnmatch_lines(["* 1 failed in *"]) else: assert result.stdout.lines == [""] pytest-4.6.9/testing/test_capture.py0000664000372000037200000013347613604173400020471 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import contextlib import io import os import pickle import subprocess import sys import textwrap from io import UnsupportedOperation import py from six import text_type import pytest from _pytest import capture from _pytest.capture import CaptureManager from _pytest.compat import _PY3 from _pytest.main import EXIT_NOTESTSCOLLECTED # note: py.io capture tests where copied from # pylib 1.4.20.dev2 (rev 13d9af95547e) needsosdup = pytest.mark.skipif( not hasattr(os, "dup"), reason="test needs os.dup, not available on this platform" ) def StdCaptureFD(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture) def StdCapture(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.SysCapture) class TestCaptureManager(object): def test_getmethod_default_no_fd(self, monkeypatch): from _pytest.capture import pytest_addoption from _pytest.config.argparsing import Parser parser = Parser() pytest_addoption(parser) default = parser._groups[0].options[0].default assert default == "fd" if hasattr(os, "dup") else "sys" parser = Parser() monkeypatch.delattr(os, "dup", raising=False) pytest_addoption(parser) assert parser._groups[0].options[0].default == "sys" @pytest.mark.parametrize( "method", ["no", "sys", pytest.param("fd", marks=needsosdup)] ) def test_capturing_basic_api(self, method): capouter = StdCaptureFD() old = sys.stdout, sys.stderr, sys.stdin try: capman = CaptureManager(method) capman.start_global_capturing() capman.suspend_global_capture() outerr = capman.read_global_capture() assert outerr == ("", "") capman.suspend_global_capture() outerr = capman.read_global_capture() assert outerr == ("", "") print("hello") capman.suspend_global_capture() out, err = capman.read_global_capture() if method == "no": assert old == (sys.stdout, sys.stderr, sys.stdin) else: assert not out capman.resume_global_capture() print("hello") capman.suspend_global_capture() out, err = capman.read_global_capture() if method != "no": assert out == "hello\n" capman.stop_global_capturing() finally: capouter.stop_capturing() @needsosdup def test_init_capturing(self): capouter = StdCaptureFD() try: capman = CaptureManager("fd") capman.start_global_capturing() pytest.raises(AssertionError, capman.start_global_capturing) capman.stop_global_capturing() finally: capouter.stop_capturing() @pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_unicode(testdir, method): if hasattr(sys, "pypy_version_info") and sys.pypy_version_info < (2, 2): pytest.xfail("does not work on pypy < 2.2") if sys.version_info >= (3, 0): obj = "'b\u00f6y'" else: obj = "u'\u00f6y'" testdir.makepyfile( """ # -*- coding: utf-8 -*- # taken from issue 227 from nosetests def test_unicode(): import sys print(sys.stdout) print(%s) """ % obj ) result = testdir.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_bytes_in_utf8_encoding(testdir, method): testdir.makepyfile( """ def test_unicode(): print('b\\u00f6y') """ ) result = testdir.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) def test_collect_capturing(testdir): p = testdir.makepyfile( """ import sys print("collect %s failure" % 13) sys.stderr.write("collect %s_stderr failure" % 13) import xyz42123 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*Captured stdout*", "collect 13 failure", "*Captured stderr*", "collect 13_stderr failure", ] ) class TestPerTestCapturing(object): def test_capture_and_fixtures(self, testdir): p = testdir.makepyfile( """ def setup_module(mod): print("setup module") def setup_function(function): print("setup " + function.__name__) def test_func1(): print("in func1") assert 0 def test_func2(): print("in func2") assert 0 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "setup module*", "setup test_func1*", "in func1*", "setup test_func2*", "in func2*", ] ) @pytest.mark.xfail(reason="unimplemented feature") def test_capture_scope_cache(self, testdir): p = testdir.makepyfile( """ import sys def setup_module(func): print("module-setup") def setup_function(func): print("function-setup") def test_func(): print("in function") assert 0 def teardown_function(func): print("in teardown") """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*test_func():*", "*Captured stdout during setup*", "module-setup*", "function-setup*", "*Captured stdout*", "in teardown*", ] ) def test_no_carry_over(self, testdir): p = testdir.makepyfile( """ def test_func1(): print("in func1") def test_func2(): print("in func2") assert 0 """ ) result = testdir.runpytest(p) s = result.stdout.str() assert "in func1" not in s assert "in func2" in s def test_teardown_capturing(self, testdir): p = testdir.makepyfile( """ def setup_function(function): print("setup func1") def teardown_function(function): print("teardown func1") assert 0 def test_func1(): print("in func1") pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*teardown_function*", "*Captured stdout*", "setup func1*", "in func1*", "teardown func1*", # "*1 fixture failure*" ] ) def test_teardown_capturing_final(self, testdir): p = testdir.makepyfile( """ def teardown_module(mod): print("teardown module") assert 0 def test_func(): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*def teardown_module(mod):*", "*Captured stdout*", "*teardown module*", "*1 error*", ] ) def test_capturing_outerr(self, testdir): p1 = testdir.makepyfile( """\ import sys def test_capturing(): print(42) sys.stderr.write(str(23)) def test_capturing_error(): print(1) sys.stderr.write(str(2)) raise ValueError """ ) result = testdir.runpytest(p1) result.stdout.fnmatch_lines( [ "*test_capturing_outerr.py .F*", "====* FAILURES *====", "____*____", "*test_capturing_outerr.py:8: ValueError", "*--- Captured stdout *call*", "1", "*--- Captured stderr *call*", "2", ] ) class TestLoggingInteraction(object): def test_logging_stream_ownership(self, testdir): p = testdir.makepyfile( """\ def test_logging(): import logging import pytest stream = capture.CaptureIO() logging.basicConfig(stream=stream) stream.close() # to free memory/release resources """ ) result = testdir.runpytest_subprocess(p) assert result.stderr.str().find("atexit") == -1 def test_logging_and_immediate_setupteardown(self, testdir): p = testdir.makepyfile( """\ import logging def setup_function(function): logging.warning("hello1") def test_logging(): logging.warning("hello2") assert 0 def teardown_function(function): logging.warning("hello3") assert 0 """ ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors show first! ) # verify proper termination assert "closed" not in s def test_logging_and_crossscope_fixtures(self, testdir): p = testdir.makepyfile( """\ import logging def setup_module(function): logging.warning("hello1") def test_logging(): logging.warning("hello2") assert 0 def teardown_module(function): logging.warning("hello3") assert 0 """ ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors come first ) # verify proper termination assert "closed" not in s def test_conftestlogging_is_shown(self, testdir): testdir.makeconftest( """\ import logging logging.basicConfig() logging.warning("hello435") """ ) # make sure that logging is still captured in tests result = testdir.runpytest_subprocess("-s", "-p", "no:capturelog") assert result.ret == EXIT_NOTESTSCOLLECTED result.stderr.fnmatch_lines(["WARNING*hello435*"]) assert "operation on closed file" not in result.stderr.str() def test_conftestlogging_and_test_logging(self, testdir): testdir.makeconftest( """\ import logging logging.basicConfig() """ ) # make sure that logging is still captured in tests p = testdir.makepyfile( """\ def test_hello(): import logging logging.warning("hello433") assert 0 """ ) result = testdir.runpytest_subprocess(p, "-p", "no:capturelog") assert result.ret != 0 result.stdout.fnmatch_lines(["WARNING*hello433*"]) assert "something" not in result.stderr.str() assert "operation on closed file" not in result.stderr.str() def test_logging_after_cap_stopped(self, testdir): testdir.makeconftest( """\ import pytest import logging log = logging.getLogger(__name__) @pytest.fixture def log_on_teardown(): yield log.warning('Logging on teardown') """ ) # make sure that logging is still captured in tests p = testdir.makepyfile( """\ def test_hello(log_on_teardown): import logging logging.warning("hello433") assert 1 raise KeyboardInterrupt() """ ) result = testdir.runpytest_subprocess(p, "--log-cli-level", "info") assert result.ret != 0 result.stdout.fnmatch_lines( ["*WARNING*hello433*", "*WARNING*Logging on teardown*"] ) assert ( "AttributeError: 'NoneType' object has no attribute 'resume_capturing'" not in result.stderr.str() ) class TestCaptureFixture(object): @pytest.mark.parametrize("opt", [[], ["-s"]]) def test_std_functional(self, testdir, opt): reprec = testdir.inline_runsource( """\ def test_hello(capsys): print(42) out, err = capsys.readouterr() assert out.startswith("42") """, *opt ) reprec.assertoutcome(passed=1) def test_capsyscapfd(self, testdir): p = testdir.makepyfile( """\ def test_one(capsys, capfd): pass def test_two(capfd, capsys): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*ERROR*setup*test_one*", "E*capfd*capsys*same*time*", "*ERROR*setup*test_two*", "E*capsys*capfd*same*time*", "*2 error*", ] ) def test_capturing_getfixturevalue(self, testdir): """Test that asking for "capfd" and "capsys" using request.getfixturevalue in the same test is an error. """ testdir.makepyfile( """\ def test_one(capsys, request): request.getfixturevalue("capfd") def test_two(capfd, request): request.getfixturevalue("capsys") """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_one*", "*capsys*capfd*same*time*", "*test_two*", "*capfd*capsys*same*time*", "*2 failed in*", ] ) def test_capsyscapfdbinary(self, testdir): p = testdir.makepyfile( """\ def test_one(capsys, capfdbinary): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( ["*ERROR*setup*test_one*", "E*capfdbinary*capsys*same*time*", "*1 error*"] ) @pytest.mark.parametrize("method", ["sys", "fd"]) def test_capture_is_represented_on_failure_issue128(self, testdir, method): p = testdir.makepyfile( """\ def test_hello(cap{}): print("xxx42xxx") assert 0 """.format( method ) ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["xxx42xxx"]) @needsosdup def test_stdfd_functional(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capfd): import os os.write(1, "42".encode('ascii')) out, err = capfd.readouterr() assert out.startswith("42") capfd.close() """ ) reprec.assertoutcome(passed=1) @needsosdup def test_capfdbinary(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capfdbinary): import os # some likely un-decodable bytes os.write(1, b'\\xfe\\x98\\x20') out, err = capfdbinary.readouterr() assert out == b'\\xfe\\x98\\x20' assert err == b'' """ ) reprec.assertoutcome(passed=1) @pytest.mark.skipif( sys.version_info < (3,), reason="only have capsysbinary in python 3" ) def test_capsysbinary(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capsysbinary): import sys # some likely un-decodable bytes sys.stdout.buffer.write(b'\\xfe\\x98\\x20') out, err = capsysbinary.readouterr() assert out == b'\\xfe\\x98\\x20' assert err == b'' """ ) reprec.assertoutcome(passed=1) @pytest.mark.skipif( sys.version_info >= (3,), reason="only have capsysbinary in python 3" ) def test_capsysbinary_forbidden_in_python2(self, testdir): testdir.makepyfile( """\ def test_hello(capsysbinary): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_hello*", "*capsysbinary is only supported on Python 3*", "*1 error in*", ] ) def test_partial_setup_failure(self, testdir): p = testdir.makepyfile( """\ def test_hello(capsys, missingarg): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*test_partial_setup_failure*", "*1 error*"]) @needsosdup def test_keyboardinterrupt_disables_capturing(self, testdir): p = testdir.makepyfile( """\ def test_hello(capfd): import os os.write(1, str(42).encode('ascii')) raise KeyboardInterrupt() """ ) result = testdir.runpytest_subprocess(p) result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) assert result.ret == 2 def test_capture_and_logging(self, testdir): """#14""" p = testdir.makepyfile( """\ import logging def test_log(capsys): logging.error('x') """ ) result = testdir.runpytest_subprocess(p) assert "closed" not in result.stderr.str() @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) @pytest.mark.parametrize("no_capture", [True, False]) def test_disabled_capture_fixture(self, testdir, fixture, no_capture): testdir.makepyfile( """\ def test_disabled({fixture}): print('captured before') with {fixture}.disabled(): print('while capture is disabled') print('captured after') assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '') def test_normal(): print('test_normal executed') """.format( fixture=fixture ) ) args = ("-s",) if no_capture else () result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines( """ *while capture is disabled* """ ) assert "captured before" not in result.stdout.str() assert "captured after" not in result.stdout.str() if no_capture: assert "test_normal executed" in result.stdout.str() else: assert "test_normal executed" not in result.stdout.str() @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) def test_fixture_use_by_other_fixtures(self, testdir, fixture): """ Ensure that capsys and capfd can be used by other fixtures during setup and teardown. """ testdir.makepyfile( """\ from __future__ import print_function import sys import pytest @pytest.fixture def captured_print({fixture}): print('stdout contents begin') print('stderr contents begin', file=sys.stderr) out, err = {fixture}.readouterr() yield out, err print('stdout contents end') print('stderr contents end', file=sys.stderr) out, err = {fixture}.readouterr() assert out == 'stdout contents end\\n' assert err == 'stderr contents end\\n' def test_captured_print(captured_print): out, err = captured_print assert out == 'stdout contents begin\\n' assert err == 'stderr contents begin\\n' """.format( fixture=fixture ) ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed*"]) assert "stdout contents begin" not in result.stdout.str() assert "stderr contents begin" not in result.stdout.str() @pytest.mark.parametrize("cap", ["capsys", "capfd"]) def test_fixture_use_by_other_fixtures_teardown(self, testdir, cap): """Ensure we can access setup and teardown buffers from teardown when using capsys/capfd (##3033)""" testdir.makepyfile( """\ import sys import pytest import os @pytest.fixture() def fix({cap}): print("setup out") sys.stderr.write("setup err\\n") yield out, err = {cap}.readouterr() assert out == 'setup out\\ncall out\\n' assert err == 'setup err\\ncall err\\n' def test_a(fix): print("call out") sys.stderr.write("call err\\n") """.format( cap=cap ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setup_failure_does_not_kill_capturing(testdir): sub1 = testdir.mkpydir("sub1") sub1.join("conftest.py").write( textwrap.dedent( """\ def pytest_runtest_setup(item): raise ValueError(42) """ ) ) sub1.join("test_mod.py").write("def test_func1(): pass") result = testdir.runpytest(testdir.tmpdir, "--traceconfig") result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"]) def test_fdfuncarg_skips_on_no_osdup(testdir): testdir.makepyfile( """ import os if hasattr(os, 'dup'): del os.dup def test_hello(capfd): pass """ ) result = testdir.runpytest_subprocess("--capture=no") result.stdout.fnmatch_lines(["*1 skipped*"]) def test_capture_conftest_runtest_setup(testdir): testdir.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest() assert result.ret == 0 assert "hello19" not in result.stdout.str() def test_capture_badoutput_issue412(testdir): testdir.makepyfile( """ import os def test_func(): omg = bytearray([1,129,1]) os.write(1, omg) assert 0 """ ) result = testdir.runpytest("--cap=fd") result.stdout.fnmatch_lines( """ *def test_func* *assert 0* *Captured* *1 failed* """ ) def test_capture_early_option_parsing(testdir): testdir.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest("-vs") assert result.ret == 0 assert "hello19" in result.stdout.str() def test_capture_binary_output(testdir): testdir.makepyfile( r""" import pytest def test_a(): import sys import subprocess subprocess.call([sys.executable, __file__]) def test_foo(): import os;os.write(1, b'\xc3') if __name__ == '__main__': test_foo() """ ) result = testdir.runpytest("--assert=plain") result.assert_outcomes(passed=2) def test_error_during_readouterr(testdir): """Make sure we suspend capturing if errors occur during readouterr""" testdir.makepyfile( pytest_xyz=""" from _pytest.capture import FDCapture def bad_snap(self): raise Exception('boom') assert FDCapture.snap FDCapture.snap = bad_snap """ ) result = testdir.runpytest_subprocess("-p", "pytest_xyz", "--version") result.stderr.fnmatch_lines( ["*in bad_snap", " raise Exception('boom')", "Exception: boom"] ) class TestCaptureIO(object): def test_text(self): f = capture.CaptureIO() f.write("hello") s = f.getvalue() assert s == "hello" f.close() def test_unicode_and_str_mixture(self): f = capture.CaptureIO() if sys.version_info >= (3, 0): f.write("\u00f6") pytest.raises(TypeError, f.write, b"hello") else: f.write(u"\u00f6") f.write(b"hello") s = f.getvalue() f.close() assert isinstance(s, text_type) @pytest.mark.skipif(sys.version_info[0] == 2, reason="python 3 only behaviour") def test_write_bytes_to_buffer(self): """In python3, stdout / stderr are text io wrappers (exposing a buffer property of the underlying bytestream). See issue #1407 """ f = capture.CaptureIO() f.buffer.write(b"foo\r\n") assert f.getvalue() == "foo\r\n" def test_dontreadfrominput(): from _pytest.capture import DontReadFromInput f = DontReadFromInput() assert not f.isatty() pytest.raises(IOError, f.read) pytest.raises(IOError, f.readlines) iter_f = iter(f) pytest.raises(IOError, next, iter_f) pytest.raises(UnsupportedOperation, f.fileno) f.close() # just for completeness @pytest.mark.skipif("sys.version_info < (3,)", reason="python2 has no buffer") def test_dontreadfrominput_buffer_python3(): from _pytest.capture import DontReadFromInput f = DontReadFromInput() fb = f.buffer assert not fb.isatty() pytest.raises(IOError, fb.read) pytest.raises(IOError, fb.readlines) iter_f = iter(f) pytest.raises(IOError, next, iter_f) pytest.raises(ValueError, fb.fileno) f.close() # just for completeness @pytest.mark.skipif("sys.version_info >= (3,)", reason="python2 has no buffer") def test_dontreadfrominput_buffer_python2(): from _pytest.capture import DontReadFromInput f = DontReadFromInput() with pytest.raises(AttributeError): f.buffer f.close() # just for completeness @pytest.yield_fixture def tmpfile(testdir): f = testdir.makepyfile("").open("wb+") yield f if not f.closed: f.close() @needsosdup def test_dupfile(tmpfile): flist = [] for i in range(5): nf = capture.safe_text_dupfile(tmpfile, "wb") assert nf != tmpfile assert nf.fileno() != tmpfile.fileno() assert nf not in flist print(i, end="", file=nf) flist.append(nf) fname_open = flist[0].name assert fname_open == repr(flist[0].buffer) for i in range(5): f = flist[i] f.close() fname_closed = flist[0].name assert fname_closed == repr(flist[0].buffer) assert fname_closed != fname_open tmpfile.seek(0) s = tmpfile.read() assert "01234" in repr(s) tmpfile.close() assert fname_closed == repr(flist[0].buffer) def test_dupfile_on_bytesio(): bio = io.BytesIO() f = capture.safe_text_dupfile(bio, "wb") f.write("hello") assert bio.getvalue() == b"hello" assert "BytesIO object" in f.name def test_dupfile_on_textio(): tio = py.io.TextIO() f = capture.safe_text_dupfile(tio, "wb") f.write("hello") assert tio.getvalue() == "hello" assert not hasattr(f, "name") @contextlib.contextmanager def lsof_check(): pid = os.getpid() try: out = subprocess.check_output(("lsof", "-p", str(pid))).decode() except (OSError, subprocess.CalledProcessError, UnicodeDecodeError): # about UnicodeDecodeError, see note on pytester pytest.skip("could not run 'lsof'") yield out2 = subprocess.check_output(("lsof", "-p", str(pid))).decode() len1 = len([x for x in out.split("\n") if "REG" in x]) len2 = len([x for x in out2.split("\n") if "REG" in x]) assert len2 < len1 + 3, out2 class TestFDCapture(object): pytestmark = needsosdup def test_simple(self, tmpfile): fd = tmpfile.fileno() cap = capture.FDCapture(fd) data = b"hello" os.write(fd, data) s = cap.snap() cap.done() assert not s cap = capture.FDCapture(fd) cap.start() os.write(fd, data) s = cap.snap() cap.done() assert s == "hello" def test_simple_many(self, tmpfile): for i in range(10): self.test_simple(tmpfile) def test_simple_many_check_open_files(self, testdir): with lsof_check(): with testdir.makepyfile("").open("wb+") as tmpfile: self.test_simple_many(tmpfile) def test_simple_fail_second_start(self, tmpfile): fd = tmpfile.fileno() cap = capture.FDCapture(fd) cap.done() pytest.raises(ValueError, cap.start) def test_stderr(self): cap = capture.FDCapture(2) cap.start() print("hello", file=sys.stderr) s = cap.snap() cap.done() assert s == "hello\n" def test_stdin(self, tmpfile): cap = capture.FDCapture(0) cap.start() x = os.read(0, 100).strip() cap.done() assert x == b"" def test_writeorg(self, tmpfile): data1, data2 = b"foo", b"bar" cap = capture.FDCapture(tmpfile.fileno()) cap.start() tmpfile.write(data1) tmpfile.flush() cap.writeorg(data2) scap = cap.snap() cap.done() assert scap == data1.decode("ascii") with open(tmpfile.name, "rb") as stmp_file: stmp = stmp_file.read() assert stmp == data2 def test_simple_resume_suspend(self, tmpfile): with saved_fd(1): cap = capture.FDCapture(1) cap.start() data = b"hello" os.write(1, data) sys.stdout.write("whatever") s = cap.snap() assert s == "hellowhatever" cap.suspend() os.write(1, b"world") sys.stdout.write("qlwkej") assert not cap.snap() cap.resume() os.write(1, b"but now") sys.stdout.write(" yes\n") s = cap.snap() assert s == "but now yes\n" cap.suspend() cap.done() pytest.raises(AttributeError, cap.suspend) def test_capfd_sys_stdout_mode(self, capfd): assert "b" not in sys.stdout.mode @contextlib.contextmanager def saved_fd(fd): new_fd = os.dup(fd) try: yield finally: os.dup2(new_fd, fd) os.close(new_fd) class TestStdCapture(object): captureclass = staticmethod(StdCapture) @contextlib.contextmanager def getcapture(self, **kw): cap = self.__class__.captureclass(**kw) cap.start_capturing() try: yield cap finally: cap.stop_capturing() def test_capturing_done_simple(self): with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert out == "hello" assert err == "world" def test_capturing_reset_simple(self): with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") out, err = cap.readouterr() assert out == "hello world\n" assert err == "hello error\n" def test_capturing_readouterr(self): with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") out, err = cap.readouterr() assert out == "hello world\n" assert err == "hello error\n" sys.stderr.write("error2") out, err = cap.readouterr() assert err == "error2" def test_capture_results_accessible_by_attribute(self): with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") capture_result = cap.readouterr() assert capture_result.out == "hello" assert capture_result.err == "world" def test_capturing_readouterr_unicode(self): with self.getcapture() as cap: print("hxąć") out, err = cap.readouterr() assert out == u"hxąć\n" @pytest.mark.skipif( "sys.version_info >= (3,)", reason="text output different for bytes on python3" ) def test_capturing_readouterr_decode_error_handling(self): with self.getcapture() as cap: # triggered an internal error in pytest print("\xa6") out, err = cap.readouterr() assert out == u"\ufffd\n" def test_reset_twice_error(self): with self.getcapture() as cap: print("hello") out, err = cap.readouterr() pytest.raises(ValueError, cap.stop_capturing) assert out == "hello\n" assert not err def test_capturing_modify_sysouterr_in_between(self): oldout = sys.stdout olderr = sys.stderr with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") sys.stdout = capture.CaptureIO() sys.stderr = capture.CaptureIO() print("not seen") sys.stderr.write("not seen\n") out, err = cap.readouterr() assert out == "hello" assert err == "world" assert sys.stdout == oldout assert sys.stderr == olderr def test_capturing_error_recursive(self): with self.getcapture() as cap1: print("cap1") with self.getcapture() as cap2: print("cap2") out2, err2 = cap2.readouterr() out1, err1 = cap1.readouterr() assert out1 == "cap1\n" assert out2 == "cap2\n" def test_just_out_capture(self): with self.getcapture(out=True, err=False) as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert out == "hello" assert not err def test_just_err_capture(self): with self.getcapture(out=False, err=True) as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert err == "world" assert not out def test_stdin_restored(self): old = sys.stdin with self.getcapture(in_=True): newstdin = sys.stdin assert newstdin != sys.stdin assert sys.stdin is old def test_stdin_nulled_by_default(self): print("XXX this test may well hang instead of crashing") print("XXX which indicates an error in the underlying capturing") print("XXX mechanisms") with self.getcapture(): pytest.raises(IOError, sys.stdin.read) class TestStdCaptureFD(TestStdCapture): pytestmark = needsosdup captureclass = staticmethod(StdCaptureFD) def test_simple_only_fd(self, testdir): testdir.makepyfile( """ import os def test_x(): os.write(1, "hello\\n".encode("ascii")) assert 0 """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_x* *assert 0* *Captured stdout* """ ) def test_intermingling(self): with self.getcapture() as cap: os.write(1, b"1") sys.stdout.write(str(2)) sys.stdout.flush() os.write(1, b"3") os.write(2, b"a") sys.stderr.write("b") sys.stderr.flush() os.write(2, b"c") out, err = cap.readouterr() assert out == "123" assert err == "abc" def test_many(self, capfd): with lsof_check(): for i in range(10): cap = StdCaptureFD() cap.stop_capturing() class TestStdCaptureFDinvalidFD(object): pytestmark = needsosdup def test_stdcapture_fd_invalid_fd(self, testdir): testdir.makepyfile( """ import os from _pytest import capture def StdCaptureFD(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture) def test_stdout(): os.close(1) cap = StdCaptureFD(out=True, err=False, in_=False) assert repr(cap.out) == "" cap.stop_capturing() def test_stderr(): os.close(2) cap = StdCaptureFD(out=False, err=True, in_=False) assert repr(cap.err) == "" cap.stop_capturing() def test_stdin(): os.close(0) cap = StdCaptureFD(out=False, err=False, in_=True) assert repr(cap.in_) == "" cap.stop_capturing() """ ) result = testdir.runpytest_subprocess("--capture=fd") assert result.ret == 0 assert result.parseoutcomes()["passed"] == 3 def test_capture_not_started_but_reset(): capsys = StdCapture() capsys.stop_capturing() def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): test_text = "test text" print(test_text.encode(sys.stdout.encoding, "replace")) (out, err) = capsys.readouterr() assert out assert err == "" def test_capsys_results_accessible_by_attribute(capsys): sys.stdout.write("spam") sys.stderr.write("eggs") capture_result = capsys.readouterr() assert capture_result.out == "spam" assert capture_result.err == "eggs" @needsosdup @pytest.mark.parametrize("use", [True, False]) def test_fdcapture_tmpfile_remains_the_same(tmpfile, use): if not use: tmpfile = True cap = StdCaptureFD(out=False, err=tmpfile) try: cap.start_capturing() capfile = cap.err.tmpfile cap.readouterr() finally: cap.stop_capturing() capfile2 = cap.err.tmpfile assert capfile2 == capfile @needsosdup def test_close_and_capture_again(testdir): testdir.makepyfile( """ import os def test_close(): os.close(1) def test_capture_again(): os.write(1, b"hello\\n") assert 0 """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_capture_again* *assert 0* *stdout* *hello* """ ) @pytest.mark.parametrize("method", ["SysCapture", "FDCapture"]) def test_capturing_and_logging_fundamentals(testdir, method): if method == "StdCaptureFD" and not hasattr(os, "dup"): pytest.skip("need os.dup") # here we check a fundamental feature p = testdir.makepyfile( """ import sys, os import py, logging from _pytest import capture cap = capture.MultiCapture(out=False, in_=False, Capture=capture.%s) cap.start_capturing() logging.warning("hello1") outerr = cap.readouterr() print("suspend, captured %%s" %%(outerr,)) logging.warning("hello2") cap.pop_outerr_to_orig() logging.warning("hello3") outerr = cap.readouterr() print("suspend2, captured %%s" %% (outerr,)) """ % (method,) ) result = testdir.runpython(p) result.stdout.fnmatch_lines( """ suspend, captured*hello1* suspend2, captured*WARNING:root:hello3* """ ) result.stderr.fnmatch_lines( """ WARNING:root:hello2 """ ) assert "atexit" not in result.stderr.str() def test_error_attribute_issue555(testdir): testdir.makepyfile( """ import sys def test_capattr(): assert sys.stdout.errors == "strict" assert sys.stderr.errors == "strict" """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.skipif( not sys.platform.startswith("win") and sys.version_info[:2] >= (3, 6), reason="only py3.6+ on windows", ) def test_py36_windowsconsoleio_workaround_non_standard_streams(): """ Ensure _py36_windowsconsoleio_workaround function works with objects that do not implement the full ``io``-based stream protocol, for example execnet channels (#2666). """ from _pytest.capture import _py36_windowsconsoleio_workaround class DummyStream(object): def write(self, s): pass stream = DummyStream() _py36_windowsconsoleio_workaround(stream) def test_dontreadfrominput_has_encoding(testdir): testdir.makepyfile( """ import sys def test_capattr(): # should not raise AttributeError assert sys.stdout.encoding assert sys.stderr.encoding """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_crash_on_closing_tmpfile_py27(testdir): p = testdir.makepyfile( """ from __future__ import print_function import threading import sys printing = threading.Event() def spam(): f = sys.stderr print('SPAMBEFORE', end='', file=f) printing.set() while True: try: f.flush() except (OSError, ValueError): break def test_spam_in_thread(): t = threading.Thread(target=spam) t.daemon = True t.start() printing.wait() """ ) result = testdir.runpytest_subprocess(str(p)) assert result.ret == 0 assert result.stderr.str() == "" assert "IOError" not in result.stdout.str() def test_pickling_and_unpickling_encoded_file(): # See https://bitbucket.org/pytest-dev/pytest/pull-request/194 # pickle.loads() raises infinite recursion if # EncodedFile.__getattr__ is not implemented properly ef = capture.EncodedFile(None, None) ef_as_str = pickle.dumps(ef) pickle.loads(ef_as_str) def test_global_capture_with_live_logging(testdir): # Issue 3819 # capture should work with live cli logging # Teardown report seems to have the capture for the whole process (setup, capture, teardown) testdir.makeconftest( """ def pytest_runtest_logreport(report): if "test_global" in report.nodeid: if report.when == "teardown": with open("caplog", "w") as f: f.write(report.caplog) with open("capstdout", "w") as f: f.write(report.capstdout) """ ) testdir.makepyfile( """ import logging import sys import pytest logger = logging.getLogger(__name__) @pytest.fixture def fix1(): print("fix setup") logging.info("fix setup") yield logging.info("fix teardown") print("fix teardown") def test_global(fix1): print("begin test") logging.info("something in test") print("end test") """ ) result = testdir.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 with open("caplog", "r") as f: caplog = f.read() assert "fix setup" in caplog assert "something in test" in caplog assert "fix teardown" in caplog with open("capstdout", "r") as f: capstdout = f.read() assert "fix setup" in capstdout assert "begin test" in capstdout assert "end test" in capstdout assert "fix teardown" in capstdout @pytest.mark.parametrize("capture_fixture", ["capsys", "capfd"]) def test_capture_with_live_logging(testdir, capture_fixture): # Issue 3819 # capture should work with live cli logging testdir.makepyfile( """ import logging import sys logger = logging.getLogger(__name__) def test_capture({0}): print("hello") sys.stderr.write("world\\n") captured = {0}.readouterr() assert captured.out == "hello\\n" assert captured.err == "world\\n" logging.info("something") print("next") logging.info("something") captured = {0}.readouterr() assert captured.out == "next\\n" """.format( capture_fixture ) ) result = testdir.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 def test_typeerror_encodedfile_write(testdir): """It should behave the same with and without output capturing (#4861).""" p = testdir.makepyfile( """ def test_fails(): import sys sys.stdout.write(b"foo") """ ) result_without_capture = testdir.runpytest("-s", str(p)) result_with_capture = testdir.runpytest(str(p)) assert result_with_capture.ret == result_without_capture.ret if _PY3: result_with_capture.stdout.fnmatch_lines( ["E TypeError: write() argument must be str, not bytes"] ) else: assert result_with_capture.ret == 0 pytest-4.6.9/testing/test_compat.py0000664000372000037200000001000213604173400020264 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys from functools import wraps import six import pytest from _pytest.compat import _PytestWrapper from _pytest.compat import get_real_func from _pytest.compat import is_generator from _pytest.compat import safe_getattr from _pytest.compat import safe_isclass from _pytest.outcomes import OutcomeException def test_is_generator(): def zap(): yield # pragma: no cover def foo(): pass # pragma: no cover assert is_generator(zap) assert not is_generator(foo) def test_real_func_loop_limit(): class Evil(object): def __init__(self): self.left = 1000 def __repr__(self): return "".format(left=self.left) def __getattr__(self, attr): if not self.left: raise RuntimeError("it's over") # pragma: no cover self.left -= 1 return self evil = Evil() with pytest.raises( ValueError, match=( "could not find real function of \n" "stopped at " ), ): get_real_func(evil) def test_get_real_func(): """Check that get_real_func correctly unwraps decorators until reaching the real function""" def decorator(f): @wraps(f) def inner(): pass # pragma: no cover if six.PY2: inner.__wrapped__ = f return inner def func(): pass # pragma: no cover wrapped_func = decorator(decorator(func)) assert get_real_func(wrapped_func) is func wrapped_func2 = decorator(decorator(wrapped_func)) assert get_real_func(wrapped_func2) is func # special case for __pytest_wrapped__ attribute: used to obtain the function up until the point # a function was wrapped by pytest itself wrapped_func2.__pytest_wrapped__ = _PytestWrapper(wrapped_func) assert get_real_func(wrapped_func2) is wrapped_func @pytest.mark.skipif( sys.version_info < (3, 4), reason="asyncio available in Python 3.4+" ) def test_is_generator_asyncio(testdir): testdir.makepyfile( """ from _pytest.compat import is_generator import asyncio @asyncio.coroutine def baz(): yield from [1,2,3] def test_is_generator_asyncio(): assert not is_generator(baz) """ ) # avoid importing asyncio into pytest's own process, # which in turn imports logging (#8) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.skipif( sys.version_info < (3, 5), reason="async syntax available in Python 3.5+" ) def test_is_generator_async_syntax(testdir): testdir.makepyfile( """ from _pytest.compat import is_generator def test_is_generator_py35(): async def foo(): await foo() async def bar(): pass assert not is_generator(foo) assert not is_generator(bar) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed*"]) class ErrorsHelper(object): @property def raise_exception(self): raise Exception("exception should be catched") @property def raise_fail(self): pytest.fail("fail should be catched") def test_helper_failures(): helper = ErrorsHelper() with pytest.raises(Exception): helper.raise_exception with pytest.raises(OutcomeException): helper.raise_fail def test_safe_getattr(): helper = ErrorsHelper() assert safe_getattr(helper, "raise_exception", "default") == "default" assert safe_getattr(helper, "raise_fail", "default") == "default" def test_safe_isclass(): assert safe_isclass(type) is True class CrappyClass(Exception): @property def __class__(self): assert False, "Should be ignored" assert safe_isclass(CrappyClass()) is False pytest-4.6.9/testing/conftest.py0000664000372000037200000000314713604173400017603 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import pytest if sys.gettrace(): @pytest.fixture(autouse=True) def restore_tracing(): """Restore tracing function (when run with Coverage.py). https://bugs.python.org/issue37011 """ orig_trace = sys.gettrace() yield if sys.gettrace() != orig_trace: sys.settrace(orig_trace) @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_collection_modifyitems(config, items): """Prefer faster tests. Use a hookwrapper to do this in the beginning, so e.g. --ff still works correctly. """ fast_items = [] slow_items = [] slowest_items = [] neutral_items = [] spawn_names = {"spawn_pytest", "spawn"} for item in items: try: fixtures = item.fixturenames except AttributeError: # doctest at least # (https://github.com/pytest-dev/pytest/issues/5070) neutral_items.append(item) else: if "testdir" in fixtures: if spawn_names.intersection(item.function.__code__.co_names): item.add_marker(pytest.mark.uses_pexpect) slowest_items.append(item) else: slow_items.append(item) item.add_marker(pytest.mark.slow) else: marker = item.get_closest_marker("slow") if marker: slowest_items.append(item) else: fast_items.append(item) items[:] = fast_items + neutral_items + slow_items + slowest_items yield pytest-4.6.9/testing/test_assertion.py0000664000372000037200000012121313604173400021017 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import textwrap import attr import six import _pytest.assertion as plugin import pytest from _pytest import outcomes from _pytest.assertion import truncate from _pytest.assertion import util from _pytest.compat import ATTRS_EQ_FIELD PY3 = sys.version_info >= (3, 0) def mock_config(): class Config(object): verbose = False def getoption(self, name): if name == "verbose": return self.verbose raise KeyError("Not mocked out: %s" % name) return Config() class TestImportHookInstallation(object): @pytest.mark.parametrize("initial_conftest", [True, False]) @pytest.mark.parametrize("mode", ["plain", "rewrite"]) def test_conftest_assertion_rewrite(self, testdir, initial_conftest, mode): """Test that conftest files are using assertion rewrite on import. (#1619) """ testdir.tmpdir.join("foo/tests").ensure(dir=1) conftest_path = "conftest.py" if initial_conftest else "foo/conftest.py" contents = { conftest_path: """ import pytest @pytest.fixture def check_first(): def check(values, value): assert values.pop(0) == value return check """, "foo/tests/test_foo.py": """ def test(check_first): check_first([10, 30], 30) """, } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess("--assert=%s" % mode) if mode == "plain": expected = "E AssertionError" elif mode == "rewrite": expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) def test_rewrite_assertions_pytester_plugin(self, testdir): """ Assertions in the pytester plugin must also benefit from assertion rewriting (#1920). """ testdir.makepyfile( """ pytest_plugins = ['pytester'] def test_dummy_failure(testdir): # how meta! testdir.makepyfile('def test(): assert 0') r = testdir.inline_run() r.assertoutcome(passed=1) """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*assert 1 == 0*"]) @pytest.mark.parametrize("mode", ["plain", "rewrite"]) def test_pytest_plugins_rewrite(self, testdir, mode): contents = { "conftest.py": """ pytest_plugins = ['ham'] """, "ham.py": """ import pytest @pytest.fixture def check_first(): def check(values, value): assert values.pop(0) == value return check """, "test_foo.py": """ def test_foo(check_first): check_first([10, 30], 30) """, } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess("--assert=%s" % mode) if mode == "plain": expected = "E AssertionError" elif mode == "rewrite": expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) @pytest.mark.parametrize("mode", ["str", "list"]) def test_pytest_plugins_rewrite_module_names(self, testdir, mode): """Test that pluginmanager correct marks pytest_plugins variables for assertion rewriting if they are defined as plain strings or list of strings (#1888). """ plugins = '"ham"' if mode == "str" else '["ham"]' contents = { "conftest.py": """ pytest_plugins = {plugins} """.format( plugins=plugins ), "ham.py": """ import pytest """, "test_foo.py": """ def test_foo(pytestconfig): assert 'ham' in pytestconfig.pluginmanager.rewrite_hook._must_rewrite """, } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess("--assert=rewrite") assert result.ret == 0 def test_pytest_plugins_rewrite_module_names_correctly(self, testdir): """Test that we match files correctly when they are marked for rewriting (#2939).""" contents = { "conftest.py": """\ pytest_plugins = "ham" """, "ham.py": "", "hamster.py": "", "test_foo.py": """\ def test_foo(pytestconfig): assert pytestconfig.pluginmanager.rewrite_hook.find_module('ham') is not None assert pytestconfig.pluginmanager.rewrite_hook.find_module('hamster') is None """, } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess("--assert=rewrite") assert result.ret == 0 @pytest.mark.parametrize("mode", ["plain", "rewrite"]) def test_installed_plugin_rewrite(self, testdir, mode, monkeypatch): monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) # Make sure the hook is installed early enough so that plugins # installed via setuptools are rewritten. testdir.tmpdir.join("hampkg").ensure(dir=1) contents = { "hampkg/__init__.py": """\ import pytest @pytest.fixture def check_first2(): def check(values, value): assert values.pop(0) == value return check """, "spamplugin.py": """\ import pytest from hampkg import check_first2 @pytest.fixture def check_first(): def check(values, value): assert values.pop(0) == value return check """, "mainwrapper.py": """\ import pytest from _pytest.compat import importlib_metadata class DummyEntryPoint(object): name = 'spam' module_name = 'spam.py' group = 'pytest11' def load(self): import spamplugin return spamplugin class DummyDistInfo(object): version = '1.0' files = ('spamplugin.py', 'hampkg/__init__.py') entry_points = (DummyEntryPoint(),) metadata = {'name': 'foo'} def distributions(): return (DummyDistInfo(),) importlib_metadata.distributions = distributions pytest.main() """, "test_foo.py": """\ def test(check_first): check_first([10, 30], 30) def test2(check_first2): check_first([10, 30], 30) """, } testdir.makepyfile(**contents) result = testdir.run( sys.executable, "mainwrapper.py", "-s", "--assert=%s" % mode ) if mode == "plain": expected = "E AssertionError" elif mode == "rewrite": expected = "*assert 10 == 30*" else: assert 0 result.stdout.fnmatch_lines([expected]) def test_rewrite_ast(self, testdir): testdir.tmpdir.join("pkg").ensure(dir=1) contents = { "pkg/__init__.py": """ import pytest pytest.register_assert_rewrite('pkg.helper') """, "pkg/helper.py": """ def tool(): a, b = 2, 3 assert a == b """, "pkg/plugin.py": """ import pytest, pkg.helper @pytest.fixture def tool(): return pkg.helper.tool """, "pkg/other.py": """ values = [3, 2] def tool(): assert values.pop() == 3 """, "conftest.py": """ pytest_plugins = ['pkg.plugin'] """, "test_pkg.py": """ import pkg.other def test_tool(tool): tool() def test_other(): pkg.other.tool() """, } testdir.makepyfile(**contents) result = testdir.runpytest_subprocess("--assert=rewrite") result.stdout.fnmatch_lines( [ ">*assert a == b*", "E*assert 2 == 3*", ">*assert values.pop() == 3*", "E*AssertionError", ] ) def test_register_assert_rewrite_checks_types(self): with pytest.raises(TypeError): pytest.register_assert_rewrite(["pytest_tests_internal_non_existing"]) pytest.register_assert_rewrite( "pytest_tests_internal_non_existing", "pytest_tests_internal_non_existing2" ) class TestBinReprIntegration(object): def test_pytest_assertrepr_compare_called(self, testdir): testdir.makeconftest( """ import pytest values = [] def pytest_assertrepr_compare(op, left, right): values.append((op, left, right)) @pytest.fixture def list(request): return values """ ) testdir.makepyfile( """ def test_hello(): assert 0 == 1 def test_check(list): assert list == [("==", 0, 1)] """ ) result = testdir.runpytest("-v") result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"]) def callequal(left, right, verbose=False): config = mock_config() config.verbose = verbose return plugin.pytest_assertrepr_compare(config, "==", left, right) class TestAssert_reprcompare(object): def test_different_types(self): assert callequal([0, 1], "foo") is None def test_summary(self): summary = callequal([0, 1], [0, 2])[0] assert len(summary) < 65 def test_text_diff(self): diff = callequal("spam", "eggs")[1:] assert "- spam" in diff assert "+ eggs" in diff def test_text_skipping(self): lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs") assert "Skipping" in lines[1] for line in lines: assert "a" * 50 not in line def test_text_skipping_verbose(self): lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs", verbose=True) assert "- " + "a" * 50 + "spam" in lines assert "+ " + "a" * 50 + "eggs" in lines def test_multiline_text_diff(self): left = "foo\nspam\nbar" right = "foo\neggs\nbar" diff = callequal(left, right) assert "- spam" in diff assert "+ eggs" in diff def test_list(self): expl = callequal([0, 1], [0, 2]) assert len(expl) > 1 @pytest.mark.parametrize( ["left", "right", "expected"], [ ( [0, 1], [0, 2], """ Full diff: - [0, 1] ? ^ + [0, 2] ? ^ """, ), ( {0: 1}, {0: 2}, """ Full diff: - {0: 1} ? ^ + {0: 2} ? ^ """, ), ( {0, 1}, {0, 2}, """ Full diff: - set([0, 1]) ? ^ + set([0, 2]) ? ^ """ if not PY3 else """ Full diff: - {0, 1} ? ^ + {0, 2} ? ^ """, ), ], ) def test_iterable_full_diff(self, left, right, expected): """Test the full diff assertion failure explanation. When verbose is False, then just a -v notice to get the diff is rendered, when verbose is True, then ndiff of the pprint is returned. """ expl = callequal(left, right, verbose=False) assert expl[-1] == "Use -v to get the full diff" expl = "\n".join(callequal(left, right, verbose=True)) assert expl.endswith(textwrap.dedent(expected).strip()) def test_list_different_lengths(self): expl = callequal([0, 1], [0, 1, 2]) assert len(expl) > 1 expl = callequal([0, 1, 2], [0, 1]) assert len(expl) > 1 def test_dict(self): expl = callequal({"a": 0}, {"a": 1}) assert len(expl) > 1 def test_dict_omitting(self): lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}) assert lines[1].startswith("Omitting 1 identical item") assert "Common items" not in lines for line in lines[1:]: assert "b" not in line def test_dict_omitting_with_verbosity_1(self): """ Ensure differing items are visible for verbosity=1 (#1512) """ lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=1) assert lines[1].startswith("Omitting 1 identical item") assert lines[2].startswith("Differing items") assert lines[3] == "{'a': 0} != {'a': 1}" assert "Common items" not in lines def test_dict_omitting_with_verbosity_2(self): lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=2) assert lines[1].startswith("Common items:") assert "Omitting" not in lines[1] assert lines[2] == "{'b': 1}" def test_dict_different_items(self): lines = callequal({"a": 0}, {"b": 1, "c": 2}, verbose=2) assert lines == [ "{'a': 0} == {'b': 1, 'c': 2}", "Left contains 1 more item:", "{'a': 0}", "Right contains 2 more items:", "{'b': 1, 'c': 2}", "Full diff:", "- {'a': 0}", "+ {'b': 1, 'c': 2}", ] lines = callequal({"b": 1, "c": 2}, {"a": 0}, verbose=2) assert lines == [ "{'b': 1, 'c': 2} == {'a': 0}", "Left contains 2 more items:", "{'b': 1, 'c': 2}", "Right contains 1 more item:", "{'a': 0}", "Full diff:", "- {'b': 1, 'c': 2}", "+ {'a': 0}", ] def test_sequence_different_items(self): lines = callequal((1, 2), (3, 4, 5), verbose=2) assert lines == [ "(1, 2) == (3, 4, 5)", "At index 0 diff: 1 != 3", "Right contains one more item: 5", "Full diff:", "- (1, 2)", "+ (3, 4, 5)", ] lines = callequal((1, 2, 3), (4,), verbose=2) assert lines == [ "(1, 2, 3) == (4,)", "At index 0 diff: 1 != 4", "Left contains 2 more items, first extra item: 2", "Full diff:", "- (1, 2, 3)", "+ (4,)", ] def test_set(self): expl = callequal({0, 1}, {0, 2}) assert len(expl) > 1 def test_frozenzet(self): expl = callequal(frozenset([0, 1]), {0, 2}) assert len(expl) > 1 def test_Sequence(self): if sys.version_info >= (3, 3): import collections.abc as collections_abc else: import collections as collections_abc if not hasattr(collections_abc, "MutableSequence"): pytest.skip("cannot import MutableSequence") MutableSequence = collections_abc.MutableSequence class TestSequence(MutableSequence): # works with a Sequence subclass def __init__(self, iterable): self.elements = list(iterable) def __getitem__(self, item): return self.elements[item] def __len__(self): return len(self.elements) def __setitem__(self, item, value): pass def __delitem__(self, item): pass def insert(self, item, index): pass expl = callequal(TestSequence([0, 1]), list([0, 2])) assert len(expl) > 1 def test_list_tuples(self): expl = callequal([], [(1, 2)]) assert len(expl) > 1 expl = callequal([(1, 2)], []) assert len(expl) > 1 def test_repr_verbose(self): class Nums: def __init__(self, nums): self.nums = nums def __repr__(self): return str(self.nums) list_x = list(range(5000)) list_y = list(range(5000)) list_y[len(list_y) // 2] = 3 nums_x = Nums(list_x) nums_y = Nums(list_y) assert callequal(nums_x, nums_y) is None expl = callequal(nums_x, nums_y, verbose=1) assert "-" + repr(nums_x) in expl assert "+" + repr(nums_y) in expl expl = callequal(nums_x, nums_y, verbose=2) assert "-" + repr(nums_x) in expl assert "+" + repr(nums_y) in expl def test_list_bad_repr(self): class A(object): def __repr__(self): raise ValueError(42) expl = callequal([], [A()]) assert "ValueError" in "".join(expl) expl = callequal({}, {"1": A()}) assert "faulty" in "".join(expl) def test_one_repr_empty(self): """ the faulty empty string repr did trigger an unbound local error in _diff_text """ class A(str): def __repr__(self): return "" expl = callequal(A(), "") assert not expl def test_repr_no_exc(self): expl = " ".join(callequal("foo", "bar")) assert "raised in repr()" not in expl def test_unicode(self): left = u"£€" right = u"£" expl = callequal(left, right) assert expl[0] == u"'£€' == '£'" assert expl[1] == u"- £€" assert expl[2] == u"+ £" def test_nonascii_text(self): """ :issue: 877 non ascii python2 str caused a UnicodeDecodeError """ class A(str): def __repr__(self): return "\xff" expl = callequal(A(), "1") if PY3: assert expl == ["ÿ == '1'", "+ 1"] else: assert expl == [u"\ufffd == '1'", u"+ 1"] def test_format_nonascii_explanation(self): assert util.format_explanation("λ") def test_mojibake(self): # issue 429 left = b"e" right = b"\xc3\xa9" expl = callequal(left, right) for line in expl: assert isinstance(line, six.text_type) msg = u"\n".join(expl) assert msg class TestAssert_reprcompare_dataclass(object): @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") def test_dataclasses(self, testdir): p = testdir.copy_example("dataclasses/test_compare_dataclasses.py") result = testdir.runpytest(p) result.assert_outcomes(failed=1, passed=0) result.stdout.fnmatch_lines( [ "*Omitting 1 identical items, use -vv to show*", "*Differing attributes:*", "*field_b: 'b' != 'c'*", ] ) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") def test_dataclasses_verbose(self, testdir): p = testdir.copy_example("dataclasses/test_compare_dataclasses_verbose.py") result = testdir.runpytest(p, "-vv") result.assert_outcomes(failed=1, passed=0) result.stdout.fnmatch_lines( [ "*Matching attributes:*", "*['field_a']*", "*Differing attributes:*", "*field_b: 'b' != 'c'*", ] ) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") def test_dataclasses_with_attribute_comparison_off(self, testdir): p = testdir.copy_example( "dataclasses/test_compare_dataclasses_field_comparison_off.py" ) result = testdir.runpytest(p, "-vv") result.assert_outcomes(failed=0, passed=1) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") def test_comparing_two_different_data_classes(self, testdir): p = testdir.copy_example( "dataclasses/test_compare_two_different_dataclasses.py" ) result = testdir.runpytest(p, "-vv") result.assert_outcomes(failed=0, passed=1) class TestAssert_reprcompare_attrsclass(object): def test_attrs(self): @attr.s class SimpleDataObject(object): field_a = attr.ib() field_b = attr.ib() left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "c") lines = callequal(left, right) assert lines[1].startswith("Omitting 1 identical item") assert "Matching attributes" not in lines for line in lines[1:]: assert "field_a" not in line def test_attrs_verbose(self): @attr.s class SimpleDataObject(object): field_a = attr.ib() field_b = attr.ib() left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "c") lines = callequal(left, right, verbose=2) assert lines[1].startswith("Matching attributes:") assert "Omitting" not in lines[1] assert lines[2] == "['field_a']" def test_attrs_with_attribute_comparison_off(self): @attr.s class SimpleDataObject(object): field_a = attr.ib() field_b = attr.ib(**{ATTRS_EQ_FIELD: False}) left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "b") lines = callequal(left, right, verbose=2) assert lines[1].startswith("Matching attributes:") assert "Omitting" not in lines[1] assert lines[2] == "['field_a']" for line in lines[2:]: assert "field_b" not in line def test_comparing_two_different_attrs_classes(self): @attr.s class SimpleDataObjectOne(object): field_a = attr.ib() field_b = attr.ib() @attr.s class SimpleDataObjectTwo(object): field_a = attr.ib() field_b = attr.ib() left = SimpleDataObjectOne(1, "b") right = SimpleDataObjectTwo(1, "c") lines = callequal(left, right) assert lines is None class TestFormatExplanation(object): def test_special_chars_full(self, testdir): # Issue 453, for the bug this would raise IndexError testdir.makepyfile( """ def test_foo(): assert '\\n}' == '' """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines(["*AssertionError*"]) def test_fmt_simple(self): expl = "assert foo" assert util.format_explanation(expl) == "assert foo" def test_fmt_where(self): expl = "\n".join(["assert 1", "{1 = foo", "} == 2"]) res = "\n".join(["assert 1 == 2", " + where 1 = foo"]) assert util.format_explanation(expl) == res def test_fmt_and(self): expl = "\n".join(["assert 1", "{1 = foo", "} == 2", "{2 = bar", "}"]) res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + and 2 = bar"]) assert util.format_explanation(expl) == res def test_fmt_where_nested(self): expl = "\n".join(["assert 1", "{1 = foo", "{foo = bar", "}", "} == 2"]) res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + where foo = bar"]) assert util.format_explanation(expl) == res def test_fmt_newline(self): expl = "\n".join(['assert "foo" == "bar"', "~- foo", "~+ bar"]) res = "\n".join(['assert "foo" == "bar"', " - foo", " + bar"]) assert util.format_explanation(expl) == res def test_fmt_newline_escaped(self): expl = "\n".join(["assert foo == bar", "baz"]) res = "assert foo == bar\\nbaz" assert util.format_explanation(expl) == res def test_fmt_newline_before_where(self): expl = "\n".join( [ "the assertion message here", ">assert 1", "{1 = foo", "} == 2", "{2 = bar", "}", ] ) res = "\n".join( [ "the assertion message here", "assert 1 == 2", " + where 1 = foo", " + and 2 = bar", ] ) assert util.format_explanation(expl) == res def test_fmt_multi_newline_before_where(self): expl = "\n".join( [ "the assertion", "~message here", ">assert 1", "{1 = foo", "} == 2", "{2 = bar", "}", ] ) res = "\n".join( [ "the assertion", " message here", "assert 1 == 2", " + where 1 = foo", " + and 2 = bar", ] ) assert util.format_explanation(expl) == res class TestTruncateExplanation(object): """ Confirm assertion output is truncated as expected """ # The number of lines in the truncation explanation message. Used # to calculate that results have the expected length. LINES_IN_TRUNCATION_MSG = 2 def test_doesnt_truncate_when_input_is_empty_list(self): expl = [] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100) assert result == expl def test_doesnt_truncate_at_when_input_is_5_lines_and_LT_max_chars(self): expl = ["a" * 100 for x in range(5)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result == expl def test_truncates_at_8_lines_when_given_list_of_empty_strings(self): expl = ["" for x in range(50)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "43 lines hidden" in result[-1] last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_8_lines_when_first_8_lines_are_LT_max_chars(self): expl = ["a" for x in range(100)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "93 lines hidden" in result[-1] last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_8_lines_when_first_8_lines_are_EQ_max_chars(self): expl = ["a" * 80 for x in range(16)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80) assert result != expl assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "9 lines hidden" in result[-1] last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_4_lines_when_first_4_lines_are_GT_max_chars(self): expl = ["a" * 250 for x in range(10)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=999) assert result != expl assert len(result) == 4 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "7 lines hidden" in result[-1] last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_truncates_at_1_line_when_first_line_is_GT_max_chars(self): expl = ["a" * 250 for x in range(1000)] result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100) assert result != expl assert len(result) == 1 + self.LINES_IN_TRUNCATION_MSG assert "Full output truncated" in result[-1] assert "1000 lines hidden" in result[-1] last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1] assert last_line_before_trunc_msg.endswith("...") def test_full_output_truncated(self, monkeypatch, testdir): """ Test against full runpytest() output. """ line_count = 7 line_len = 100 expected_truncated_lines = 2 testdir.makepyfile( r""" def test_many_lines(): a = list([str(i)[0] * %d for i in range(%d)]) b = a[::2] a = '\n'.join(map(str, a)) b = '\n'.join(map(str, b)) assert a == b """ % (line_len, line_count) ) monkeypatch.delenv("CI", raising=False) result = testdir.runpytest() # without -vv, truncate the message showing a few diff lines only result.stdout.fnmatch_lines( [ "*- 1*", "*- 3*", "*- 5*", "*truncated (%d lines hidden)*use*-vv*" % expected_truncated_lines, ] ) result = testdir.runpytest("-vv") result.stdout.fnmatch_lines(["* 6*"]) monkeypatch.setenv("CI", "1") result = testdir.runpytest() result.stdout.fnmatch_lines(["* 6*"]) def test_python25_compile_issue257(testdir): testdir.makepyfile( """ def test_rewritten(): assert 1 == 2 # some comment """ ) result = testdir.runpytest() assert result.ret == 1 result.stdout.fnmatch_lines( """ *E*assert 1 == 2* *1 failed* """ ) def test_rewritten(testdir): testdir.makepyfile( """ def test_rewritten(): assert "@py_builtins" in globals() """ ) assert testdir.runpytest().ret == 0 def test_reprcompare_notin(): config = mock_config() detail = plugin.pytest_assertrepr_compare(config, "not in", "foo", "aaafoobbb")[1:] assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"] def test_reprcompare_whitespaces(): config = mock_config() detail = plugin.pytest_assertrepr_compare(config, "==", "\r\n", "\n") assert detail == [ r"'\r\n' == '\n'", r"Strings contain only whitespace, escaping them using repr()", r"- '\r\n'", r"? --", r"+ '\n'", ] def test_pytest_assertrepr_compare_integration(testdir): testdir.makepyfile( """ def test_hello(): x = set(range(100)) y = x.copy() y.remove(50) assert x == y """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["*def test_hello():*", "*assert x == y*", "*E*Extra items*left*", "*E*50*"] ) def test_sequence_comparison_uses_repr(testdir): testdir.makepyfile( """ def test_hello(): x = set("hello x") y = set("hello y") assert x == y """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*def test_hello():*", "*assert x == y*", "*E*Extra items*left*", "*E*'x'*", "*E*Extra items*right*", "*E*'y'*", ] ) def test_assertrepr_loaded_per_dir(testdir): testdir.makepyfile(test_base=["def test_base(): assert 1 == 2"]) a = testdir.mkdir("a") a_test = a.join("test_a.py") a_test.write("def test_a(): assert 1 == 2") a_conftest = a.join("conftest.py") a_conftest.write('def pytest_assertrepr_compare(): return ["summary a"]') b = testdir.mkdir("b") b_test = b.join("test_b.py") b_test.write("def test_b(): assert 1 == 2") b_conftest = b.join("conftest.py") b_conftest.write('def pytest_assertrepr_compare(): return ["summary b"]') result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*def test_base():*", "*E*assert 1 == 2*", "*def test_a():*", "*E*assert summary a*", "*def test_b():*", "*E*assert summary b*", ] ) def test_assertion_options(testdir): testdir.makepyfile( """ def test_hello(): x = 3 assert x == 4 """ ) result = testdir.runpytest() assert "3 == 4" in result.stdout.str() result = testdir.runpytest_subprocess("--assert=plain") assert "3 == 4" not in result.stdout.str() def test_triple_quoted_string_issue113(testdir): testdir.makepyfile( """ def test_hello(): assert "" == ''' '''""" ) result = testdir.runpytest("--fulltrace") result.stdout.fnmatch_lines(["*1 failed*"]) assert "SyntaxError" not in result.stdout.str() def test_traceback_failure(testdir): p1 = testdir.makepyfile( """ def g(): return 2 def f(x): assert x == g() def test_onefails(): f(3) """ ) result = testdir.runpytest(p1, "--tb=long") result.stdout.fnmatch_lines( [ "*test_traceback_failure.py F*", "====* FAILURES *====", "____*____", "", " def test_onefails():", "> f(3)", "", "*test_*.py:6: ", "_ _ _ *", # "", " def f(x):", "> assert x == g()", "E assert 3 == 2", "E + where 2 = g()", "", "*test_traceback_failure.py:4: AssertionError", ] ) result = testdir.runpytest(p1) # "auto" result.stdout.fnmatch_lines( [ "*test_traceback_failure.py F*", "====* FAILURES *====", "____*____", "", " def test_onefails():", "> f(3)", "", "*test_*.py:6: ", "", " def f(x):", "> assert x == g()", "E assert 3 == 2", "E + where 2 = g()", "", "*test_traceback_failure.py:4: AssertionError", ] ) @pytest.mark.skipif( sys.version_info[:2] <= (3, 3), reason="Python 3.4+ shows chained exceptions on multiprocess", ) def test_exception_handling_no_traceback(testdir): """ Handle chain exceptions in tasks submitted by the multiprocess module (#1984). """ p1 = testdir.makepyfile( """ from multiprocessing import Pool def process_task(n): assert n == 10 def multitask_job(): tasks = [1] with Pool(processes=1) as pool: pool.map(process_task, tasks) def test_multitask_job(): multitask_job() """ ) result = testdir.runpytest(p1, "--tb=long") result.stdout.fnmatch_lines( [ "====* FAILURES *====", "*multiprocessing.pool.RemoteTraceback:*", "Traceback (most recent call last):", "*assert n == 10", "The above exception was the direct cause of the following exception:", "> * multitask_job()", ] ) @pytest.mark.skipif( "'__pypy__' in sys.builtin_module_names or sys.platform.startswith('java')" ) def test_warn_missing(testdir): testdir.makepyfile("") result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h") result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"]) result = testdir.run(sys.executable, "-OO", "-m", "pytest") result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"]) def test_recursion_source_decode(testdir): testdir.makepyfile( """ def test_something(): pass """ ) testdir.makeini( """ [pytest] python_files = *.py """ ) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines( """ """ ) def test_AssertionError_message(testdir): testdir.makepyfile( """ def test_hello(): x,y = 1,2 assert 0, (x,y) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( """ *def test_hello* *assert 0, (x,y)* *AssertionError: (1, 2)* """ ) @pytest.mark.skipif(PY3, reason="This bug does not exist on PY3") def test_set_with_unsortable_elements(): # issue #718 class UnsortableKey(object): def __init__(self, name): self.name = name def __lt__(self, other): raise RuntimeError() def __repr__(self): return "repr({})".format(self.name) def __eq__(self, other): return self.name == other.name def __hash__(self): return hash(self.name) left_set = {UnsortableKey(str(i)) for i in range(1, 3)} right_set = {UnsortableKey(str(i)) for i in range(2, 4)} expl = callequal(left_set, right_set, verbose=True) # skip first line because it contains the "construction" of the set, which does not have a guaranteed order expl = expl[1:] dedent = textwrap.dedent( """ Extra items in the left set: repr(1) Extra items in the right set: repr(3) Full diff (fallback to calling repr on each item): - repr(1) repr(2) + repr(3) """ ).strip() assert "\n".join(expl) == dedent def test_diff_newline_at_end(monkeypatch, testdir): testdir.makepyfile( r""" def test_diff(): assert 'asdf' == 'asdf\n' """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( r""" *assert 'asdf' == 'asdf\n' * - asdf * + asdf * ? + """ ) @pytest.mark.filterwarnings("default") def test_assert_tuple_warning(testdir): msg = "assertion is always true" testdir.makepyfile( """ def test_tuple(): assert(False, 'you shall not pass') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*test_assert_tuple_warning.py:2:*{}*".format(msg)]) # tuples with size != 2 should not trigger the warning testdir.makepyfile( """ def test_tuple(): assert () """ ) result = testdir.runpytest() assert msg not in result.stdout.str() def test_assert_indirect_tuple_no_warning(testdir): testdir.makepyfile( """ def test_tuple(): tpl = ('foo', 'bar') assert tpl """ ) result = testdir.runpytest("-rw") output = "\n".join(result.stdout.lines) assert "WR1" not in output def test_assert_with_unicode(monkeypatch, testdir): testdir.makepyfile( u""" # -*- coding: utf-8 -*- def test_unicode(): assert u'유니코드' == u'Unicode' """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*AssertionError*"]) def test_raise_unprintable_assertion_error(testdir): testdir.makepyfile( r""" def test_raise_assertion_error(): raise AssertionError('\xff') """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [r"> raise AssertionError('\xff')", "E AssertionError: *"] ) def test_raise_assertion_error_raisin_repr(testdir): testdir.makepyfile( u""" class RaisingRepr(object): def __repr__(self): raise Exception() def test_raising_repr(): raise AssertionError(RaisingRepr()) """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["E AssertionError: "] ) def test_issue_1944(testdir): testdir.makepyfile( """ def f(): return assert f() == 10 """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 error*"]) assert ( "AttributeError: 'Module' object has no attribute '_obj'" not in result.stdout.str() ) def test_exit_from_assertrepr_compare(monkeypatch): def raise_exit(obj): outcomes.exit("Quitting debugger") monkeypatch.setattr(util, "istext", raise_exit) with pytest.raises(outcomes.Exit, match="Quitting debugger"): callequal(1, 1) pytest-4.6.9/testing/test_collection.py0000664000372000037200000012305713604173400021153 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import pprint import sys import textwrap import py import pytest from _pytest.main import _in_venv from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import Session class TestCollector(object): def test_collect_versus_item(self): from pytest import Collector, Item assert not issubclass(Collector, Item) assert not issubclass(Item, Collector) def test_check_equality(self, testdir): modcol = testdir.getmodulecol( """ def test_pass(): pass def test_fail(): assert 0 """ ) fn1 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn1, pytest.Function) fn2 = testdir.collect_by_name(modcol, "test_pass") assert isinstance(fn2, pytest.Function) assert fn1 == fn2 assert fn1 != modcol if sys.version_info < (3, 0): assert cmp(fn1, fn2) == 0 # NOQA assert hash(fn1) == hash(fn2) fn3 = testdir.collect_by_name(modcol, "test_fail") assert isinstance(fn3, pytest.Function) assert not (fn1 == fn3) assert fn1 != fn3 for fn in fn1, fn2, fn3: assert fn != 3 assert fn != modcol assert fn != [1, 2, 3] assert [1, 2, 3] != fn assert modcol != fn def test_getparent(self, testdir): modcol = testdir.getmodulecol( """ class TestClass(object): def test_foo(): pass """ ) cls = testdir.collect_by_name(modcol, "TestClass") fn = testdir.collect_by_name(testdir.collect_by_name(cls, "()"), "test_foo") parent = fn.getparent(pytest.Module) assert parent is modcol parent = fn.getparent(pytest.Function) assert parent is fn parent = fn.getparent(pytest.Class) assert parent is cls def test_getcustomfile_roundtrip(self, testdir): hello = testdir.makefile(".xxx", hello="world") testdir.makepyfile( conftest=""" import pytest class CustomFile(pytest.File): pass def pytest_collect_file(path, parent): if path.ext == ".xxx": return CustomFile(path, parent=parent) """ ) node = testdir.getpathnode(hello) assert isinstance(node, pytest.File) assert node.name == "hello.xxx" nodes = node.session.perform_collect([node.nodeid], genitems=False) assert len(nodes) == 1 assert isinstance(nodes[0], pytest.File) def test_can_skip_class_with_test_attr(self, testdir): """Assure test class is skipped when using `__test__=False` (See #2007).""" testdir.makepyfile( """ class TestFoo(object): __test__ = False def __init__(self): pass def test_foo(): assert True """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["collected 0 items", "*no tests ran in*"]) class TestCollectFS(object): def test_ignored_certain_directories(self, testdir): tmpdir = testdir.tmpdir tmpdir.ensure("build", "test_notfound.py") tmpdir.ensure("dist", "test_notfound.py") tmpdir.ensure("_darcs", "test_notfound.py") tmpdir.ensure("CVS", "test_notfound.py") tmpdir.ensure("{arch}", "test_notfound.py") tmpdir.ensure(".whatever", "test_notfound.py") tmpdir.ensure(".bzr", "test_notfound.py") tmpdir.ensure("normal", "test_found.py") for x in tmpdir.visit("test_*.py"): x.write("def test_hello(): pass") result = testdir.runpytest("--collect-only") s = result.stdout.str() assert "test_notfound" not in s assert "test_found" in s @pytest.mark.parametrize( "fname", ( "activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1", ), ) def test_ignored_virtualenvs(self, testdir, fname): bindir = "Scripts" if sys.platform.startswith("win") else "bin" testdir.tmpdir.ensure("virtual", bindir, fname) testfile = testdir.tmpdir.ensure("virtual", "test_invenv.py") testfile.write("def test_hello(): pass") # by default, ignore tests inside a virtualenv result = testdir.runpytest() assert "test_invenv" not in result.stdout.str() # allow test collection if user insists result = testdir.runpytest("--collect-in-virtualenv") assert "test_invenv" in result.stdout.str() # allow test collection if user directly passes in the directory result = testdir.runpytest("virtual") assert "test_invenv" in result.stdout.str() @pytest.mark.parametrize( "fname", ( "activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1", ), ) def test_ignored_virtualenvs_norecursedirs_precedence(self, testdir, fname): bindir = "Scripts" if sys.platform.startswith("win") else "bin" # norecursedirs takes priority testdir.tmpdir.ensure(".virtual", bindir, fname) testfile = testdir.tmpdir.ensure(".virtual", "test_invenv.py") testfile.write("def test_hello(): pass") result = testdir.runpytest("--collect-in-virtualenv") assert "test_invenv" not in result.stdout.str() # ...unless the virtualenv is explicitly given on the CLI result = testdir.runpytest("--collect-in-virtualenv", ".virtual") assert "test_invenv" in result.stdout.str() @pytest.mark.parametrize( "fname", ( "activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1", ), ) def test__in_venv(self, testdir, fname): """Directly test the virtual env detection function""" bindir = "Scripts" if sys.platform.startswith("win") else "bin" # no bin/activate, not a virtualenv base_path = testdir.tmpdir.mkdir("venv") assert _in_venv(base_path) is False # with bin/activate, totally a virtualenv base_path.ensure(bindir, fname) assert _in_venv(base_path) is True def test_custom_norecursedirs(self, testdir): testdir.makeini( """ [pytest] norecursedirs = mydir xyz* """ ) tmpdir = testdir.tmpdir tmpdir.ensure("mydir", "test_hello.py").write("def test_1(): pass") tmpdir.ensure("xyz123", "test_2.py").write("def test_2(): 0/0") tmpdir.ensure("xy", "test_ok.py").write("def test_3(): pass") rec = testdir.inline_run() rec.assertoutcome(passed=1) rec = testdir.inline_run("xyz123/test_2.py") rec.assertoutcome(failed=1) def test_testpaths_ini(self, testdir, monkeypatch): testdir.makeini( """ [pytest] testpaths = gui uts """ ) tmpdir = testdir.tmpdir tmpdir.ensure("env", "test_1.py").write("def test_env(): pass") tmpdir.ensure("gui", "test_2.py").write("def test_gui(): pass") tmpdir.ensure("uts", "test_3.py").write("def test_uts(): pass") # executing from rootdir only tests from `testpaths` directories # are collected items, reprec = testdir.inline_genitems("-v") assert [x.name for x in items] == ["test_gui", "test_uts"] # check that explicitly passing directories in the command-line # collects the tests for dirname in ("env", "gui", "uts"): items, reprec = testdir.inline_genitems(tmpdir.join(dirname)) assert [x.name for x in items] == ["test_%s" % dirname] # changing cwd to each subdirectory and running pytest without # arguments collects the tests in that directory normally for dirname in ("env", "gui", "uts"): monkeypatch.chdir(testdir.tmpdir.join(dirname)) items, reprec = testdir.inline_genitems() assert [x.name for x in items] == ["test_%s" % dirname] class TestCollectPluginHookRelay(object): def test_pytest_collect_file(self, testdir): wascalled = [] class Plugin(object): def pytest_collect_file(self, path, parent): if not path.basename.startswith("."): # Ignore hidden files, e.g. .testmondata. wascalled.append(path) testdir.makefile(".abc", "xyz") pytest.main([testdir.tmpdir], plugins=[Plugin()]) assert len(wascalled) == 1 assert wascalled[0].ext == ".abc" def test_pytest_collect_directory(self, testdir): wascalled = [] class Plugin(object): def pytest_collect_directory(self, path, parent): wascalled.append(path.basename) testdir.mkdir("hello") testdir.mkdir("world") pytest.main(testdir.tmpdir, plugins=[Plugin()]) assert "hello" in wascalled assert "world" in wascalled class TestPrunetraceback(object): def test_custom_repr_failure(self, testdir): p = testdir.makepyfile( """ import not_exists """ ) testdir.makeconftest( """ import pytest def pytest_collect_file(path, parent): return MyFile(path, parent) class MyError(Exception): pass class MyFile(pytest.File): def collect(self): raise MyError() def repr_failure(self, excinfo): if excinfo.errisinstance(MyError): return "hello world" return pytest.File.repr_failure(self, excinfo) """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*ERROR collecting*", "*hello world*"]) @pytest.mark.xfail(reason="other mechanism for adding to reporting needed") def test_collect_report_postprocessing(self, testdir): p = testdir.makepyfile( """ import not_exists """ ) testdir.makeconftest( """ import pytest @pytest.hookimpl(hookwrapper=True) def pytest_make_collect_report(): outcome = yield rep = outcome.get_result() rep.headerlines += ["header1"] outcome.force_result(rep) """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*ERROR collecting*", "*header1*"]) class TestCustomConftests(object): def test_ignore_collect_path(self, testdir): testdir.makeconftest( """ def pytest_ignore_collect(path, config): return path.basename.startswith("x") or \ path.basename == "test_one.py" """ ) sub = testdir.mkdir("xy123") sub.ensure("test_hello.py").write("syntax error") sub.join("conftest.py").write("syntax error") testdir.makepyfile("def test_hello(): pass") testdir.makepyfile(test_one="syntax error") result = testdir.runpytest("--fulltrace") assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) def test_ignore_collect_not_called_on_argument(self, testdir): testdir.makeconftest( """ def pytest_ignore_collect(path, config): return True """ ) p = testdir.makepyfile("def test_hello(): pass") result = testdir.runpytest(p) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED result.stdout.fnmatch_lines(["*collected 0 items*"]) def test_collectignore_exclude_on_option(self, testdir): testdir.makeconftest( """ collect_ignore = ['hello', 'test_world.py'] def pytest_addoption(parser): parser.addoption("--XX", action="store_true", default=False) def pytest_configure(config): if config.getvalue("XX"): collect_ignore[:] = [] """ ) testdir.mkdir("hello") testdir.makepyfile(test_world="def test_hello(): pass") result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED assert "passed" not in result.stdout.str() result = testdir.runpytest("--XX") assert result.ret == 0 assert "passed" in result.stdout.str() def test_collectignoreglob_exclude_on_option(self, testdir): testdir.makeconftest( """ collect_ignore_glob = ['*w*l[dt]*'] def pytest_addoption(parser): parser.addoption("--XX", action="store_true", default=False) def pytest_configure(config): if config.getvalue("XX"): collect_ignore_glob[:] = [] """ ) testdir.makepyfile(test_world="def test_hello(): pass") testdir.makepyfile(test_welt="def test_hallo(): pass") result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED result.stdout.fnmatch_lines(["*collected 0 items*"]) result = testdir.runpytest("--XX") assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) def test_pytest_fs_collect_hooks_are_seen(self, testdir): testdir.makeconftest( """ import pytest class MyModule(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule(path, parent) """ ) testdir.mkdir("sub") testdir.makepyfile("def test_x(): pass") result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*MyModule*", "*test_x*"]) def test_pytest_collect_file_from_sister_dir(self, testdir): sub1 = testdir.mkpydir("sub1") sub2 = testdir.mkpydir("sub2") conf1 = testdir.makeconftest( """ import pytest class MyModule1(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule1(path, parent) """ ) conf1.move(sub1.join(conf1.basename)) conf2 = testdir.makeconftest( """ import pytest class MyModule2(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": return MyModule2(path, parent) """ ) conf2.move(sub2.join(conf2.basename)) p = testdir.makepyfile("def test_x(): pass") p.copy(sub1.join(p.basename)) p.copy(sub2.join(p.basename)) result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["*MyModule1*", "*MyModule2*", "*test_x*"]) class TestSession(object): def test_parsearg(self, testdir): p = testdir.makepyfile("def test_func(): pass") subdir = testdir.mkdir("sub") subdir.ensure("__init__.py") target = subdir.join(p.basename) p.move(target) subdir.chdir() config = testdir.parseconfig(p.basename) rcol = Session(config=config) assert rcol.fspath == subdir parts = rcol._parsearg(p.basename) assert parts[0] == target assert len(parts) == 1 parts = rcol._parsearg(p.basename + "::test_func") assert parts[0] == target assert parts[1] == "test_func" assert len(parts) == 2 def test_collect_topdir(self, testdir): p = testdir.makepyfile("def test_func(): pass") id = "::".join([p.basename, "test_func"]) # XXX migrate to collectonly? (see below) config = testdir.parseconfig(id) topdir = testdir.tmpdir rcol = Session(config) assert topdir == rcol.fspath # rootid = rcol.nodeid # root2 = rcol.perform_collect([rcol.nodeid], genitems=False)[0] # assert root2 == rcol, rootid colitems = rcol.perform_collect([rcol.nodeid], genitems=False) assert len(colitems) == 1 assert colitems[0].fspath == p def get_reported_items(self, hookrec): """Return pytest.Item instances reported by the pytest_collectreport hook""" calls = hookrec.getcalls("pytest_collectreport") return [ x for call in calls for x in call.report.result if isinstance(x, pytest.Item) ] def test_collect_protocol_single_function(self, testdir): p = testdir.makepyfile("def test_func(): pass") id = "::".join([p.basename, "test_func"]) items, hookrec = testdir.inline_genitems(id) (item,) = items assert item.name == "test_func" newid = item.nodeid assert newid == id pprint.pprint(hookrec.calls) topdir = testdir.tmpdir # noqa hookrec.assert_contains( [ ("pytest_collectstart", "collector.fspath == topdir"), ("pytest_make_collect_report", "collector.fspath == topdir"), ("pytest_collectstart", "collector.fspath == p"), ("pytest_make_collect_report", "collector.fspath == p"), ("pytest_pycollect_makeitem", "name == 'test_func'"), ("pytest_collectreport", "report.result[0].name == 'test_func'"), ] ) # ensure we are reporting the collection of the single test item (#2464) assert [x.name for x in self.get_reported_items(hookrec)] == ["test_func"] def test_collect_protocol_method(self, testdir): p = testdir.makepyfile( """ class TestClass(object): def test_method(self): pass """ ) normid = p.basename + "::TestClass::test_method" for id in [p.basename, p.basename + "::TestClass", normid]: items, hookrec = testdir.inline_genitems(id) assert len(items) == 1 assert items[0].name == "test_method" newid = items[0].nodeid assert newid == normid # ensure we are reporting the collection of the single test item (#2464) assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] def test_collect_custom_nodes_multi_id(self, testdir): p = testdir.makepyfile("def test_func(): pass") testdir.makeconftest( """ import pytest class SpecialItem(pytest.Item): def runtest(self): return # ok class SpecialFile(pytest.File): def collect(self): return [SpecialItem(name="check", parent=self)] def pytest_collect_file(path, parent): if path.basename == %r: return SpecialFile(fspath=path, parent=parent) """ % p.basename ) id = p.basename items, hookrec = testdir.inline_genitems(id) pprint.pprint(hookrec.calls) assert len(items) == 2 hookrec.assert_contains( [ ("pytest_collectstart", "collector.fspath == collector.session.fspath"), ( "pytest_collectstart", "collector.__class__.__name__ == 'SpecialFile'", ), ("pytest_collectstart", "collector.__class__.__name__ == 'Module'"), ("pytest_pycollect_makeitem", "name == 'test_func'"), ("pytest_collectreport", "report.nodeid.startswith(p.basename)"), ] ) assert len(self.get_reported_items(hookrec)) == 2 def test_collect_subdir_event_ordering(self, testdir): p = testdir.makepyfile("def test_func(): pass") aaa = testdir.mkpydir("aaa") test_aaa = aaa.join("test_aaa.py") p.move(test_aaa) items, hookrec = testdir.inline_genitems() assert len(items) == 1 pprint.pprint(hookrec.calls) hookrec.assert_contains( [ ("pytest_collectstart", "collector.fspath == test_aaa"), ("pytest_pycollect_makeitem", "name == 'test_func'"), ("pytest_collectreport", "report.nodeid.startswith('aaa/test_aaa.py')"), ] ) def test_collect_two_commandline_args(self, testdir): p = testdir.makepyfile("def test_func(): pass") aaa = testdir.mkpydir("aaa") bbb = testdir.mkpydir("bbb") test_aaa = aaa.join("test_aaa.py") p.copy(test_aaa) test_bbb = bbb.join("test_bbb.py") p.move(test_bbb) id = "." items, hookrec = testdir.inline_genitems(id) assert len(items) == 2 pprint.pprint(hookrec.calls) hookrec.assert_contains( [ ("pytest_collectstart", "collector.fspath == test_aaa"), ("pytest_pycollect_makeitem", "name == 'test_func'"), ("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"), ("pytest_collectstart", "collector.fspath == test_bbb"), ("pytest_pycollect_makeitem", "name == 'test_func'"), ("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"), ] ) def test_serialization_byid(self, testdir): testdir.makepyfile("def test_func(): pass") items, hookrec = testdir.inline_genitems() assert len(items) == 1 (item,) = items items2, hookrec = testdir.inline_genitems(item.nodeid) (item2,) = items2 assert item2.name == item.name assert item2.fspath == item.fspath def test_find_byid_without_instance_parents(self, testdir): p = testdir.makepyfile( """ class TestClass(object): def test_method(self): pass """ ) arg = p.basename + "::TestClass::test_method" items, hookrec = testdir.inline_genitems(arg) assert len(items) == 1 (item,) = items assert item.nodeid.endswith("TestClass::test_method") # ensure we are reporting the collection of the single test item (#2464) assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] class Test_getinitialnodes(object): def test_global_file(self, testdir, tmpdir): x = tmpdir.ensure("x.py") with tmpdir.as_cwd(): config = testdir.parseconfigure(x) col = testdir.getnode(config, x) assert isinstance(col, pytest.Module) assert col.name == "x.py" assert col.parent.parent is None for col in col.listchain(): assert col.config is config def test_pkgfile(self, testdir): """Verify nesting when a module is within a package. The parent chain should match: Module -> Package -> Session. Session's parent should always be None. """ tmpdir = testdir.tmpdir subdir = tmpdir.join("subdir") x = subdir.ensure("x.py") subdir.ensure("__init__.py") with subdir.as_cwd(): config = testdir.parseconfigure(x) col = testdir.getnode(config, x) assert col.name == "x.py" assert isinstance(col, pytest.Module) assert isinstance(col.parent, pytest.Package) assert isinstance(col.parent.parent, pytest.Session) # session is batman (has no parents) assert col.parent.parent.parent is None for col in col.listchain(): assert col.config is config class Test_genitems(object): def test_check_collect_hashes(self, testdir): p = testdir.makepyfile( """ def test_1(): pass def test_2(): pass """ ) p.copy(p.dirpath(p.purebasename + "2" + ".py")) items, reprec = testdir.inline_genitems(p.dirpath()) assert len(items) == 4 for numi, i in enumerate(items): for numj, j in enumerate(items): if numj != numi: assert hash(i) != hash(j) assert i != j def test_example_items1(self, testdir): p = testdir.makepyfile( """ def testone(): pass class TestX(object): def testmethod_one(self): pass class TestY(TestX): pass """ ) items, reprec = testdir.inline_genitems(p) assert len(items) == 3 assert items[0].name == "testone" assert items[1].name == "testmethod_one" assert items[2].name == "testmethod_one" # let's also test getmodpath here assert items[0].getmodpath() == "testone" assert items[1].getmodpath() == "TestX.testmethod_one" assert items[2].getmodpath() == "TestY.testmethod_one" s = items[0].getmodpath(stopatmodule=False) assert s.endswith("test_example_items1.testone") print(s) def test_class_and_functions_discovery_using_glob(self, testdir): """ tests that python_classes and python_functions config options work as prefixes and glob-like patterns (issue #600). """ testdir.makeini( """ [pytest] python_classes = *Suite Test python_functions = *_test test """ ) p = testdir.makepyfile( """ class MyTestSuite(object): def x_test(self): pass class TestCase(object): def test_y(self): pass """ ) items, reprec = testdir.inline_genitems(p) ids = [x.getmodpath() for x in items] assert ids == ["MyTestSuite.x_test", "TestCase.test_y"] def test_matchnodes_two_collections_same_file(testdir): testdir.makeconftest( """ import pytest def pytest_configure(config): config.pluginmanager.register(Plugin2()) class Plugin2(object): def pytest_collect_file(self, path, parent): if path.ext == ".abc": return MyFile2(path, parent) def pytest_collect_file(path, parent): if path.ext == ".abc": return MyFile1(path, parent) class MyFile1(pytest.Item, pytest.File): def runtest(self): pass class MyFile2(pytest.File): def collect(self): return [Item2("hello", parent=self)] class Item2(pytest.Item): def runtest(self): pass """ ) p = testdir.makefile(".abc", "") result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) res = testdir.runpytest("%s::hello" % p.basename) res.stdout.fnmatch_lines(["*1 passed*"]) class TestNodekeywords(object): def test_no_under(self, testdir): modcol = testdir.getmodulecol( """ def test_pass(): pass def test_fail(): assert 0 """ ) values = list(modcol.keywords) assert modcol.name in values for x in values: assert not x.startswith("_") assert modcol.name in repr(modcol.keywords) def test_issue345(self, testdir): testdir.makepyfile( """ def test_should_not_be_selected(): assert False, 'I should not have been selected to run' def test___repr__(): pass """ ) reprec = testdir.inline_run("-k repr") reprec.assertoutcome(passed=1, failed=0) COLLECTION_ERROR_PY_FILES = dict( test_01_failure=""" def test_1(): assert False """, test_02_import_error=""" import asdfasdfasdf def test_2(): assert True """, test_03_import_error=""" import asdfasdfasdf def test_3(): assert True """, test_04_success=""" def test_4(): assert True """, ) def test_exit_on_collection_error(testdir): """Verify that all collection errors are collected and no tests executed""" testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest() assert res.ret == 2 res.stdout.fnmatch_lines( [ "collected 2 items / 2 errors", "*ERROR collecting test_02_import_error.py*", "*No module named *asdfa*", "*ERROR collecting test_03_import_error.py*", "*No module named *asdfa*", ] ) def test_exit_on_collection_with_maxfail_smaller_than_n_errors(testdir): """ Verify collection is aborted once maxfail errors are encountered ignoring further modules which would cause more collection errors. """ testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--maxfail=1") assert res.ret == 1 res.stdout.fnmatch_lines( ["*ERROR collecting test_02_import_error.py*", "*No module named *asdfa*"] ) assert "test_03" not in res.stdout.str() def test_exit_on_collection_with_maxfail_bigger_than_n_errors(testdir): """ Verify the test run aborts due to collection errors even if maxfail count of errors was not reached. """ testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--maxfail=4") assert res.ret == 2 res.stdout.fnmatch_lines( [ "collected 2 items / 2 errors", "*ERROR collecting test_02_import_error.py*", "*No module named *asdfa*", "*ERROR collecting test_03_import_error.py*", "*No module named *asdfa*", ] ) def test_continue_on_collection_errors(testdir): """ Verify tests are executed even when collection errors occur when the --continue-on-collection-errors flag is set """ testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--continue-on-collection-errors") assert res.ret == 1 res.stdout.fnmatch_lines( ["collected 2 items / 2 errors", "*1 failed, 1 passed, 2 error*"] ) def test_continue_on_collection_errors_maxfail(testdir): """ Verify tests are executed even when collection errors occur and that maxfail is honoured (including the collection error count). 4 tests: 2 collection errors + 1 failure + 1 success test_4 is never executed because the test run is with --maxfail=3 which means it is interrupted after the 2 collection errors + 1 failure. """ testdir.makepyfile(**COLLECTION_ERROR_PY_FILES) res = testdir.runpytest("--continue-on-collection-errors", "--maxfail=3") assert res.ret == 1 res.stdout.fnmatch_lines(["collected 2 items / 2 errors", "*1 failed, 2 error*"]) def test_fixture_scope_sibling_conftests(testdir): """Regression test case for https://github.com/pytest-dev/pytest/issues/2836""" foo_path = testdir.mkdir("foo") foo_path.join("conftest.py").write( textwrap.dedent( """\ import pytest @pytest.fixture def fix(): return 1 """ ) ) foo_path.join("test_foo.py").write("def test_foo(fix): assert fix == 1") # Tests in `food/` should not see the conftest fixture from `foo/` food_path = testdir.mkpydir("food") food_path.join("test_food.py").write("def test_food(fix): assert fix == 1") res = testdir.runpytest() assert res.ret == 1 res.stdout.fnmatch_lines( [ "*ERROR at setup of test_food*", "E*fixture 'fix' not found", "*1 passed, 1 error*", ] ) def test_collect_init_tests(testdir): """Check that we collect files from __init__.py files when they patch the 'python_files' (#3773)""" p = testdir.copy_example("collect/collect_init_tests") result = testdir.runpytest(p, "--collect-only") result.stdout.fnmatch_lines( [ "collected 2 items", "", " ", " ", " ", ] ) result = testdir.runpytest("./tests", "--collect-only") result.stdout.fnmatch_lines( [ "collected 2 items", "", " ", " ", " ", ] ) # Ignores duplicates with "." and pkginit (#4310). result = testdir.runpytest("./tests", ".", "--collect-only") result.stdout.fnmatch_lines( [ "collected 2 items", "", " ", " ", " ", " ", ] ) # Same as before, but different order. result = testdir.runpytest(".", "tests", "--collect-only") result.stdout.fnmatch_lines( [ "collected 2 items", "", " ", " ", " ", " ", ] ) result = testdir.runpytest("./tests/test_foo.py", "--collect-only") result.stdout.fnmatch_lines( ["", " ", " "] ) assert "test_init" not in result.stdout.str() result = testdir.runpytest("./tests/__init__.py", "--collect-only") result.stdout.fnmatch_lines( ["", " ", " "] ) assert "test_foo" not in result.stdout.str() def test_collect_invalid_signature_message(testdir): """Check that we issue a proper message when we can't determine the signature of a test function (#4026). """ testdir.makepyfile( """ import pytest class TestCase: @pytest.fixture def fix(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( ["Could not determine arguments of *.fix *: invalid method signature"] ) def test_collect_handles_raising_on_dunder_class(testdir): """Handle proxy classes like Django's LazySettings that might raise on ``isinstance`` (#4266). """ testdir.makepyfile( """ class ImproperlyConfigured(Exception): pass class RaisesOnGetAttr(object): def raises(self): raise ImproperlyConfigured __class__ = property(raises) raises = RaisesOnGetAttr() def test_1(): pass """ ) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed in*"]) assert result.ret == 0 def test_collect_with_chdir_during_import(testdir): subdir = testdir.tmpdir.mkdir("sub") testdir.tmpdir.join("conftest.py").write( textwrap.dedent( """ import os os.chdir(%r) """ % (str(subdir),) ) ) testdir.makepyfile( """ def test_1(): import os assert os.getcwd() == %r """ % (str(subdir),) ) with testdir.tmpdir.as_cwd(): result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 passed in*"]) assert result.ret == 0 # Handles relative testpaths. testdir.makeini( """ [pytest] testpaths = . """ ) with testdir.tmpdir.as_cwd(): result = testdir.runpytest("--collect-only") result.stdout.fnmatch_lines(["collected 1 item"]) def test_collect_pyargs_with_testpaths(testdir, monkeypatch): testmod = testdir.mkdir("testmod") # NOTE: __init__.py is not collected since it does not match python_files. testmod.ensure("__init__.py").write("def test_func(): pass") testmod.ensure("test_file.py").write("def test_func(): pass") root = testdir.mkdir("root") root.ensure("pytest.ini").write( textwrap.dedent( """ [pytest] addopts = --pyargs testpaths = testmod """ ) ) monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir), prepend=os.pathsep) with root.as_cwd(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed in*"]) @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) def test_collect_symlink_file_arg(testdir): """Test that collecting a direct symlink, where the target does not match python_files works (#4325).""" real = testdir.makepyfile( real=""" def test_nodeid(request): assert request.node.nodeid == "real.py::test_nodeid" """ ) symlink = testdir.tmpdir.join("symlink.py") symlink.mksymlinkto(real) result = testdir.runpytest("-v", symlink) result.stdout.fnmatch_lines(["real.py::test_nodeid PASSED*", "*1 passed in*"]) assert result.ret == 0 @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) def test_collect_symlink_out_of_tree(testdir): """Test collection of symlink via out-of-tree rootdir.""" sub = testdir.tmpdir.join("sub") real = sub.join("test_real.py") real.write( textwrap.dedent( """ def test_nodeid(request): # Should not contain sub/ prefix. assert request.node.nodeid == "test_real.py::test_nodeid" """ ), ensure=True, ) out_of_tree = testdir.tmpdir.join("out_of_tree").ensure(dir=True) symlink_to_sub = out_of_tree.join("symlink_to_sub") symlink_to_sub.mksymlinkto(sub) sub.chdir() result = testdir.runpytest("-vs", "--rootdir=%s" % sub, symlink_to_sub) result.stdout.fnmatch_lines( [ # Should not contain "sub/"! "test_real.py::test_nodeid PASSED" ] ) assert result.ret == 0 def test_collectignore_via_conftest(testdir, monkeypatch): """collect_ignore in parent conftest skips importing child (issue #4592).""" tests = testdir.mkpydir("tests") tests.ensure("conftest.py").write("collect_ignore = ['ignore_me']") ignore_me = tests.mkdir("ignore_me") ignore_me.ensure("__init__.py") ignore_me.ensure("conftest.py").write("assert 0, 'should_not_be_called'") result = testdir.runpytest() assert result.ret == EXIT_NOTESTSCOLLECTED def test_collect_pkg_init_and_file_in_args(testdir): subdir = testdir.mkdir("sub") init = subdir.ensure("__init__.py") init.write("def test_init(): pass") p = subdir.ensure("test_file.py") p.write("def test_file(): pass") # NOTE: without "-o python_files=*.py" this collects test_file.py twice. # This changed/broke with "Add package scoped fixtures #2283" (2b1410895) # initially (causing a RecursionError). result = testdir.runpytest("-v", str(init), str(p)) result.stdout.fnmatch_lines( [ "sub/test_file.py::test_file PASSED*", "sub/test_file.py::test_file PASSED*", "*2 passed in*", ] ) result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init), str(p)) result.stdout.fnmatch_lines( [ "sub/__init__.py::test_init PASSED*", "sub/test_file.py::test_file PASSED*", "*2 passed in*", ] ) def test_collect_pkg_init_only(testdir): subdir = testdir.mkdir("sub") init = subdir.ensure("__init__.py") init.write("def test_init(): pass") result = testdir.runpytest(str(init)) result.stdout.fnmatch_lines(["*no tests ran in*"]) result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init)) result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"]) @pytest.mark.skipif( not hasattr(py.path.local, "mksymlinkto"), reason="symlink not available on this platform", ) @pytest.mark.parametrize("use_pkg", (True, False)) def test_collect_sub_with_symlinks(use_pkg, testdir): sub = testdir.mkdir("sub") if use_pkg: sub.ensure("__init__.py") sub.ensure("test_file.py").write("def test_file(): pass") # Create a broken symlink. sub.join("test_broken.py").mksymlinkto("test_doesnotexist.py") # Symlink that gets collected. sub.join("test_symlink.py").mksymlinkto("test_file.py") result = testdir.runpytest("-v", str(sub)) result.stdout.fnmatch_lines( [ "sub/test_file.py::test_file PASSED*", "sub/test_symlink.py::test_file PASSED*", "*2 passed in*", ] ) def test_collector_respects_tbstyle(testdir): p1 = testdir.makepyfile("assert 0") result = testdir.runpytest(p1, "--tb=native") assert result.ret == EXIT_INTERRUPTED result.stdout.fnmatch_lines( [ "*_ ERROR collecting test_collector_respects_tbstyle.py _*", "Traceback (most recent call last):", ' File "*/test_collector_respects_tbstyle.py", line 1, in ', " assert 0", "AssertionError: assert 0", "*! Interrupted: 1 errors during collection !*", "*= 1 error in *", ] ) pytest-4.6.9/testing/test_stepwise.py0000664000372000037200000001447713604173400020670 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import sys import pytest @pytest.fixture def stepwise_testdir(testdir): # Rather than having to modify our testfile between tests, we introduce # a flag for wether or not the second test should fail. testdir.makeconftest( """ def pytest_addoption(parser): group = parser.getgroup('general') group.addoption('--fail', action='store_true', dest='fail') group.addoption('--fail-last', action='store_true', dest='fail_last') """ ) # Create a simple test suite. testdir.makepyfile( test_a=""" def test_success_before_fail(): assert 1 def test_fail_on_flag(request): assert not request.config.getvalue('fail') def test_success_after_fail(): assert 1 def test_fail_last_on_flag(request): assert not request.config.getvalue('fail_last') def test_success_after_last_fail(): assert 1 """ ) testdir.makepyfile( test_b=""" def test_success(): assert 1 """ ) # customize cache directory so we don't use the tox's cache directory, which makes tests in this module flaky testdir.makeini( """ [pytest] cache_dir = .cache """ ) return testdir @pytest.fixture def error_testdir(testdir): testdir.makepyfile( test_a=""" def test_error(nonexisting_fixture): assert 1 def test_success_after_fail(): assert 1 """ ) return testdir @pytest.fixture def broken_testdir(testdir): testdir.makepyfile( working_testfile="def test_proper(): assert 1", broken_testfile="foobar" ) return testdir def test_run_without_stepwise(stepwise_testdir): result = stepwise_testdir.runpytest("-v", "--strict-markers", "--fail") result.stdout.fnmatch_lines(["*test_success_before_fail PASSED*"]) result.stdout.fnmatch_lines(["*test_fail_on_flag FAILED*"]) result.stdout.fnmatch_lines(["*test_success_after_fail PASSED*"]) def test_fail_and_continue_with_stepwise(stepwise_testdir): # Run the tests with a failing second test. result = stepwise_testdir.runpytest( "-v", "--strict-markers", "--stepwise", "--fail" ) assert not result.stderr.str() stdout = result.stdout.str() # Make sure we stop after first failing test. assert "test_success_before_fail PASSED" in stdout assert "test_fail_on_flag FAILED" in stdout assert "test_success_after_fail" not in stdout # "Fix" the test that failed in the last run and run it again. result = stepwise_testdir.runpytest("-v", "--strict-markers", "--stepwise") assert not result.stderr.str() stdout = result.stdout.str() # Make sure the latest failing test runs and then continues. assert "test_success_before_fail" not in stdout assert "test_fail_on_flag PASSED" in stdout assert "test_success_after_fail PASSED" in stdout def test_run_with_skip_option(stepwise_testdir): result = stepwise_testdir.runpytest( "-v", "--strict-markers", "--stepwise", "--stepwise-skip", "--fail", "--fail-last", ) assert not result.stderr.str() stdout = result.stdout.str() # Make sure first fail is ignore and second fail stops the test run. assert "test_fail_on_flag FAILED" in stdout assert "test_success_after_fail PASSED" in stdout assert "test_fail_last_on_flag FAILED" in stdout assert "test_success_after_last_fail" not in stdout def test_fail_on_errors(error_testdir): result = error_testdir.runpytest("-v", "--strict-markers", "--stepwise") assert not result.stderr.str() stdout = result.stdout.str() assert "test_error ERROR" in stdout assert "test_success_after_fail" not in stdout def test_change_testfile(stepwise_testdir): result = stepwise_testdir.runpytest( "-v", "--strict-markers", "--stepwise", "--fail", "test_a.py" ) assert not result.stderr.str() stdout = result.stdout.str() assert "test_fail_on_flag FAILED" in stdout # Make sure the second test run starts from the beginning, since the # test to continue from does not exist in testfile_b. result = stepwise_testdir.runpytest( "-v", "--strict-markers", "--stepwise", "test_b.py" ) assert not result.stderr.str() stdout = result.stdout.str() assert "test_success PASSED" in stdout @pytest.mark.parametrize("broken_first", [True, False]) def test_stop_on_collection_errors(broken_testdir, broken_first): """Stop during collection errors. Broken test first or broken test last actually surfaced a bug (#5444), so we test both situations.""" files = ["working_testfile.py", "broken_testfile.py"] if broken_first: files.reverse() result = broken_testdir.runpytest("-v", "--strict-markers", "--stepwise", *files) result.stdout.fnmatch_lines("*errors during collection*") def test_xfail_handling(testdir): """Ensure normal xfail is ignored, and strict xfail interrupts the session in sw mode (#5547) """ contents = """ import pytest def test_a(): pass @pytest.mark.xfail(strict={strict}) def test_b(): assert {assert_value} def test_c(): pass def test_d(): pass """ testdir.makepyfile(contents.format(assert_value="0", strict="False")) result = testdir.runpytest("--sw", "-v") result.stdout.fnmatch_lines( [ "*::test_a PASSED *", "*::test_b XFAIL *", "*::test_c PASSED *", "*::test_d PASSED *", "* 3 passed, 1 xfailed in *", ] ) testdir.makepyfile(contents.format(assert_value="1", strict="True")) result = testdir.runpytest("--sw", "-v") result.stdout.fnmatch_lines( [ "*::test_a PASSED *", "*::test_b FAILED *", "* Interrupted*", "* 1 failed, 1 passed in *", ] ) # because we are writing to the same file, mtime might not be affected enough to # invalidate the cache, making this next run flaky if not sys.dont_write_bytecode: testdir.tmpdir.join("__pycache__").remove() testdir.makepyfile(contents.format(assert_value="0", strict="True")) result = testdir.runpytest("--sw", "-v") result.stdout.fnmatch_lines( [ "*::test_b XFAIL *", "*::test_c PASSED *", "*::test_d PASSED *", "* 2 passed, 1 deselected, 1 xfailed in *", ] ) pytest-4.6.9/AUTHORS0000664000372000037200000000764613604173400015007 0ustar travistravis00000000000000Holger Krekel, holger at merlinux eu merlinux GmbH, Germany, office at merlinux eu Contributors include:: Aaron Coleman Abdeali JK Abhijeet Kasurde Adam Johnson Adam Uhlir Ahn Ki-Wook Akiomi Kamakura Alan Velasco Alexander Johnson Alexei Kozlenok Allan Feldman Aly Sivji Anatoly Bubenkoff Anders Hovmöller Andras Mitzki Andras Tim Andrea Cimatoribus Andreas Zeidler Andrey Paramonov Andrzej Ostrowski Andy Freeland Anthon van der Neut Anthony Shaw Anthony Sottile Anton Lodder Antony Lee Arel Cordero Armin Rigo Aron Coyle Aron Curzon Aviral Verma Aviv Palivoda Barney Gale Ben Webb Benjamin Peterson Bernard Pratz Bob Ippolito Brian Dorsey Brian Maissy Brian Okken Brianna Laugher Bruno Oliveira Cal Leeming Carl Friedrich Bolz Carlos Jenkins Ceridwen Charles Cloud Charnjit SiNGH (CCSJ) Chris Lamb Christian Boelsen Christian Fetzer Christian Theunert Christian Tismer Christopher Gilling Christopher Dignam Claudio Madotto CrazyMerlyn Cyrus Maden Damian Skrzypczak Dhiren Serai Daniel Grana Daniel Hahler Daniel Nuri Daniel Wandschneider Danielle Jenkins Dave Hunt David Díaz-Barquero David Mohr David Szotten David Vierra Daw-Ran Liou Denis Kirisov Diego Russo Dmitry Dygalo Dmitry Pribysh Duncan Betts Edison Gustavo Muenz Edoardo Batini Eduardo Schettino Eli Boyarski Elizaveta Shashkova Endre Galaczi Eric Hunsberger Eric Siegerman Erik M. Bray Evan Kepner Fabien Zarifian Fabio Zadrozny Feng Ma Florian Bruhin Floris Bruynooghe Gabriel Reis George Kussumoto Georgy Dyuldin Graham Horler Greg Price Grig Gheorghiu Grigorii Eremeev (budulianin) Guido Wesdorp Guoqiang Zhang Harald Armin Massa Henk-Jaap Wagenaar Hugo van Kemenade Hui Wang (coldnight) Ian Bicking Ian Lesperance Ilya Konstantinov Ionuț Turturică Iwan Briquemont Jaap Broekhuizen Jan Balster Janne Vanhala Jason R. Coombs Javier Domingo Cansino Javier Romero Jeff Rackauckas Jeff Widman Jenni Rinker John Eddie Ayson John Towler Jon Sonesen Jonas Obrist Jordan Guymon Jordan Moldow Jordan Speicher Joseph Hunkeler Joshua Bronson Jurko Gospodnetić Justyna Janczyszyn Kale Kundert Katarzyna Jachim Katerina Koukiou Kevin Cox Kevin J. Foley Kodi B. Arfer Kostis Anagnostopoulos Kristoffer Nordström Kyle Altendorf Lawrence Mitchell Lee Kamentsky Lev Maximov Llandy Riveron Del Risco Loic Esteve Lukas Bednar Luke Murphy Maciek Fijalkowski Maho Maik Figura Mandeep Bhutani Manuel Krebber Marc Schlaich Marcelo Duarte Trevisani Marcin Bachry Mark Abramowitz Markus Unterwaditzer Martijn Faassen Martin Altmayer Martin K. Scherer Martin Prusse Mathieu Clabaut Matt Bachmann Matt Duck Matt Williams Matthias Hafner Maxim Filipenko mbyt Michael Aquilina Michael Birtwell Michael Droettboom Michael Seifert Michal Wajszczuk Mihai Capotă Mike Hoyle (hoylemd) Mike Lundy Miro Hrončok Nathaniel Waisbrot Ned Batchelder Neven Mundar Nicholas Devenish Nicholas Murphy Niclas Olofsson Nicolas Delaby Nikolay Kondratyev Oleg Pidsadnyi Oleg Sushchenko Oliver Bestwalter Omar Kohl Omer Hadari Ondřej Súkup Oscar Benjamin Patrick Hayes Paweł Adamczak Pedro Algarvio Pieter Mulder Piotr Banaszkiewicz Pulkit Goyal Punyashloka Biswal Quentin Pradet Ralf Schmitt Ran Benita Raphael Castaneda Raphael Pierzina Raquel Alegre Ravi Chandra Roberto Polli Roland Puntaier Romain Dorgueil Roman Bolshakov Ronny Pfannschmidt Ross Lawley Russel Winder Ryan Wooden Samuel Dion-Girardeau Samuel Searles-Bryant Samuele Pedroni Sankt Petersbug Segev Finer Serhii Mozghovyi Simon Gomizelj Skylar Downes Srinivas Reddy Thatiparthy Stefan Farmbauer Stefan Zimmermann Stefano Taschini Steffen Allner Stephan Obermann Sven-Hendrik Haase Tadek Teleżyński Takafumi Arakaki Tarcisio Fischer Tareq Alayan Ted Xiao Thomas Grainger Thomas Hisch Tim Strazny Tom Dalton Tom Viner Tomer Keren Trevor Bekolay Tyler Goodlet Tzu-ping Chung Vasily Kuznetsov Victor Maryama Victor Uriarte Vidar T. Fauske Virgil Dupras Vitaly Lashmanov Vlad Dragos Volodymyr Piskun Wil Cooley William Lee Wim Glenn Wouter van Ackooy Xuan Luong Xuecong Liao Zac Hatfield-Dodds Zoltán Máté