@unittest.skipIf(test01_connect.result != unittest.TestResult().wasSuccessful(),"skip because last case failed") AttributeError: 'function' object has no attribute 'result'
时间: 2024-02-28 11:53:50 浏览: 106
这个错误是因为 `test01_connect` 是一个函数,不是一个 `unittest.TestCase` 对象,所以没有 `result` 属性。
如果你想在一个测试用例中判断上一个测试用例是否通过,可以使用 `self._outcome.result` 来获取上一个测试用例的执行结果。具体代码如下:
``` python
import unittest
class MyTestCase(unittest.TestCase):
def test_case1(self):
self.assertEqual(1+1, 2)
def test_case2(self):
self.assertEqual(2+2, 3)
def test_case3(self):
self.assertEqual(3+3, 6)
def test_case4(self):
last_result = self._outcome.result
if not last_result.wasSuccessful():
self.skipTest("skip because last test case failed")
self.assertEqual(4+4, 8)
```
在上面的代码中,我们在 `test_case4` 中使用了 `self._outcome.result` 来获取上一个测试用例的执行结果。然后判断上一个测试用例是否成功,如果失败了,则跳过当前测试用例。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)