简体   繁体   English

如何在py.test中确定测试脚本的名称?

[英]How to determine the name of the test script in py.test?

I am running a small test py.test script, and I want to determine the name of the test script being used. 我正在运行一个小的测试py.test脚本,我想确定正在使用的测试脚本的名称。

The current script is run as 当前脚本运行为

pytest selenium/test_morph_viz.py  -s

but later it is supposed to be run without the argument. 但后来应该在没有参数的情况下运行它。 Part of the output of the run is as follows: 运行的部分输出如下:

platform linux2 -- Python 2.7.12, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /home/adietz/Projects/Invest/bsp-usecase-tests, inifile:
collected 1 item                                                                                                                                        

selenium/test_morph_viz.py E  

Within the code I define a setup method in which I want to get the name of the test script, ie 在代码中,我定义了一个setup方法,在该方法中,我想获取测试脚本的名称,即

selenium/test_morph_viz.py

How to do that? 怎么做?

I tried to use inspect or os.path.basename , but this only returns the path of a script in which I define other things. 我尝试使用inspectos.path.basename ,但这仅返回我在其中定义其他内容的脚本的路径。 So how to get the name of the script currently run in py.test? 那么如何获取当前在py.test中运行的脚本的名称呢?

Found the correct answer by chance: 偶然找到正确答案:

self.__module__

That gives you the module currently used. 这样就为您提供了当前使用的module

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

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