如何编写Nunit TestCases来测试返回的字符串列表是否正确
我有一个类似的方法:
public virtual IList<string> Validate()
{
...
}
我想使用 NUnit 对其进行单元测试。这是车辆类的一部分。
Vehicle
可以有不同的类型,即。 Car
Boat
Truck
等。
在我的 TestFixture
顶部,我设置了 VehicleTypes
>:
private VehicleType[] _vehicleTypes;
[SetUp]
public void MyTestInitialize()
{
transScope = new TransactionScope();
var boat= new VehicleType { Name = "boat" };
var car = new VehicleType { Name = "car" };
var truck = new VehicleType { Name = "truck" };
_vehicleTypes= new VehicleType[] { boat, car, truck };
...
}
我想要的是测试仅从船的方法发回错误消息。
我的单元测试如下:
[TestCase(0, "This vehicle is inappropriate because it doesn't have wheels")]
[TestCase(1, null)]
[TestCase(2, null)]
public void Validate_Vehicle_ReturnsAppropriateErrorMessage(int vehicleType, string expectedResult)
{
var vehicle = new Vehicle { VehicleType = _vehicleTypes[vehicleType] };
var results = vehicle.Validate();
string result;
if (results.Count == 0)
result = null;
else
result = results[0];
Assert.IsTrue(expectedResult == result);
}
这就是我尝试使用测试用例测试它的方式。但是我不确定这是否是正确的方法,因为单元测试不应包含 ifs
?
另外,也许这是为不同类型编写测试的奇怪方法?
有人有更好的建议吗?
I have a method like:
public virtual IList<string> Validate()
{
...
}
I want to unit test this using NUnit. This is part of a class Vehicle.
A Vehicle
can be of different types ie. Car
Boat
Truck
etc.
At the top of my TestFixture
I set up the VehicleTypes
:
private VehicleType[] _vehicleTypes;
[SetUp]
public void MyTestInitialize()
{
transScope = new TransactionScope();
var boat= new VehicleType { Name = "boat" };
var car = new VehicleType { Name = "car" };
var truck = new VehicleType { Name = "truck" };
_vehicleTypes= new VehicleType[] { boat, car, truck };
...
}
What I want is to test that an error message is sent back from the method for the boat only.
My unit test is as follows:
[TestCase(0, "This vehicle is inappropriate because it doesn't have wheels")]
[TestCase(1, null)]
[TestCase(2, null)]
public void Validate_Vehicle_ReturnsAppropriateErrorMessage(int vehicleType, string expectedResult)
{
var vehicle = new Vehicle { VehicleType = _vehicleTypes[vehicleType] };
var results = vehicle.Validate();
string result;
if (results.Count == 0)
result = null;
else
result = results[0];
Assert.IsTrue(expectedResult == result);
}
So this was how I was trying to test it using TestCases. However I'm not sure this is the right approach as unit tests shouldn't contain ifs
?
Also maybe this is a weird approach to writing a test for different types?
Anyone have any better suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将它们分成多个测试。通过这样做,您可以编写一个测试正常行为(非船)以及船的行为。如果将来任何这些测试失败,您不必尝试找出数据驱动测试的哪个迭代失败了。测试会说明一切。
在这种情况下,我会为船的行为编写一个,为非船的行为编写一个。其他迭代并不有趣(并且很可能使用此测试与其他非船验证相同的代码路径)
您可以添加其他测试来验证结果集也包含您想要的所有数据。
无论如何,希望这有帮助
I would break these up into multiple tests. By doing so you can write one that test the normal behavior (non boat) as well as the boat. If any of these test fail in the future, you won't have to try and figure out what iteration of the data driven tests failed. The test will speak for itself.
In this case I would write one for the behavior for the boat and one for the non boat. The other iterations aren't interesting (and likly use the same code path this test is validating as the other non boats)
You could add additional tests to validate the result sets have all the data you want as well.
Anyway, hope this helps