python--pytest库
pytest:是一个框架,使构建简单和可扩展的测试变得容易。
安装:pip install -U pytest
检查安装:pytest --version
官方文档:https://docs.pytest.org/en/latest/contents.html#toc
Running pytest can result in six different exit codes:
| Exit code 0: | All tests were collected and passed successfully |
|---|---|
| Exit code 1: | Tests were collected and run but some of the tests failed |
| Exit code 2: | Test execution was interrupted by the user |
| Exit code 3: | Internal error happened while executing tests |
| Exit code 4: | pytest command line usage error |
| Exit code 5: | No tests were collected |
To stop the testing process after the first (N) failures:
pytest -x # stop after first failure
pytest --maxfail=2 # stop after two failures
Pytest supports several ways to run and select tests from the command-line.(多种方式运行测试用例)
Run tests in a module:pytest test_mod.py
Run tests in a directory:pytest testing/
Run tests by keyword expressions:pytest -k "MyClass and not method"
This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple.
Run tests by node ids:
To run a specific test within a module:pytest test_mod.py::test_func
Another example specifying a test method in the command line:pytest test_mod.py::TestClass::test_method
Run tests by marker expressions:pytest -m slow
Will run all tests which are decorated with the @pytest.mark.slow decorator.
Run tests from packages:pytest --pyargs pkg.testing
This will import pkg.testing and use its filesystem location to find and run tests from.
@pytest.fixture(scope="module")
scope作用域:function,class,module,packageorsession.function,class,module,packageorsession.
conftest.py: sharing fixture functions
If during implementing your tests you realize that you want to use a fixture function from multiple test files you can move it to a conftest.py file. You don’t need to import the fixture you want to use in a test, it automatically gets discovered by pytest. The discovery of fixture functions starts at test classes, then test modules, then conftest.py files and finally builtin and third party plugins.
详情:https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions
以下是方程无解老师整理的内容:
Unittest:python内嵌的测试框架
编写简范:
测试模块:import unittest
测试类必须继承:unittest.TestCase
测试方法必须必须以“test_”开头
模块名字、类名不做要求
测试方法级别:setUp、tearDown
测试类级别:setUpClass、tearDownClass
模块级别:setUpModule、tearDownModule
更高级的框架:Pytest
具有很多第三方插件:http://plugincompat.herokuapp.com/
编写规范:
测试文件以“test_”开头(以"_test"结尾也行)
测试类以"Test"开头,并且不能带有__init__方法
测试函数以"test_"开头
部分应用:
# encoding: utf-8 import pytest # pytest:参数化
@pytest.mark.parametrize("x,y", [(3, 3), (3+5, 8), (6*2, 12), ("a", "a")])
def test_add(x, y):
assert x == y value = 0 def test_add1():
global value
value = 10
assert value == 10 def test_add2():
print("I am 2")
assert value == 10 @pytest.fixture()
def login_and_login_out():
return 1
# print("login start")
# yield
# print("login out") class TestSample:
def test_answer1(self, login_and_login_out):
result = login_and_login_out
assert result == 1 # fixtures名字调用
def test_answer2(self, login_and_login_out):
result = login_and_login_out
assert result == 1 # fixtures decorator调用
@pytest.mark.usefixtures("login_and_login_out")
def test_answer3(self):
assert 1 == 1 # fixtures autouse调用
@pytest.fixture(scope="module", autouse=True)
def login():
print("login -----------------")
yield
print("end login -------------") @pytest.fixture(scope="class", autouse=True)
def out():
print("login out start -------")
yield
print("login out end ---------") class TestSample2:
def test_answer4(self):
assert "hello 2019" == "hello 2019 " def test_answer5(self):
assert "fine" == "fine " class TestSample3:
def test_answer(self):
assert "welcome" == "welcome "
pytest执行用例的方式:
1)执行一个module:pytest -v src/testcase/api/xxx.py
2)执行一个类:pytest -v src/testcases/api/xxx.py::TestSample(类名)
3)执行一个方法:pytest -v src/testcases/api/xxx.py::TestSample(类名)::test_xx(方法名)
4)执行一个目录或package:pytest -v src/testcases/api
5)通过标签来运行测试用例:pytest -m P0(标签名) src/testcases/api/
通过pytest.main运行:pytest.main(['-v', '--instafail', 'testcases/api/xxx.py', '-m=P0'])
python--pytest库的更多相关文章
- python常用库
本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:vinta.欢迎加入翻译组. Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 ...
- python第三方库,你要的这里都有
Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst Chardet,字符编码探测器,可以自动检测文本. ...
- python常用库(转)
转自http://www.west999.com/info/html/wangluobiancheng/qita/20180729/4410114.html Python常用的库简单介绍一下 fuzz ...
- Python第三方库资源
[转载]Python第三方库资源 转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...
- 【转载】Python第三方库资源
转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github.com/jobbole/awesom ...
- Python全部库整理
库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...
- python 三方库字典
参考:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 ...
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- python第三方库大全
环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 pyenv:简单的 Python 版本管理工具.官网 Vex:可以在虚拟环境中执行命令.官网 v ...
- python+paramiko库+svn写的自动化部署脚本
第一篇博文 直接开门见山的说了. 这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞. 一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一 ...
随机推荐
- MVC5 Entity Framework学习之实现主要的CRUD功能
在上一篇文章中,我们使用Entity Framework 和SQL Server LocalDB创建了一个MVC应用程序,并使用它来存储和显示数据.在这篇文章中,你将对由 MVC框架自己主动创建的CR ...
- Window日志分析
0X00 简介 0x01 基本设置 A.Windows审核策略设置 前提:开启审核策略,若日后系统出现故障.安全事故则可以查看系统的日志文件,排除故障,追查入侵者的信息等. 打开设置窗口 Window ...
- informix中的时间计算
今天看SUN服务器是的mail(vi /var/mail/xxxuser),发现定时任务上的一些存储过程执行有错误,其中有一个错误是long transaction,长事务错误,到数据库一查,天哪 ...
- c++学习笔记—动态内存与智能指针浅析
我们的程序使用内存包含以下几种: 静态内存用来保存局部static对象.类static数据成员以及定义在任何函数之外的变量,在使用之前分配,在程序结束时销毁. 栈内存用来保存定义在函数内部的非stat ...
- CSS-筛选 获取第一个td
tr td:first-child{ font-weight:bold; } 看样子,应该是jquery中有些筛选,css也是能够同样进行筛选的,只是模式有些可能不同 a[data-toggle*=' ...
- Mysql错误:Duplicate entry '127' for key 'PRIMARY'的解决方法
有时候真是挺幸运,正当我自以为是地认为掌握了某个知识点的时候,现实就会马上出现另外一个问题,让我知道之前的认知是不全面的. 正如我上篇博文中所述,如果一个自增字段达到了上限,而且继续向里面插入数据的话 ...
- git怎么使用
1_创建一个git服务器 2_开发人员小A从服务器拉取代码 3_小A提交代码 4_小c拉取代码 5_小a现在的代码 6_小c改变了小a的代码 7_小c将变更提交一下 8_小a拉取服务器的代码 9_小A ...
- vue工具 - vue-cli安装使用流程
1.全局安装vue-cli cnpm i vue-cli -g 2.监测安装版本 vue -V 大写V : version 3.指定目录下新建项目 vue init webpack [项目名] 按自 ...
- 百度前端学院js课堂作业合集+分析(更新中...)
第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 安卓APP动态调试-IDA实用攻略
0x00 前言 随着智能手机的普及,移动APP已经贯穿到人们生活的各个领域.越来越多的人甚至已经对这些APP应用产生了依赖,包括手机QQ.游戏.导航地图.微博.微信.手机支付等等,尤其2015年春节期 ...