Python 官方文档:入门教程 => 点击学习
目录创建项目配置 app编写测试用例编写代码执行测试最后的话所谓测试驱动开发(TDD),就是先编写测试用例,然后编写代码来满足测试用例,具体包含以下步骤: 编写测试用例。
所谓测试驱动开发(TDD),就是先编写测试用例,然后编写代码来满足测试用例,具体包含以下步骤:
通常情况下,我们都是先写代码,然后编写测试用例,因此测试驱动开发是反直觉的,那为什么还要这么做呢?基于以下几点原因:
然而,测试驱动开发也不是银弹,以下情形并不适合测试驱动开发:
了解了测试驱动开发之后,我们用 Django 来演示一下测试驱动开发的过程。(python 3.7 以上,DjanGo 2.0 以上)
首先描述需求,我们要实现这样一个单位换算功能的 WEB 应用,可以在厘米、米、英里直接互相转换,Web 界面如图所示:
首先,我们创建一个名字叫 convert 的项目:
pip install django
django-admin startproject converter
此时 Django 已经为我们生成了 converter 目录及基本的项目文件:
converter/
converter/
__init__.py
settings.py
urls.py
wsgi.py
manage.py
然后,进入 converter 目录,创建一个名字叫 length 的 app:
cd converter
Python manage.py startapp length
然后你会看到这样的目录结构:
converter/
converter/
__init__.py
settings.py
urls.py
wsgi.py
length/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
manage.py
修改 converter/settings.py,在 INSTALLED_APPS 里加入 lengh :
INSTALLED_APPS = [
.
.
.
'length',
]
然后在 length 目录下新建 urls.py,写入以下内容:
from django.urls import path
from length import views
app_name = 'length'
urlpatterns = [
path('convert/', views.convert, name='convert'),
]
最后在 converter/urls.py 中指向 length/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('length/', include('length.urls')),
]
这样一个没有任何业务逻辑的项目就创建成功了,接下来编写测试用例:
在 lengh 目录下新建 tests.py,写入以下内容:
from django.test import TestCase, Client
from django.urls import reverse
class TestLengthConversion(TestCase):
"""
This class contains tests that convert measurements from one
unit of measurement to another.
"""
def setUp(self):
"""
This method runs before the execution of each test case.
"""
self.client = Client()
self.url = reverse("length:convert")
def test_centimetre_to_metre_conversion(self):
"""
Tests conversion of centimetre measurements to metre.
"""
data = {
"input_unit": "centimetre",
"output_unit": "metre",
"input_value": 8096.894
}
response = self.client.get(self.url, data)
self.assertContains(response, 80.96894)
def test_centimetre_to_mile_conversion(self):
data = {
"input_unit": "centimetre",
"output_unit": "mile",
"input_value": round(985805791.3527409, 3)
}
response = self.client.get(self.url, data)
self.assertContains(response, 6125.5113)
上述代码有两个测试用例,分别代表两个需求。test_centimetre_to_metre_conversion 代表厘米转米的需求,而 test_centimetre_to_mile_conversion 代表厘米转英里的需求。
这和 Django 开发没什么两样,先编写一个 fORMs.py,内容如下:
from django import forms
class LengthConverterForm(forms.Form):
MEASUREMENTS = (
('centimetre', '厘米'),
('metre', '米'),
('mile', '英里')
)
input_unit = forms.ChoiceField(choices=MEASUREMENTS)
input_value = forms.DecimalField(decimal_places=3)
output_unit = forms.ChoiceField(choices=MEASUREMENTS)
output_value = forms.DecimalField(decimal_places=3, required=False)
然后编写 html,在 length 目录下新建 templates/length.html,内容如下:
<html lang="en">
<head>
<title>Length Conversion</title>
</head>
<body>
<form action={% url "length:convert" %} method="get">
<div>
{{ form.input_unit }}
{{ form.input_value }}
</div>
<input type="submit" value="转换为:"/>
<div>
{{ form.output_unit }}
{{ form.output_value }}
</div>
</form>
</body>
</html>
然后编写最重要的视图函数 views.py,内容如下:
from django.shortcuts import render
from length.forms import LengthConverterForm
convert_to_metre = {
"centimetre": 0.01,
"metre": 1.0,
"mile": 1609.34
}
convert_from_metre = {
"centimetre": 100,
"metre": 1.0,
"mile": 0.000621371
}
# Create your views here.
def convert(request):
form = LengthConverterForm()
if request.GET:
input_unit = request.GET['input_unit']
input_value = request.GET['input_value']
output_unit = request.GET['output_unit']
metres = convert_to_metre[input_unit] * float(input_value)
print(f"{metres = }, {input_value = }")
output_value = metres * convert_from_metre[output_unit]
data = {
"input_unit": input_unit,
"input_value": input_value,
"output_unit": output_unit,
"output_value": round(output_value,5)
}
form = LengthConverterForm(initial=data)
return render(
request, "length.html", context={"form": form})
return render(
request, "length.html", context={"form": form})
执行策四并不需要启动 django 的 runserver:
出现 OK 说明测试通过,启动 django:
python manage.py runserver
打开浏览器,访问 Http://localhost:8000/length/convert/ 即可看到界面:
本文分享了什么是测试驱动开发,并用测试驱动开发的方式 创建了一个简单的 Django 应用程序,用于长度转换。和一般开发的区别就是先写好测试用例,编码是为了让测试用例通过,这样的方式可以使得需求更明确,开发周期更短,增量可控,提高开发效率,保证测试覆盖率。
到此这篇关于使用 Django 进行测试驱动开发的文章就介绍到这了,更多相关Django测试驱动开发内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 使用Django进行测试驱动开发
本文链接: https://www.lsjlt.com/news/158767.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0