如何编写Nunit TestCases来测试返回的字符串列表是否正确

发布于 2024-11-27 04:44:43 字数 1360 浏览 1 评论 0原文

我有一个类似的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2024-12-04 04:44:43

我会将它们分成多个测试。通过这样做,您可以编写一个测试正常行为(非船)以及船的行为。如果将来任何这些测试失败,您不必尝试找出数据驱动测试的哪个迭代失败了。测试会说明一切。

在这种情况下,我会为船的行为编写一个,为非船的行为编写一个。其他迭代并不有趣(并且很可能使用此测试与其他非船验证相同的代码路径)

public void Validate_VehicleIsBoat_ReturnsAppropriateErrorMessage()
{   
   string expectedResult = "This vehicle is inappropriate because it doesn't have wheels";
   var vehicle = new Vehicle { VehicleType = VehicleType.Boat };  //or whatever it is in your enum

   var results = vehicle.Validate();   

   Assert.AreEqual( expectedResult, results[0] );
}

public void Validate_VehicleIsNotBoat_DoesNotReturnErrorMessage()
{   
   var vehicle = new Vehicle { VehicleType = VehicleType.Car };  //or whatever it is in your enum

   var results = vehicle.Validate();   

   Assert.IsNull( results ); // or whatever the no error message case is. Will results[0] have an empty string?
}

您可以添加其他测试来验证结果集也包含您想要的所有数据。

无论如何,希望这有帮助

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)

public void Validate_VehicleIsBoat_ReturnsAppropriateErrorMessage()
{   
   string expectedResult = "This vehicle is inappropriate because it doesn't have wheels";
   var vehicle = new Vehicle { VehicleType = VehicleType.Boat };  //or whatever it is in your enum

   var results = vehicle.Validate();   

   Assert.AreEqual( expectedResult, results[0] );
}

public void Validate_VehicleIsNotBoat_DoesNotReturnErrorMessage()
{   
   var vehicle = new Vehicle { VehicleType = VehicleType.Car };  //or whatever it is in your enum

   var results = vehicle.Validate();   

   Assert.IsNull( results ); // or whatever the no error message case is. Will results[0] have an empty string?
}

You could add additional tests to validate the result sets have all the data you want as well.

Anyway, hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文