-
上述 class-level
atomic塊允許在類級別創建初始數據,一次用於整個TestCase。與使用setUp()相比,此技術允許更快的測試。例如:
from django.test import TestCase class MyTests(TestCase): @classmethod def setUpTestData(cls): # Set up data for the whole TestCase cls.foo = Foo.objects.create(bar="Test") ... def test1(self): # Some test using self.foo ... def test2(self): # Some other test using self.foo ...請注意,如果測試在不支持事務的數據庫上運行(例如,MySQL 和 MyISAM 引擎),則將在每次測試之前調用
setUpTestData(),從而抵消速度優勢。在 Django 3.2 中更改:分配給
setUpTestData()中的類屬性的對象必須支持使用創建深層副本,以便將它們與每個測試方法執行的更改隔離開來。在以前的 Django 版本中,這些對象被重用,並且對它們所做的更改在測試方法之間保持不變。copy.deepcopy()
本文介紹 django.test.TestCase.setUpTestData 的用法。
聲明
classmethod TestCase.setUpTestData()
相關用法
- Python Django TestCase.captureOnCommitCallbacks用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
- Python TextBlob.correct()用法及代碼示例
- Python Tensorflow math.accumulate_n()用法及代碼示例
- Python Tensorflow cosh()用法及代碼示例
- Python TextCalendar prmonth()用法及代碼示例
- Python Tensorflow sin()用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tensorflow asinh()用法及代碼示例
- Python Tensorflow nn.softplus()用法及代碼示例
- Python Tensorflow exp()用法及代碼示例
- Python Tensorflow logical_and()用法及代碼示例
- Python Tensorflow logical_or()用法及代碼示例
- Python TextCalendar formatyear()用法及代碼示例
- Python Tensorflow atanh()用法及代碼示例
- Python TextBlob.Word.spellcheck()用法及代碼示例
- Python Tensorflow bitwise.bitwise_and()用法及代碼示例
- Python TextCalendar pryear()用法及代碼示例
- Python TextBlob.noun_phrases()用法及代碼示例
- Python Tensorflow nn.sigmoid()用法及代碼示例
- Python Tensorflow bitwise.invert()用法及代碼示例
- Python TextBlob.word_counts()用法及代碼示例
- Python Tensorflow abs()用法及代碼示例
- Python Tensorflow nn.tanh()用法及代碼示例
- Python Tensorflow acosh()用法及代碼示例
注:本文由純淨天空篩選整理自djangoproject.com大神的英文原創作品 django.test.TestCase.setUpTestData。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
