gpt4 book ai didi

Python单元测试tearDownClass()实例,如何拥有它?

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:42 25 4
gpt4 key购买 nike

我需要该实例的 tearDownClass(cls) 方法。我的意思是我可以引用self(实例),而不是cls()。
一种tearDownTestCase(self)。

我的目的是在运行所有测试用例后清理数据库。
tearDown(self) 在每次测试结束时执行,我不想使用它。
tearDownClass(cls) 在所有测试完成后执行一次,但它不包含对 self 的引用,并且我需要访问 self 的属性(更准确地说是一个函数)。

有办法实现这一点吗?

Python 3.6

真实场景示例:

import unittest

'''
The real records saved in the database came from an external source (an API) so the ID is preassigned.
For the test I use everywhere a predefined fixed id, so the code result more clean.
'''

record_id = "TEST"

class RepositoryTest(unittest.TestCase):


def setUp(self):
# real initialization, reading connection string from config, database name, collection...
self.repository = None
# self._cleanup_record() # maybe here is executed too many unnecessary times

def tearDown(self):
# here is executed unnecessarily, because (where needed) the cleanup is eventually executed BEFORE the test (or in its beginning)
self._cleanup_record()

### pseudo (desired) method ###
def tearDownTestCase(self):
self._cleanup_record()

def tearDownClass(cls):
# self._cleanup_record() # self is not available
# rewrite the same code of initialization and _cleanup_record()
# I want to a void (or simplify this)
pass

# this is 1 of N tests
def test_save_record(self):

# cleanup (because I don't know in which state the database is)
self._cleanup_record() # almost every test require this, so it can be done in setUp()

# arrange
record = self._create_record()

# act
self.repository.save_record(record)

# assert
saved_record = self._get_record()
self.assertEquals(saved_record["my field"], record["my field"])



# utility methods

def _get_record(self):
# use self.repository and return the record with id = record_id
pass # return the record

def _create_record(self):
# use self.repository and create (and save) a record with id = record_id
return None # return the saved record

def _cleanup_record(self):
# use self.repository and delete the record with id = record_id (if exists)
pass
<小时/>

在tearDown()方法中进行清理会导致:

设置

.测试1
清理
测试
清理(​​=多余)
。 。 .
.测试N
清理
测试
清理

相反,我想要这个:
(如果在所有测试完成后执行tearDownX()方法是可能的)

设置

(测试1)
清理
测试
。 。 .
(测试N)
清理
测试

tearDownX(自身)
清理(​​最终)

这或多或少是我在过去几年中设计测试的方式。它尝试对中断的调试 session (无清理)和脏的初始数据库状态进行防弹。

<小时/>

作为临时解决方案,我在tearDownClass(cls) 方法中复制了清理方法,但我不满意。理想情况下,我可以简单地调用 self._cleanup_record 但这是不可能的,因为tearDownClass 是一个类方法。

我希望这一切都有意义。

谢谢,

亚历山德罗

最佳答案

是的,unittest.TestCase下有一对实例方法setUptearDown,分别在每次测试之前和之后执行。

来自docs :

setUp() Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

tearDown() Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception, other than AssertionError or SkipTest, raised by this method will be considered an additional error rather than a test failure (thus increasing the total number of reported errors). This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.

更新(评论后)

好吧,您可能别无选择,只能重新设计代码。您可以将数据库清理方法设置为类方法而不是实例方法。

无论如何,由于您不应该依赖测试执行顺序,也不应该让您的测试相互依赖,所以使用 setUp 方法为每个测试创建数据库固定装置仍然是明智的做法并在每次测试后使用 tearDown 方法清理它。

另一个选择是使用 mock对于测试中的数据库,因此您无需担心清理它。

关于Python单元测试tearDownClass()实例,如何拥有它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43483683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com