1
有沒有辦法檢查一個設置並僅在該設置爲True時才運行測試? 是否可以在setup方法中這樣做,以便在標誌設置爲false時不運行測試用例?有沒有辦法檢查設置並僅在設置了該設置的情況下才運行測試?
有沒有辦法檢查一個設置並僅在該設置爲True時才運行測試? 是否可以在setup方法中這樣做,以便在標誌設置爲false時不運行測試用例?有沒有辦法檢查設置並僅在設置了該設置的情況下才運行測試?
這樣做的典型方法是使用skipIf and skipUnless。您可以使用它們跳過整個測試用例或特定測試。
from unittest import skipIf
from django.conf import settings
from django.test import TestCase
@skipIf(settings.MY_SETTING == 'whatever')
class MyTestCase(TestCase):
# ...
pass
class MyTestCase2(TestCase):
@skipIf(settings.MY_SETTING == 'whatever')
def test_something(self):
# ...
pass
您可以創建自己的test
command,通過擴展django的test
命令。
在那裏你會檢查設置並優先運行測試。
from django.core.management import CommandError
from django.core.management.commands.test import Command as TestCommand
class Command(TestCommand):
def __init__(self):
from django.conf import settings
try:
if not settings.TEST_SETTING:
raise CommandError('Command error')
except:
raise CommandError('Command Error')
super(Command, self).__init__()
不錯,那對我來說是新的,tx! – sergiuz