是否可以仅在模块中使用doctest测试特定功能? [英] Is it possible to only test specific functions with doctest in a module?

查看:82
本文介绍了是否可以仅在模块中使用doctest测试特定功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用doctest模块进行Python测试.此刻我做

I am trying to get into testing in Python using the doctest module. At the moment I do

  1. 编写功能测试.
  2. 实现功能代码.
  3. 如果测试通过,请编写更多测试和更多代码.
  4. 该功能完成后,继续执行下一个功能.

因此,在同一个模块中执行了3或4个(独立的)函数并进行了许多测试之后,我通过doctest获得了巨大的输出.而且有点烦人.

So after 3 or 4 (independent) functions in the same module with many tests I get a huge output by doctest. And it is a little annoysing.

有没有办法告诉doctest 不要测试功能a()b()c() ",以便仅运行未标记功能?

Is there a way to tell doctest "don't test functions a(), b() and c()", so that it runs only the unmarked functions?

我只找到了 doctest.SKIP 标志,足以满足我的需求.我将不得不在很多行中放置此标志.而且,如果我想再次检查带有 marked 的功能,则必须手动遍历代码并删除我在其中设置的所有标志.

I only found the doctest.SKIP flag, which is not sufficient for my needs. I would have to place this flag in a lot of lines. And if I would want to check a marked function again, I would have to go manually through the code and remove any flag I set inside.

推荐答案

看起来像您可以将函数传递给run_docstring_examples:

looks like you could pass the function to run_docstring_examples:

def f(a, b, c):
    '''
    >>> f(1,2,3)
    42
    '''

if __name__ == '__main__':
    import doctest
#    doctest.testmod()
    doctest.run_docstring_examples(f, globals())

找到

示例通过Google.

example found via google.

这篇关于是否可以仅在模块中使用doctest测试特定功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆