[英]Nested TestFixture in different classes
我正在嘗試創建一個僅登錄和注銷一次的 web 自動化。 由於網站由不同的產品組成,我需要在不同的類/文件中構建不同的測試集(例如:ReportsTests、AuthenticationTests 等)我完全能夠創建一個使用OneTimeSetup
和OneTimeTearDown
登錄和注銷的TestFixture
但是每個 class 都會發生一次。
我要創建的是TestFixure
中的TestFixture
,登錄和注銷在第一個TestFixture
上發生一次,第二個執行測試集。
到目前為止,我有這個:
安裝程序.cs
using NUnit.Framework;
using System;
namespace TestsSetup
{
[TestFixture]
public class TestSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Login in");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out");
}
}
}
測試服.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TestsSetup;
namespace TCISuiteSetup
{
[TestFixture]
public class CWTestSuite : TestSetup
{
[TestFixture(1)]
public class SuiteSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Nothing happens on this step");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Nothing happens on this step");
}
}
}
}
測試1.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TCISuiteSetup;
namespace TCI.Tests
{
[TestFixture(1)]
public class UserManagerTests : CWTestSuite.SuiteSetup
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
測試2.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TCISuiteSetup;
namespace TCI.Tests
{
[TestFixture(1)]
public class ReportTests : CWTestSuite.SuiteSetup
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
如果我像這樣運行測試,所有測試都會失敗。 如果我從TestFixture(1)
(1)
會傳遞所有內容,但如果我將UserManagerTests: TestSuite.SuiteSetup
更改為UserManager: TestSetup
它只會命中第一個[TestFixture]
[OneTimeSetup]/[OneTimeTearDown]
[TestFixture]
我有什么遺漏嗎。
測試夾具類的嵌套對 NUnit 沒有任何意義。 幾年來,我們一直在談論讓它變得有意義,但從來沒有做過任何事情。
所以,就測試夾具而言, CWTestSuite
和CWTestSuite.SetUpFixture
根本沒有任何關系。 就好像它們是單獨定義的,沒有嵌套。
如果你想要一個fixture 對fixture 進行分組,NUnit 提供了SetUpFixture
,這是一種定義一次性設置和拆卸行為的方法,適用於多個類。 一個SetUpFixture
是為一個特定的命名空間定義的,並且基本上包裝了在該命名空間和從屬命名空間中定義的所有TestFixtures
。
例如,如果您要將所有TestFixtures
放在某個公共命名空間中,那么您可以像這樣在同一個命名空間中添加一個SetUpFixture
...
namespace Some.Common.Namespace
{
[SetUpFixture]
public class TestSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Login in");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out");
}
}
public class UserManagerTests
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
public class ReportTests
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
請注意,在此示例中,我沒有使用 inheritance。 這不是必需的,如果我使用它會破壞東西。 如果您需要命名空間中某些但不是全部的固定裝置的一些通用初始化邏輯,那么您可以給它們一個通用的基礎 class,但它必須與SetUpFixture
分開並且理想情況下應該是抽象的。
作為一個小提示,我刪除了所有多余的[TestFixture]
注釋。 這就是我推薦的風格。
我找到了一種方法,但也許它不是最優雅的:
安裝程序.cs
using NUnit.Framework;
using System;
namespace TestsSetup
{
[TestFixture]
public class CWTestSuite : TestSetup
{
[TestFixture]
public class SuiteSetup
{
static private bool isLoggedOn;
[OneTimeSetUp]
public void TestSuiteSetup()
{
if (isLoggedOn == false)
{
Console.WriteLine("Loggin happens here");
isLoggedOn = true;
}
}
}
}
[SetUpFixture]
public class TestSetup
{
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out happens here");
}
[TestFixture]
public class Testing
{
[Test]
public void Test1()
{
Console.WriteLine("All tests for this product has been executed");
}
}
}
}
任何符合以下格式的 test.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using static TestsSetup.CWTestSuite;
namespace TestSetup
{
[TestFixture]
public class UserManagerTests : SuiteSetup
{
[Test]
public void Test3()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test4()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
[TestFixture]
[TestFixture]
(Setup.cs/public class CWTestSuite) 中的 [TestFixture]。 如果我刪除它,它將在 class 中運行測試后點擊[OneTimeTearDown]
。[OneTimeSetUp]
。[OneTimeTearDown]
的唯一方法是之后添加[TestFixture]/[Test]
。 它在最后被調用,它可能只是打印一個 console.log 但它需要在那里並且需要被調用。 順便提一句。 這是一個可能的解決方案/解決方法,但根本不推薦。 如果你想做這樣的事情,我建議使用 NUnit + SpecFlow ( [BeforeTestRun]
就是你要找的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.