没有任何数据可供显示
开源项目社区 | 当前位置 : |
|
www.trustie.net/open_source_projects | 主页 > 开源项目社区 > coretdd |
coretdd
|
0 | 0 | 5 |
贡献者 | 讨论 | 代码提交 |
WhyThere are several options when choosing a test framework: VSTS, NUnit, xUnit and others. Moreover, different frameworks support different platforms (i.e. few support Silverlight at this time, or the Compact Framework), making it hard to reuse your unit tests for projects that target multiple platforms.
One way to avoid this situation is to use an abstraction where the concrete implementation of the abstraction will be the only one to know about the test framework of choice. This way, you can reuse your tests simply by changing the referenced concrete implementation of the abstractions.
CoreTDD provides those abstractions and the implementations for specific frameworks, so that you only need to add a single file (no new binary dependencies!) to your project, and start enjoying cross-platform reusable tests right-away. Just remember to stay away from your test framework-specific APIs!
HowThe idea is simple: rather than using your chosen unit test framework attributes and assertion classes, which vary slightly between frameworks, you use a unified fluent assertion API provided by a single file you add to your project.
For example, you could add the CoreTDD from our trunk http://code.google.com/p/coretdd/source/browse/trunk/Source/CoreTdd.xUnit.cs, which allows you to use the xUnit test framework. Once you have that file in your test project, your test classes don't even need to add an imports/using statement for xUnit. They simply use the attributes provided by CoreTDD:
[TestClass]
public class MyFixture
{
[TestMethod]
public void ShouldDo()
{
// arrange
// act
var result = service.Do();
// assert
result.ShouldNotBeNull();
typeof(ArgumentNullException).ShouldBeThrownBy(() => service.Do(null));
}
}The full set of fluent assertions are:
ShouldBeTrue/ShouldBeFalse: for booleans only ShouldBeEmpty/ShouldNotBeEmpty: for strings and IEnumerables. ShouldBeNull/ShouldNotBeNull ShouldEqual/ShouldNotEqual ShouldBeSameAs/ShouldNotBeSameAs ShouldBeOfType/ShouldNotBeOfType: tests for specific type as well as inheritance hierarchy and interfaces ShouldBeOfExactType: checks that the actual type matches exactly. ShouldContain/ShouldNotContain: works for strings and IEnumererables ShouldBeThrownBy: acts on an exception type (i.e. typeof(RepositoryException).ShouldBeThrownBy(() => repository.CreateOrder(...)) ShouldSatisfy: the most flexible and general-purpose assertion, allows you to pass a predicate that will be checked against the actual value.
Why TestClass and TestMethod?Aren't those VSTS attributes? They are indeed. Unfortunately, from all the test frameworks we looked at, VSTS is the only one where the attributes are sealed, meaning we cannot inherit them to have a unified set that is named differently. This works for all other frameworks, so essentially CoreTDD defines TestClassAttribute as inheriting from TestFixtureAttribute for NUnit support, and so on.
Supported Test FrameworksVSTS NUnit xUnit MbUnit Microsoft Silverlight Unit Test