简体   繁体   English

如何针对MS Test中的接口的两种不同实现运行一组单元测试?

[英]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.

相关问题 如何使用不同的配置运行Multipe MS单元测试 - How can i run multipe MS unit tests with different configurations 如何设置单元测试以在不同的环境中运行? - How can I set up unit tests to run for different environments? 针对不同接口实现的共享 XCTest 单元测试 - Shared XCTest unit tests for different implementations of interface 如何针对测试数据库运行Selenium测试? - How can I run Selenium tests against a test database? 如何在两种不同的文件格式上运行单元测试? - How do I run unit tests on two different file formats? 如何针对不同的依赖版本运行 javascript 单元测试? - How to run javascript unit tests against different dependency versions? 如何重用相同的测试来测试不同的实现? - How to reuse the same tests to test different implementations? 在坚持DRY的同时,我应该如何为接口的多个实现编写单元测试? - How should I write unit tests for multiple implementations of an interface, while adhering to DRY? 如何使用两种不同的设置运行一组nUnit测试? - How do I run a set of nUnit tests with two different setups? Rails单元测试可以在不同于测试的环境下运行吗? - Can Rails unit tests be run on a different environment than test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM