當測試案例越來越多的時候,執行的時間會越來越長,這時候就可以靠並行測試 (Parallel Test),來縮短測試時間,只要確定測試案例之間沒有共用資源,就可以使用囉
開發環境
- VS 2017
- Specflow 3.0.213,設定方式 傳送門
- MSTest.TestAdapter 1.3.2
- MSTest.TestFramework 1.3.2
MsTest v2 已經改由 Nuget 安裝,會有 MSTest.TestAdapter、MSTest.TestFramework 兩個套件
Runsettings
將以下內容存成 *.runsettings 放在方案根目錄
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<MSTest>
<Parallelize>
<Workers>8</Workers>
<Scope>MethodLevel</Scope>
</Parallelize>
</MSTest>
</RunSettings>
VS IDE 選擇該檔案
assembly: Parallelize
在測試類別加上 assembly: Parallelize
這裡有 4 個測試方法,每個方法都睡個 3 秒鐘
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] //0 means use as many workers as possible namespace Lab.SafeContext.UniTest { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() => Thread.Sleep(3000); [TestMethod] public void TestMethod2() => Thread.Sleep(3000); [TestMethod] public void TestMethod3() => Thread.Sleep(3000); [TestMethod] public void TestMethod4() => Thread.Sleep(3000); } }
或是放在 AssemblyInfo.cs,擇一即可
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
在 Specflow 也是一樣的用法
Parallelize Property
Workers:
- 0 - 要運行測試的執行緒數,設定為 0 為自動配置,根據 CPU 和內核計數使用盡可能多的執行緒。
- n - 產生執行測試的執行緒的數量 (整數)
Scope:
- ExecutionScope.ClassLevel - 以類別為主,最大的執行緒數量
- ExecutionScope.MethodLevel - 以方法為主,最大的執行緒數量
Parallel Test 設定成功的話,在測試總管可以看到測試案例同時轉圈圈,每個案例執行3秒,同時跑總共要 3 秒
反之,一次只會執行一個,每個案例執行3秒,一個一個跑總共要 12 秒
另外,測試總管有一個 Run Tests in Parallel ,不過我不知道怎麼讓他工作
MsTes 與 Specflow 對應
Specflow 是建構在 Test Provider 上面,Test Provider 本身要提供 Parallel Test 才會有作用
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)]
ClassLevel → Feature-level
MethodLevel → Scenario-level
在 Specflow 使用 MethodLevel,會失敗
更新:2019.06.06
Scenario level parallelization doesn't work currently. Please see issue #1535 for details.
https://github.com/techtalk/SpecFlow/issues/1599
排除 Parallel Test
在 MsTest,TestMethod 或是 TestClass 加上 [DoNotParallelize] Attribute
[TestMethod] [DoNotParallelize] public void TestMethod4() => Thread.Sleep(3000);
[TestClass] [DoNotParallelize] public class UnitTest1 { ... }
在 Specflow,Scenario 或是 Feature 加上 @mstest:donotparallelize
@mstest:donotparallelize Scenario: Add two numbers_no parallel Given I have entered 50 into the calculator And I also have entered 70 into the calculator When I press add Then the result should be 120 on the screen
@mstest:donotparallelize Feature: 實作Steps In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET