TestCase 类中的 setUpClass、setUpTestData 和 setUp 之间有什么区别? [英] What are the differences between setUpClass, setUpTestData and setUp in TestCase class?

查看:39
本文介绍了TestCase 类中的 setUpClass、setUpTestData 和 setUp 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体地说,每个用例是什么?

More specifically, what are the use cases for each?

到目前为止我所理解的:

What I've understood so far:

  • 此方法运行一次,在测试类中的所有测试之前
  • 如果数据库具有事务支持,则此方法运行一次.否则它会在每次测试之前运行.
  • 此方法在测试类中的每个测试之前运行.

从我上面提到的理解来看,setUpTestData 似乎位于 setUpClass 和 setUp 之间的区域.为什么我们需要 setuUpTestData 的类级别方法,而通过 setUpClasssetUp 或两者的组合可以达到相同的效果?

From the understanding I mentioned above, it seems setUpTestData lies in the area between setUpClass and setUp. Why do we need a class level method for setuUpTestData while the same effect could be achieved by either setUpClass or setUp or a combination of the both?

推荐答案

Update/Correction after Alasdair's comment

Update/Correction after Alasdair's comment

  • setUpClass 用于执行类范围的初始化/配置(例如,创建连接、加载网络驱动程序).例如,当使用 setUpClass 打开数据库连接/会话时,您可以使用 tearDownClass 关闭它们.
  • setUpClass 在运行任何测试之前为 TestCase 调用一次.类似地,tearDownClass 在所有测试运行后被调用.
  • setUpClass is used to perform class-wide initialization/configuration (e.g. creating connections, loading webdrivers). When using setUpClass for instance to open database connection/session you can use tearDownClass to close them.
  • setUpClass is called once for the TestCase before running any of the tests. Similarly tearDownClass is called after all the tests have run.

文档说明:

SimpleTestCase 及其子类(例如 TestCase,...)依靠 setUpClass() 和 tearDownClass() 来执行一些类范围的初始化(例如覆盖设置).如果您需要覆盖这些方法,请不要忘记调用超级实现:

SimpleTestCase and its subclasses (e.g. TestCase, ...) rely on setUpClass() and tearDownClass() to perform some class-wide initialization (e.g. overriding settings). If you need to override those methods, don’t forget to call the super implementation:

setUpTestData

  • setUpTestData 用于为每个 TestCase 创建初始测试数据.此方法由 TestCase.setUpClass() (src)
  • setUpTestData 为 TestCase 调用一次,如 文档.如果数据库不支持事务,将在每次测试运行之前调用 setUpTestData(感谢 @Alasdair 纠正我)
  • setUpTestData

    • setUpTestData is used to create initial test data per TestCase. This method is called by TestCase.setUpClass() (src)
    • setUpTestData is called is once for TestCase, as explained in documentation. In case databases does not support transactions, setUpTestData will be called before each test run (thanks @Alasdair for correcting me)
      • setUp 将在每次测试运行之前调用,并应用于为每次测试运行准备测试数据集.
      • setUp will be called before each test run, and should be used to prepare test dataset for each test run.

      使用 setUpTestData 可以提高测试性能,请注意测试中对此数据的更改将在不同的测试运行之间持续存在.如果需要重新加载,可以通过 setUp 方法完成.如果用于运行测试的数据库不支持事务,则性能提升无效(因为 setUpTestData 将在每次测试运行前调用)

      Using setUpTestData allows for test performance improvement, be aware that change to this data in tests will persist between different test runs. If needs to be reloaded it can be done so from setUp method. If database used for running tests does not support transactions, performance improvement is negated (as setUpTestData will be called before each test run)

      这篇关于TestCase 类中的 setUpClass、setUpTestData 和 setUp 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆