qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          Dev環(huán)境中的集成測試用例執(zhí)行時(shí)上下文環(huán)境檢查

           我們在開發(fā)服務(wù)時(shí)為了調(diào)試方便會在本地進(jìn)行一個(gè)基本的模塊測試,你也可以認(rèn)為是集成測試,只不過你的測試用例不會覆蓋到80%以上,而是一些我們認(rèn)為在開發(fā)時(shí)不是很放心的點(diǎn)才會編寫適當(dāng)?shù)挠美齺頊y試它。
            集成測試用例通常有多個(gè)執(zhí)行上下文,對于我們開發(fā)人員來說我們的執(zhí)行上下文通常都在本地,測試人員的上下文在測試環(huán)境中。開發(fā)人員的測試用來是不能夠連接到其他環(huán)境中去的(當(dāng)然視具體情況而定,有些用例很危險(xiǎn)是不能夠亂連接的,本文會講如何解決),開發(fā)人員運(yùn)行的集成測試用例所要訪問的所有資源、服務(wù)都是在開發(fā)環(huán)境中的。這里依然存在但是,但是為了調(diào)試方便,我們還是需要能夠在必要的時(shí)候連接到其他環(huán)境中去調(diào)試問題,為了能夠真實(shí)的模擬出問題的環(huán)境、可真實(shí)的數(shù)據(jù),我們需要能有一個(gè)這樣的機(jī)制,在需要的時(shí)候我能夠打開某個(gè)設(shè)置讓其能夠切換集成測試運(yùn)行的環(huán)境上下文,其實(shí)說白了就是你所要連接的環(huán)境、數(shù)據(jù)源的連接地址。
            本篇文章我們將通過一個(gè)簡單的實(shí)例來了解如何簡單的處理這中情況,這其實(shí)基于對測試用來不斷重構(gòu)后的效果。
          1 using System;
          2 using Microsoft.VisualStudio.TestTools.UnitTesting;
          3
          4 namespace OrderManager.Test
          5 {
          6     using ProductService.Contract;
          7
          8     /// <summary>
          9     /// Product service integration tests.
          10     /// </summary>
          11     [TestClass]
          12     public class ProductServiceIntegrationTest
          13     {
          14         /// <summary>
          15         /// service address.
          16         /// </summary>
          17         public const string ServiceAddress = "http://dev.service.ProductService/";
          18
          19         /// <summary>
          20         /// Product service get product by pid test.
          21         /// </summary>
          22         [TestMethod]
          23         public void ProductService_GetProductByPid_Test()
          24         {
          25             var serviceInstance = ProductServiceClient.CreateClient(ServiceAddress);
          26             var testResult = serviceInstance.GetProductByPid(0393844);
          27
          28             Assert.AreNotEqual(testResult, null);
          29             Assert.AreEqual(testResult.Pid, 0393844);
          30         }
          31     }
          32 }
            這是一個(gè)實(shí)際的集成測試用例代碼,有一個(gè)當(dāng)前測試類共用的服務(wù)地址,這個(gè)地址是DEV環(huán)境的,當(dāng)然你也可以定義其他幾個(gè)環(huán)境的服務(wù)地址,前提是環(huán)境是允許你連接的,那才有實(shí)際意義。
            我們來看測試用例,它是一個(gè)查詢方法測試用例,用來對ProductServiceClient.GetProductByPid服務(wù)方法進(jìn)行測試,由于面向查詢的操作是等幕的,不論我們查詢多少次這個(gè)ID的Product,都不會對數(shù)據(jù)造成影響,但是如果我們測試的是一個(gè)更新或者刪除就會帶來問題。
            在DEV環(huán)境中,測試更新、刪除用例沒有問題,但是如果你的機(jī)器是能夠連接到遠(yuǎn)程某個(gè)生產(chǎn)或者PRD測試上時(shí)會帶來一定的危險(xiǎn)性,特別是在忙的時(shí)候,加班加點(diǎn)的干進(jìn)度,你很難記住你當(dāng)前的機(jī)器的host配置中是否還連接著遠(yuǎn)程的生產(chǎn)機(jī)器上,或者根本就不需要配置host就能夠連接到某個(gè)你不應(yīng)該連接的環(huán)境上。
            這是目前的問題,那么我們?nèi)绾谓鉀Q這個(gè)問題呢 ,我們通過對測試代碼進(jìn)行一個(gè)簡單的重構(gòu)就可以避免由于連接到不該連接的環(huán)境中運(yùn)行危險(xiǎn)的測試用例。
          其實(shí)很多時(shí)候,重構(gòu)真的能夠幫助我們找到出口,就好比俗話說的:"出口就在轉(zhuǎn)角處“,只有不斷重構(gòu)才能夠逐漸的保證項(xiàng)目的質(zhì)量,而這種效果是很難得的。
            提取抽象基類,對測試要訪問的環(huán)境進(jìn)行明確的定義。
          1 namespace OrderManager.Test
          2 {
          3     public abstract class ProductServiceIntegrationBase
          4     {
          5         /// <summary>
          6         /// service address.
          7         /// </summary>
          8         protected const string ServiceAddressForDev = "http://dev.service.ProductService/";
          9
          10         /// <summary>
          11         /// service address.
          12         /// </summary>
          13         protected const string ServiceAddressForPrd = "http://Prd.service.ProductService/";
          14
          15         /// <summary>
          16         /// service address.
          17         /// </summary>
          18         protected const string ServiceAddressTest = "http://Test.service.ProductService/";
          19     }
          20 }
            對具體的測試類消除重復(fù)代碼,加入統(tǒng)一的構(gòu)造方法。
          1 using System;
          2 using Microsoft.VisualStudio.TestTools.UnitTesting;
          3
          4 namespace OrderManager.Test
          5 {
          6     using ProductService.Contract;
          7
          8     /// <summary>
          9     /// Product service integration tests.
          10     /// </summary>
          11     [TestClass]
          12     public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
          13     {
          14         /// <summary>
          15         /// product service client.
          16         /// </summary>
          17         private ProductServiceClient serviceInstance;
          18
          19         /// <summary>
          20         /// Initialization test instance.
          21         /// </summary>
          22         [TestInitialize]
          23         public void InitTestInstance()
          24         {
          25             serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForDev/*for dev*/);
          26         }
          27
          28         /// <summary>
          29         /// Product service get product by pid test.
          30         /// </summary>
          31         [TestMethod]
          32         public void ProductService_GetProductByPid_Test()
          33         {
          34             var testResult = serviceInstance.GetProductByPid(0393844);
          35
          36             Assert.AreNotEqual(testResult, null);
          37             Assert.AreEqual(testResult.Pid, 0393844);
          38         }
          39
          40         /// <summary>
          41         /// Product service delete search index test.
          42         /// </summary>
          43         [TestMethod]
          44         public void ProductService_DeleteProductSearchIndex_Test()
          45         {
          46             var testResult = serviceInstance.DeleteProductSearchIndex();
          47
          48             Assert.IsTrue(testResult);
          49         }
          50     }
          51 }
            消除重復(fù)代碼后,我們需要加入對具體測試用例檢查是否能夠連接到某個(gè)環(huán)境中去。我加入了一個(gè)DeleteProductSearchIndex測試用例,該用例是用來測試刪除搜索索引的,這個(gè)測試用例只能夠在本地DEV環(huán)境中運(yùn)行(你可能覺得這個(gè)刪除接口不應(yīng)該放在這個(gè)服務(wù)里,這里只是舉一個(gè)例子,無需糾結(jié))。
            為了能夠有一個(gè)檢查機(jī)制能提醒開發(fā)人員你目前連接的地址是哪一個(gè),我們需要借助于測試上下文。
            重構(gòu)后,我們看一下現(xiàn)在的測試代碼結(jié)構(gòu)。
          1 using System;
          2 using Microsoft.VisualStudio.TestTools.UnitTesting;
          3
          4 namespace OrderManager.Test
          5 {
          6     using ProductService.Contract;
          7
          8     /// <summary>
          9     /// Product service integration tests.
          10     /// </summary>
          11     [TestClass]
          12     public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
          13     {
          14         /// <summary>
          15         /// product service client.
          16         /// </summary>
          17         private ProductServiceClient serviceInstance;
          18
          19         /// <summary>
          20         /// Initialization test instance.
          21         /// </summary>
          22         [TestInitialize]
          23         public void InitTestInstance()
          24         {
          25             serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForPrd/*for dev*/);
          26
          27             this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .
          28         }
          29
          30         /// <summary>
          31         /// Product service get product by pid test.
          32         /// </summary>
          33         [TestMethod]
          34         public void ProductService_GetProductByPid_Test()
          35         {
          36             var testResult = serviceInstance.GetProductByPid(0393844);
          37
          38             Assert.AreNotEqual(testResult, null);
          39             Assert.AreEqual(testResult.Pid, 0393844);
          40         }
          41
          42         /// <summary>
          43         /// Product service delete search index test.
          44         /// </summary>
          45         [TestMethod]
          46         public void ProductService_DeleteProductSearchIndex_Test()
          47         {
          48             var testResult = serviceInstance.DeleteProductSearchIndex();
          49
          50             Assert.IsTrue(testResult);
          51         }
          52     }
          53 }
          我們加入了一個(gè)很重要的測試實(shí)例運(yùn)行時(shí)方法InitTestInstance,該方法會在測試用例每次實(shí)例化時(shí)先執(zhí)行,在方法內(nèi)部有一個(gè)用來檢查當(dāng)前測試用例運(yùn)行的環(huán)境
            this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .,我們轉(zhuǎn)到基類中。
          1 using System;
          2 using Microsoft.VisualStudio.TestTools.UnitTesting;
          3
          4 namespace OrderManager.Test
          5 {
          6     public abstract class ProductServiceIntegrationBase
          7     {
          8         /// <summary>
          9         /// service address.
          10         /// </summary>
          11         protected const string ServiceAddressForDev = "http://dev.service.ProductService/";
          12
          13         /// <summary>
          14         /// get service address.
          15         /// </summary>
          16         protected const string ServiceAddressForPrd = "http://Prd.service.ProductService/";
          17
          18         /// <summary>
          19         /// service address.
          20         /// </summary>
          21         protected const string ServiceAddressTest = "http://Test.service.ProductService/";
          22
          23         /// <summary>
          24         /// Test context .
          25         /// </summary>
          26         public TestContext TestContext { get; set; }
          27
          28         /// <summary>
          29         /// is check is run for current test case.
          30         /// </summary>
          31         protected void CheckCurrentTestCaseIsRun(ProductService.Contract.ProductServiceClient testObject)
          32         {
          33             if (testObject.ServiceAddress.Equals(ServiceAddressForPrd))// Prd 環(huán)境,需要小心檢查
          34             {
          35                 if (this.TestContext.TestName.Equals("ProductService_DeleteProductSearchIndex_Test"))
          36                     Assert.IsTrue(false, "當(dāng)前測試用例連接的環(huán)境為PRD,請停止當(dāng)前用例的運(yùn)行。");
          37             }
          38             else if (testObject.ServiceAddress.Equals(ServiceAddressTest))//Test 環(huán)境,檢查約定幾個(gè)用例
          39             {
          40                 if (this.TestContext.TestName.Equals("ProductService_DeleteProductSearchIndex_Test"))
          41                     Assert.IsTrue(false, "當(dāng)前測試用例連接的環(huán)境為TEST,為了不破壞TEST環(huán)境,請停止用例的運(yùn)行。");
          42             }
          43         }
          44     }
          45 }
            在檢查方法中我們使用簡單的判斷某個(gè)用例不能夠在PRD、TEST環(huán)境下執(zhí)行,雖然判斷有點(diǎn)簡單,但是在真實(shí)的項(xiàng)目中足夠了,簡單有時(shí)候是一種設(shè)計(jì)思想。我們運(yùn)行所有的測試用例,查看各個(gè)狀態(tài)。
            一目了然,更為重要的是它不會影響你對其他用例的執(zhí)行。當(dāng)你在深夜12點(diǎn)排查問題的時(shí)候,你很難控制自己的眼花、體虛導(dǎo)致的用例執(zhí)行錯(cuò)誤帶來的大問題,甚至是無法挽回的的錯(cuò)誤。

          posted on 2014-09-29 09:47 順其自然EVO 閱讀(151) 評論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 夏河县| 连城县| 兴和县| 五家渠市| 准格尔旗| 广宗县| 怀来县| 美姑县| 常熟市| 天全县| 鹤壁市| 澄城县| 义马市| 洛扎县| 扎赉特旗| 远安县| 新乐市| 乐业县| 绿春县| 出国| 满洲里市| 大同县| 濉溪县| 福清市| 鹿邑县| 道孚县| 那坡县| 舒城县| 陈巴尔虎旗| 丰顺县| 南昌市| 沙湾县| 东兴市| 武陟县| 通化县| 东方市| 故城县| 灵山县| 天水市| 上高县| 萨迦县|