日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Visual Studio 2008单元测试实践一

發布時間:2023/12/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Visual Studio 2008单元测试实践一 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關鍵字:單元測試,Visual Studio 2008?

??????? 單元測試是在軟件開發過程中要進行的最低級別的測試活動,在單元測試活動中,軟件的獨立單元將在與程序的其他部分相隔離的情況下進行測試。?

??????? 在一種傳統的結構化編程語言中,比如C,要進行測試的單元一般是函數或子過程。在象C++這樣的面向對象的語言中, 要進行測試的基本單元是類。對Ada語言來說,開發人員可以選擇是在獨立的過程和函數,還是在Ada包的級別上進行單元測試。單元測試的原則同樣被擴展到第四代語言(4GL)的開發中,在這里基本單元被典型地劃分為一個菜單或顯示界面。

??????? 單元測試不僅僅是作為無錯編碼的一種輔助手段在一次性的開發過程中使用,單元測試必須是可重復的,無論是在軟件修改,或是移植到新的運行環境的過程中。因此,所有的測試都必須在整個軟件系統的生命周期中進行維護。

Visual Studio 2008 單元測試功能介紹?

一、測試代碼與被測代碼分離成獨立的兩個項目??

??????? 單元測試中,測試的代碼不能對被測試的代碼施加影響。如果將測試代碼寫入被測試的代碼中,測試完成后再刪除的話,測試的正確性將得不到保證。因此,在Visual Studio 2008種提供了一種“Test Project”的項目,測試代碼寫在Test Project中,并且測試工程可以進行重復使用。

二、測試代碼的自動生成??

??????? 書寫測試代碼是一件很煩瑣的事情,這些代碼沒有像程序代碼一樣具有“創造性”,因此該部分代碼可以進行自動化生成。Visual Studio 2008就提供了一個自動生成測試代碼的測試框架。利用Visual Studio 2008自動生成的代碼,只需要很少的改動就可以完成整個測試程序。?

三、測試管理

??????? Visual Studio 2008提供了測試列表來進行測試工作的管理工作,我們需要一個反映目前測試狀況的工具,那些測試通過了,那些沒有通過,應該提供一個列表來為我們改進測試手段,進行更全面的測試提供指導。?

利用Visual Studio 2008來進行單元測試?

??????? 假設我們有一個類BankAccount,該類定義了一個銀行的賬戶,私有屬性_currentBalance是銀行儲戶的賬戶金額,depositMoney是存款方法,對帳戶增加一筆資金,makePayment是支付方法,對賬戶減少一筆資金。代碼如下:

using System;

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”來作為測試項目的名字。??

?

生成的測試代碼如下,為了緊湊的表現代碼,將注釋代碼作了刪除。

using BankAccountDemo.Business;

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;

??????????? }

??????? }

?

??????? #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.");

??????? }

?

?轉自:http://www.51testing.com/html/index.html

轉載于:https://www.cnblogs.com/junzhongxu/archive/2009/05/14/1456476.html

總結

以上是生活随笔為你收集整理的Visual Studio 2008单元测试实践一的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。