[英]How can I run a set of unit tests against two different implementations of an interface in MS Test?
I have two services which both store data to disk and read it from there. 我有两个服务,它们都将数据存储到磁盘并从那里读取数据。 One is using SQL, the other one is pure file based. 一种是使用SQL,另一种是基于纯文件。 Both implement my interface IDataStorage
. 两者都实现了我的接口IDataStorage
。
One layer above is a facade class that does processing to the data before passing it on to the IDataStorage
implementation. 上面的一层是Facade类,该类在将数据传递给IDataStorage
实现之前对其进行处理。
I have a set of unit tests for the facade class in Visual Studio 2012. 我在Visual Studio 2012中为Facade类提供了一组单元测试。
Problem is that there is currently a 问题是目前有一个
#define USE_SQL_STORAGE
and 和
#define USE_FILE_STORAGE
in the first line of the test class that decides which concrete IDataStorage
implementation gets passed to the facade class. 在测试类的第一行中,它决定将哪个具体IDataStorage
实现传递给Facade类。 I always test both implementations manually by changing the defined storage. 我总是通过更改定义的存储来手动测试这两种实现。
Is there a way to automate this and have both implementations tested against the same set of tests without having to maintain the same tests twice ? 有没有一种方法可以自动执行此操作,并且使两个实现都针对同一组测试进行测试, 而不必两次维护相同的测试 ?
Found it out myself. 自己发现了。
The solution is to create an abstract base test class: 解决方案是创建一个抽象的基础测试类:
[TestClass]
public abstract class BaseTest
{
public abstract IDataStorage GetService();
[TestInitialize]
public virtual void Init()
{
this.service = GetService();
}
}
and then have two implementations of the bas class: 然后有bas类的两个实现:
[TestClass]
public SQLiteTest : BaseTest
{
public override IDataStorage GetService()
{
return new SQLiteStorage();
}
}
[TestClass]
public FileTest : BaseTest
{
public override IDataStorage GetService()
{
return new FileStorage();
}
}
Then the two concrete implementations will show in the list of test cases but not the abstract base. 然后,这两个具体的实现将显示在测试用例列表中,而不显示在抽象库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.