項目搭建(二):NUnit&TestStack.White

1、單元測試框架NUnitshell

NUnit是全部.net語言的單元測試框架。使用C#語言編寫。框架

測試框架:NUnit3ide

1. 安裝NuGet包管理器函數

2. 在NuGet中安裝NUnit、NUnit.Console和NUnit 3 Test Adapter工具

本地安裝版本爲:NUnit(3.12.0)、NUnit.Console(3.10.0)NUnit 3 Test Adapter(V3.15.1.0)單元測試

此時查看項目引用中包含nunit.framework.dll,安裝完重啓VS。測試

 

2、測試框架TestStack.Whitespa

1. 在NuGet中安裝TestStack.White.net

2. 或者可經過NuGet控制檯(PowerShell)進行安裝code

  在VS中打開工具->NuGet包管理器->程序包管理器控制檯(Powershell,若是沒有可手動下載)

  a. 輸入install-package TestStack.White

  b. 輸入install-package Nunit

此時查看項目引用中包含TestStack.White,安裝完重啓VS。

 

3、代碼中引入

NUnit做用是添加【TestFixture】等測試標識,引入using NUnit.Framework;

TestStack.White做用是封裝了識別應用程序和控件的方法。

using TestStack.White; 
using TestStack.White.Factory;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using NUnit.Framework;

namespace myProject01
{
    [TestFixture]
    class Program
    {
        static Application posApp;
        [SetUp]
        public void setUp() {
            posApp = Application.Launch(@"xx\xx.exe");
            Console.WriteLine("setup");
        }
        [TearDown]
        public void tearDown()
        {
            posApp.Close();
            Console.WriteLine("teardown");
        }
        //項目執行須要有main函數,此處先和在這裏,沒有實際做用,爲了編譯經過。。。
        static void Main(string[] args) { }

        [Test]
        public void testcase_login()
        {
             Window posWindow = posApp.GetWindow("登陸", InitializeOption.NoCache);
             TextBox usertext = posWindow.Get<TextBox>(SearchCriteria.ByAutomationId("txt_no"));
             usertext.SetValue("0114");
         }

 打開【測試】-【窗口】-測試資源管理器;在解決方案右鍵->生成解決方案;

 

 

 

4、測試固件

【TestFixture】:

用在class前面,表示該類包含測試,此類必須是public的

【Test】

表示該函數爲一個testcase

【SetUp】【TearDown】:

測試固件前置條件和後置條件,做用於每一個testcase

【TestFixtureSetUp】【TestFixtureTearDown】

測試固件前置條件和後置條件,做用於每一個【TestFixture】

【ExpectedException】 :

用例預期會拋出Exception,若是沒有拋用例就會失敗。好比用任何數除以0會獲得DivideByZeroException,這時能夠在TestCase前面加上ExpectedException

[Test]
[ExpectedException("System.DivideByZeroException")]
public void testException() {
int zero = 0 ;
int number = 2 / zero;
}

【Explicit】

執行所有用例時會忽略執行,單獨選中該testcase能夠執行

【Ignore】

忽略執行,即便單獨執行也不能夠

 

5、控件識別

【TextBox】

         TextBox usertext = posWindow.Get<TextBox>(SearchCriteria.ByAutomationId("txt_no"));

         usertext.SetValue("0114");

【Button】

      var button = posWindow.Get<Button>("btn_login");

         button.Click();

   [Label]

        classname爲Text的可以使用Label,通常爲點擊文字事件的。

     var serverSet = posWindow.Get<Label>(SearchCriteria.ByAutomationId("lbl_chgServer"));

        serverSet.Click();

【自定義類】

  添加引用,引入自定義的ClassLibrary2.dll

  using BaseTest;

       BaseClass.ClickLeftMouse(processId, "CtrButtonCon5");

 

6、Assert斷言

Assert.AreEqual(expectedResult, actualResult);

Assert 類包含 AreEqual,AreSame,Equals, Fail,IsFalse,IsNotNull ..等方法。

相關文章
相關標籤/搜索