<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程式設計 遇上 小提琴 &#187; 測試</title>
	<atom:link href="http://blog.ez2learn.com/tag/%e6%b8%ac%e8%a9%a6/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ez2learn.com</link>
	<description>Victor&#039;s個人部落格，關於程式設計與小提琴</description>
	<lastBuildDate>Tue, 07 Feb 2012 03:26:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Google Unit Test Framework試玩</title>
		<link>http://blog.ez2learn.com/2009/01/21/try-google-unit-test-framework/</link>
		<comments>http://blog.ez2learn.com/2009/01/21/try-google-unit-test-framework/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 11:00:42 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[中文文章]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[單元測試]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Unit Test]]></category>
		<category><![CDATA[測試]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=485</guid>
		<description><![CDATA[介紹Unit Test和 測試驅動開發 (Test-driven development)，以及Google Unit Test Framework的試玩心得 <a href="http://blog.ez2learn.com/2009/01/21/try-google-unit-test-framework/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>一直一來我都知道Unit Test對於程式的開發是蠻重要的一個環節，在以往我們常常不停地改寫程式，在這個過程中，有意無意地常常會有東西被改壞掉還不知道，而我們檢查的方式可能很常是透過一直修改程式碼或輸入的資料來測試，但是當程式越來越大，我們測試時只專注在一個地方，此時所要測試的程式越來越多，不小心出錯的可能也越來越多，每改寫一次就是一次昂貴的人力檢查，最後程式就像一艘漏水的船，補了這裡，那裡卻又多了一個洞，船員像無頭蒼蠅一樣四處補洞，這樣的情況就是Unit Test能解決的問題，透過Unit Test，每次寫的程式碼都交給程式自動測試，一來測試的速度很快，不管是一百次還是一千次，總比人工測試來得快速、正確、廣範，但這不是免費的，代價就是程式設計師要寫很多Unit Test的程式碼，還要想要如何寫Unit Test的程式，進一步的還有測試導向的開發模式(<a href="http://en.wikipedia.org/wiki/Test-driven_development">Test-driven development</a>)，程式是在Unit Test寫完之後才開始寫，這樣一來迫使程式設計師在一開始就得把設計想好，邊寫邊想的做法不再可行，這或許算缺點也算優點</p>
<p>這些我都知道，但都沒有很確實去做，之前有用Python寫過Unit Test，他那種寫法我很喜歡，但C++卻沒有，直到這次想開發某些東西，我決定要試著落實TDD，但在開始以前，要有合適的工具，也就是自動測試的框架，起初我使用Boost.Test，雖然說可以使用，但是我不得不抱怨，他的文件是寫給鬼看的，好幾十頁的文件裡面廢話連篇，分類也很不明確，難以找到重點，或許是我英文程度太差，我真的讀不太懂Boost.Test到底怎麼用，靠著東拼西湊勉強終於還算可以使用，但是或許是我搞不太清楚它一堆Compile時用的Macro定義的Flag如何使用，編譯起來不知為何非常地慢</p>
<p>不合胃口(有些反胃的感覺)的文件，讓我覺得該是時候和Boost.Test說再見，尋找新歡的時候到了，無意間發現，在不知道什麼時候突然冒出來的<a href="http://code.google.com/p/googletest/">Google Unit Test Framework</a>，似乎才剛釋放出來沒多久，是Google內部C++寫Unit Test的框架，開放成Open source的專案，看起來很不錯，文件簡單明瞭，不像Boost.Test幾十頁連篇翻半天還不知道Unit Test到底要怎樣執行，他們主張為什麼要使用Google Unit Test Framework的理由:</p>
<blockquote>
<ol>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">Tests should be <em>independent</em> and <em>repeatable</em>. It&#8217;s a pain to debug a test that succeeds or fails as a result of other tests. Google C++ Testing Framework isolates the tests by running each of them on a different object. When a test fails, Google C++ Testing Framework allows you to run it in isolation for quick debugging. </a></li>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">Tests should be well <em>organized</em> and reflect the structure of the tested code. Google C++ Testing Framework groups related tests into test cases that can share data and subroutines. This common pattern is easy to recognize and makes tests easy to maintain. Such consistency is especially helpful when people switch projects and start to work on a new code base. </a></li>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">Tests should be <em>portable</em> and <em>reusable</em>. The open-source community has a lot of code that is platform-neutral, its tests should also be platform-neutral. Google C++ Testing Framework works on different OSes, with different compilers (gcc, MSVC, and others), with or without exceptions, so Google C++ Testing Framework tests can easily work with a variety of configurations. (Note that the current release only contains build scripts for Linux &#8211; we are actively working on scripts for other platforms.) </a></li>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">When tests fail, they should provide as much <em>information</em> about the problem as possible. Google C++ Testing Framework doesn&#8217;t stop at the first test failure. Instead, it only stops the current test and continues with the next. You can also set up tests that report non-fatal failures after which the current test continues. Thus, you can detect and fix multiple bugs in a single run-edit-compile cycle. </a></li>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">The testing framework should liberate test writers from housekeeping chores and let them focus on the test <em>content</em>. Google C++ Testing Framework automatically keeps track of all tests defined, and doesn&#8217;t require the user to enumerate them in order to run them. </a></li>
<li><a name="Introduction:_Why_Google_C++_Testing_Framework?">Tests should be <em>fast</em>. With Google C++ Testing Framework, you can reuse shared resources across tests and pay for the set-up/tear-down only once, without making tests depend on each other. </a></li>
</ol>
</blockquote>
<p>聽起來很合我胃口，於是就下載來試試，首先，要得編譯Google Test的library，以Visual Studio來說，開啟它的.sln檔，然後編譯就可以了</p>
<p>接著是如何使用Google Test? 雖然有點小麻煩，但其實也還好，首先include "gtest/gtest.h"，接著是lib的設定，因為Google Test的lib是使用/MT參數進行編譯，所以你的專案也一樣要設為/MT或/MTd(Debug 模式下使用)，然後link gtest.lib就可以了，如果不這樣做，你會看見一大堆xxx aleardy defined in xxx的link錯誤</p>
<p>而我用Code Blocks似乎因為編譯參數不太一樣又遇到一堆link錯誤，後來發現好像是Code Blocks的Console Application專案一開始就link的msvcrtd.lib一系列.lib所造成的，將那些移除只剩gtest.lib或gtestd.lib就可以正確執行，我隨手從它的範例裡組合出一個測試的執行檔</p>
<pre lang="c++">
#include

#include 

using namespace std;

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n) {
  int result = 1;
  for (int i = 1; i &lt;= n; i++) {
    result *= i;
  }

  return result;
}

// Returns true iff n is a prime number.
bool IsPrime(int n) {
  // Trivial case 1: small numbers
  if (n &lt;= 1) return false;

  // Trivial case 2: even numbers
  if (n % 2 == 0) return n == 2;

  // Now, we have that n is odd and n &gt;= 3.

  // Try to divide n by every odd number i, starting from 3
  for (int i = 3; ; i += 2) {
    // We only have to try i up to the squre root of n
    if (i &gt; n/i) break;

    // Now, we have i &lt;= n/i &lt; n.
    // If n is divisible by i, n is not prime.
    if (n % i == 0) return false;
  }

  // n has no integer factor in the range (1, n), and thus is prime.
  return true;
}

// Step 2. Use the TEST macro to define your tests.
//
// TEST has two parameters: the test case name and the test name.
// After using the macro, you should define your test logic between a
// pair of braces.  You can use a bunch of macros to indicate the
// success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
// examples of such macros.  For a complete list, see gtest.h.
//
//
//
// In Google Test, tests are grouped into test cases.  This is how we
// keep test code organized.  You should put logically related tests
// into the same test case.
//
// The test case name and the test name should both be valid C++
// identifiers.  And you should not use underscore (_) in the names.
//
// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  Therefore, you should write your tests in such a way
// that their results don't depend on their order.
//
// 

// Tests Factorial().

// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ(1, Factorial(-5));
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_TRUE(Factorial(-10) &gt; 0);

  //
  //
  // EXPECT_EQ(expected, actual) is the same as
  //
  //   EXPECT_TRUE((expected) == (actual))
  //
  // except that it will print both the expected value and the actual
  // value when the assertion fails.  This is very helpful for
  // debugging.  Therefore in this case EXPECT_EQ is preferred.
  //
  // On the other hand, EXPECT_TRUE accepts any Boolean expression,
  // and is thus more general.
  //
  //
}

// Tests factorial of 0.
TEST(FactorialTest, Zero) {
  EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}

// Tests IsPrime()

// Tests negative input.
TEST(IsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.

  EXPECT_FALSE(IsPrime(-1));
  EXPECT_FALSE(IsPrime(-2));
  EXPECT_FALSE(IsPrime(INT_MIN));
}

// Tests some trivial cases.
TEST(IsPrimeTest, Trivial) {
  EXPECT_FALSE(IsPrime(0));
  EXPECT_FALSE(IsPrime(1));
  EXPECT_TRUE(IsPrime(2));
  EXPECT_TRUE(IsPrime(3));
}

// Tests positive input.
TEST(IsPrimeTest, Positive) {
  EXPECT_FALSE(IsPrime(4));
  EXPECT_TRUE(IsPrime(5));
  EXPECT_FALSE(IsPrime(6));
  EXPECT_TRUE(IsPrime(23));
}

int main(int argc, char** argv) {
  // Prints elapsed time by default.
  //testing::GTEST_FLAG(print_time) = true;

  // This allows the user to override the flag on the command line.
  testing::InitGoogleTest(&amp;argc, argv);

  return RUN_ALL_TESTS();
}</pre>
<p>很簡單明瞭吧? 然後它執行的結果畫面如下</p>
<div id="attachment_486" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.ez2learn.com/wp-content/uploads/2009/01/gtest_result.png"><img class="size-medium wp-image-486" title="Google test 執行結果" src="http://blog.ez2learn.com/wp-content/uploads/2009/01/gtest_result-300x196.png" alt="Google test 執行結果" width="300" height="196" /></a><p class="wp-caption-text">Google test 執行結果</p></div>
<p>沒想到還有顏色，看起來蠻酷的對吧，很酷有時候也是選擇專案的理由之一，在這樣試玩下來，雖然還沒深入研究，還有實際寫Unit Test，但是感覺起來真的很不錯，有興趣的人可以試一試</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2009/01/21/try-google-unit-test-framework/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>新版的嵌入式語法測試</title>
		<link>http://blog.ez2learn.com/2008/11/04/new-embedded-syntax-test/</link>
		<comments>http://blog.ez2learn.com/2008/11/04/new-embedded-syntax-test/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 05:57:55 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[中文文章]]></category>
		<category><![CDATA[哇咧咧]]></category>
		<category><![CDATA[網站]]></category>
		<category><![CDATA[測試]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=316</guid>
		<description><![CDATA[話說 我發現很多web app都不支援iframe，應該是說為了安全問題會被慮掉，我仔細考慮了一下，就只有一張圖片，實在沒什麼必要用iframe，因此我改成用圖片，然後增加了投選項的語法，到結果其實已經不算式嵌入式了 XD 只是好像大部份大家都習慣用嵌入式這個名字來稱呼這種語法，就隨便吧 XD 前往Walele投票 我同意 麥坎 我同意 歐巴馬 我同意 洛克馬 我同意 可愛的馬]]></description>
			<content:encoded><![CDATA[<h2>話說</h2>
<p>我發現很多web app都不支援iframe，應該是說為了安全問題會被慮掉，我仔細考慮了一下，就只有一張圖片，實在沒什麼必要用iframe，因此我改成用圖片，然後增加了投選項的語法，到結果其實已經不算式嵌入式了 XD</p>
<p>只是好像大部份大家都習慣用嵌入式這個名字來稱呼這種語法，就隨便吧 XD</p>
<p><img style="border-style: none;" src="http://walele.com/walele/figure?id=56" alt="" width="375" height="375" /><br />
<a href="http://walele.com/walele?id=56">前往Walele投票</a></p>
<div>
<div class="votingOptions"><a href="http://walele.com/walele/agree?id=57">我同意</a> <span><strong>麥坎</strong></span></div>
<div class="votingOptions"><a href="http://walele.com/walele/agree?id=58">我同意</a> <span><strong>歐巴馬</strong></span></div>
<div class="votingOptions"><a href="http://walele.com/walele/agree?id=59">我同意</a> <span><strong>洛克馬</strong></span></div>
<div class="votingOptions"><a href="http://walele.com/walele/agree?id=60">我同意</a> <span><strong>可愛的馬</strong></span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2008/11/04/new-embedded-syntax-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>測試的好幫手: 虛擬機器</title>
		<link>http://blog.ez2learn.com/2008/10/04/virtual-machine-useful-tool-for-testing/</link>
		<comments>http://blog.ez2learn.com/2008/10/04/virtual-machine-useful-tool-for-testing/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 08:47:02 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[中文文章]]></category>
		<category><![CDATA[測試]]></category>
		<category><![CDATA[程式設計]]></category>
		<category><![CDATA[虛擬機器]]></category>
		<category><![CDATA[Virtual Box]]></category>
		<category><![CDATA[Virtual PC]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=87</guid>
		<description><![CDATA[麻煩的測試 今天當我要把demo程式送到客戶手中前，我煩腦要如何測試demo程式，確保不會因為我少了某樣程式依賴的東西，但是我的開發環境因為都已經裝好了，但客戶的電腦未必是如此，因此最好找另外一台電腦來測試，我第一個想到就是用我的筆電來做測試，可是為了測試我還得裝好網路，又得花不少時間，於是我想到，為什麼我不用虛擬機器? 虛擬機器不正就是用來測試用的嗎? 我居然忘記有這樣好用的工具存在 Virtual Box 首先我使用了Virtual Box，一套Open source且跨平台的模擬程式，目前似乎由Sun接手主導，它的功能比起其它的模擬程式來得強大不少，速度聽說也比較快，不幸的是&#8230;，每次在安裝XP一進行格式化磁碟就會當機，不穩定似乎到目前還是它最大的問題，相信解決了之後應該是相當優秀的模擬軟體 不知道是跟什麼東西犯太歲，重開以後居然就不會當機了，也成功地安裝了XP 成功安裝XP的執行畫面 Virtual PC Microsoft的模擬程式，相較於Virtual Box，功能就陽春了許多，但是穩定度很好，不像Virtual Box不停當機，為了能夠測試，最後還是選擇了Virtual PC進行測試，缺點是要錢就是了，好在學校有買大量受權的軟體，一開始我還不知道如何跳離Virtual PC的OS控制權，將控制權交回給真正的OS，原來是要按右邊的Alt Virtual PC執行Windows XP的畫面]]></description>
			<content:encoded><![CDATA[<h2>麻煩的測試</h2>
<p>今天當我要把demo程式送到客戶手中前，我煩腦要如何測試demo程式，確保不會因為我少了某樣程式依賴的東西，但是我的開發環境因為都已經裝好了，但客戶的電腦未必是如此，因此最好找另外一台電腦來測試，我第一個想到就是用我的筆電來做測試，可是為了測試我還得裝好網路，又得花不少時間，於是我想到，為什麼我不用虛擬機器? 虛擬機器不正就是用來測試用的嗎? 我居然忘記有這樣好用的工具存在</p>
<p><span id="more-87"></span></p>
<h2>Virtual Box</h2>
<p>首先我使用了<a href="http://www.virtualbox.org/">Virtual Box</a>，一套Open source且跨平台的模擬程式，目前似乎由Sun接手主導，它的功能比起其它的模擬程式來得強大不少，速度聽說也比較快，不幸的是&#8230;，每次在安裝XP一進行格式化磁碟就會當機，不穩定似乎到目前還是它最大的問題，相信解決了之後應該是相當優秀的模擬軟體</p>
<p>不知道是跟什麼東西犯太歲，重開以後居然就不會當機了，也成功地安裝了XP</p>
<div id="attachment_91" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181116.png"><img class="size-medium wp-image-91" title="Virtual Box畫面" src="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181116-300x225.png" alt="Virtual Box畫面" width="300" height="225" /></a><p class="wp-caption-text">Virtual Box畫面</p></div>
<p>成功安裝XP的執行畫面</p>
<div id="attachment_92" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181049.png"><img class="size-medium wp-image-92" title="Virtual Box Windows XP 執行畫面" src="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181049-300x250.png" alt="Virtual Box Windows XP 執行畫面" width="300" height="250" /></a><p class="wp-caption-text">Virtual Box Windows XP 執行畫面</p></div>
<h2>Virtual PC</h2>
<p>Microsoft的模擬程式，相較於Virtual Box，功能就陽春了許多，但是穩定度很好，不像Virtual Box不停當機，為了能夠測試，最後還是選擇了Virtual PC進行測試，缺點是要錢就是了，好在學校有買大量受權的軟體，一開始我還不知道如何跳離Virtual PC的OS控制權，將控制權交回給真正的OS，原來是要按右邊的Alt</p>
<div id="attachment_93" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181409.png"><img class="size-medium wp-image-93" title="Virtual PC畫面" src="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181409-300x212.png" alt="Virtual PC畫面" width="300" height="212" /></a><p class="wp-caption-text">Virtual PC畫面</p></div>
<p>Virtual PC執行Windows XP的畫面</p>
<div id="attachment_94" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181636.png"><img class="size-medium wp-image-94" title="Virtual PC執行Windows XP的畫面" src="http://blog.ez2learn.com/wp-content/uploads/2008/10/2008-10-04_181636-300x245.png" alt="Virtual PC執行Windows XP的畫面" width="300" height="245" /></a><p class="wp-caption-text">Virtual PC執行Windows XP的畫面</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2008/10/04/virtual-machine-useful-tool-for-testing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

