简体   繁体   English

如何在xUnit中对类的多个实例进行单元测试

[英]How To Unit Test Multiple Instances Of Class In xUnit

I'm fairly new to unit testing. 我是单元测试的新手。 I understand the concept, but I keep find myself bumping into walls with the execution. 我理解这个概念,但是我发现自己在执行过程中碰壁。 How, in xUnit, do I test multiple instances of a class? 如何在xUnit中测试一个类的多个实例? For instance, say I have a constructor with a signature: 例如,假设我有一个带有签名的构造函数:

public FileWatcher(string path = "", bool startWatching = true)

I want to run all of my unit tests for each public property and method that I'm testing against multiple instances of FileWatcher , each with the constructor passed different arguments. 我想针对要针对FileWatcher多个实例测试的每个公共属性和方法运行所有单元测试,每个实例的构造函数都传递了不同的参数。 This way I can run all of my tests against an instance of FileWatcher where no arguments were passed in, and then one where arguments were. 这样,我可以对没有传递任何参数的FileWatcher实例然后传递参数的实例运行所有测试。 I don't know if what I'm trying to do reeks of code smell or if I don't know the correct nomenclature to turn up any results, but I can't find anything on this subject. 我不知道我要尝试执行的代码臭味是什么,还是我不知道正确的术语来得出任何结果,但是我在这个主题上找不到任何东西

I believe, you should think about your approach. 我相信,您应该考虑一下自己的方法。 From the signature, I think your FileWatcher should/would/does behave differently when path is null, when file exists, when the file does not exist. 从签名来看,我认为您的FileWatcherpath为null,文件存在时,文件不存在时应该/应该/会有所不同。

There is not much point in testing the same scenario over and over, because you're basically running the same test. 一遍又一遍地测试相同的场景没有多大意义,因为您基本上是在运行相同的测试。

You should try to find different edge cases and ideally create one test for each. 您应该尝试查找不同的边缘情况,并理想地为每个情况创建一个测试。

That said, it is possible to create Theories in xUnit, these are tests with parameters. 也就是说,可以在xUnit中创建理论,这些是带有参数的测试。 I would suggest xUnit Theory: Working With InlineData, MemberData, ClassData , this helped me. 我会建议xUnit理论:使用InlineData,MemberData,ClassData可以帮助我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM