pytest

Define a pytest fixture providing multiple arguments to test function

℡╲_俬逩灬. 提交于 2020-01-20 06:47:47
问题 With pytest, I can define a fixture like so: @pytest.fixture def foo(): return "blah" And use it in a test like so: def test_blah(foo): assert foo == "blah" That's all very well. But what I want to do is define a single fixture function that "expands" to provide multiple arguments to a test function. Something like this: @pytest.multifixture("foo,bar") def foobar(): return "blah", "whatever" def test_stuff(foo, bar): assert foo == "blah" and bar == "whatever" I want to define the two objects

Define a pytest fixture providing multiple arguments to test function

懵懂的女人 提交于 2020-01-20 06:47:26
问题 With pytest, I can define a fixture like so: @pytest.fixture def foo(): return "blah" And use it in a test like so: def test_blah(foo): assert foo == "blah" That's all very well. But what I want to do is define a single fixture function that "expands" to provide multiple arguments to a test function. Something like this: @pytest.multifixture("foo,bar") def foobar(): return "blah", "whatever" def test_stuff(foo, bar): assert foo == "blah" and bar == "whatever" I want to define the two objects

1)用例设计及运行规则

风流意气都作罢 提交于 2020-01-19 16:19:00
用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 Cmd执行Pytest用例 1)pytest( 推荐 ) 2)py.pytest 3)python -m pytest 如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有符合条件的用例 执行用例规则 1.执行某个目录下所有的用例 pytest 文件名/ 2.执行某一个py文件下用例 pytest 脚本名称.py 3.-k 按关键字匹配 pytest -k "MyClass and not method" 这将运行包含与给定字符串表达式匹配的名称的测试,其中包括Python 使用文件名,类名和函数名作为变量的运算符。 上面的例子将运行 TestMyClass.test_something但不运行TestMyClass.test_method_simple 4.按节点运行 每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成 来自参数化的类名,函数名和参数,由:: characters分隔。 运行.py模块里面的某个函数 pytest test_mod.py::test_func 运行.py模块里面,测试类里面的某个方法 pytest test_mod.py:

Python - pytest:

喜欢而已 提交于 2020-01-18 16:14:51
快速入门 pytest是Python的单元测试框架,同自带的unittest框架类似,但pytest框架使用起来更简洁,效率更高。 pytest特点 入门简单易上手,文档支持较好。 支持单元测试和功能测试。 支持参数化。 可以跳过指定用例,或对某些预期失败的case标记成失败。 支持重复执行失败的case。 支持运行由unittest编写的测试用例。 有很多第三方插件,并且可自定义扩展。 方便和支持集成工具进行集成。 安装 pip install pytest 测试 C:\Users\Anthony>pytest --version This is pytest version 5.2.2, imported from c:\python36\lib\site-packages\pytest.py 在测试之前要做的准备 我的演示脚本处于这样一个的目录中: M:\py_tests\ # 我的是M盘的 py_tests 目录,所有操作都在 py_tests 目录内完成 ├─scripts │ ├─test_case_dir1 │ │ ├─test_case_02.py # 用例脚本文件 │ │ └─__init__.py │ ├─test_allure_case.py # 脚本文件 │ ├─test_case_01.py # 脚本文件 │ └─__init__.py ├─report

Why are most of my project's Django files missing from the PyTest Coverage report?

霸气de小男生 提交于 2020-01-16 16:47:40
问题 I'm running pytest-cov and pytest-django using tox . I have a very simple tox.ini file with limited omit files. The problem is when I run pytest using tox -e unit , I get a limited Coverage report: ---------- coverage: platform darwin, python 3.7.4-final-0 ----------- Name Stmts Miss Cover Missing ---------------------------------------------------------------------------- components/__init__.py 0 0 100% components/client/__init__.py 0 0 100% components/client/admin.py 27 0 100% components

How to click on QMessageBox with pytest-qt?

ⅰ亾dé卋堺 提交于 2020-01-15 10:14:45
问题 I am creating some unit tests for a PyQt application with pytest-qt . And I would like to create open the graphical window, do some tests then close the window, rather than open a new window for every test, ie. use a module fixture for the window itself. I succeeded to do this part, by calling in a local function a QtBot rather than using the default fixture, and removing the mocker ones. So I am pretty close to my objective. However, but I am not able to close the window (and test the

pytest自动化之脚本实现和优化

淺唱寂寞╮ 提交于 2020-01-15 06:46:49
【记录自己的实现过程,和结果无关】 初始实现: # coding:utf-8 """ author:@zhouxuan file:conftest.py """ from selenium import webdriver import pytest from selenium.webdriver.support.wait import WebDriverWait import time from selenium.webdriver.remote.webdriver import By @pytest.fixture(scope='function') def login_baidu(request): dr = webdriver.Chrome() dr.get('http://www.baidu.com') dr.maximize_window() time.sleep(4) WebDriverWait(dr, 2).until(lambda x: x.find_element_by_xpath('//*[@id="u1"]/a[7]').is_displayed()) dr.find_element(By.XPATH,'//*[@id="u1"]/a[7]').click() "判断默认展示的是扫码登录还是用户名登录" try: WebDriverWait(dr, 2)

How do i make the pytest driver instance available in my testcase

核能气质少年 提交于 2020-01-15 01:46:10
问题 I am trying to build an selenium based automation framework, using, Python, Pytest. My intention is to create a driver instance at the class level by initializing it in conftest.py and making it available in all testcases, so that user doesn't need to create the driver instance in each testcase. Driver instance in conftest.py: @pytest.fixture(scope="class") def get_driver(request): from selenium import webdriver driver = webdriver.Chrome() request.cls.driver = driver yield driver.quit()

python的pytest框架

白昼怎懂夜的黑 提交于 2020-01-14 21:57:19
pytest 1)简介 官方文档地址:http://www.pytest.org/en/latest/getting-started.html pytest中定义测试用例有三种方式:   1)兼容unittest,已有的unittest库和文件都可以直接进行调用;   2)可以基于简单的类进行定义,如果一个类里边没有初始化方法并且以test开头,系统便会认为这是一个测试用例(不需要继承,直接定义就行)   3)可以直接定义测试函数来定义测试用例 2)安装:   pip install pytest 3)修改配置:    例1: import pytest def func(x): return x + 1 def test_answer(): assert func(3) == 5 if __name__ == "__main__": pytest.main() 例2:(修改运行方式后,编写代码生效) import pytest def func(x): return x + 1 def test_answer(): assert func(3) == 5 class TestFunc: def test_answer(self): assert func(3) == 5 if __name__ == "__main__": pytest.main() 4

How to capture screenshot on test case failure for Python Unittest

廉价感情. 提交于 2020-01-14 20:08:51
问题 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