.NET程序員項目開發必知必會—Dev環境中的集成測試用例執行時上下文環境檢查(實戰)

Microsoft.NET 解決方案,項目開發必知必會。

從這篇文章開始我將分享一系列我認爲在實際工作中很有必要的一些.NET項目開發的核心技術點,所以我稱爲必知必會。儘管這一些列是使用.NET/C#來展現,但是同樣適用於其他類似的OO技術平臺,這些技術點可能稱不上完整的技術,但是它是經驗的總結,是掉過多少坑之後的覺醒,所以有必要花幾分鐘時間記住它,在真實的項目開發中你就知道是多麼的有幫助。好了,廢話不說了,進入主題。

我們在開發服務時爲了調試方便會在本地進行一個基本的模塊測試,你也可以認爲是集成測試,只不過你的測試用例不會覆蓋到80%以上,而是一些我們認爲在開發時不是很放心的點纔會編寫適當的用例來測試它。

集成測試用例通常有多個執行上下文,對於我們開發人員來說我們的執行上下文通常都在本地,測試人員的上下文在測試環境中。開發人員的測試用來是不能夠連接到其他環境中去的(當然視具體情況而定,有些用例很危險是不能夠亂連接的,本文會講如何解決),開發人員運行的集成測試用例所要訪問的所有資源、服務都是在開發環境中的。這裏依然存在但是,但是爲了調試方便,我們還是需要能夠在必要的時候連接到其他環境中去調試問題,爲了能夠真實的模擬出問題的環境、可真實的數據,我們需要能有一個這樣的機制,在需要的時候我能夠打開某個設置讓其能夠切換集成測試運行的環境上下文,其實說白了就是你所要連接的環境、數據源的連接地址。

本篇文章我們將通過一個簡單的實例來了解如何簡單的處理這中情況,這其實基於對測試用來不斷重構後的效果。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace OrderManager.Test
{
    using ProductService.Contract; 

    /// <summary>
    /// Product service integration tests.
    /// </summary>
    [TestClass]
    public class ProductServiceIntegrationTest
    {
        /// <summary>
        /// service address.
        /// </summary>
        public const string ServiceAddress = "http://dev.service.ProductService/"; 

        /// <summary>
        /// Product service get product by pid test.
        /// </summary>
        [TestMethod]
        public void ProductService_GetProductByPid_Test()
        {
            var serviceInstance = ProductServiceClient.CreateClient(ServiceAddress);
            var testResult = serviceInstance.GetProductByPid(0393844); 

            Assert.AreNotEqual(testResult, null);
            Assert.AreEqual(testResult.Pid, 0393844);
        }
    }
}

這是一個實際的集成測試用例代碼,有一個當前測試類共用的服務地址,這個地址是DEV環境的,當然你也可以定義其他幾個環境的服務地址,前提是環境是允許你連接的,那纔有實際意義。

我們來看測試用例,它是一個查詢方法測試用例,用來對ProductServiceClient.GetProductByPid服務方法進行測試,由於面向查詢的操作是等幕的,不論我們查詢多少次這個ID的Product,都不會對數據造成影響,但是如果我們測試的是一個更新或者刪除就會帶來問題。

在DEV環境中,測試更新、刪除用例沒有問題,但是如果你的機器是能夠連接到遠程某個生產或者PRD測試上時會帶來一定的危險性,特別是在忙的時候,加班加點的幹進度,你很難記住你當前的機器的host配置中是否還連接着遠程的生產機器上,或者根本就不需要配置host就能夠連接到某個你不應該連接的環境上。

這是目前的問題,那麼我們如何解決這個問題呢 ,我們通過對測試代碼進行一個簡單的重構就可以避免由於連接到不該連接的環境中運行危險的測試用例。

其實很多時候,重構真的能夠幫助我們找到出口,就好比俗話說的:"出口就在轉角處“,只有不斷重構才能夠逐漸的保證項目的質量,而這種效果是很難得的。

提取抽象基類,對測試要訪問的環境進行明確的定義。

namespace OrderManager.Test
{
    public abstract class ProductServiceIntegrationBase
    {
        /// <summary>
        /// service address.
        /// </summary>
        protected const string ServiceAddressForDev = "http://dev.service.ProductService/"; 

        /// <summary>
        /// service address.
        /// </summary>
        protected const string ServiceAddressForPrd = "http://Prd.service.ProductService/"; 

        /// <summary>
        /// service address.
        /// </summary>
        protected const string ServiceAddressTest = "http://Test.service.ProductService/";
    }
} 

對具體的測試類消除重複代碼,加入統一的構造方法。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace OrderManager.Test
{
    using ProductService.Contract; 

    /// <summary>
    /// Product service integration tests.
    /// </summary>
    [TestClass]
    public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
    {
        /// <summary>
        /// product service client.
        /// </summary>
        private ProductServiceClient serviceInstance; 

        /// <summary>
        /// Initialization test instance.
        /// </summary>
        [TestInitialize]
        public void InitTestInstance()
        {
            serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForDev/*for dev*/);
        } 

        /// <summary>
        /// Product service get product by pid test.
        /// </summary>
        [TestMethod]
        public void ProductService_GetProductByPid_Test()
        {
            var testResult = serviceInstance.GetProductByPid(0393844); 

            Assert.AreNotEqual(testResult, null);
            Assert.AreEqual(testResult.Pid, 0393844);
        } 

        /// <summary>
        /// Product service delete search index test.
        /// </summary>
        [TestMethod]
        public void ProductService_DeleteProductSearchIndex_Test()
        {
            var testResult = serviceInstance.DeleteProductSearchIndex(); 

            Assert.IsTrue(testResult);
        }
    }
} 

消除重複代碼後,我們需要加入對具體測試用例檢查是否能夠連接到某個環境中去。我加入了一個DeleteProductSearchIndex測試用例,該用例是用來測試刪除搜索索引的,這個測試用例只能夠在本地DEV環境中運行(你可能覺得這個刪除接口不應該放在這個服務裏,這裏只是舉一個例子,無需糾結)。

爲了能夠有一個檢查機制能提醒開發人員你目前連接的地址是哪一個,我們需要藉助於測試上下文。

重構後,我們看一下現在的測試代碼結構。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace OrderManager.Test
{
    using ProductService.Contract; 

    /// <summary>
    /// Product service integration tests.
    /// </summary>
    [TestClass]
    public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
    {
        /// <summary>
        /// product service client.
        /// </summary>
        private ProductServiceClient serviceInstance; 

        /// <summary>
        /// Initialization test instance.
        /// </summary>
        [TestInitialize]
        public void InitTestInstance()
        {
            serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForPrd/*for dev*/); 

            this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .
        } 

        /// <summary>
        /// Product service get product by pid test.
        /// </summary>
        [TestMethod]
        public void ProductService_GetProductByPid_Test()
        {
            var testResult = serviceInstance.GetProductByPid(0393844); 

            Assert.AreNotEqual(testResult, null);
            Assert.AreEqual(testResult.Pid, 0393844);
        } 

        /// <summary>
        /// Product service delete search index test.
        /// </summary>
        [TestMethod]
        public void ProductService_DeleteProductSearchIndex_Test()
        {
            var testResult = serviceInstance.DeleteProductSearchIndex(); 

            Assert.IsTrue(testResult);
        }
    }
}

我們加入了一個很重要的測試實例運行時方法InitTestInstance,該方法會在測試用例每次實例化時先執行,在方法內部有一個用來檢查當前測試用例運行的環境 this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .,我們轉到基類中。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace OrderManager.Test
{
    public abstract class ProductServiceIntegrationBase
    {
        /// <summary>
        /// service address.
        /// </summary>
        protected const string ServiceAddressForDev = "http://dev.service.ProductService/";

        /// <summary>
        /// get service address.
        /// </summary>
        protected const string ServiceAddressForPrd = "http://Prd.service.ProductService/";

        /// <summary>
        /// service address.
        /// </summary>
        protected const string ServiceAddressTest = "http://Test.service.ProductService/";

        /// <summary>
        /// Test context .
        /// </summary>
        public TestContext TestContext { get; set; } 

        /// <summary>
        /// is check is run for current test case.
        /// </summary>
        protected void CheckCurrentTestCaseIsRun(ProductService.Contract.ProductServiceClient testObject)
        {
            if (testObject.ServiceAddress.Equals(ServiceAddressForPrd))// Prd 環境,需要小心檢查
            {
                if (this.TestContext.TestName.Equals("ProductService_DeleteProductSearchIndex_Test"))
                    Assert.IsTrue(false, "當前測試用例連接的環境爲PRD,請停止當前用例的運行。");
            }
            else if (testObject.ServiceAddress.Equals(ServiceAddressTest))//Test 環境,檢查約定幾個用例
            {
                if (this.TestContext.TestName.Equals("ProductService_DeleteProductSearchIndex_Test"))
                    Assert.IsTrue(false, "當前測試用例連接的環境爲TEST,爲了不破壞TEST環境,請停止用例的運行。");
            }
        }
    }
}

在檢查方法中我們使用簡單的判斷某個用例不能夠在PRD、TEST環境下執行,雖然判斷有點簡單,但是在真實的項目中足夠了,簡單有時候是一種設計思想。我們運行所有的測試用例,查看各個狀態。



一目瞭然,更爲重要的是它不會影響你對其他用例的執行。當你在深夜12點排查問題的時候,你很難控制自己的眼花、體虛導致的用例執行錯誤帶來的大問題,甚至是無法挽回的的錯誤。

此文獻給那些跟我一樣的.NET程序員們,通過簡單的重構,解放了自己。


發佈了156 篇原創文章 · 獲贊 100 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章