pytest

coverage.py does not cover script if py.test executes it from another directory

我是研究僧i 提交于 2019-12-18 11:07:55
问题 I got a python script which takes command line arguments, working with some files. I'm writing succeeding tests with py.test putting this script through its paces, executing it with subprocess.call . Now I want to analyze code coverage with coverage.py . Coverage, when used via the pytest-cov plugin (which has subprocess-handling built-in), does not see/cover my script when it is called from a temporary testing directory created with py.test 's tmpdir fixture. Coverage does see my script when

How can I see normal print output created during pytest run?

China☆狼群 提交于 2019-12-18 09:58:26
问题 Sometimes I want to just insert some print statements in my code, and see what gets printed out when I exercise it. My usual way to "exercise" it is with existing pytest tests. But when I run these, I don't seem able to see any standard output (at least from within PyCharm, my IDE). Is there a simple way to see standard output during a pytest run? 回答1: The -s switch disables per-test capturing. 回答2: In an upvoted comment to the accepted answer, Joe asks: Is there any way to print to the

Can I patch Python's assert to get the output that py.test provides?

无人久伴 提交于 2019-12-18 09:18:58
问题 Pytest's output for failed asserts is much more informative and useful than the default in Python. I would like to leverage this when normally running my Python program, not just when executing tests. Is there a way to, from within my script, overwrite Python's assert behavior to use pytest to print the stacktrace instead while still running my program as python script/pytest_assert.py ? Example program def test_foo(): foo = 12 bar = 42 assert foo == bar if __name__ == '__main__': test_foo()

pytest之参数化

风流意气都作罢 提交于 2019-12-18 04:38:56
格式 @pytest.mark.parametrize(variable,[value]) 可以参考官方文档 http://doc.pytest.org/en/latest/example/parametrize.html @pytest.mark.parametrize( "example_input,expectation", [ (3, does_not_raise()), (2, does_not_raise()), (1, does_not_raise()), (0, pytest.raises(ZeroDivisionError)), ], ) def test_division(example_input, expectation): """Test how much I know division.""" with expectation: assert (6 / example_input) is not None 这样,执行的时候,循环去读取每个tuplue的对应值 来源: CSDN 作者: lion_zhou 链接: https://blog.csdn.net/zhouxuan623/article/details/103584884

How do I correctly setup and teardown my pytest class with tests?

家住魔仙堡 提交于 2019-12-17 21:54:17
问题 I am using selenium for end to end testing and I can't get how to use setup_class and teardown_class methods. I need to set up browser in setup_class method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_clas s method. But logically it seems bad solution, because in fact my tests will work not with class, but with object. I pass self param inside every test method, so I can access objects' vars: class TestClass: def setup_class(cls): pass def test

pytest cannot find module [duplicate]

被刻印的时光 ゝ 提交于 2019-12-17 20:27:45
问题 This question already has answers here : Ensuring py.test includes the application directory in sys.path (3 answers) Closed last year . I am following the pytest good practices or at least I think I am . However, pytest cannot find my module. It seems not to include the current directory in its PYTHONPATH . The source file: def add(x, y): return x + y The test file: import pytest from junk.ook import add def test_add_true(): assert add(1, 1) == 2 And the shell output with a Python 3 virtual

module 'pytest' has no attribute 'allure'问题解决

◇◆丶佛笑我妖孽 提交于 2019-12-17 19:22:06
安装allure后执行命令后报错module 'pytest' has no attribute 'allure' C:\Users\Desktop\xin>pytest -s -q --alluredir report INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "c:\python34\lib\site-packages\_pytest\main.py", line 199, in wrap_session INTERNALERROR> config._do_configure() INTERNALERROR> File "c:\python34\lib\site-packages\_pytest\config\__init__.py", line 636, in _do_configure INTERNALERROR> self.hook.pytest_configure.call_historic(kwargs=dict(config=self)) INTERNALERROR> File "c:\python34\lib\site-packages\pluggy\hooks.py", line 306, in call_historic INTERNALERROR> res =

Test if code is executed from within a py.test session

て烟熏妆下的殇ゞ 提交于 2019-12-17 18:25:13
问题 I'd like to connect to a different database if my code is running under py.test. Is there a function to call or an environment variable that I can test that will tell me if I'm running under a py.test session? What's the best way to handle this? 回答1: A simpler solution I came to: import sys if "pytest" in sys.modules: ... Pytest runner will always load the pytest module, making it available in sys.modules . Of course, this solution only works if the code you're trying to test does not use

pytest使用简介

淺唱寂寞╮ 提交于 2019-12-17 16:46:27
pytest是基于py unittest的一个单元测试框架,用起来比unittest简单不少,不过和unittest使用不太一样,总结如下: 格式: case的py文件名必须是test开头 def用例必须是test开头 class名必须是Test开头,注意大写 class中的def用例必须是test开头 pytest提供了很多运行参数,比较常用的有: -k:只执行指定的用例-s:命令行显示测试代码的输出,如果需要输出html结果最好不要-s -v:显示详细信息 -q:不显示详细信息 --html=path:输出测试结果到html 初始化,pytest提供了如下初始化和清理环境方法: setup_function、teardown_function setup_module、teardown_module setup、teardown setup_class、teardown_class setup_method、teardown_method @pytest.fixture() 对于class用例集类来说: setup_function、teardown_function 不能用 setup_module、teardown_module 放在类外可以使用、放在类内不能使用,只在最前和最后调用 setup、teardown 放在类内可以使用、放在类外不能使用,每个case都会调用

03、pytest查找测试策略

一世执手 提交于 2019-12-17 15:58:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 默认情况下,pytest会递归查找当前目录下所有以 test 开始或结尾的 Python 脚本,并执行文件内的所有以 test 开始或结束的函数和方法. etc ├── robocop.toml ├── test_data.py └── test_demo.py 一、执行某文件夹下所有 test 打头的py文件中 test 打头的函数(测试用例) ╰ pytest -v etc =================================================== test session starts ==================================================== platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {