單元測試是在軟件開發過程中要進行的最低級別的測試活動,在單元測試活動中,軟件的獨立單元將在與程序的其他部分相隔離的情況下進行測試。
在一種傳統的結構化編程語言中,比如C,要進行測試的單元一般是函數或子過程。在象C++這樣的面向對象的語言中, 要進行測試的基本單元是類。對Ada語言來說,開發人員可以選擇是在獨立的過程和函數,還是在Ada包的級別上進行單元測試。單元測試的原則同樣被擴展到第四代語言(4GL)的開發中,在這裏基本單元被典型地劃分爲一個菜單或顯示界面。
單元測試不僅僅是作爲無錯編碼的一種輔助手段在一次性的開發過程中使用,單元測試必須是可重複的,無論是在軟件修改,或是移植到新的運行環境的過程中。因此,所有的測試都必須在整個軟件系統的生命週期中進行維護。
Visual Studio 2008 單元測試功能介紹
1.測試代碼與被測代碼分離成獨立的兩個項目
單元測試中,測試的代碼不能對被測試的代碼施加影響。如果將測試代碼寫入被測試的代碼中,測試完成後再刪除的話,測試的正確性將得不到保證。因此,在Visual Studio 2008種提供了一種“Test Project”的項目,測試代碼寫在Test Project中,並且測試工程可以進行重複使用。
2.測試代碼的自動生成
書寫測試代碼是一件很煩瑣的事情,這些代碼沒有像程序代碼一樣具有“創造性”,因此該部分代碼可以進行自動化生成。Visual Studio 2008就提供了一個自動生成測試代碼的測試框架。利用Visual Studio 2008自動生成的代碼,只需要很少的改動就可以完成整個測試程序。
3.測試管理
Visual Studio 2008提供了測試列表來進行測試工作的管理工作,我們需要一個反映目前測試狀況的工具,那些測試通過了,那些沒有通過,應該提供一個列表來爲我們改進測試手段,進行更全面的測試提供指導。
利用Visual Studio 2008來進行單元測試
假設我們有一個類BankAccount,該類定義了一個銀行的賬戶,私有屬性_currentBalance是銀行儲戶的賬戶金額,depositMoney是存款方法,對帳戶增加一筆資金,makePayment是支付方法,對賬戶減少一筆資金。代碼如下:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankAccountDemo.Business
...{
class BankAccount
...{
private float _currentBalance;
public float CurrentBalance
...{
get ...{ return _currentBalance; }
set ...{ _currentBalance = value; }
}
public BankAccount(float initialBalance)
...{
this._currentBalance = initialBalance;
}
public void depositMoney(float depositAmount)
...{
this._currentBalance += depositAmount;
}
public void makePayment(float paymentAmount)
...{
this._currentBalance -= paymentAmount;
}
}
}
要對BankAccount類進行單元測試,只需要在BankAccount的定義處鼠標右鍵,在菜單中選擇“Create Unit Tests”即可進入測試項目的創建工作。如下圖所示:
在彈出的創建單元測試的對話框中,對需要創建測試的方法和屬性進行選擇,然後點擊“OK”按鈕,如圖所示:
緊接着在出現的文本框中輸入測試項目的名稱“BankAccountDemo.Business.Tests”,點擊確定後,測試項目被創建。在這裏“BankAccountDemo.Business.”只是用於更好的對命名空間進行規劃,完全可以直接使用“BankAccountDemoTest”來作爲測試項目的名字。
生成的測試代碼如下,爲了緊湊的表現代碼,將註釋代碼作了刪除。
這個時候的代碼並不能開始測試,而需要我們按照測試用例的要求將測試用例的數據加入到測試方法中,並進行結果的比較,修改後的depositMoneyTest方法如下:
public void depositMoneyTest()
{
float initialBalance = 0F; // TODO: Initialize to an appropriate value
BankAccount target = new BankAccount(initialBalance); // TODO: Initialize to an appropriate value
float depositAmount = 100F; // TODO: Initialize to an appropriate value
target.depositMoney(depositAmount);
Assert.AreEqual(initialBalance + depositAmount, target.CurrentBalance, "Deposit Test: Deposit not applied correctly");
}
鼠標右鍵在depositMoneyTest方法內任意位置單擊,在彈出的菜單中選擇“Run Tests”,即可以對該方法進行測試。在“Test Results”窗口中顯示測試的結果,如下圖所示:
可以看出,Visual Studio 2008給我們提供了一個功能強大,操作簡單的單元測試功能。利用該功能,程序員在編寫代碼後,可以馬上對所編寫的類進行單元測試,通過了程序員自行組織的單元測試後再將代碼交給測試人員進行進一步測試。
總結:微軟將單元測試功能從Visual Studio 2005 Team System開始集成到開發環境中,是經過了微軟公司多年的實踐經驗證明的。如今,開發環境從以前的單一開發功能,將關注點分散到軟件的整個生命週期過程中來,已經成爲一個ALM平臺。軟件開發人員不僅需要做開發工作,而且需要對自己開發的代碼進行單元測試,不能將所有的問題全部拋給測試人員。測試人員可以將更多的精力放在系統一級的測試工作上面。
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BankAccountDemo.Business.Tests
...{
[TestClass()]
public class BankAccountTest
...{
private TestContext testContextInstance;
public TestContext TestContext
...{
get
...{
return testContextInstance;
}
set
...{
testContextInstance = value;
}
}
Additional test attributes#region Additional test attributes
#endregion
[TestMethod()]
public void CurrentBalanceTest()
...{
float initialBalance = 0F; // TODO: Initialize to an appropriate value
BankAccount target = new BankAccount(initialBalance); // TODO: Initialize to an appropriate value
float expected = 0F; // TODO: Initialize to an appropriate value
float actual;
target.CurrentBalance = expected;
actual = target.CurrentBalance;
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
[TestMethod()]
public void makePaymentTest()
...{
float initialBalance = 0F; // TODO: Initialize to an appropriate value
BankAccount target = new BankAccount(initialBalance); // TODO: Initialize to an appropriate value
float paymentAmount = 0F; // TODO: Initialize to an appropriate value
target.makePayment(paymentAmount);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
[TestMethod()]
public void depositMoneyTest()
...{
float initialBalance = 0F; // TODO: Initialize to an appropriate value
BankAccount target = new BankAccount(initialBalance); // TODO: Initialize to an appropriate value
float depositAmount = 0F; // TODO: Initialize to an appropriate value
target.depositMoney(depositAmount);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
[TestMethod()]
public void BankAccountConstructorTest()
...{
float initialBalance = 0F; // TODO: Initialize to an appropriate value
BankAccount target = new BankAccount(initialBalance);
Assert.Inconclusive("TODO: Implement code to verify target");
}
}
}
Visual C++ can generate verifiable components with using /clr:safe, which causes the compiler to generate errors for each non-verifiable code construct.
The following issues generate verifiability errors:
- Native types. Even if it isn't used, the declaration of native classes, structures, pointers, or arrays will prevent compilation.
- Global variables
- Function calls into any unmanaged library, including common language runtime function calls
- A verifiable function cannot contain a static_cast Operator for down-casting. The static_cast operator can be used for casting between primitive types, but for down-casting, safe_cast or a C-Style cast (which is implemented as a safe_cast) must be used.
- A verifiable function cannot contain a reinterpret_cast operator (or any C-style cast equivalent).
- A verifiable function cannot perform arithmetic on an interior_ptr. It may only assign to it and dereference it.
- A verifiable function can only throw or catch pointers to reference types, so value types must be boxed before throwing.
- A verifiable function can only call verifiable functions (such that calls to the common language runtime are not allowed, include AtEntry/AtExit, and so global constructors are disallowed).
- A verifiable class cannot use Explicit.
- If building an EXE, a main function cannot declare any parameters, so GetCommandLineArgs must be used to retrieve command-line arguments.
- Making a non-virtual call to a virtual function.
Also, the following keywords cannot be used in verifiable code:
- unmanaged and pack pragmas
- naked and align __declspec modifiers
- __asm
- __based
- __try and __except
I reckon that will keep you busy for a while. There is no magic wand to wave to turn native C++ into verifiable code. Are you sure this is worth the investment?
msdn中文版內容如下:http://msdn.microsoft.com/zh-cn/library/ykbbt679(v=vs.80).aspx
看來想把自己的native項目轉化爲 /clr:safe真是不容易,那麼還有沒有一個可行的方式對msvc2008 c++項目進行單元測試呢?
找到了一個方法,現在還沒有實驗成功:http://www.cnblogs.com/LeftNotEasy/archive/2009/12/03/1616003.html
使用VS2008自帶的單元測試組件來測試純c++程序
沒有試驗成功,先記錄,等等再解決。