笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

编写你的第一个 Django 程序 第5部分

发布者: 三寸日光

引入自动化测试

什么是自动化测试?

测试是用于检测你的代码运行是否正确的简单程序。

测试操作分不同的级别。有些人可能只测试小的细节 - 测试一个模型方法返回的值 是否如预期一样? , 而其他人则检测软件的整体运行是否正确 - 检测用户在网站上的一系列操作产生的结果正确与否? 这其实和前面 教程 第1部分 中的方法没有什么不同,就是使用 shell 检查方法的行为,或者运行程序并输入数据来检查它的行为方式。

自动化测试 的不同之处就在由系统为你完成测试工作。你一旦创建了一组测试案例,当你修改了你的 应用,你就可以检查你的代码是否仍然同预期的那样运行,而无需执行耗时的手动测试。

为什么你需要创建测试

那么为什么要创建测试?而且是现在呢?

你可能感觉学习 Python/Django 已经足够了,再去学习其他的东西也许没有必要。 毕竟,我们的投票应用现在正常运行;而通过创建自动测试会使其运行得更好。 如果创建的投票应用的是通过 Django 编程实现的,你从始至终都是这样做的,若是真的,那么你 不需要知道如何创建自动化测试。但是如果不是这样的话,现在是一个很好的学习时间。

测试将节省你的时间

到了一定程度,’检查代码是否运行正常’将是一个理想的测试。在更复杂的应用中,你可能有 几十个组件之间复杂的相互作用。

在这些组件中的任何修改对应用的行为都可能产生意想不到的后果。 检查应用仍然’似乎运行正常’可能意味着你的测试数据经过你的代码中二十多种变化功能后只是为了确保 你没有破坏它们 - 没有很好地利用你的时间。

特别是自动化测试可以在几秒中内为你做这件事。如果有什么地方出问题了,测试也将协助你找出 造成意外的代码。

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.”

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!


12345下一页

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-7-27 17:37 , Processed in 0.067208 second(s), 17 queries .

© 2001-2020

返回顶部