pytest

In pytest, what is the use of conftest.py files?

不羁的心 提交于 2019-11-26 08:58:30
问题 I recently discovered pytest . It seems great. However, I feel the documentation could be better. I\'m trying to understand what conftest.py files are meant to be used for. In my (currently small) test suite I have one conftest.py file at the project root. I use it to define the fixtures that I inject into my tests. I have two questions: Is this the correct use of conftest.py ? Does it have other uses? Can I have more than one conftest.py file? When would I want to do that? Examples will be

How to skip the rest of tests in the class if one has failed?

ぐ巨炮叔叔 提交于 2019-11-26 08:11:22
问题 I\'m creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks. So far I\'m organizing my tests in the following structure: each Class is the Test Case and each test_ method is a Test Step . This setup works GREAT when everything is working fine, however when one step crashes the rest of the \"Test Steps\" go crazy. I\'m able to contain the failure inside the Class (Test Case) with the help of teardown_class() , however I\'m looking into how to

How to pass arguments in pytest by command line

自闭症网瘾萝莉.ら 提交于 2019-11-26 07:40:41
问题 I have a code and I need to pass the arguments like name from terminal. Here is my code and how to pass the arguments. I am getting a \"File not found\" kind error that I don\'t understand. I have tried the command in the terminal: pytest <filename>.py -almonds I should get the name printed as \"almonds\" @pytest.mark.parametrize(\"name\") def print_name(name): print (\"Displaying name: %s\" % name) 回答1: In your pytest test, don't use @pytest.mark.parametrize : def test_print_name(name):

使用pytest————持续更新中

大兔子大兔子 提交于 2019-11-26 01:43:25
pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: 如图所示: 在执行完成之后先会显示test01.py,后面跟着.F 其中.代表执行成功,F代表执行失败,并且在下方会展示错误的提示 2,pytest使用步骤: 1,导入pytest 2,编写测试用例 一,无需在测试类下编写测试用例,可以直接编写测试函数 二,测试函数名必须包含test_ 开头,或者_test结尾; 3,在pytest框架下执行测试用例 在py文件内执行测试用例: pytest