pytest.ini 文件运行

很多同伴在执行py文件时, 用的都是函数入口运行,如下

if __name__ == '__main__':
    pytest.main(['-s'])

因为我们在学习python时用的也都是此方法,但是无论是接口自动化还是web自动化,项目上一般都是使用 pytest.ini 文件配置运行方法。以下介绍使用方法

 

首先在项目根路径下建一个 pytest.ini 文件

 

 

然后在文件中配置我们想要的信息, 意思请看注释。

注意:如果运行报这个错误('gbk' codec can't decode byte 0x9a in position 12: illegal multibyte sequence),则需要将pytest.ini文件的编码格式修改为GBK

# [pytest]: 标记pytest.ini这个文件是配置文件
[pytest]

# 相当于pytest.mia([在这里面写的命令])
addopts = -s
# 指定运行文件路径,指定哪个目录下的用例,只会运行test开头的py文件
testpaths = ./testcases

# 自定义pytest文件名规则
python_files = test*.py
# 自定义pytest类名规则
python_classes = Test*
# 自定义pytest方法名规则
python_functions = test*

 

 

这时有同学可能会发现,如果使用这个配置文件,生成allure报告的命令该怎么写?如果没有人会使用allure报告请看这篇文章:https://www.cnblogs.com/FBGG/articles/15491711.html

一样的,首先在pytest.ini文件中,写上生成报告命令

 

 

然后在运行命令中补充os渲染方法即可。

 

posted @ 2022-05-09 22:57  博无止境  阅读(229)  评论(0编辑  收藏  举报