安装:

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. 大网扫描在发现APT组织方面的一个应用

    如何发现CobalStike服务端? 答: 扫HTTP response header: "HTTP/1.1 404 Not Found" balaba-"Server& ...

  2. Kubernetes 使用 ingress 配置 https 集群(十五)

    目录 一.背景 1.1 需求 1.2 Ingress 1.3 环境介绍 二.安装部署 2.1.创建后端 Pod 应用 2.2 创建后端 Pod Service 2.3.创建 ingress 资源 2. ...

  3. layui父页面执行子页面方法

    parent.window[layero.find('iframe')[0]['name']].子页面方法(); layero.find('iframe')[0].contentWindow.子页面方 ...

  4. 【转帖】两种IO模式:Proactor与Reactor模式

    两种IO模式:Proactor与Reactor模式 https://www.cnblogs.com/pigerhan/p/3474217.html. 挺好的说明了epoll和IOCP的区别 在高性能的 ...

  5. Servlet知识点小结

    Servlet: server applet * 概念:运行在服务器端的小程序 * Servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则. * 将来我们自定义一个类,实 ...

  6. 嵌入式02 STM32 实验01 端口复用和重映射

    内设与外设: 端口复用和端口重映射都需要了解内设和外设,那么什么是内设?什么是外设? 内设:单片机内部集成的功能一般包括:串口模块.SPI模块(Serial Peripheral Interface  ...

  7. [HAOI2015]树上操作 题解

    题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增 ...

  8. DFS解决八皇后问题

    2019-07-29 16:49:15 #include <bits/stdc++.h> using namespace std; ][]; int tot; int check(int ...

  9. golang面对接口

  10. 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035

    package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...