解释“设置”和“拆卸”测试用例中使用的Python方法
任何人都可以解释在编写测试用例时使用 Python 的 setUp
和 tearDown
方法,除了在调用测试方法之前立即调用 setUp
和 < code>tearDown 被调用后立即被调用?
Can anyone explain the use of Python's setUp
and tearDown
methods while writing test cases apart from that setUp
is called immediately before calling the test method and tearDown
is called immediately after it has been called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您将所有先决条件步骤添加到 setup 中,将所有清理步骤添加到tearDown 中。
您可以在此处阅读更多示例。
例如,您有一个测试,要求项目存在或某些状态 - 因此您将这些操作(创建对象实例、初始化数据库、准备规则等)放入设置中。
另外,正如您所知,每个测试都应该停止在它开始的地方 - 这意味着我们必须将应用程序状态恢复到其初始状态 - 例如关闭文件、连接、删除新创建的项目、调用事务回调等等 - 所有这些步骤将包含在拆卸中。
因此,测试本身应该只包含在测试对象上执行以获得结果的操作,而 setUp 和tearDown 是帮助您保持测试代码干净和灵活的方法。
您可以为一堆测试创建setUp和tearDown,并在父类中定义它们 - 这样您就可以轻松支持此类测试并更新常见的准备和清理工作。
如果您正在寻找一个简单的示例,请使用以下示例链接
In general you add all prerequisite steps to setUp and all clean-up steps to tearDown.
You can read more with examples here.
For example you have a test that requires items to exist, or certain state - so you put these actions(creating object instances, initializing db, preparing rules and so on) into the setUp.
Also as you know each test should stop in the place where it was started - this means that we have to restore app state to it's initial state - e.g close files, connections, removing newly created items, calling transactions callback and so on - all these steps are to be included into the tearDown.
So the idea is that test itself should contain only actions that to be performed on the test object to get the result, while setUp and tearDown are the methods to help you to leave your test code clean and flexible.
You can create a setUp and tearDown for a bunch of tests and define them in a parent class - so it would be easy for you to support such tests and update common preparations and clean ups.
If you are looking for an easy example please use the following link with example
您可以使用它们来分解测试套件中所有测试通用的代码。
如果您的测试中有很多重复的代码,您可以通过将此代码移动到 setUp/tearDown 来缩短它们。
您可以使用它来创建测试数据(例如设置假货/模拟),或用假货删除函数。
如果您正在进行集成测试,则可以在setUp中使用检查环境先决条件,如果某些设置不正确则跳过测试。
例如:
You can use these to factor out code common to all tests in the test suite.
If you have a lot of repeated code in your tests, you can make them shorter by moving this code to setUp/tearDown.
You might use this for creating test data (e.g. setting up fakes/mocks), or stubbing out functions with fakes.
If you're doing integration testing, you can use check environmental pre-conditions in setUp, and skip the test if something isn't set up properly.
For example:
假设您有一个包含 10 个测试的套件。其中 8 个测试共享相同的设置/拆卸代码。其他2个没有。
设置和拆卸为您提供了重构这 8 个测试的好方法。现在你如何处理其他两个测试?您可以将它们移动到另一个测试用例/套件。因此,使用设置和拆卸也有助于提供一种将测试分解为案例/套件的自然方法
Suppose you have a suite with 10 tests. 8 of the tests share the same setup/teardown code. The other 2 don't.
setup and teardown give you a nice way to refactor those 8 tests. Now what do you do with the other 2 tests? You'd move them to another testcase/suite. So using setup and teardown also helps give a natural way to break the tests into cases/suites