在每種測試方法之前清理django中的數據庫

[英]Cleaning up a database in django before every test method


By default when Django runs against sqlite backend it creates a new in memory database for a test. That means for every class that derives from unittest.TestCase, I get a new database. Can this be changed so that it is cleared before every test method is run?

默認情況下,當Django針對sqlite后端運行時,它會為測試創建一個新的內存數據庫。這意味着對於從unittest.TestCase派生的每個類,我得到一個新的數據庫。是否可以更改以便在每個測試方法運行之前將其清除?

Example: I am testing a manager class that provides additional abstraction on top of Django persistent objects. The code looks more-less like that

示例:我正在測試一個在Django持久對象之上提供額外抽象的管理器類。代碼看起來更像那樣

class TestForManager(unittest.TestCase):
  def testAddingBlah(self):
    manager = Manager()
    self.assertEquals(manager.getBlahs(), 0)
    manager.addBlah(...)
    self.assertEquals(manager.getBlahs(), 1)

  def testAddingBlahInDifferentWay(self):
    manager = Manager()
    self.assertEquals(manager.getBlahs(), 0)
    manager.addBlahInDifferentWay(...)
    self.assertEquals(manager.getBlahs(), 1)

Now, the first assertion of second test fails, because the state of the database is preserved between test calls and there already is an instance of Blah in the database.

現在,第二次測試的第一次斷言失敗,因為在測試調用之間保留了數據庫的狀態,並且數據庫中已經存在Blah的實例。

3 个解决方案

#1


35  

As always, solution is trivial: use django.test.TestCase not unittest.TestCase. And it works in all major versions of Django!

與往常一樣,解決方案是微不足道的:使用django.test.TestCase而不是unittest.TestCase。它適用於Django的所有主要版本!

#2


0  

Why not do the following? This accomplishes what you need without a significant change to your code.

為什么不這樣做呢?這樣就可以在不對代碼進行重大更改的情況下完成您的需求。

class TestOneForManager(unittest.TestCase):
  def testAddingBlah(self):
    manager = Manager()
    self.assertEquals(manager.getBlahs(), 0)
    manager.addBlah(...)
    self.assertEquals(manager.getBlahs(), 1)

class TestTwoForManager(unittest.TestCase):
  def testAddingBlahInDifferentWay(self):
    manager = Manager()
    self.assertEquals(manager.getBlahs(), 0)
    manager.addBlahInDifferentWay(...)
    self.assertEquals(manager.getBlahs(), 1)

Edit. The "reset on TestCase" feature gives you complete control.

編輯。 “重置TestCase”功能可讓您完全控制。

  • Many test methods in a single TestCase are good when you have test cases that don't interfere with each other.

    當您擁有不會相互干擾的測試用例時,單個TestCase中的許多測試方法都很好。

  • Few test methods in a single TestCase are good when you have test cases that interfere with each other.

    當測試用例相互干擾時,單個TestCase中的測試方法很少。

You can choose which model applies to your tests by grouping your test methods in one or many TestCases. You have total and complete control.

您可以通過將測試方法分組到一個或多個TestCase中來選擇適用於您的測試的模型。你有完全和完全的控制權。

#3


0  

You can use the tearDown method. It will be called after your test is run. You can delete all Blahs there.

您可以使用tearDown方法。它將在您的測試運行后調用。你可以刪除那里的所有Blahs。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/01/12/725fa27e9bb6542d54568896eef4ecd7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com