【问题标题】:How to make UnitTests run twice with different mockup settings如何使用不同的模型设置使单元测试运行两次
【发布时间】:2017-02-11 04:35:43
【问题描述】:

我有一个项目支持多种部署模式:InMem、OnPremise、Cloud。 此外,每个项目都有像 TimeDistance 这样的小型服务,可以连接到 WCF,也可以连接到 API。

在 unitTestMockup 中我可以说出我想使用哪一个:

Service.TimeDistance = new WCFTimeDistance() / new APITimeDistance().

到目前为止,我只有 WCFTimeDistance,但现在我们处于过渡模式以转移到 APITimeDistance,但与此同时,我希望在运行测试时运行两次,一次使用 WCF,一次使用 API。

有什么好的方法可以做到这一点?

I use C# 4.5
Microsoft.VisualStudio.QualityTools.UnitTestFramework as framework for unitTests

所需工作流程的一个简单示例如下:

1)Mockup: Service.TimeDistance = new WCFTimeDistance();
2)UnitTest: CheckDistanceBetweenTwoLocationsTest()
{
Service.TimeDistance.CalculateDistance(Location1, Location2) // WCFTimeDistance
}
3)Mockup: Service.TimeDistance = new APITimeDistance();
UnitTest: CheckDistanceBetweenTwoLocationsTest()
{
4)Service.TimeDistance.CalculateDistance(Location1, Location2) //    APITimeDistance
}

【问题讨论】:

  • 对不起 - 什么语言,什么单元测试框架?另外,你能提供一个minimal reproducible example吗?不过,很好的问题!
  • @AndreiDutu 创建两个单元测试。还要考虑使用抽象而不是静态类。这将使模拟和测试更容易

标签: c# unit-testing vs-unit-testing-framework mockups


【解决方案1】:

创建两个单元测试。还要考虑使用抽象而不是静态类。这将使模拟和测试更容易。

这是针对您当前的设置

[TestClass]
public class TimeDistanceTests {
    //change these to the needed types
    object Location1;
    object Location2;
    double expected;

    [TestInitialize]
    public void Init() {
        //...setup the two locations and expectations
        Location1 = //...
        Location2 = //...
        expected = //...the distance.
    }

    [TestMethod]
    public void Check_Distance_Between_Two_Locations_Using_WCF() {
        //Arrange
        var sut = new WCFTimeDistance();
        //Act
        var actual = sut.CalculateDistance(Location1, Location2);
        //Assert
        //...some assertion proving that test passes or failed
        Assert.AreEqual(expected, actual);
    }

    [TestMethod]
    public void Check_Distance_Between_Two_Locations_Using_API() {
        //Arrange
        var sut = new APITimeDistance();
        //Act
        var actual = sut.CalculateDistance(Location1, Location2);
        //Assert
        //...some assertion proving that test passes or failed
        Assert.AreEqual(expected, actual);
    }
}

理想情况下,您希望抽象服务。假设类似

public interface ITimeDistance {
    double CalculateDistance(Location location1, Location location2);
}

您的服务将使用抽象而不是具体化。

public class Service {
    ITimeDistance timeDistance
    public Service(ITimeDistance timeDistance) {
        this.timeDistance = timeDistance;
    }

    public ITimeDistance TimeDistance { get { return timeDistance; } }
}

所以现在在您的单元测试中,您可以交换时间距离服务的不同实现。

【讨论】:

  • 这正是服务的抽象方式。我避免重复测试,因为我已经有 ~110 个单元测试,并且重复代码太多。这就是我现在正在做的事情,我创建了一个新的枚举:WCF,API,我首先为 WCF 运行单元测试,然后为 API 运行。我认为有一种方法可以自动完成所有这些
  • @AndreiDutu 然后研究像 NUnit 或 xUnit 这样的单元测试框架,它们提供了这样的功能,例如看这里:nunit.org/?p=testCase&r=2.5
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 2019-11-29
  • 2017-03-09
  • 2013-03-25
  • 2010-10-29
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多