安装:

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使用的更多相关文章

  1. flask-sqlalchemy、pytest 的单元测试和事务自动回滚

    flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...

  2. Python单元测试框架之pytest -- 断言

    对于测试来讲,不管是功能测试,自动化测试,还是单元测试.一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果.测试的成功与否就是拿实际的结果与预期的结果进行比较.这个比的过程实际就 ...

  3. Python单元测试框架之pytest -- fixtures

    fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干.通常用setup/teardown来表示.它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统 ...

  4. Python单元测试框架之pytest -- 生成测试报告

    继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告.这一节就来学习pytest如何生成测试报告. 创建test_calss.py 测试用例文件,这里以测试该文件为例. #cod ...

  5. 使用 tox flake8 pytest 规范 python 项目

    使用 tox flake8 pytest 规范 python 项目 python 中有些很好的工作来规范整个项目的开发,而其中使用较多的就是使用 tox . flake8 . pytest . tox ...

  6. pytest学习笔记(三)

    接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...

  7. pytest学习笔记(二)

    继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...

  8. pytest进阶之配置文件

    前言 pytest配置文件能够改变pytest框架代码的运行规则.比如修改pytest收集用例的规则,添加命令行参数等等!下面我们来一一讲解常用的一些配置项 Help 通过命令pytest --hel ...

  9. pytest进阶之html测试报告

    前言 Pytest系列已经写了几篇文章了,也不知道对多少人有帮助,总之对于我自己来说该掌握的都已经掌握了,那么今天我们再来说说pytest如何生成一个完整的html测试报告,让你在吹牛逼的路上再多一份 ...

  10. pytest进阶之xunit fixture

    前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...

随机推荐

  1. mongoDB索引相关

    参考链接:MongoDB索引管理-索引的创建.查看.删除 索引 db.集合名.ensureIndex({"key":1}) 使用了ensureIndex在name上建立了索引.”1 ...

  2. 了解一下JVM和GC工作机制

    题外话:很久没有写博客了,事情颇多,今天空闲下来,学习一下顺便写一下自己的了解,机会总是留给有准备的人,所以平常一定要注意知识的巩固和积累.知识的深度也要有一定的理解,不比别人知道的多,公司干嘛选你? ...

  3. LeetCode 290. 单词规律(Word Pattern) 41

    290. 单词规律 290. Word Pattern 题目描述 给定一种规律 pattern 和一个字符串 str,判断 str 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如,pattern ...

  4. Vue(六)插槽(2.6.0+)

    插槽在vue2.6.0开始有了新的更新 具名插槽(数据来自父组件) 子组件(定义插槽)这里版本前后没什么变化 <template> <div> <header> & ...

  5. 永久修改 Linux pip国内源

    一些常用的国内源 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https://mirrors.aliyun.com/pypi/simple 中国 ...

  6. 11 IO流(八)——装饰器设计模式,Filter装饰流

    声明:本文部分图片及内容引用自:https://www.cnblogs.com/qiumingcheng/p/5219631.html java装饰器设计模式 举一个形象的例子,人可以说话,而扩音器可 ...

  7. windows 开始→运行→命令集锦

    windows 开始→运行→命令集锦 来源于网络,侵权请通知我删除 命令 说明 vwinver 检查Windows版本 wmimgmt.msc 打开windows管理体系结构(WMI) wupdmgr ...

  8. Oracle 11g 总结篇2

    第一部分: 字段名的别名用""括起来,如:last_name as "姓名". 去除重复:在投影的字段名前加上 distinct 就可以了. 比如:select ...

  9. DFS集训

    2019-07-29 09:01:06 A PARTY A company has n employees numbered from 1 to n. Each employee either has ...

  10. REST framework之URL控制

    REST framework之URL控制 一 自定义路由 1.1 原始方式 from django.conf.urls import url from app01 import views urlpa ...