pytest

Coverage.py warning: No data was collected. (no-data-collected)

佐手、 提交于 2020-04-04 10:37:27
问题 I am trying to find the coverage using coverage module for a django project but gets "Coverage.py warning: No data was collected. (no-data-collected)". My project folder has src and tests folders. When I run coverage run -m pytest && coverage report It produces a report with 100% coverage with the list of files inside the tests folder. Whereas when I run coverage run --source=src -m pytest && coverage report it says Coverage.py warning: No data was collected. (no-data-collected) No data to

pytest+python下的UI自动化基础框架

三世轮回 提交于 2020-03-26 17:25:24
整体设计模式: config目录:存放一些公共的静态文件,如项目名称,配置文件等这些环境变量(可以用其他组件替换,如sql,主要能把配置文件的内容被程序识别)。 httptrquest目录:存放接口代码,UI自动化因为其稳定性问题会出现有些地方出错导致后续无法进行,故添加接口操作。 initailize目录:初始化代码,用于存放初始化操作的代码,比如初始化一些全局变量,初始化webdriver等,应用于整个项目的代码。 test目录:真正执行的目录,用于存放测试用例的代码,会被pytest识别将其转化为可执行的测试用例(PS:该文件的目录名及其模块、类、方法名都要根据pytest的设置来进行设置);该目录下的代码都会被pytest装饰,控制执行顺序,执行依赖等操作。 testcase目录:为test目录服务,执行相关的操作,定位元素,执行操作,输出结果等等,该目录下的模块、方法都会被test目录下相应的代码调用实例化 testcase_utils: 辅助工具类,在操作过程中某些页面上有些公共的操作,如表格table定位,批量元素的输入,js操作等等,便于维护。 utils目录: 工具类,为整个项目的运行提供基础的操作,如读取文件,将yaml文件转化为WebElement类对象元素,日志、截图、测试报告、sql操作、装饰器、发送报告等基础操作。与testcase

十二、结合Allure 生成HTML 图形化测试报告

我的梦境 提交于 2020-03-25 22:52:14
1、Allure Allure 是一款非常轻量级并且非常灵活的开源测试报告生成框架。它支持绝大多数测试框架, 例如 TestNG、Pytest、JUint 等。它简单易用,易于集成。 2、Pytest集成Allure windows下安装 Allure工具 1、安装JDK1.8+ 2、安装Allure (1)在 下载Allure的zip安装包 (2)解压到allure-commandline目录 (3)进入bin目录,运行allure.bat (4)添加allure到环境变量PATH 3、安装Allure插件 4、安装 Allure Pytest Adaptor 插件 Allure Pytest Adaptor 是 Pytest 的一个插件,通过它可以生成 Allure 所需要的用于生成测试报告的数据。安装 pytest-allure-adaptor 插件方法: 官网: https://pypi.org/project/allure-pytest/ 环境搭建产生错误 解决: 卸载Allure Pytest Adaptor 插件 3、使用Allure Pytest Adaptor改造基于Pytest的测试用例(可选) 为了使用 Allure 生成报告,需要在 conftest.py 和测试脚本中加入 Allure 特性。 首先,conftest.py 中可以通过 allure

How to handle rerun in pytest with pytest_runtest_makereport fixture

梦想与她 提交于 2020-03-25 17:44:43
问题 I am using pytest-testrail in order to publish some python test cases to testrail. I have a couple test cases that are flaky and using --rerun in order to rerun the test cases that fail. After the rerun, some test cases will pass (meaning the case failed once and passed on the rerun), but pytest will publish the test as failed and pass both. //conftest.py file @pytest.hookimpl(trylast=True, hookwrapper=True) def pytest_runtest_makereport(item, call): report = (yield).get_result() if report

How to handle rerun in pytest with pytest_runtest_makereport fixture

六眼飞鱼酱① 提交于 2020-03-25 17:44:17
问题 I am using pytest-testrail in order to publish some python test cases to testrail. I have a couple test cases that are flaky and using --rerun in order to rerun the test cases that fail. After the rerun, some test cases will pass (meaning the case failed once and passed on the rerun), but pytest will publish the test as failed and pass both. //conftest.py file @pytest.hookimpl(trylast=True, hookwrapper=True) def pytest_runtest_makereport(item, call): report = (yield).get_result() if report

pytest、tox、Jenkins实现python接口自动化持续集成

不问归期 提交于 2020-03-25 09:32:27
3 月,跳不动了?>>> pytest介绍 pytest是一款强大的python测试工具,可以胜任各种级别的软件测试工作,可以自动查找测试用并执行,并且有丰富的基础库,可以大幅度提高用户编写测试用例的效率,具备可扩展性,用户自己也可以编写插件实现特定的功能,也可以安装第三方插件,非常容易的与其他工具集成到一起,比如持续集成、接口自动化测试等。小编之前也写过几篇关于pytest的文章,可以到主页查看哈。 tox介绍 tox是一个命令行工具,允许测试在多种环境下执行,tox不仅能测试不同的python版本,还可以用它来测试不同的依赖配置和不同的操作系统的配置。 工作原理大致是,通过setup.py文件为待测程序创建源码安装包,它会查看tox.ini中的所有环境设置,并针对每个环境执行如下操作: 在.tox目录下创建一个虚拟环境 使用pip安装依赖包 使用pip在步骤1的虚拟环境中安装自己的程序包 运行测试用例 下面通过一个实例来看一下tox的运行过程: 在项目根目录下新建tox.ini文件,然后加入下面的配置 [tox] envlist = py36 [testenv:dev] ; deps告诉tox确保pytest已经安装,如果有多个测试依赖,可以按行罗列,同时也可以指定版本 deps = pytest ; 告诉tox在每个测试环境里运行pytest commands =

Selenium unable to login to Django LiveServerTestCase

我的未来我决定 提交于 2020-03-25 03:28:46
问题 I'm struggling to get Selenium working with my Django project. I can (finally) get it to get pages during testing, but I'm unable to get it to login for some reason. This is my (very simple) test case: import pytest from django.conf import settings from django.contrib.auth import get_user_model from django.test.client import Client from pytest_django.live_server_helper import LiveServer from selenium.webdriver import Remote from users.tests.factories import UserFactory pytestmark = pytest

七、捕获告警信息

谁说胖子不能爱 提交于 2020-03-24 20:46:21
1、告警信息的默认捕获行为 pytest 可以自动捕获测试中产生的告警信息,并在执行结束后进行展示; 可以通过 -W arg 命令行选项来自定义告警的捕获行为: arg 参数的格式为: action:message:category:module:lineno ; action 只能在 "error", "ignore", "always(all)", "default", "module", "once" 中取值,默认取值为 default ; category 必须是 Warning 的子类,默认取值为 Warning 类,表示所有的告警; module 必须为字符串,表示特定模块产生的告警信息; 使用场景: (1)忽略某一种类型的告警信息;例如,忽略 UserWarning 类型的告警( -W ignore::UserWarning ): (2)将某一种类型的告警转换为异常来处理;例如,将 UserWarning 告警转换为异常处理( -W error::UserWarning ): (3)只展示某一个模块中产生的告警;例如,只展示 test_show_warnings 模块产生的告警,忽略其它所有的告警( -W ignore -W default:::test_show_warnings ): 多个 -W 选项的组合操作,优先级是从左到右依次递增的

@pytest.mark.标签

放肆的年华 提交于 2020-03-24 20:43:58
在pytest当中,先注册标签,再给用例打标签,最后运行时,通过标签名来过滤测试用例。 1)注册标签名 官方提供的注册方式有2种,这里只提供一种最简单直接的方式,可以打单个或者多个标签: 通过pytest.ini配置文件给用例注册标签(pytest.ini 文件名是固定的,并且是放在testcases的文件夹下的,与用例同一个层级,放在其他地方会报错,不识别标签) 在pytest.ini文件当中: [pytest] # 固定的section名 markers= # 固定的option名称   标签名1: 标签名的说明内容。   标签名2   标签名N 2)在测试用例/测试类中给用例打标记(只能使用已注册的标记名,否则会报错,不识别标签) 在 测试用例的前面加上:@pytest.mark.已注册标签名 3)运行时,根据用例标签过滤(-m 标签名) 调用pytest.main()函数,将运行时的参数以列表传进去 来源: https://www.cnblogs.com/yzwdcjs/p/12561397.html

八、集成文档测试

允我心安 提交于 2020-03-24 20:41:13
1、集成doctest模块 doctest 是 python 内置的一个标准库,它可以查找代码中类似交互式会话形式的注释,并检查它们是否正确; (1)默认情况下, pytest 会自动收集所有名称匹配 test*.txt 规则的文件,并调用 doctest 执行它们; doctest 文件的默认编码是 UTF-8 ,你可以在 pytest.ini 中使用 doctest_encoding 选项指定新的编码; (2)通过编写文档字符串 --doctest-modules 会查找所有名称匹配 *.py 的文件,收集文档字符串中类似交互式会话形式的注释,并把每一个文档字符串作为一个用例来执行,所以上面执行了两个测试,其中一个是文档测试; 如果想让 pytest --doctest-modules 正确收集到相关注释,需满足以下条件: 文件名称符合 *.py 规则,但无需满足 test_*.py 或者 *_test.py 规则; 文档字符串中的注释必须是类似 python 交互式会话形式的注释; 也可在pytest.ini汇总配置--doctest-modules 而不用每次执行时添加--doctest-modules参数 项 (3)指定额外的项 doctest自带: https://docs.python.org/3/library/doctest.html#option-flags