[英]django unit testing and global fixtures
I'm working on a web project in Django, and I'm using the python unittest framework. 我正在Django的一个Web项目,我正在使用python unittest框架。 For every app I have some fixtures.
对于每个应用程序,我有一些固定装置。 This means, that every app has some identical tables in fixtures.
这意味着,每个应用程序在灯具中都有一些相同的表格。 I would like to share fixtures between apps and testcases, because otherwise if I change a model, I will have to change all json fixtures where this concrete table is referenced.
我想在应用程序和测试用例之间共享装置,否则如果我更改模型,我将不得不更改所有引用此具体表的json装置。
Is it sensible to use global fixtures? 使用全球设备是否明智?
Do not use static fixtures, it is a bad automated tests pattern. 不要使用静态夹具,这是一个糟糕的自动化测试模式。 Use dynamic fixtures.
使用动态灯具。
Django Dynamic Fixture has options to create global fixtures. Django Dynamic Fixture可以选择创建全局灯具。 Check its Nose plugin or the Shelve option .
检查其Nose插件或Shelve选项 。
I would highly recommend looking into Django's Testing architecture . 我强烈建议调查Django的测试架构 。 Check out TestCase.fixtures especially;
特别检查TestCase.fixtures; this is far more advanced and Django-specific than unittest.
这比单元测试更先进,特定于Django。
I can't think of anything wrong with using global fixtures as long as you delete them in your tearDown
method (or teardown_test_environment
method - see below). 只要你在
tearDown
方法中删除它们(或teardown_test_environment
方法 - 见下文),我就不会想到使用全局灯具有什么问题。
I am not sure if you are asking to find out how to do this. 我不确定你是否要求了解如何做到这一点。 If so there are two ways that I can think of.
如果是这样,我可以想到两种方式。
Use a common base class for all your tests. 为所有测试使用公共基类。 Something like this:
像这样的东西:
class TestBase(django.test.TestCase): fixtures = ['common_fixtures.xml'] class MyTestClass(TestBase): fixtures = TestBase.fixtures + ['fixtures_for_this_test.xml'] def test_foo(self): # test stuff
Use a custom test runner. 使用自定义测试运行器。 In your test runner load all the fixtures you need before running the tests and take them down after executing the tests.
在测试运行器中,在运行测试之前加载所需的所有夹具,并在执行测试后将其取下。 You should preferably do this by using your own
setup_
and teardown_test_environment
methods. 您最好使用自己的
setup_
和teardown_test_environment
方法执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.