0

I've read this article and a couple of others about testing and I think I've understand the theory but not the implementation in a real .NET project.

My project is decomposed in multiple (functional) assemblies and I wish to test it. Most of my methods and properties are private or internal so I can't access them from my test assembly. I only test the public one (in order to test all the paths of my assembly).

I'm wondering about the test I'm doing because these tests aren't unit-test because I'm using other services like a database, or other services like Microsoft HPC... Am I right ? In my opinion, if I want to do some unit test, I have to test private methods and properties, thing that is controversial.

I'm using Nunit and I was wondering if I could put integration test AND unit test in the framework ?

Is there any .NET opensource project with unit test and acceptance test that I could check out ?

Thanks

4

2 回答 2

1

许多问题...

public/private:通常,您只想通过公共接口进行测试。如果您想测试私有方法,这可能表明您应该将类​​分成两个并让私有方法变为公共(或内部)。建议零售价。要测试内部方法,请检查此答案

在编写测试时,您想测试系统的行为。对于行为,考虑从最终用户的角度来看系统的行为方式(尽管也可能有其他有趣的利益相关者)。如果你这样做,你很少会遇到想要测试私有方法的问题。

单元测试集成测试之间的区别是有争议的,在我看来,这不是一个非常重要的争论。更重要的是问问自己,考试的目的是什么?

如果您的目的是测试您的系统(而不是与外部系统的集成),那么您的测试应该是可重复的、快速的并且不包括您不拥有的任何东西。可重复我的意思是,如果测试失败一次,它应该总是失败(没有随机性)。快速我的意思是你不应该等待太久运行所有测试的反馈。就您所拥有的而言,我的意思是测试不应该由于您组织外部的更改而开始失败。如果您可以实现这一点并且仍然包括对数据库和/或 Microsoft HPC 的调用,那么我看不出有任何不这样做的理由。

要区分NUnit中不同的测试类型,您可以使用categories。大多数工具允许您运行基于这些的子集。

于 2013-08-15T11:13:20.500 回答
0

抱歉,我必须干预 Torbjörns 的回答。

单元测试和集成测试有很大的区别,不能只用类别来处理。想想集成测试中需要的所有引用或依赖项。曾经尝试在构建机器上运行集成测试的人可以告诉你这个故事。

经验法则:永远不要混淆单元测试和集成测试。您可以重复使用相同的测试运行程序,例如 NUnit,但您必须拆分测试项目。

PS:欢迎来到TDD之​​旅!

于 2016-07-20T22:26:54.547 回答