国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

.NET程序員項(xiàng)目開(kāi)發(fā)必知必會(huì)—Dev環(huán)境中的集成測(cè)試用例執(zhí)行時(shí)上下文環(huán)境檢查(實(shí)戰(zhàn))

2019-11-17 02:53:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

.NET程序員項(xiàng)目開(kāi)發(fā)必知必會(huì)—Dev環(huán)境中的集成測(cè)試用例執(zhí)行時(shí)上下文環(huán)境檢查(實(shí)戰(zhàn))

Microsoft.NET 解決方案,項(xiàng)目開(kāi)發(fā)必知必會(huì)。

從這篇文章開(kāi)始我將分享一系列我認(rèn)為在實(shí)際工作中很有必要的一些.NET項(xiàng)目開(kāi)發(fā)的核心技術(shù)點(diǎn),所以我稱(chēng)為必知必會(huì)。盡管這一系列是使用.NET/C#來(lái)展現(xiàn),但是同樣適用于其他類(lèi)似的OO技術(shù)平臺(tái),這些技術(shù)點(diǎn)可能稱(chēng)不上完整的技術(shù),但是它是經(jīng)驗(yàn)的總結(jié),是掉過(guò)多少坑之后的覺(jué)醒,所以有必要花幾分鐘時(shí)間記住它,在真實(shí)的項(xiàng)目開(kāi)發(fā)中你就知道是多么的有幫助。好了,廢話(huà)不說(shuō)了,進(jìn)入主題。

我們?cè)陂_(kāi)發(fā)服務(wù)時(shí)為了調(diào)試方便會(huì)在本地進(jìn)行一個(gè)基本的模塊測(cè)試,你也可以認(rèn)為是集成測(cè)試,只不過(guò)你的測(cè)試用例不會(huì)覆蓋到80%以上,而是一些我們認(rèn)為在開(kāi)發(fā)時(shí)不是很放心的點(diǎn)才會(huì)編寫(xiě)適當(dāng)?shù)挠美齺?lái)測(cè)試它。

集成測(cè)試用例通常有多個(gè)執(zhí)行上下文,對(duì)于我們開(kāi)發(fā)人員來(lái)說(shuō)我們的執(zhí)行上下文通常都在本地,測(cè)試人員的上下文在測(cè)試環(huán)境中。開(kāi)發(fā)人員的測(cè)試用來(lái)是不能夠連接到其他環(huán)境中去的(當(dāng)然視具體情況而定,有些用例很危險(xiǎn)是不能夠亂連接的,本文會(huì)講如何解決),開(kāi)發(fā)人員運(yùn)行的集成測(cè)試用例所要訪(fǎng)問(wèn)的所有資源、服務(wù)都是在開(kāi)發(fā)環(huán)境中的。這里依然存在但是,但是為了調(diào)試方便,我們還是需要能夠在必要的時(shí)候連接到其他環(huán)境中去調(diào)試問(wèn)題,為了能夠真實(shí)的模擬出問(wèn)題的環(huán)境、可真實(shí)的數(shù)據(jù),我們需要能有一個(gè)這樣的機(jī)制,在需要的時(shí)候我能夠打開(kāi)某個(gè)設(shè)置讓其能夠切換集成測(cè)試運(yùn)行的環(huán)境上下文,其實(shí)說(shuō)白了就是你所要連接的環(huán)境、數(shù)據(jù)源的連接地址。

本篇文章我們將通過(guò)一個(gè)簡(jiǎn)單的實(shí)例來(lái)了解如何簡(jiǎn)單的處理這中情況,這其實(shí)基于對(duì)測(cè)試用來(lái)不斷重構(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 ProductServiceIntegrationTest13     {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í)際的集成測(cè)試用例代碼,有一個(gè)當(dāng)前測(cè)試類(lèi)共用的服務(wù)地址,這個(gè)地址是DEV環(huán)境的,當(dāng)然你也可以定義其他幾個(gè)環(huán)境的服務(wù)地址,前提是環(huán)境是允許你連接的,那才有實(shí)際意義。

我們來(lái)看測(cè)試用例,它是一個(gè)查詢(xún)方法測(cè)試用例,用來(lái)對(duì)ProductServiceClient.GetProductByPid服務(wù)方法進(jìn)行測(cè)試,由于面向查詢(xún)的操作是等幕的,不論我們查詢(xún)多少次這個(gè)ID的Product,都不會(huì)對(duì)數(shù)據(jù)造成影響,但是如果我們測(cè)試的是一個(gè)更新或者刪除就會(huì)帶來(lái)問(wèn)題。

在DEV環(huán)境中,測(cè)試更新、刪除用例沒(méi)有問(wèn)題,但是如果你的機(jī)器是能夠連接到遠(yuǎn)程某個(gè)生產(chǎn)或者PRD測(cè)試上時(shí)會(huì)帶來(lái)一定的危險(xiǎn)性,特別是在忙的時(shí)候,加班加點(diǎn)的干進(jìn)度,你很難記住你當(dāng)前的機(jī)器的host配置中是否還連接著遠(yuǎn)程的生產(chǎn)機(jī)器上,或者根本就不需要配置host就能夠連接到某個(gè)你不應(yīng)該連接的環(huán)境上。

這是目前的問(wèn)題,那么我們?nèi)绾谓鉀Q這個(gè)問(wèn)題呢 ,我們通過(guò)對(duì)測(cè)試代碼進(jìn)行一個(gè)簡(jiǎn)單的重構(gòu)就可以避免由于連接到不該連接的環(huán)境中運(yùn)行危險(xiǎn)的測(cè)試用例。

其實(shí)很多時(shí)候,重構(gòu)真的能夠幫助我們找到出口,就好比俗話(huà)說(shuō)的:"出口就在轉(zhuǎn)角處“,只有不斷重構(gòu)才能夠逐漸的保證項(xiàng)目的質(zhì)量,而這種效果是很難得的。

提取抽象基類(lèi),對(duì)測(cè)試要訪(fǎng)問(wèn)的環(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 } 

對(duì)具體的測(cè)試類(lèi)消除重復(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 : ProductServiceIntegrationBase13     {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ù)代碼后,我們需要加入對(duì)具體測(cè)試用例檢查是否能夠連接到某個(gè)環(huán)境中去。我加入了一個(gè)DeleteProductSearchIndex測(cè)試用例,該用例是用來(lái)測(cè)試刪除搜索索引的,這個(gè)測(cè)試用例只能夠在本地DEV環(huán)境中運(yùn)行(你可能覺(jué)得這個(gè)刪除接口不應(yīng)該放在這個(gè)服務(wù)里,這里只是舉一個(gè)例子,無(wú)需糾結(jié))。

為了能夠有一個(gè)檢查機(jī)制能提醒開(kāi)發(fā)人員你目前連接的地址是哪一個(gè),我們需要借助于測(cè)試上下文。

重構(gòu)后,我們看一下現(xiàn)在的測(cè)試代碼結(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 : ProductServiceIntegrationBase13     {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è)很重要的測(cè)試實(shí)例運(yùn)行時(shí)方法InitTestInstance,該方法會(huì)在測(cè)試用例每次實(shí)例化時(shí)先執(zhí)行,在方法內(nèi)部有一個(gè)用來(lái)檢查當(dāng)前測(cè)試用例運(yùn)行的環(huán)境 this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .,我們轉(zhuǎn)到基類(lèi)中。

 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)前測(cè)試用例連
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 张家界市| 田阳县| 定兴县| 巧家县| 浦东新区| 富平县| 鱼台县| 曲松县| 宾川县| 长沙县| 香港 | 海宁市| 大余县| 嵊州市| 彭山县| 樟树市| 福泉市| 江陵县| 南昌县| 安宁市| 昔阳县| 保康县| 连州市| 绥芬河市| 本溪市| 延津县| 保山市| 那坡县| 永顺县| 五寨县| 泗阳县| 台湾省| 通江县| 太仆寺旗| 平罗县| 调兵山市| 博罗县| 延川县| 三都| 石家庄市| 衡阳县|