安装:

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. 【Shell常用命令二】管道符 通配符

    ======================================

  2. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  3. [LeetCode] 351. Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  4. Python3 列表list合并的4种方法

    方法1: 直接使用"+"号合并列表 aList = [1,2,3] bList = ['www', 'pythontab.com'] cList = aList + bList   ...

  5. OpenJudge 1088 滑雪

    总时间限制: 1000ms 内存限制: 65536kB 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者 ...

  6. Javaspring+mybit+maven中实现定时任务

    背景:在Javaspring中,定时的启动某一个任务时,使用@Scheduled来实现 Javaspring工程创建好之后,直接创建下面的class文件即可.具体的用法可参照 https://www. ...

  7. vcf和bed的位置信息区别

    vcf和bed的位置信息区别 vcf和gff一般是从1-base开始计数,也就是文件里所代表的的位置是染色体的真实位置 bed文件一般所代表的位置是从0开始计数的,是一个半闭合区间,也就是(0,200 ...

  8. TP5 模型CURD

    ThinkPHP5的模型是一种对象-关系映射(Object / Relation Mapping ,简称 ORM)的封装,并且提供了简洁的ActiveRecord实现.一般来说,每个数据表会和一个“模 ...

  9. c++ 初始化

    1. 内置类型默认初始化 内置类型如果没有被显示初始化,则会被编译器默认初始化.初始化会根据①变量类型的不同②变量类型位置,来决定初始化之后的值.但是内置类型如果在函数体内部,则将不被初始化——也就是 ...

  10. DS 壹之型 头指针与头结点

    之前结合网上博客整理的笔记,希望能帮你解除疑惑!