《 Test-Driven Development for Embedded C 》心得分享。
TDD(測試驅動開發)是任何開發人員應該掌握的編程實踐,開發者依照需求設計單元測試,然後編寫程式滿足測試,在快速密集的回饋循環中逐漸完善功能,並隨時維持良好的軟體品質。這種開發方式對於物件導向語言陣營的朋友來說應該不陌生,但由於開發環境的特性,使用程序語言的嵌入式平台開發者可能壓根沒聽過或者自認今生無緣。
希望這次交流能為嵌入式平台開發者介紹一些不同於以往的開發方式,打開每個通往敏捷軟體開發的可能。分享內容包含嵌入式TDD原理與策略,單元測試相關工具,如何斷開模組依賴關係,如何得到可測試的設計,以及實務上的建議。
《 Test-Driven Development for Embedded C 》心得分享。
TDD(測試驅動開發)是任何開發人員應該掌握的編程實踐,開發者依照需求設計單元測試,然後編寫程式滿足測試,在快速密集的回饋循環中逐漸完善功能,並隨時維持良好的軟體品質。這種開發方式對於物件導向語言陣營的朋友來說應該不陌生,但由於開發環境的特性,使用程序語言的嵌入式平台開發者可能壓根沒聽過或者自認今生無緣。
希望這次交流能為嵌入式平台開發者介紹一些不同於以往的開發方式,打開每個通往敏捷軟體開發的可能。分享內容包含嵌入式TDD原理與策略,單元測試相關工具,如何斷開模組依賴關係,如何得到可測試的設計,以及實務上的建議。
7. 安装成功
运行 phpunit
– PHPUnit 3.2.18 by Sebastian Bergmann.
– Usage: phpunit [switches] UnitTest
[UnitTest.php]
– --log-json Log test execution in JSON format.
– --log-tapLog test execution in TAP format to
file.
– --log-xml Logtest execution in XML
PHP User Group Shanghai
7
2008
12. 数据提供者
class DataTest extends PHPUnit_Framework_TestCase{
public static function provider() {
return array(
array(1, 0, 1),
array(1, 1, 3)
);
}
/* @dataProvider provider */
public function testAdd($a, $b, $c) {
$this->assertEquals($c, $a + $b);
}
}
13. 含有通过 和失败 的测试
require_once 'PHPUnit/Framework.php';
class MyTest extends
PHPUnit_Framework_TestCase {
public function testSuccess() {
$this->assertEquals(3, 1 + 2);
}
public function testFailure() {
$this->assertTrue(2 > 3);
}
}
14. 含有通过 和失败 的测试结 果
.F
Time: 0 seconds
There was 1 failure:
1) testFailure(MyTest)
Failed asserting that <boolean:false> is true.
/home/zhouxh/php/phpunit/MyTest.php:11
FAILURES!
Tests: 2, Failures: 1
15. 不同的测试结 果
.
测试成功输出
F
测试当中断言失败时输出
E
测试有错误发生时输出
S
测试被跳过时输出
I
测试未完成时输出
16. 有错误发 生的测试 ( E )
public function testError() {
$this->assertLessThan(3);
$this->assertEquals(”E?”);
}
---------
E
1) testFailure(MyTest)
Missing argument 2 for
PHPUnit_Framework_Assert::assertLessThan(),
called in ... ...
17. 被跳过 的测试 ( S )
public function testSkip() {
$this->markTestSkipped("Skipped test!");
// stop!!!
$this->assertTrue(true);
}
---
$phpunit –verbose MyTest.php
S
...
1) testSkip(MyTest)
Skipped test!
...
18. 未完成的测试 ( I )
public function testIncomplete() {
$this->markTestIncomplete("Incomplete test!");
// stop!!!
$this->assertTrue(true);
}
---
$phpunit –verbose MyTest.php
I
...
1) testIncomplete(MyTest)
Incomplete test!
...
19. 夹 具 (Fixtures)
class MyTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$this->mysqlConnection = mysql_connection(”localhost”, ”root”,
”password”);
$this->data = array(1, 2, 3, 4);
}
public function testOne() {}
public function testTwo() {}
public function tearDown() {
mysql_close($this->mysqlConnection);
unset($this->data);
}
}
31. 测试驱动开发 (TDD) :例子
phpunit MyTest.php
F
Time: 0 seconds
There was 1 failure:
1) testAdd(MyTest)
Failed asserting that <null> matches expected value <integer:3>.