我必须用NUnit编写一些集成测试,这些测试应该测试HTTP。这意味着URL应该在所有测试方法之间共享。
[TestFixture("http://my.endpoint.com")]
public class TestSuiteOne
{
[TestCase(10, 20, Expected = 30)]
[TestCase(20, 30, Expected = 50)]
public int TestA(int a, int b, HttpResponseMessage sut)
{
// AAA.
}
[TestCase("XXX", "YYY", Expected = "ABC")]
public int TestA(string a, string b, HttpResponseMessage sut)
{
// AAA.
}
}要获得来自测试套件属性的每次测试运行的SUT值,我知道两个选项:
选项A(从类属性读取SUT )
public abstract class TestSuiteBase
{
protected(string endpoint)
{
Sut = endpoint;
}
protected HttpResponseMessage Sut { get; }
}
[TestFixture("http://my.endpoint.com")]
public class TestSuiteOne : TestSuiteBase
{
public TestSuiteOne(string endpoint) : base(endpoint)
{
}
[TestCase(10, 20, Expected = 30)]
[TestCase(20, 30, Expected = 50)]
public int TestA(int a, int b)
{
// act (read content/response code/headers/etc); Making a call I do not consider as act.
var actual = Sut.DoSomething();
// assert
}
[TestCase("XXX", "YYY", Expected = "ABC")]
public int TestA(string a, string b)
{
// act (read content/response code/headers/etc); Making a call I do not consider as act.
var actual = Sut.DoSomething();
// assert
}
}选项B(拦截测试方法调用)
public class MyTestCase: TestCaseAttribute
{
public MyTestCase(params object[] args) : base(Resize(args))
{
}
// I do resize before method call because VS Test Adapters discover tests to show a list in the test explorer
private static object[] Resize(params object[] args)
{
Array.Resize(ref args, args.Length + 1);
args[args.Length - 1] = "{response}";
return args;
}
}
public abstract class TestSuiteBase
{
[SetUp]
public void OnBeforeTestRun()
{
var ctx = TestContext.CurrentContext;
var args = ctx.Test.Arguments;
// Making a call I do not consider as act.
args[args.Length - 1] = MakeCallAndGetHttpResponseMessage(args);
}
}
[TestFixture("http://my.endpoint.com")]
public class TestSuiteOne : TestSuiteBase
{
[MyTestCase(10, 20, Expected = 30)]
[MyTestCase(20, 30, Expected = 50)]
public int TestA(int a, int b, HttpResponseMessage sut)
{
// act (read content/response code/headers/etc); Making a call I do not consider as act.
var actual = sut.DoSomething();
// assert
}
[MyTestCase("XXX", "YYY", Expected = "ABC")]
public int TestA(string a, string b, HttpResponseMessage sut)
{
// AAA.
}
}是否有更方便的方法将来自TestFixture的值与TestCase结合起来?
发布于 2020-01-03 02:31:55
因为您将端点字符串放置在TestFixtureAttribute上,所以我将假设
您的示例中唯一缺少的是一个将接受您所提供的参数的构造函数。只需加些类似..。
public TestSuiteOne(string url)
{
Sut = endpoint;
}只要您使Sut成为只读属性或字段,您的测试用例就无法更改它,无论它们是顺序运行还是并行运行。
你提供的两个选项对我来说似乎都过于复杂了,至少就你已经解释了问题而言。第一个是最接近这个答案的。
当然,您完全有可能对网站做其他事情,这会导致并行(甚至是顺序的)测试相互干扰。别干那事。-说真的,共享一个驱动程序与共享一个字符串完全不同,但这是一个不同的问题。
顺便说一句,海事组织,你和你的团队应该习惯这样或那样的测试框架。您提到的生命周期问题并不是NUnit和xUnit之间唯一微妙的区别!
https://stackoverflow.com/questions/59562606
复制相似问题