【问题标题】:how to automatically change to pytest temporary directory for all doctests如何为所有 doctests 自动更改为 pytest 临时目录
【发布时间】:2017-10-26 19:06:47
【问题描述】:

我想在每个 doctest 开头更改为 pytest 临时目录,我想知道是否有一种方法可以自动执行此操作,而无需使用以下命令开始每个 doctest:

>>> tmp = getfixture('tmpdir')                                                                                            
>>> old = tmp.chdir()                                                                                                   

【问题讨论】:

    标签: python testing pytest doctest


    【解决方案1】:

    一切皆有可能:

    conftest.py:

    import pytest
    
    @pytest.fixture(autouse=True)
    def _docdir(request):
    
        # Trigger ONLY for the doctests.
        doctest_plugin = request.config.pluginmanager.getplugin("doctest")
        if isinstance(request.node, doctest_plugin.DoctestItem):
    
            # Get the fixture dynamically by its name.
            tmpdir = request.getfuncargvalue('tmpdir')
    
            # Chdir only for the duration of the test.
            with tmpdir.as_cwd():
                yield
    
        else:
            # For normal tests, we have to yield, since this is a yield-fixture.
            yield
    

    test_me.py:

    import os.path
    
    # Regular tests are NOT chdir'ed.
    def test_me():
        moddir = os.path.dirname(__file__)
        assert os.getcwd() == moddir
    

    import_me.py:

    import os, os.path
    
    # Doctests are chdir'ed.
    def some_func():
        """
        >>> 2+3
        5
        >>> os.getcwd().startswith('/private/')
        True
        """
        pass
    

    希望这能让您了解如何检测 doctest,以及如何在测试期间临时 chdir。


    另外,你还可以下断点,查看夹具中request.node.dtest的内容。这样,您可以将可选的 cmets/marks 添加到 docstring 或 doctest 行,并相应地执行:

    (Pdb++) pp request.node.dtest.docstring
    "\n    >>> 2+3\n    5\n    >>> os.getcwd().startswith('/private/')\n    True\n    "
    
    (Pdb++) pp request.node.dtest.examples[0].source
    '2+3\n'
    (Pdb++) pp request.node.dtest.examples[0].want
    '5\n'
    
    (Pdb++) pp request.node.dtest.examples[1].source
    "os.getcwd().startswith('/private/')\n"
    (Pdb++) pp request.node.dtest.examples[1].want
    'True\n'
    (Pdb++) pp request.node.dtest.examples[1].exc_msg
    None
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      相关资源
      最近更新 更多