简体   繁体   English

如何从夹具中读取测试函数的文档字符串?

[英]How to read the docstring of test functions from a fixture?

I was trying to get the docstrings of all the test functions from a fixture defined in conftest.py, as shown in the code below, so that they can be analyzed for purposes.我试图从 conftest.py 中定义的夹具中获取所有测试函数的文档字符串,如下面的代码所示,以便对其进行分析。

But, from here how can I access the __doc__ attribute of that function when the function is only available as a string ( request.node.name )??但是,当 function 只能作为字符串( request.node.name )使用时,如何从这里访问 function 的__doc__属性?

Is there a way to read docstrings through request OR from other default pytest fixtures??有没有办法通过request或从其他默认 pytest 装置读取文档字符串?

Contents of conftest.py conftest.py 的内容

  1 import pytest
  2 
  3 @pytest.fixture(scope='function', autouse=True)
  4 def publish_to_pubsub(request):
  5     print("\n\nSTARTED Test '{}'".format(request.node.name))
  6     test_name = request.node.name
        // Here -  need to get the docstring of this function . 
  7     
  9     def fin():
 12         print("COMPLETED Test '{}'\n".format(request.node.name))
 13 
 14     request.addfinalizer(fin)

Figured it out.弄清楚了。

Reference: docs.pytest.org/en/6.2.x/reference.html#request参考: docs.pytest.org/en/6.2.x/reference.html#request

print(request.function.__doc__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM