python __doc__修改_python-如何为所有doctest自动更改为pytest临时...

一切都可行:

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的内容.这样,您可以向文档字符串或文档测试行添加可选的注释/标记,并相应地执行以下操作:

(Pdb++) pp request.node.dtest.docstring

"

>>> 2+3

5

>>> os.getcwd().startswith('/private/')

True

"

(Pdb++) pp request.node.dtest.examples[0].source

'2+3

'

(Pdb++) pp request.node.dtest.examples[0].want

'5

'

(Pdb++) pp request.node.dtest.examples[1].source

"os.getcwd().startswith('/private/')

"

(Pdb++) pp request.node.dtest.examples[1].want

'True

'

(Pdb++) pp request.node.dtest.examples[1].exc_msg

None

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值