Django 1.10中文文档:第一个应用 part 5

本文档介绍了Django应用的自动化测试,包括测试的重要性、自动化测试的基础知识,如为何编写测试、测试如何节省时间及提升代码质量,以及如何编写和运行测试,通过实例展示了如何测试视图和DetailView,强调了测试在团队协作和代码维护中的关键作用。
摘要由CSDN通过智能技术生成

已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档

Writing your first Django app, part 5

This tutorial begins where Tutorial 4 left off. We’ve built a Web-poll application, and we’ll now create some automated tests for it.

紧接着教程4。我们已经创建了一个投票的web应用,现在我们为它创建一些自动化测试用例。

Introducing automated testing

自动化测试简介

What are automated tests?
什么是自动化测试

Tests are simple routines that check the operation of your code.

测试是检查你的代码是否能正常运行。

Testing operates at different levels. Some tests might apply to a tiny detail (does a particular model method return values as expected?) while others examine the overall operation of the software (does a sequence of user inputs on the site produce the desired result?). That’s no different from the kind of testing you did earlier in Tutorial 2, using theshell to examine the behavior of a method, or running the application and entering data to check how it behaves.

测试可以划分为不同的级别。 一些测试可能专注于小细节(某一个模型的方法是否会返回预期的值?), 其他的测试可能会检查软件的整体运行是否正常(用户在对网站进行了一系列的操作后,是否返回了正确的结果?)。这些其实和你早前在教程2做的差不多, 使用shell 来检测一个方法的行为,或者运行程序并输入数据来检查它的行为方式。

What’s different in automated tests is that the testing work is done for you by the system. You create a set of tests once, and then as you make changes to your app, you can check that your code still works as you originally intended, without having to perform time consuming manual testing.

自动化测试的不同之处就在于这些测试会由系统来帮你完成。你创建了一组测试程序,当你修改了你的应用,你就可以用这组测试程序来检查你的代码是否仍然同预期的那样运行,而无需执行耗时的手动测试。

Why you need to create tests
你为什么需要测试

So why create tests, and why now?

那么为什么要测试,而且是现在?

You may feel that you have quite enough on your plate just learning Python/Django, and having yet another thing to learn and do may seem overwhelming and perhaps unnecessary. After all, our polls application is working quite happily now; going through the trouble of creating automated tests is not going to make it work any better. If creating the polls application is the last bit of Django programming you will ever do, then true, you don’t need to know how to create automated tests. But, if that’s not the case, now is an excellent time to learn.

你可能感觉学习Python/Django已经足够,再去学习其他的东西也许需要付出巨大的努力而且没有必要。 毕竟,我们的投票应用已经运行良好了; 将时间运用在自动化测试上,也并不能改进我们的应用。 如果你学习Django就是为了创建一个投票应用,那么创建自动化测试显然没有必要。 但如果不是这样,现在是一个很好的学习机会。

Tests will save you time
测试将节省你的时间

Up to a certain point, ‘checking that it seems to work’ will be a satisfactory test. In a more sophisticated application, you might have dozens of complex interactions between components.

在某种程度上, ‘确认它似乎在正常运行’,对测试来说是一种令人满意的事情。 在更复杂的应用中,你可能有几十种组件之间的复杂的交互行为。

A change in any of those components could have unexpected consequences on the application’s behavior. Checking that it still ‘seems to work’ could mean running through your code’s functionality with twenty different variations of your test data just to make sure you haven’t broken something - not a good use of your time.

这些组件的任何一个小的变化,都可能对应用造成意想不到的影响。 确定它‘似乎正常工作’可能意味着你需要运用二十种不同的测试数据测试你的代码功能,来确保你没有搞砸某些事情 —— 这会浪费很多时间。

That’s especially true when automated tests could do this for you in seconds. If something’s gone wrong, tests will also assist in identifying the code that’s causing the unexpected behavior.

自动化测试只需要数秒就可以完成以上的任务。 如果出现了错误,测试程序还能够帮助找出引发这个异常行为的代码。

Sometimes it may seem a chore to tear yourself away from your productive, creative programming work to face the unglamorous and unexciting business of writing tests, particularly when you know your code is working properly.

有时候你可能会觉得这单调乏味、无趣的编写测试程序简直像家务活,让你脱离了有价值的、创造性的编程工作,尤其是当你知道你的代码能工作正常。

However, the task of writing tests is a lot more fulfilling than spending hours testing your application manually or trying to identify the cause of a newly-introduced problem.

而,比起用几个小时的时间来手动测试你的程序,或者试图找出代码中一个新引入的问题的原因,编写测试程序还是令人惬意的。

Tests don’t just identify problems, they prevent them

It’s a mistake to think of tests merely as a negative aspect of development.

将测试工作仅仅当成开发过程中的负担,是错误的

Without tests, the purpose or intended behavior of an application might be rather opaque. Even when it’s your own code, you will sometimes find yourself poking around in it trying to find out what exactly it’s doing.

没有测试,应用程序的目的和行为将会变得相当模糊。 甚至即使是你自己的代码,有时候你也需要费很大力气去搞懂它到底干了些什么。

Tests change that; they light up your code from the inside, and when something goes wrong, they focus light on the part that has gone wrong - even if you hadn’t even realized it had gone wrong.

测试改变了这一切; 它们使你的代码内部变得明晰,当错误出现后,它们会明确地指出哪部分代码出了问题 —— 甚至你自己都不会料到问题会出现在那里。

Tests make your code more attractive
让你的代码更有魅力

You might have created a brilliant piece of software, but you will find that many other developers will simply refuse to look at it because it lacks tests; without tests, they won’t trust it. Jacob Kaplan-Moss, one of Django’s original developers, says “Code without tests is broken by design.”

你可能开发过堪称辉煌的软件,但是你会发现许多其他的开发者会由于它缺少测试程序而拒绝查看它一眼;没有测试程序,他们不会信任它。 Jacob Kaplan-Moss,Django最初的几个开发者之一,说过“不具有测试程序的代码是设计上的错误。”

That other developers want to see tests in your software before they take it seriously is yet another reason for you to start writing tests.

你需要开始编写测试的另一个原因就是其他的开发者在他们深入使用你的程序前可能想要查看一下它有没有测试。

Tests help teams work together
测试有助于团队合作

The previous points are written from the point of view of a single developer maintaining an application. Complex applications will be maintained by teams. Tests guarantee that colleagues don’t inadvertently break your code (and that you don’t break theirs without knowing). If you want to make a living as a Django programmer, you must be good at writing tests!

之前的观点是从单个开发人员来维护一个程序这个方向来阐述的。 复杂的应用将会被一个团队来维护。 测试能够减少同事在无意间破坏你的代码的机会(和你在不知情的情况下破坏别人的代码的机会)。 如果你想在团队中做一个好的Django开发者,你必须擅长测试!

Basic testing strategies

There are many ways to approach writing tests.

编写测试有很多种方法。

Some programmers follow a discipline called “test-driven development”; they actually write their tests before they write their code. This might seem counter-intuitive, but in fact it’s similar to what most people will often do anyway: they describe a problem, then create some code to solve it. Test-driven development simply formalizes the problem in a Python test case.

一些开发者遵循一种叫做“test-driven development”的规则;
他们在编写代码前会先编好测试。
这似乎与直觉不符,尽管这种方法与大多数人经常的做法很相似:人们先描述一个问题,然后创建一些代码来解决这个问题。
测试驱动开发可以用Python测试用例将这个问题简单地形式化。

More often, a newcomer to testing will create some code and later decide that it should have some tests. Perhaps it would have been better to write some tests earlier, but it’s never too late to get started.

更常见的情况是,刚接触测试的人会先编写一些代码,然后才决定为这些代码创建一些测试。也许在之前就编写一些测试会好一点,但什么时候开始都不算晚。

Sometimes it’s difficult to figure out where to get started with writing tests. If you have written several thousand lines of Python, choosing something to test might not be easy. In such a case, it’s fruitful to write your first test the next time you make a change, either when you add a new feature or fix a bug.

有时候很难解决从什么地方开始编写测试。
如果你已经编写了数千行Python代码,挑出一些功能去测试是很困难的。
这种情况下,在下次你对代码进行变更,或者添加一个新功能或者修复一个bug时,编写你的第一个测试,效果会非常好。

So let’s do that right away.

现在,让我们马上来编写一个测试。

Writing our first test
编写我们的第一个测试
We identify a bug
我们找出一个错误

Fortunately, there’s a little bug in the polls application for us to fix

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值