简体   繁体   English

单元测试构造函数时有多个断言?

[英]Multiple assertions when unit testing constructor?

I'm trying to unit test a class with 2 constructors. 我正在尝试用2个构造函数对一个类进行单元测试。 Each constructor has multiple parameters that set public properties. 每个构造函数都有多个参数来设置公共属性。 My question is, should I have only 2 unit tests with multiple asserts to check that each property was set OR a test for each parameter for each constructor? 我的问题是,我应该只有2个单元测试和多个断言来检查每个属性是否已设置或每个构造函数的每个参数的测试?

Public Person(string name, string phone, string birthday)
{
   name = name;
   phone = phone;
   birthday = birthday;
}

Public Person(string name) : this(name, null, null)
{}

I've never been a fan of the dogma of "only a single assertion per test." 我从未成为“每次测试只有一个断言”的教条的粉丝。 It just doesn't seem practical to me - you end up with a lot of fluff (test declarations) around what you're actually interested in. 这对我来说似乎并不实际 - 你最终会对你真正感兴趣的内容产生很多不满(测试声明)。

Yes, if you've got multiple issues you'll only have one test failure. 是的,如果您遇到多个问题,那么您只会遇到一次测试失败。 You fix the test, run it again, spot the next failure, fix that and repeat until it succeeds. 您修复测试,再次运行它,发现下一个故障,修复它并重复直到成功。 No great loss. 没有很大的损失。

I'm not saying that you should be testing huge amounts of functionality in each test - but going to the other extreme isn't pragmatic either. 我不是说你应该在每次测试中测试大量的功能 - 但是走向另一个极端也不是务实。

I would normally only go for one error condition per test though - so if your constructors would actually throw exceptions for null arguments, I'd check each of those in a separate test. 我通常只会针对每个测试使用一个错误条件 - 所以如果你的构造函数实际上会抛出null参数的异常,我会在单独的测试中检查每个错误。 It's easy to accidentally miss something otherwise. 否则很容易意外遗漏。

The operation that you are testing is that the constructor accepts __ parameters and that the values are set to the proper value. 您正在测试的操作是构造函数接受__参数,并将值设置为正确的值。

Therefore I would say 1 test per constructor, with multiple asserts on each, to ensure that all members are properly set. 因此,我会说每个构造函数有1个测试,每个构造函数都有多个断言,以确保正确设置所有成员。

You might be interested to see what Kent Beck himself said, right here on Stack Overflow. 你可能有兴趣看看Kent Beck自己说的是什么,就在Stack Overflow上。 He said... 他说...

I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). 我得到的代码是有效的,而不是测试,所以我的理念是尽可能少地测试以达到给定的置信水平(我怀疑这种信心水平与行业标准相比很高,但这可能只是傲慢) 。 If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it. 如果我通常不会犯一个错误(比如在构造函数中设置错误的变量),我不会测试它。 I do tend to make sense of test errors, so I'm extra careful when I have logic with complicated conditionals. 我确实倾向于理解测试错误,所以当我有复杂条件的逻辑时我会格外小心。 When coding on a team, I modify my strategy to carefully test code that we, collectively, tend to get wrong. 在对团队进行编码时,我会修改策略以仔细测试我们共同出错的代码。

Here's the link. 这是链接。

I have no problem admitting that made me re-think some things I was doing. 我没有问题,承认这让我重新思考一些我正在做的事情。 And for the better. 为了更好。

I second the recommendation of not having one test per property, but one test with several asserts for each constructor. 我建议不要对每个属性进行一次测试,而是针对每个构造函数进行一次测试。

I would add that, depending on where yor class will be used, you may want to have also a test to verify the behavior of your constructor when the name parameter is a null string (negative testing). 我想补充一点,根据yor类的使用位置,你可能还希望在name参数为空字符串时进行测试以验证构造函数的行为(否定测试)。

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

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