今天的实作内容主要根据教学网站进行。
接续昨天的内容,今天将实作model和form的测试程序。
使用以下指令即开始自动化测试,此指令会将专案资料夹下所有符合test*.py档名格式的程序进行自动化测试。
python manage.py test
若想只执行特定程序,则以以下指令进行。 (引用自教学网站)
python3 manage.py test catalog.tests # Run the specified module
python3 manage.py test catalog.tests.test_models # Run the specified module
python3 manage.py test catalog.tests.test_models.YourTestClass # Run the specified class
python3 manage.py test catalog.tests.test_models.YourTestClass.test_one_plus_one_equals_two # Run the specified method
测试程序的重点在於测试我们自己开发的程序,而不是测试Django原有功能。
在Model中,我们主要进行的是设定栏位及其规格,和部分客制化开发的Method。
故在测试环节,我们可以测试:
栏位设定是否符合规格,例如label、最大长度。
Method回传内容是否符合预期。
於test_models.py中,import Django的测试模组,开发Author的Model测试,并於初始化阶段新增一个Author供其他测试方法使用。
from django.test import TestCase
# Create your tests here.
from track.models import Author
class AuthorModelTest(TestCase):
@classmethod
def setUpTestData(cls):
Author.objects.create(authorname='A')
def test_authorname_max_length(self):
author=Author.objects.get(authorname='A')
max_length = author._meta.get_field('authorname').max_length
self.assertEquals(max_length,30)
def test_authorname_label(self):
author=Author.objects.get(authorname='A')
field_label = author._meta.get_field('authorname').verbose_name
self.assertEquals(field_label,'author name')
def test_authorid_label(self):
author=Author.objects.get(authorname='A')
field_label = author._meta.get_field('authorid').verbose_name
self.assertEquals(field_label,'author id')
def test_object_name_is_authorname(self):
author=Author.objects.get(authorname='A')
expected_object_name = author.authorname
self.assertEqual(expected_object_name, str(author))
在Form的部分,我们自己开发的内容主要为Form所使用的栏位、显示内容、与栏位检核。
故在测试环节,可以对於以下环节进行测试:
栏位设定是否符合规格,例如label、最大长度、显示的辅助资讯。
栏位检核:Django本身提供的栏位类型检核不需要另外测试,例如如果我们在forms.py中设定了UrlField,则我们不需要另外测试此栏位的格式是否为URL。但对於我们自己开发的客制化检核机制,则还是需要进行测试,例如网域是否符合系统支援范围。
於test_models.py中,import Django的测试模组,开发TrackBookForm的测试。
from django.test import TestCase
# Create your tests here.
from track.forms import TrackBookForm
class TrackBookFormTest(TestCase):
def test_oriurl_label(self):
form = TrackBookForm()
self.assertEqual(form.fields['oriurl'].label, '网址')
def test_oriurl_help_text(self):
form = TrackBookForm()
self.assertEqual(form.fields['oriurl'].help_text, '输入小说主页的网址')
def test_oriurl_max_length(self):
form = TrackBookForm()
self.assertEqual(form.fields['oriurl'].max_length, 100)
def test_oriurl_is_valid(self):
url = 'http://www.jjwxc.net/onebook.php?novelid=3860813'
form_data = {'oriurl': url}
form = TrackBookForm(data = form_data)
self.assertTrue(form.is_valid())
def test_oriurl_is_invalid(self):
url = 'https://m.qidian.com/book/1023422452.html'
form_data = {'oriurl': url}
form = TrackBookForm(data = form_data)
self.assertFalse(form.is_valid())
<<: 成为工具人应有的工具包-17 MyLastSearch
>>: [Day 27] 实作 Redis PubSub Keyspace Notification 订阅 Session Key Expired 事件通知
Hello, 各位 iT邦帮忙 的粉丝们大家好~~~ 本篇是 Re: 从零开始用 Xamarin 技...
前言 前一篇介绍了函式与变数,那麽接下来要来回头补充一下关於前面所没聊到的变数作用域。 变数作用域 ...
JUCE 提供 juce::var 类别,可用来储存多种资料型别,如 int, int64, flo...
纪录时间:2022/05/02 【碎碎念】 欧~天啊...距离上次纪录已经快半个月前了w 主要是自己...
简介 又有一个大老曾经说过 : Programming = Data structures + Al...