unit-testing - TDD流程讲解

标签 unit-testing testing tdd phpunit automated-tests

<分区>

我是单元测试和 TDD 的新手。我明白它的重要性,所以我想开始学习它。问题是我在 TDD 中看不到全貌。我的意思是,当我刚开始一个项目时,我该怎么做?我已经进行了一些测试,测试了一种方法或其他东西,但是我如何在更大范围内进行测试,比如整个项目?

所以,请问有人可以写下要点或我该如何开始吗?

我是否测试每个类并为每个方法编写测试,我如何测试我的应用程序作为一个整体,等等。

最佳答案

Do I test every single class and write tests for every single methods and how can i test my application as a hole, and so on and so forth.

基本上,在一个完美的世界中,是的。您甚至可以为每个方法编写多个测试。测试驱动意味着,您开始编写测试,然后开始开发并使测试通过。

例子:

您正准备编写配置文件类并希望开始使用测试驱动开发。因此,您将在实现之前开始编写测试:

class ConfigFileTest extends PHPUnit_Framework_TestCase {


    public function testOpen() {
        $config = new ConfigFile(); // 1 .test will fail because the class doesn't exists
        // -> solution write the class
        $this->assertTrue($config->open('test.config')); // 2. test will fail because open isn't implemented yet
        // -> solution: implement open()..

        // and so on ...
    }

}

如您所见,该测试可以指导您完成实现,作为一个特殊的好处,它在实现后仍然有用,因为您可以使用该测试来检查该类是否按预期工作,即使在以后更改代码后也是如此。


但是我应该告诉你 behat .它遵循一种名为“行为驱动开发”的不同测试方法。您应该看看。

关于unit-testing - TDD流程讲解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949908/

相关文章:

python - 如何在请求上下文之外工作时模拟 Flask.g 和 Flask-RESTful.request

phpspec - 方法返回对象而不是字符串

ruby-on-rails - Minitest-NoMethodError : undefined method `get'

ruby-on-rails - rails : start writing tests late in development

设备通信器的 TDD

python - 如何对 Google Cloud Endpoints 进行单元测试

c# - 内部类应该进行单元测试吗?

android - 无法在 android (Android studio) 中测试 protected 方法

ruby - 针对外部 UDP API 进行测试。从哪儿开始?

c# - 在类中的所有方法上使用 Virtual 关键字的后果?