一般Python中异常可以用 raise 来抛出,此时单测中想要测试错误用例是否触发异常了,可以用pytest中的 with pytest.raises(xxx) 如下:
import pytest import unittest class Info(object): """info class""" def __init__(self, name): """ 初始化 Args: name (str): 用户名称 """ if name == "": raise Exception("name is empty") self.name = name class TestInfo(unittest.TestCase): """ Testcase for Info """ def test_init(self): some_value = "" # 判断是否raise了一个Exception with pytest.raises(Exception): one = Info("") expected = "" assert some_value == expected
标签:Info,Exception,name,抛出,self,pytest,测试函数,raise From: https://www.cnblogs.com/joion/p/17897360.html