Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断言失败后,如何继续单元测试?_Python_Unit Testing_Python 3.x - Fatal编程技术网

Python 断言失败后,如何继续单元测试?

Python 断言失败后,如何继续单元测试?,python,unit-testing,python-3.x,Python,Unit Testing,Python 3.x,我想在testidProperty中执行第二个断言,不管第一个断言是否通过。如何在不在第一个断言周围放置try/except块的情况下执行此操作?代码如下: class BlockTests(unittest.TestCase): def setUp(self): self.city1 = City(1, "New York") self.city2 = City(2, "Boston") def tearDown(self):

我想在testidProperty中执行第二个断言,不管第一个断言是否通过。如何在不在第一个断言周围放置try/except块的情况下执行此操作?代码如下:

class BlockTests(unittest.TestCase):
    def setUp(self):
        self.city1 = City(1, "New York")
        self.city2 = City(2, "Boston")

    def tearDown(self):
        self.city1 = None

    def testIdProperty(self):
        self.assertEqual(2, self.city1.id_city, "Assertion Error!") #This assert might fail
        self.assertEqual(2, self.city2.id_city, "Assertion Error!") #Execute this anyway

    def testIdGetter(self):
        self.assertEqual(1, self.city1.get_id_city(), "Error!")

当前,如果第一个断言失败,测试用例会立即报告失败,第二个断言永远不会运行。

从您的测试中,您似乎正在尝试测试class
City
id
属性。因此,您可以测试在
setUp
中定义的两个实例是否设置了正确的值-类似于您所做的:

def testIdProperty(self):
    self.assertEqual(1, self.city1.id_city, "Fail if City1 does not have id 1")
    self.assertEqual(2, self.city2.id_city, "Fail if City2 does not have id 2")
现在,当您运行此测试时,它应该通过。如果有一天您破坏了代码,而这两个断言中的一个失败,那么您希望在运行测试并修复它时看到它

但是,如果出于某种原因,您不希望这些断言在稍后完成测试之前暂时失败,那么您可以这样跳过该测试:

@unittest.skip("skip this test im not done writing it yet!")
def testIdProperty(self):
    self.assertEqual(1, self.city1.id_city, "Fail if City1 does not have id 1")
    self.assertEqual(2, self.city2.id_city, "Fail if City2 does not have id 2")
编辑:确定如果要抑制失败的断言导致的错误,请不要尝试/除非在每个需要的地方都尝试。最好在测试类之外编写通用函数:

def silent_assert(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except Exception as exc:
        print exc.message
并将此调用作为断言,这样它将运行代码,并且仅在失败时以静默方式打印错误:

def testIdProperty(self):
    silent_assert(self.assertEqual, 1, self.city1.id_city, "City1 does not have id 1")
    silent_assert(self.assertEqual, 2, self.city2.id_city, "City2 does not have id 2")
您将能够对其调用任何断言,并传递每个断言接受的任意数量的参数。不确定这样隐藏错误是否是个好主意,我从来没有这样做过,但那只是我,每个人都有自己的组织风格

用什么


如果您正在执行多个检查,并且始终希望运行所有检查,最简单的解决方案就是将它们分解为多个测试,并使每个测试用例只有一个检查:

class BlockTests(unittest.TestCase):
    def setUp(self):
        self.city1 = City(1, "New York")
        self.city2 = City(2, "Boston")

    def tearDown(self):
        self.city1 = None

    def testIdPropertyCity1(self):
        self.assertEqual(2, self.city1.id_city, "Assertion Error!") #This assert might fail

    def testIdPropertyCity2(self):
        self.assertEqual(2, self.city2.id_city, "Assertion Error!") #Execute this anyway

    def testIdGetter(self):
        self.assertEqual(1, self.city1.get_id_city(), "Error!") 

如果您不关心该测试用例中的
self.city1.id\u city
,而只关心
self.city2.id\u city
,为什么不删除行
self.assertEqual(2,self.city1.id\u city,“断言错误!”)
?我在这里键入之前删除了几行。。。我现在修好了!要操作,为什么要传递失败的断言?如果是这样的话,为什么你甚至需要一个断言呢?在阅读了这篇讨论之后,我似乎很清楚OP在问什么:“我怎样才能让一个测试用例执行一系列断言并报告任何和所有的失败结果,而不是在断言第一次失败时停止执行?”@Edward yes,这是我需要做的。我不想跳过所有断言,我只想跳过一个断言(谁失败了),对失败的断言进行注释怎么样,这是一个选项吗?e、 g.
#self.assert…
。如果没有,为什么要运行它,因为你不知道/无法看到发生了什么?我需要这样做:`try:self.assertEqual(2,self.city1.id_city,“如果city1没有id 1就失败!”),除了:print(“Assert Fail”)self.assertEqual(2,self.city2.id_city,“如果city2没有id 2就失败!”)`什么意思是某个错误?用你期望该行出现的任何错误替换它。(您也可以将其替换为
异常
,但这是一种不好的做法,因为它可能会隐藏一个沿途引入的bug)。
class BlockTests(unittest.TestCase):
    def setUp(self):
        self.city1 = City(1, "New York")
        self.city2 = City(2, "Boston")

    def tearDown(self):
        self.city1 = None

    def testIdPropertyCity1(self):
        self.assertEqual(2, self.city1.id_city, "Assertion Error!") #This assert might fail

    def testIdPropertyCity2(self):
        self.assertEqual(2, self.city2.id_city, "Assertion Error!") #Execute this anyway

    def testIdGetter(self):
        self.assertEqual(1, self.city1.get_id_city(), "Error!")