pytest

Chaining pytest fixtures

杀马特。学长 韩版系。学妹 提交于 2020-05-13 18:09:37
问题 I've been unable to find the magic keywords to google this problem or find it in the pytest documentation. I'm looking to be able to setup my tests to combine multiple fixtures into a single fixture - or inversely filter fixtures from another fixture. An example will explain it much better: import pytest @pytest.fixture(params=[0,1,2,3,4,5,6]) def number(request): return request.param @pytest.fixture() def odd_number(number): if number % 2 == 1: return number else: return None # Skip (or some

pytest: how to make dedicated test directory

我们两清 提交于 2020-05-10 20:55:47
问题 I want the following project structure: |--folder/ | |--tests/ | |--project/ Let's write a simple example: |--test_pytest/ | |--tests/ | | |--test_sum.py | |--t_pytest/ | | |--sum.py | | |--__init__.py sum.py: def my_sum(a, b): return a + b test_sum.py: from t_pytest.sum import my_sum def test_my_sum(): assert my_sum(2, 2) == 5, "math still works" Let's run it: test_pytest$ py.test ./ ========== test session starts =========== platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0

pytest: how to make dedicated test directory

落爺英雄遲暮 提交于 2020-05-10 20:55:11
问题 I want the following project structure: |--folder/ | |--tests/ | |--project/ Let's write a simple example: |--test_pytest/ | |--tests/ | | |--test_sum.py | |--t_pytest/ | | |--sum.py | | |--__init__.py sum.py: def my_sum(a, b): return a + b test_sum.py: from t_pytest.sum import my_sum def test_my_sum(): assert my_sum(2, 2) == 5, "math still works" Let's run it: test_pytest$ py.test ./ ========== test session starts =========== platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0

pytest文档39-参数化(parametrize)结合allure.title()生成不同标题报告

 ̄綄美尐妖づ 提交于 2020-05-09 08:14:07
前言 pytest的参数化(parametrize)可以实现只需维护测试数据,就能生成不同的测试用例目的。可以在参数化的时候加 ids 参数对每个用例说明使用场景。 最终我们希望在 allure 报告上能详细的展示出每个用例的标题描述,这样才能更直观的知道每个用例是干什么的。 参数化parametrize 先看一个简单的pytest参数化案例演示test_a.py # test_a.py import pytest import allure # 作者:上海-悠悠 QQ交流群:779429633 def login(username, password): '''登录''' print("输入账号:%s" % username) print("输入密码:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 测试数据 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure

Setting dynamic folder and report name in pytest

…衆ロ難τιáo~ 提交于 2020-05-09 05:57:08
问题 I have a problem with setting report name and folder with it dynamically in Python's pytest. For example: I've run all pytest's tests @ 2020-03-06 21:50 so I'd like to have my report stored in folder 20200306 with name report_2150.html . I want it to be automated and triggered right after the tests are finished. I'm working in VS Code and I'm aiming to share my work with colleagues with no automation experience so I'm aiming to use it as "click test to start". My project structure: webtools/

ModuleNotFoundError: No module named 'pytest'

天大地大妈咪最大 提交于 2020-05-09 05:16:45
问题 After installing the pytest module in a virtual environment, I used the python code to call and run the prompt to find the pytest module. I installed the pytest module outside the virtual environment. I can call it normally with python. import pytest def test_main(): assert 5!=5 if __name__ == "__main__": pytest.main() The error is as follows: [Running] python -u "d:\MyPytest\test_sample.py" Traceback (most recent call last): File "d:\MyPytest\test_sample.py", line 1, in import pytest

ModuleNotFoundError: No module named 'pytest'

妖精的绣舞 提交于 2020-05-09 05:16:06
问题 After installing the pytest module in a virtual environment, I used the python code to call and run the prompt to find the pytest module. I installed the pytest module outside the virtual environment. I can call it normally with python. import pytest def test_main(): assert 5!=5 if __name__ == "__main__": pytest.main() The error is as follows: [Running] python -u "d:\MyPytest\test_sample.py" Traceback (most recent call last): File "d:\MyPytest\test_sample.py", line 1, in import pytest

pytest自动化7:assert断言

浪子不回头ぞ 提交于 2020-05-08 23:45:30
前言:assert断言就是将实际结果和期望结果做对比,符合预期结果就测试pass,不符合预期就测试failed。 实例1:简单断言 实例1优化版--增加异常信息文字描述 异常断言 excinfo 是一个异常信息实例,它是围绕实际引发的异常的包装器。主要属性是.type、 .value 和 .traceback 注意:断言type的时候,异常类型是不需要加引号的,断言value值的时候需转str ------------------------------------------------------------------------------------------------------------------------------------- 常用断言 pytest里面断言实际上就是python里面的assert断言方法,常用的有以下几种: assert xx 判断xx为真 assert not xx 判断xx不为真 assert a in b 判断b包含a assert a == b 判断a等于b assert a != b 判断a不等于b 来源: oschina 链接: https://my.oschina.net/u/4262536/blog/3614841

pytest文档38-allure.setp添加测试用例步骤

五迷三道 提交于 2020-05-08 08:15:21
前言 一般流程性的测试用例,写成自动化用例时,步骤较多写起来会比较长。在测试用例里面添加详细的步骤有助于更好的阅读,也方便报错后快速的定位到问题。 举个常见的测试场景用例:从登陆开始,到浏览商品添加购物车,最后下单支付 用例步骤:1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功 用例设计 先把上面的每个环节,写个函数放到common_fucntion.py # common_fucntion.py import allure import pytest ''' 流程性的用例,添加测试步骤,让用例更清晰 用例步骤:1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功 作者:上海-悠悠 QQ交流群:779429633 ''' def login(username, password): '''登陆''' print("前置操作:先登陆") def open_goods(): '''浏览商品''' print("浏览商品") def add_shopping_cart(goods_id="10086"): '''添加购物车''' print("添加购物车") def buy_goods(): '''生成订单''' print("buy") def pay_goods(): '''支付''' print("pay") 接下来测试用例设计

python之json库的使用

倾然丶 夕夏残阳落幕 提交于 2020-05-08 00:19:16
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。 1.json库的使用 使用 JSON 函数需要导入 json 库: import json 。 函数 描述 json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 将已编码的 JSON 字符串解码为 Python 对象 1. json.dumps 将 Python 对象编码成 JSON 字符串。 语法 json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding= " utf-8 " , default=None, sort_keys=False, **kw) 例如:将python数组对象转为JSON字符串 # !/usr/bin/python import json data = [ { ' name ' : ' 张三 ' , ' age ' : 25}, { ' name ' : ' 李四 ' , ' age ' : 26 } ] jsonStr = json.dumps(data) print (jsonStr) 结果: