python测试驱动开发_测试驱动开发与Python

最近在看一本书《Test-Driven Development with Python》,里面非常详细的介绍了如何一步一步通过测试驱动开发(TDD)的方式开发Web项目。刚好这本书中使用了我之前所了解的一些技术,Django、selenium、unittest等。所以,读下来受益匪浅。

我相信不少开发都写单元测试,不过,一般是先写功能代码,然后,再写单元测试用例,在编写单元测试用例的过程中,可能需要调整功能代码,从而使单元测试用例通过。但是TDD就特别要求先写测试用例,后写实现代码。这一开始确实有些难。

这里就选择一个简单的例子向各位介绍一下TDD的流程(套路)。

编写功能测试用例:

首先,编写功能测试用例,functional_tests.py

from selenium importwebdriver

browser=webdriver.Firefox()

browser.get("http://127.0.0.1:8000")assert "Django" in browser.title

你没看错,这就是由Selenium编写的功能测试代码。打开Firefox浏览器,并访问http://127.0.0.1:8000,通过assert 判断浏览器标题是否包含“Django”。

然后,运行该测试用例。

D:\pydj>python functional_tests.py

Traceback (most recent call last):

File"functional_tests.py", line 6, in

assert "Django" inbrowser.title

AssertionError

测试用例失败了,这是必然的,因为我们还没有创建被测试的项目。但,这同样也是我们想要的结果。TDD的套路就是通过编写功能代码,使测试用例通过。

创建项目:

接下来创建Django项目:

D:\pydj>django-admin startproject superlists

当前项目结构如下:

├─ functional_tests.py

└─ superlists

├─ manage.py

└─ superlists

├─ __init__.py

├─ settings.py

├─ urls.py

└─ wsgi.py

进入项目目录,启动项目:

D:\pydj>cd superlists

D:\pydj\superlists>python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app maynotwork properly until they are applied.

Run'python manage.py migrate'to apply them.

June13, 2016 - 23:23:29Django version1.9.7, using settings 'superlists.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.

再次运行功能测试用例,functional_tests.py

311516-20160619181112319-1107271135.png

接下来继续编写功能测试用例。functional_tests.py

#coding=utf-8

from selenium importwebdriverimportunittestclassNewVisitorTest(unittest.TestCase):defsetUp(self):

self.browser=webdriver.Firefox()

self.browser.implicitly_wait(3)deftearDown(self):

self.browser.close()deftest_case_start_a_list_and_retrieve_it_later(self):#小明听说有一个很酷的在线代办事项应用

#她去看了这个应用首页

self.browser.get("http://127.0.0.1:8000")#它注意到网页的标题和头部包含“To-Do”这个词语。

self.assertIn("To-Do", self.browser.title)if __name__ == '__main__':

unittest.main()

这里用到了unittest 单元测试框架。如果,你不懂Python的单元测试,建议读者去学习unittest。

执行测试用例:

C:\Python35\python.exe D:/pydj/functional_tests.py

F======================================================================FAIL: test_case_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)----------------------------------------------------------------------Traceback (most recent call last):

File"D:/pydj/functional_tests.py", line 21, intest_case_start_a_list_and_retrieve_it_later

self.assertIn("To-Do", self.browser.title)

AssertionError:'To-Do' not found in 'Welcome to Django'

----------------------------------------------------------------------Ran1 test in 3.491s

FAILED (failures=1)

测试用例又在预料之内的失败了!先不要着急解决这个问题,把项目创建完成。

D:\pydj\superlists>python3 manage.py startapp lists

将functional_tests.py放到superlists项目目录下。

311516-20160619181357444-1172521915.png

单元测试与功能测试的区别:

正如给事物所贴的众多标签一样,单元测试和功能测试之间的界线有时不那么清晰。不过,二者之间有个基本区别:功能测试站在用户的角度从外部测试应用,单元测试则站在程序员的角度从内部测试应用。

我遵从的 TDD 方法同时使用这两种类型测试应用。采用的工作流程大致如下。

(1) 先写功能测试,从用户的角度描述应用的新功能。

(2) 功能测试失败后,想办法编写代码让它通过(或者说至少让当前失败的测试通过)。此时,使用一个或多个单元测试定义希望代码实现的效果,保证为应用中的每一行代码

(3) 单元测试失败后,编写最少量的应用代码,刚好让单元测试通过。有时,要在第 2 步和第 3 步之间多次往复,直到我们觉得功能测试有一点进展为止。

(4) 然后,再次运行功能测试,看能否通过,或者有没有进展。这一步可能促使我们编写一些新的单元测试和代码等。

由此可以看出,这整个过程中,功能测试站在高层驱动开发,而单元测试则从低层驱动我们做些什么。

打开/lists/tests.py文件,编写单元测试。

from django.test importTestCase#Create your tests here.

classSmokeTest(TestCase):deftest_bad_moths(self):

self.assertEqual(1 + 1,2)

运行单元测试:

D:\pydj\superlists>python3 manage.py test

Creating test databasefor alias 'default'...

.----------------------------------------------------------------------Ran1 test in0.001s

OK

Destroying test databasefor alias 'default'...

OK,说明单元测试没问题。接下来就要编写真正的单元测试了(/lists/tests.py)。

from django.core.urlresolvers importresolvefrom django.test importTestCasefrom django.http importHttpRequestfrom lists.views importhome_pageclassHomePageTest(TestCase):deftest_root_url_resolves_to_home_page_view(self):

found= resolve('/')

self.assertEqual(found.func, home_page)deftest_home_page_returns_correct_html(self):

request=HttpRequest()

response=home_page(request)

self.assertTrue(response.content.startswith(b''))

self.assertIn(b'

To-Do lists', response.content)

self.assertTrue(response.content.endswith(b''))

第一个用例(test_root_url_resolves_to_home_page_view):

resolve 是 Django 内部使用的函数,用于解析 URL,并将其映射到相应的视图函数上。检查解析网站根路径“ /” 时,是否能找到名为 home_page 的函数。

第二个用例(test_home_page_returns_correct_html):

创建了一个 HttpRequest 对象,用户在浏览器中请求网页时, Django 看到的就是HttpRequest 对象。把这个 HttpRequest 对象传给 home_page 视图,得到响应。听说响应对象是 HttpResponse类的实例时,你应该不会觉得奇怪。接下来我们断定响应的 .content 属性(即发送给用户的 HTML)中有特定的内容。

assertTrue()希望响应以 标签开头,并在结尾处关闭该标签。注意, response.content 是原始字节,不是 Python 字符串,因此对比时要使用 b'' 句法。b是BYTE字符串

assertIn()希望响应中有一个

标签,其内容包含单词“ To-Do”——因为在功能测试中做了这项测试。

在书中,这里的单元测试也不是一次写成的,这里省略中间过程,一次写成一个相对健全的单元测试。

根据单元测试编写视图文件lists/views.py

from django.shortcuts importrenderfrom django.http importHttpResponse#Create your views here.

defhome_page(request):return HttpResponse('

To-Do lists')

运行单元测试使其通过:

D:pydj\superlists>python3 manage.py test

Creating test databasefor alias 'default'...

..----------------------------------------------------------------------Ran2 tests in0.002s

OK

Destroying test databasefor alias 'default'...

最后不要忘了,配置superlists/urls.py文件。

urlpatterns =[#url(r'^admin/', admin.site.urls),

url(r'^$', views.home_page),

]

最后的最后,启动服务:

D:\pydj\superlists>python manage.py runserver

运行功能测试用例使其通过:

C:\Users\fnngj\Desktop\superlists>python3 functional_tests.py

F======================================================================FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)----------------------------------------------------------------------Traceback (most recent call last):

File"functional_tests.py", line 21, intest_can_start_a_list_and_retrieve_it_later

self.fail('Finish the test!')

AssertionError: Finish the test!----------------------------------------------------------------------Ran1 test in 7.070s

FAILED (failures=1)

------------------------------------

书的内容极其连贯,整本书学下来,相当于自己动手通过TDD的方式开发了一个项目。我这里有所删减。

感兴趣的可以买这本书的中文版来学习。《Python web开发:测试驱动方法》 这中文名翻译的。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值