pytest使用
安装:
pip install pytest
pip install pytest-cov
utils.py代码
def add(a, b):
return a+b def inc(x):
return x + 1 def func_except():
raise SystemExit(1)
test_utils.py 代码
# -*- coding:utf-8 -*- import pytest
from utils import add, inc, func_except def test_add():
assert add(1,1) == 2
assert add(1,2) == 3 def test_answer():
assert inc(3) == 4 def test_mytest():
with pytest.raises(SystemExit):
func_except() # content of test_class.py
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x def test_two(self):
x = "hello"
assert hasattr(x, 'find')
运行测试:
>pytest .
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.7.3, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\l00379637\PycharmProjects\cis_algo_refator
plugins: cov-2.7.1
collected 5 items test_utils.py ..... [100%] ======================================================================================================== 5 passed in 0.16 seconds ========================================================================================================
查看覆盖率:
pytest --cov=./
pytest --cov=./
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.7.3, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\l00379637\PycharmProjects\cis_algo_refator
plugins: cov-2.7.1
collected 5 items test_utils.py ..... [100%] ----------- coverage: platform win32, python 3.7.3-final-0 -----------
Name Stmts Miss Cover
-----------------------------------
test_utils.py 17 0 100%
utils.py 6 0 100%
-----------------------------------
TOTAL 23 0 100% ======================================================================================================== 5 passed in 0.20 seconds ========================================================================================================
pytest使用的更多相关文章
- flask-sqlalchemy、pytest 的单元测试和事务自动回滚
flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...
- Python单元测试框架之pytest -- 断言
对于测试来讲,不管是功能测试,自动化测试,还是单元测试.一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果.测试的成功与否就是拿实际的结果与预期的结果进行比较.这个比的过程实际就 ...
- Python单元测试框架之pytest -- fixtures
fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干.通常用setup/teardown来表示.它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统 ...
- Python单元测试框架之pytest -- 生成测试报告
继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告.这一节就来学习pytest如何生成测试报告. 创建test_calss.py 测试用例文件,这里以测试该文件为例. #cod ...
- 使用 tox flake8 pytest 规范 python 项目
使用 tox flake8 pytest 规范 python 项目 python 中有些很好的工作来规范整个项目的开发,而其中使用较多的就是使用 tox . flake8 . pytest . tox ...
- pytest学习笔记(三)
接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...
- pytest学习笔记(二)
继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...
- pytest进阶之配置文件
前言 pytest配置文件能够改变pytest框架代码的运行规则.比如修改pytest收集用例的规则,添加命令行参数等等!下面我们来一一讲解常用的一些配置项 Help 通过命令pytest --hel ...
- pytest进阶之html测试报告
前言 Pytest系列已经写了几篇文章了,也不知道对多少人有帮助,总之对于我自己来说该掌握的都已经掌握了,那么今天我们再来说说pytest如何生成一个完整的html测试报告,让你在吹牛逼的路上再多一份 ...
- pytest进阶之xunit fixture
前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...
随机推荐
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- 警方破获超大DDoS黑产案,20万个僵尸网络运营商被抓
中国警方已镇压并逮捕了一个犯罪集团,该集团经营着一个由200,000多个受感染网站构成的僵尸网络,这些网站被用来发起DDoS攻击. 这是中国当局针对兴旺的本地DDoS租用场景进行的首次重大镇压,最大的 ...
- Component 'TABCTL32.OCX'错误的处理方法
错误:Component 'TABCTL32.OCX' or one of its dependencies not correctyly registered:a file is missing o ...
- memcached概述与基本操作
memcached 什么是memcached memcached之前是danga的一个项目,最早是为LiveJournal服务的,当初设计师为了加速LiveJournal访问速度而开发的,后来被很多大 ...
- python gzip
通常用gzip压缩过的云端数据需要做解压处理,以下代码主要用python3实现对获取到的云端gzip压缩数据进行还原. # -*- coding: utf-8 -*- ""&quo ...
- 02 Struts2框架----学习笔记2(了解一下,已过时)
1.*号通配符优化struts.xml代码 创建一个UserAction的动作类 package action; import com.opensymphony.xwork2.ActionSuppor ...
- 17.Python略有小成(包,logging模块)
Python(包,logging模块) 一.包 什么是包 官网解释 : 包是一种通过使用'.模块名'来组织python模块名称空间的方式 , 具体来讲 , 包就是一个包含有__ init __.py文 ...
- ORACLE链接SQLSERVER数据库数据操作函数范例
ORACLE链接SQLSERVER数据库数据操作函数范例 create or replace function FUN_NAME(LS_DJBH IN varchar2 ,LS_ITM varchar ...
- proxy_banner
- python pip 切换阿里云镜像
示例: pip install xxx -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com