pytest使用

安装:


pip install pytest

pip install pytest-cov

 

utils.py代码

1
2
3
4
5
6
7
8
def add(a, b):
    return a+b
 
def inc(x):
    return x + 1
 
def func_except():
    raise SystemExit(1)  

test_utils.py 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- 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')

 

运行测试:

1
2
3
4
5
6
7
8
9
10
>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=./

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 ========================================================================================================

  

  

posted @   bonelee  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2018-07-02 leetcode 720. Longest Word in Dictionary
2018-07-02 发送垃圾邮件的僵尸网络——药物(多)、赌博、股票债券等广告+钓鱼邮件、恶意下载链接、勒索软件+推广加密货币、垃圾股票、色情网站(带宏的office文件、pdf等附件)
2018-07-02 Docker 修改镜像源地址
点击右上角即可分享
微信分享提示