Is it possible to only test specific functions with doctest in a module?

亡梦爱人 提交于 2019-12-09 09:02:09

问题


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

  1. Write the tests for the functions.
  2. implement the functions code.
  3. If Tests pass, write more tests and more code.
  4. When the function is done move on to the next function to implement.

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.

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

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.


回答1:


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())

example found via google.



来源:https://stackoverflow.com/questions/10080157/is-it-possible-to-only-test-specific-functions-with-doctest-in-a-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!