I like the feel of the Assert class, but wanted something that would serve more as a general validation framework. I started with Roger Alsing's article on using extension methods, and now have a system that works like:
Enforce.That(variable).IsNotNull();
Enforce.That(variable).IsInRange(10, 20);
Enforce.That(variable).IsTypeOf(typeof(System.String));
etc.
If any enforcement fails, it throws an exception. I have been considering refactoring so that I can also incorporate a non-critical evaluation that does not throw an exception. Some like Check.That as a variant of Enforce.That which would return booleans, but have extension methods with identical signatures.
What I like about this approach, so far, is that I can use these in my unit tests, as well as for pre-validation and post-validation concerns in my actual code without referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. I put it in my root assembly for my application framework, and Enforce is at the root, so it is very easy to get to.
A lot of my tests revolve around loading a object that (like a CuttingPath) with a known good state.. Performing the test, then comparing the result to the loaded object. If they differ then somehthing 'happened' to cause a change in the code.
This approach save tons of time and allow for custom comparisons when needed.
I think that if you're refactoring your tests to reduce duplication then you end up creating your own framework as a by-product, and of course your test framework will have assert helpers that make sense in your context.
An example I have in mind was when testing xhtml report we ended up with tests that looked something like:
assertCoverageEquals(45.5);
where behind assert coverage was something like:
assertPercentage(COVERAGE_ID, 45.5);
and then behind that was something that used xpath to get the value and another method that knew what the formatting was for percentages.
发布评论
评论(5)
这是我的解决方案:
That's my solution:
我喜欢 Assert 类的感觉,但想要一些可以更多地用作通用验证框架的东西。 我从 Roger Alsing 的文章 使用扩展方法,现在有一个系统,其工作原理如下:
如果任何执行失败,它都会抛出异常。 我一直在考虑重构,以便我还可以合并不引发异常的非关键评估。 有些人喜欢将 Check.That 作为 Enforce.That 的变体,它会返回布尔值,但具有具有相同签名的扩展方法。
到目前为止,我喜欢这种方法的原因是,我可以在单元测试中使用这些方法,以及在实际代码中解决预验证和后验证问题,而无需引用 Microsoft.VisualStudio.QualityTools.UnitTestFramework 程序集。 我将它放在我的应用程序框架的根程序集中,Enforce 位于根目录中,因此很容易访问。
I like the feel of the Assert class, but wanted something that would serve more as a general validation framework. I started with Roger Alsing's article on using extension methods, and now have a system that works like:
If any enforcement fails, it throws an exception. I have been considering refactoring so that I can also incorporate a non-critical evaluation that does not throw an exception. Some like Check.That as a variant of Enforce.That which would return booleans, but have extension methods with identical signatures.
What I like about this approach, so far, is that I can use these in my unit tests, as well as for pre-validation and post-validation concerns in my actual code without referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. I put it in my root assembly for my application framework, and Enforce is at the root, so it is very easy to get to.
我的很多测试都围绕着加载一个具有已知良好状态的对象(如 CuttingPath)。执行测试,然后将结果与加载的对象进行比较。 如果它们不同,那么“发生了”一些事情导致代码发生变化。
这种方法可以节省大量时间,并允许在需要时进行自定义比较。
A lot of my tests revolve around loading a object that (like a CuttingPath) with a known good state.. Performing the test, then comparing the result to the loaded object. If they differ then somehthing 'happened' to cause a change in the code.
This approach save tons of time and allow for custom comparisons when needed.
我认为,如果您重构测试以减少重复,那么您最终会创建自己的框架作为副产品,当然您的测试框架将具有在您的上下文中有意义的断言助手。
我想到的一个例子是,当测试 xhtml 报告时,我们最终得到的测试看起来像:
断言覆盖率后面的内容是这样的:
然后后面是使用 xpath 获取值的东西和另一种知道格式的方法用于百分比。
I think that if you're refactoring your tests to reduce duplication then you end up creating your own framework as a by-product, and of course your test framework will have assert helpers that make sense in your context.
An example I have in mind was when testing xhtml report we ended up with tests that looked something like:
where behind assert coverage was something like:
and then behind that was something that used xpath to get the value and another method that knew what the formatting was for percentages.
我刚刚添加了一个实现 ImageAssert 正如我上面所写(在我的问题中)
我很高兴听到更多此类样本
I've just added an implementation to ImageAssert as I wrote above (in my question)
I would be glad to hear more of that kind of samples