- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要该实例的 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
下有一对实例方法setUp
和tearDown
,分别在每次测试之前和之后执行。
来自docs :
setUp()
Method called to prepare the test fixture. This is called immediately before calling the test method; other thanAssertionError
orSkipTest
, 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 thanAssertionError
orSkipTest
, 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 thesetUp()
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/
拥有/想要列表匹配算法 我正在一个高流量网站上实现一个元素交易系统。我有大量用户,每个用户都为许多特定项目维护一个 HAVE 列表和一个 WANT 列表。我正在寻找一种算法,使我能够根据您的 HAVE
我可以知道拥有 .hgtags 的目的是什么吗?我可以删除它吗?因为它不在 .hg文件夹,似乎“污染”了我的实际源代码目录。 https://bz.mercurial-scm.org/show_bug
在哪些用例中使用 [[nodiscard]] 有益类型? 关于类型,[[nodiscard]]如果任何返回该类型实例的函数的返回值被省略,则发出警告; (引自 p0068r0): If [[nodis
我知道当一个对象只有一个所有者时使用std::unique_ptr,当一个对象有多个所有者时使用std::shared_ptr。成为对象的唯一所有者意味着什么? 成为唯一所有者是否意味着其他人都无法看
我无法真正理解拥有 vendor 文件夹的目的。根据我了解到的情况, vendor 文件夹似乎只有在您尝试使您的存储库与早于 1.11 的 golang 版本兼容时才有用。 .我们正在运行 golan
我正在尝试学习如何在 Raspberry PI 上编码。我来自使用 Windows 和 VS Code 进行编码。现在我使用 Linux Mint 19.1 和 ssh 访问 Raspbian 4.1
我有一个带有“BlockType”枚举的图形程序。根据州的不同,这可能是以下几种情况之一:木头、石头、草地等。 最初,必须针对每种可能性执行各种纹理操作,但由于进行了一些重构,枚举仅用作整数,swit
我正在创建一个网络应用程序,让用户可以搜索餐馆和咖啡馆。由于我目前除了类型之外没有其他数据来区分这两者,所以我有两种存储餐馆列表的选择。 对餐厅和咖啡馆使用同一张表,并使用枚举(文本)列说明条目是餐厅
我有一个 5 人的小团队,我需要帮助寻找有关如何拥有一个集中式 MySQL 数据库的资源或建议。我们都以虚拟方式工作,并将所有文件托管在一个 github 上以集中所有内容。因此,在不同的计算机上拥有
SELECT dnum , SUM(IF(salary>20000, 1, 0)) AS Employee FROM project INNER JOIN works_on ON pno=pnumbe
我想在 javascript 中有一个数组,其中每个项目包含 2 个属性而不是 1 个,这怎么可能? 默认情况下,以下内容仅向项目添加一个属性: var headerCellWidths = new
我确定我对这个问题的措辞很糟糕,但这是我的问题: 我正在创建一长串地毯,每 block 地毯都有图片、标题、尺寸、原价和清仓价。我将每 block 地毯都放在自己的类别中,并对其余的地毯重复这样的操作
我正在编写一个延迟渲染器,并试图打包我的 gbuffer。 将漫反射和高光存储在一起会更好吗: vec4 difSpec = (diffuse.xyz, specular) // FORMAT_RGB
下面的程序给我一个链接时错误: #include struct Test { static constexpr char text[] = "Text"; }; int main() { s
想知道 - 如果了解Linux 的生活方式或 Linux 架构,是否会为在嵌入式设备上编程提供更好的思路,尤其是当它们具有某种操作系统时。 只是想确定我没有错过任何重要的事情:) 注意: 我来自 wi
如果要测试 iPhone 应用程序,拥有一个开发代码签名条款来运行任何带有 com.mydomain.* 的应用程序是不是一个坏主意? 为什么? 编辑: 请参阅上面的斜体编辑。 最佳答案 我对此表示怀
我正在开发我自己的 Swift 框架,以便在我的两个应用程序之间私下使用。我正在使用 Carthage 来管理该依赖项和其他依赖项。 我终于完成了框架的开发并将其连接到我的一个应用程序,毫不奇怪,该应
如果我有一个 C 类型的原始指针,是否可以从拥有指针数据的相同类型创建一个 std::vector 而无需任何数据复制(仅 move )?促使我问这个问题的是 std::vector 的 data()
有 2 个部分的好处是什么 - .data 和 .bss 用于流程范围变量。为什么不只拥有一个呢?我知道每个部分的用途。我正在使用 gcc。 最佳答案 .bss 消耗“内存”但不消耗可执行文件中的空间
我注意到,我们可以使用 StatefulWidget 构建 UI 的任何部分,而无需担心状态。 我想问的很简单,当我们可以将任何 UI 类构建为 StatefulWidget 时,首先拥有 State
我是一名优秀的程序员,十分优秀!