解释“设置”和“拆卸”测试用例中使用的Python方法

发布于 2024-11-26 20:40:55 字数 150 浏览 1 评论 0原文

任何人都可以解释在编写测试用例时使用 Python 的 setUptearDown 方法,除了在调用测试方法之前立即调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2024-12-03 20:40:55

通常,您将所有先决条件步骤添加到 setup 中,将所有清理步骤添加到tearDown 中。

您可以在此处阅读更多示例。

定义 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.

When a setUp() method is defined, the test runner will run that method
prior to each test. Likewise, if a tearDown() method is defined, the
test runner will invoke that method after each test.

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

瞎闹 2024-12-03 20:40:55

您可以使用它们来分解测试套件中所有测试通用的代码。

如果您的测试中有很多重复的代码,您可以通过将此代码移动到 setUp/tearDown 来缩短它们。

您可以使用它来创建测试数据(例如设置假货/模拟),或用假货删除函数。

如果您正在进行集成测试,则可以在setUp中使用检查环境先决条件,如果某些设置不正确则跳过测试。

例如:

class TurretTest(unittest.TestCase):

    def setUp(self):
        self.turret_factory = TurretFactory()
        self.turret = self.turret_factory.CreateTurret()

    def test_turret_is_on_by_default(self):
        self.assertEquals(True, self.turret.is_on())

    def test_turret_turns_can_be_turned_off(self):
        self.turret.turn_off()
        self.assertEquals(False, self.turret.is_on())

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:

class TurretTest(unittest.TestCase):

    def setUp(self):
        self.turret_factory = TurretFactory()
        self.turret = self.turret_factory.CreateTurret()

    def test_turret_is_on_by_default(self):
        self.assertEquals(True, self.turret.is_on())

    def test_turret_turns_can_be_turned_off(self):
        self.turret.turn_off()
        self.assertEquals(False, self.turret.is_on())
情仇皆在手 2024-12-03 20:40:55

假设您有一个包含 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文