pytest

How can I access the overall test result of a pytest test run during runtime?

心已入冬 提交于 2019-11-28 06:59:15
问题 Dependent on the overall test result of a pytest test run I would like to execute conditional tear down. This means the access to the overall test result must happen after all tests have been executed but before the test runner has been left. How can I achieve this? 回答1: I could not find a suitable pytest hook to access the overall test result yet. You don't need one; just collect the test results yourself. This is the blueprint I usually use when in need of accessing the test results in

pytest 用 @pytest.mark.usefixtures(\"fixtureName\")或@pytest.fixture(scope=\"function\", autouse=True)装饰,实现类似setup和TearDown的功能

一个人想着一个人 提交于 2019-11-28 05:17:55
conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("") print("class-begin") yield print("class-end") test_autouse.py 1 import pytest 2 3 4 @pytest.mark.usefixtures("class_auto") 5 class TestClass(object): 6 7 @pytest.fixture(scope="function", autouse=True) 8 def funcion_auto(self): 9 print("begin") 10 yield 11 print("end") 12 13 def test_case1(self): 14 print("test_case1:") 15 assert 0 == 0 16 17 def test_case2(self): 18 print("test_case2:") 19 assert 0 == 0 执行命令 pytest -s test_autouse.py 执行结果: 注意: 1.fixture中的yield需要注意,不能缺 2.上面conftest.py中的fixture,也可以放在test

py.test does not start (EBUSY: [Resource device])

血红的双手。 提交于 2019-11-28 04:53:43
问题 Just installed py.test with pip install -U pytest with no errors, however we I'm trying to launch py.test I get error: EBUSY: [Resource device]: listdir('C:\\Users\\Administrator\\AppData\\Local\\Application Data',) Here is the output: C:\Users\Administrator>py.test ========================== test session starts =========================== platform win32 -- Python 2.7.3 -- pytest-2.2.4 collected 0 items / 1 errors ================================= ERRORS ================================= ____

How to run unittest discover from “python setup.py test”?

。_饼干妹妹 提交于 2019-11-28 03:53:25
I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover . I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test ). It's OK if the solution only works on python 2.7. In setup.py , I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly: config = { 'name': name, 'version': version, 'url': url, 'test_suite': '???', 'test_loader': '???', } Is this possible using only unittest built into python 2.7?

python测试框架 pytest

梦想与她 提交于 2019-11-28 03:50:48
一: pytest 是python的一套全功能的测试框架. 优点如下: 1、操作简单,支持多组数据参数化, 支持用例的skip和xfail; 2、支持简单的单元测试和复杂的功能测试,还可以做UI和接口自动化测试; 3、pytest有很多第三方的插件并且支持定义扩展; 如失败重新执行, 断言失败也继续运行,自定义出错停止, 自定义mark标记灵活运行用例.... 4、可以很好的集成CI 二: 框架结构: 类似的setup,teardown同样更灵活, 模块级(setup_module/teardown_module)模块始末,全局的 函数级(setup_function/teardown_function)不在类中的函数有用 类级(setup_class/teardown_class)只在类中前后运行一次。 方法级(setup_method/teardown_methond)运行在类中方法始末 session() 跨文件级 三: Pytest调用方式和用例设计原则: 1. Pytest/py.test(终端,命令行,pycharm都行,可配置pycharm 使用pytest方式执行) Pytest –v (最高级别信息—verbose -q静默模式) pytest -v -s -q 文件名 (s是带控制台输出结果,也是输出详细) 2. pytest将在当前目录及其子目录中运行test

pytest单元测试框架(day02) pytest单元测试框架的执行

拥有回忆 提交于 2019-11-28 03:50:38
pytest 用法和调动 1、设置失败次数后停止 pytest - x 失败一次后停止 pytest - maxifle =2 失败两次后停止 2、pytest 可以选择测试/指定测试 1、运行模块 pytest test_load.py 2、可运行目录 pytest case/ 3、匹配关键字执行 pytest -k test_one 匹配所有测试用例中的test_one函数 4、执行模块中制定的函数   1、模块中的函数  pytest test_load.py::test_create_file      2、模块中类的函数 pytest test_loan.py::TestClass:test_one 来源: https://www.cnblogs.com/ioan/p/9785569.html

Dependencies between files with pytest-dependency?

十年热恋 提交于 2019-11-28 03:15:08
问题 I'm working on a functional test suite using pytest with pytest-dependency. I 99% love these tools, but I can't figure out how to have a test in one file depend on a test in another file. Ideally, I'd like to have zero changes required to the dependee, and only change things in the depender. I'd like tests to be able to depend on test_one both like this: # contents of test_one.py @pytest.mark.dependency() def test_one(): # do stuff @pytest.mark.dependency(depends=["test_one"]) def test_point

pytest问题持续更新

感情迁移 提交于 2019-11-28 03:06:13
  1. 在python中如果在子类中重写了父类的方法,而又需要执行父类的该方法,可以用Super(子类名, self).方法名 来在子类中重新执行该方法   2. MySqlDB问题:在查询时不能拿到当前数据的状态,记录的status已经更改,但没有查到    解决:python引用的MySQLDB需要在每次执行操作后进行commit, 即使是select语句,不然会有缓存。此问题就是前次查询后没有commit, 导致第二次查询仍然显示第一次的查询结果。   3. 用例出现random failure时使用重试机制:    解决:       方法1:使用pytest的插件,pip install pytest-rerunfailures           pytest --reruns 5 --reruns-delay 1 (出现错误时最多重试5次,重试的等待时间是1秒)       方法2: 使用pytest自带装饰器           @pytest.mark.flaky(retuns=5, reruns_delay=1)           def test_example():             pass      方法3:自己写装饰器实现重试 来源: https://www.cnblogs.com/tlmn2008/p/9354761.html

KeyError in module 'threading' after a successful py.test run

孤者浪人 提交于 2019-11-28 02:41:46
I'm running a set of tests with py.test. They pass. Yippie! But I'm getting this message: Exception KeyError: KeyError(4427427920,) in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored How should I go about tracking down the source of that? (I'm not using threading directly, but am using gevent.) Code Painters I observed a similar issue and decided to see what's going on exactly - let me describe my findings. I hope someone will find it useful. Short story It is indeed related to monkey-patching the threading module. In

py.test: how to get the current test's name from the setup method?

女生的网名这么多〃 提交于 2019-11-28 02:27:53
问题 I am using py.test and wonder if/how it is possible to retrieve the name of the currently executed test within the setup method that is invoked before running each test. Consider this code: class TestSomething(object): def setup(self): test_name = ... def teardown(self): pass def test_the_power(self): assert "foo" != "bar" def test_something_else(self): assert True Right before TestSomething.test_the_power becomes executed, I would like to have access to this name in setup as outlined in the