pytest

测试框架pytest, 文档方面感觉比nose好很多

pip install pytest==3.4.2
pip install pytest-html==1.16.1


import os.path
import pytest


def getssh():  # pseudo application code
    return os.path.join(os.path.expanduser("~admin"), '.ssh')


def test_mytest(monkeypatch):
    # 使用monkeypatch模拟返回数据, 替换掉不易执行的接口
    def mockreturn(path):
        return '/abc'
    monkeypatch.setattr(os.path, 'expanduser', mockreturn)

    x = getssh()
    assert x == r'/abc\.ssh'  # windows平台的路径

if __name__ == "__main__":
    import os
    pytest.main([os.path.join(os.path.curdir, __file__), "-s", "--tb=short", "--html=./report.html"], plugins=[])

setup, teardown等的用法

#coding=utf-8
import pytest

# 功能函数
def multiply(a,b):
    return a * b

# =====fixtures========
def setup_module(module):
    print ("\n")
    print ("setup_module================>")

def teardown_module(module):
    print ("teardown_module=============>")

def setup_function(function):
    print ("setup_function------>")

def teardown_function(function):
    print ("teardown_function--->")

# =====测试用例========
def test_numbers_3_4():
    print 'test_numbers_3_4'
    assert multiply(3,4) == 12 


def test_strings_a_3():
    print 'test_strings_a_3'
    assert multiply('a',3) == 'aaa' 

if __name__ == '__main__':
    pytest.main("-s test_fixtures.py")


setup_module/teardown_module      在所有测试用例执行之后和之后执行。

setup_function/teardown_function    在每个测试用例之后和之后执行。
#coding=utf-8
import pytest

# 功能函数
def multiply(a,b):
    return a * b

class TestUM:

    # =====fixtures========

    def setup(self):
        print ("setup----->")

    def teardown(self):
        print ("teardown-->")

    def setup_class(cls):
        print ("\n")
        print ("setup_class=========>")

    def teardown_class(cls):
        print ("teardown_class=========>")

    def setup_method(self, method):
        print ("setup_method----->>")

    def teardown_method(self, method):
        print ("teardown_method-->>")

    # =====测试用例========

    def test_numbers_5_6(self):
        print 'test_numbers_5_6'
        assert multiply(5,6) == 30 

    def test_strings_b_2(self):
        print 'test_strings_b_2'
        assert multiply('b',2) == 'bb'

if __name__ == '__main__':
pytest.main("-s test_fixtures.py")

setup_class/teardown_class  在当前测试类的开始与结束执行。

setup/treadown                   在每个测试方法开始与结束执行。

setup_method/teardown_method     在每个测试方法开始与结束执行,与setup/treadown级别相同。

捕获错误

def test_fooErrorHandling():
    with pytest.raises(ValueError) as excinfo:
        foo()
    assert excinfo.value.message == 'everything is broken'

def test_barSimpleErrorHandling():
    # don't care about the specific message
    with pytest.raises(CustomError):
        bar()

def test_barSpecificErrorHandling():
    # check the specific error message
    with pytest.raises(MyErr) as excinfo:
        bar()
    assert excinfo.value.message == 'oh no!'

def test_barWithoutImportingExceptionClass():
    # if for some reason you can't import the specific exception class,
    # catch it as generic and verify it's in the str(excinfo)
    with pytest.raises(Exception) as excinfo:
        bar()
    assert 'MyErr:' in str(excinfo)

猜你喜欢

转载自blog.csdn.net/flyDeDog/article/details/79567019