python 的单元测试框架pytest(其一)

1,最简单的测试(新建的文件要以(test _ * .py或* _test.py)的命名形式!)

# content of test_sample.py
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

运行测试pytest将在当前目录及其子目录中运行test _ * .py或* _test.py形式的所有文件
在实际项目之中的使用

在项目之中会新建一个模块(test)用来进行项目的测试.

新建test_project.py 文件,运行test_project.py 文件,这样就进行整个模块的测试

#coding=utf-8
import pytest
if __name__ == '__main__':
    pytest.main("/home/sxu/Desktop/code/lico-cn/openHPC_web_project/tests") 

也可以将测试跑起来这样可以及时抛出异常!

#when-change 一个python 模块 只要是代码改动都会触发
when-change 'pytest a.py'

 

posted @ 2018-08-02 15:15  十七楼的羊  阅读(157)  评论(0编辑  收藏  举报