pytest

How to capture screenshot on test case failure for Python Unittest

邮差的信 提交于 2020-01-14 20:08:13
问题 I am using Python 3.6.5 with the following libraries: Appium-Python-Client==0.26 unittest2==1.1.0 selenium==3.5.0 pytest==3.6.3 Now I need to capture the screenshot in case of test failure, So intentionally I did put a false statement self.driver.find_element_by_css_selector('test') . I am using sys.exc_info() . But when I executing below code using a command: py.test untitled.py or python3 -m unittest untitled.py it does not capturing it. Code: import sys, time, unittest2 from selenium

有关取消pytest的问题

本秂侑毒 提交于 2020-01-14 11:53:19
pycharm 中文件取消用 pytest模式打开: 刚刚开始接触python的时候,基本就靠debug来学习代码,一步步调试一步步学习。这个时候通常我们是要run debug自己的文件,初来乍到不明白,有时候把python文件名就以“test”开头了,直接run或者debug,就会报错,各种错误,这时候如果通过显示的erro来排查问题往往找不到答案。 现将本小白在pytest上的踩坑事件记录下来: 首先描述下问题,如果py文件名字开头带有test,一般pycharm 默认运行pytest(这个应该是python自带的测试框架),如果直接run自己的文件,会发现其实是运行的pytest,然后各种报错。一般修改run---edit configuration就行了,界面修改如下: + 和 –可以添加和删除,删除pytest,添加了自己要运行的文件名和文件路径(黄色框:script path)等,如下所示 基本上这样就OK了,然后我这边又出现了新的问题: Run之后还是显示错误: _pytest/debugging.py: Pdb.py Module “cmd”has no attribute cmd pytest都取消了,这是什么错误,黑人问号????好像经常遇到,大概就是解释器的问题 然后通过在另一个账户下运行正常的设置对比我猜到了,我设置的解释器有问题

How to test redirection in Django using pytest?

淺唱寂寞╮ 提交于 2020-01-14 10:16:31
问题 I already know that one can implement a class that inherits from SimpleTestCase , and one can test redirection by: SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix='', fetch_redirect_response=True) However, I am wondering what is the way I can check for redirection using pytest: @pytest.mark.django_db def test_redirection_to_home_when_group_does_not_exist(create_social_user): """Some docstring defining what the test is

How to test redirection in Django using pytest?

落爺英雄遲暮 提交于 2020-01-14 10:14:08
问题 I already know that one can implement a class that inherits from SimpleTestCase , and one can test redirection by: SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix='', fetch_redirect_response=True) However, I am wondering what is the way I can check for redirection using pytest: @pytest.mark.django_db def test_redirection_to_home_when_group_does_not_exist(create_social_user): """Some docstring defining what the test is

Pass a fixture to a helper function in PyTest?

南笙酒味 提交于 2020-01-14 09:05:11
问题 I have a function that needs to utilize a fixture in my test suite. This is just a small helper function that helps to generate a full URL. def gen_url(endpoint): return "{}/{}".format(api_url, endpoint) I have a fixture in conftest.py that returns the URL: @pytest.fixture(params=["http://www.example.com"]) def api_url(request): return request.param @pytest.fixture(params=["MySecretKey"]) def api_key(request): return request.param Finally, in my test function, I need to call my gen_url : def

Python测试框架之unittest和pytest 的区别

社会主义新天地 提交于 2020-01-13 20:12:08
一、Unittest Unittest是Python标准库中自带的单元测试框架,Unittest有时候也被称为PyUnit,就像JUnit是Java语言的标准单元测试框架一样,Unittest则是Python语言的标准单元测试框架。 Unittest支持自动化测试,测试用例的初始化、关闭和测试用例的聚合等功能,它有一个很重要的特性:它是通过类(class)的方式,将测试用例组织在一起。 示例: 执行结果: 注:unittest有一个关联模块unittest2,但unittest2仅适用于Python 2.4-2.6。这是由于从Python 2.7开始,unittest增加一些新的特性。为了在老的版本中支持这些特性,所以提供了unittest2这个库。但对于Python 2.7及之后的版本,unittest是唯一的。本次示例中使用的为python2.7。 二、Pytest Pytest是Python的另一个第三方单元测试库。它的目的是让单元测试变得更容易,并且也能扩展到支持应用层面复杂的功能测试。 pytest的特性有: 支持用简单的assert语句实现丰富的断言,无需复杂的self.assert*函数 自动识别测试模块和测试函数 模块化夹具用以管理各类测试资源 对 unittest 完全兼容,对 nose基本兼容 支持Python3和PyPy3 丰富的插件生态

Running the same test on two different fixtures

柔情痞子 提交于 2020-01-13 08:31:31
问题 I have a test which currently runs with a single fixture like this: @pytest.fixture() def foo(): return 'foo' def test_something(foo): # assert something about foo Now I am creating a slightly different fixture, say @pytest.fixture def bar(): return 'bar' I need to repeat the exact same test against this second fixture. How can I do that without just copy/pasting the test and changing the parameter name? 回答1: Beside the test generation, you can do it the "fixture-way" for any number of sub

一篇文学会 Pytest 测试框架

∥☆過路亽.° 提交于 2020-01-12 22:35:16
之前有介绍过 python 自带的 Unittest 单元测试框架, Python 自动化测试实战 Zero to Hero 单元测试框架unittest的使用 ,今天再来简单的讲诉下测试框架 pytest 本文首发自伊洛的个人博客: https://yiluotalk.com ,欢迎关注并查看更多内容!!! #####1. 安装开始 直接使用 pip 安装就可以, pytest 支持 python 的 2.0 或 3.0 版本 #!/usr/bin/python3 # 伊洛Yiluo # https://yiluotalk.com pip install - U pytest 检查版本,校验下是否安装成功 #!/usr/bin/python3 # 伊洛Yiluo # https://yiluotalk.com ( yiluo ) ➜ ~ pytest - - version 2. pytest的基础语法规范 文件名应必须以 test 开头或结尾,例如 test_example.py 或 example_test.py 如果将测试定义为类上的方法,类名应以“ Test ”开头。例如, TestExample 。且不包含 __init__ 方法 测试方法名称或函数名称应以 test_ 开头,例如 test_example ,名称与该模式不匹配的方法将不会作为测试执行

pytest capsys: checking output AND getting it reported?

Deadly 提交于 2020-01-12 13:55:33
问题 Python 3.4.1, pytest 2.6.2. When a test fails, pytest will routinely report what was printed to stdout by the test. For instance this code: def method_under_test(): print("Hallo, Welt!") return 41 def test_result_only(): result = method_under_test() assert result == 42 when executed as python -m pytest myfile.py , will report this: ================================== FAILURES =================================== ______________________________ test_result_only _______________________________ def

Chaining tests and passing an object from one test to another

断了今生、忘了曾经 提交于 2020-01-12 08:08:48
问题 I'm trying to pass the result of one test to another in pytest - or more specifically, reuse an object created by the first test in the second test. This is how I currently do it. @pytest.fixture(scope="module") def result_holder: return [] def test_creation(result_holder): object = create_object() assert object.status == 'created' # test that creation works as expected result_holder.append(object.id) # I need this value for the next test # ideally this test should only run if the previous