简体   繁体   English

在这种情况下有多少个测试用例?

[英]How many test cases in this scenario?

I have a method which does many things, so that mean many side affects are created. 我有一个方法可以执行许多操作,因此意味着会产生许多副作用。 For instance, say a single call to REST API returns a JSON object with many fields set. 例如,说一次对REST API的调用将返回设置了许多字段的JSON对象。 In this case if we want to check for each individual fields should we have one single test method which will contain many assertEquals or should we have one single test method per field validation containing one assertEquals. 在这种情况下,如果我们要检查每个单独的字段,我们应该有一个包含多个assertEquals的单一测试方法,还是每个字段验证包含一个assertEquals的单一测试方法。

Similarly a method can have many other side affects, for eg, saving to the database, sending emails etc. In this case should I have one unit test method per side affect? 同样,一种方法可能会有很多其他副作用,例如,保存到数据库,发送电子邮件等。在这种情况下,我应该针对每种副作用使用一种单元测试方法吗?

In addition, if there are multiple test inputs per SUT methods then will this affect the decision of how many test methods are created? 另外,如果每个SUT方法有多个测试输入,那么这会影响创建多少个测试方法的决定吗?

Moreover, it might be related to story that means the story says this, this sub functionalities belong to the story in that case should not they belong to the same test method? 而且,可能与故事有关,这意味着故事是这样说的,在这种情况下,该子功能属于故事,它们是否应该属于同一测试方法? Because if the requirement changes then all the test methods per side affects need to be changed as well. 因为如果需求发生变化,那么也需要改变所有影响方方面面的测试方法。 Would that be manageable? 那可以管理吗?

In this case if we want to check for each individual fields should we have one single test method which will contain many assertEquals or should we have one single test method per field validation containing one assertEquals. 在这种情况下,如果我们要检查每个单独的字段,我们应该有一个包含多个assertEquals的单一测试方法,还是每个字段验证包含一个assertEquals的单一测试方法。

Worrying about the number of asserts in a test case is like worrying about how many lines are in a function. 担心测试用例中断言的数量就像担心函数中有多少行。 It's a first order approximation of complexity, but it's not really what you should be concerned with. 这是复杂度的一阶近似,但并不是您真正应该关注的。

What you should be concerned with is how hard that test is to maintain, and how good its diagnostics are. 您应该关心的是该测试的维护难度和诊断性能。 When the test fails, will you be able to figure out why? 如果测试失败,您能否找出原因? A lot of that depends on how good your assertEquals is with large data structures. 这很大程度上取决于您的assertEquals对大型数据结构的性能如何。

json = call_rest();
want = { ...whatever you expect... };
assertEquals( json.from_json(), want );

If that just tells you they're not equal, that's not very useful. 如果那只是告诉您它们不相等,那不是很有用。 You then have to manually go in and look at json and want . 然后,您必须手动进入并查看jsonwant If it dumps out both data structures, that's also not very useful, you have to look for the differences by eye. 如果它转储了两个数据结构,那也就不是很有用,您必须逐一寻找差异。

But if it presents a useful diff of the two data structures, that is useful. 但是,如果它表示两个数据结构的有用区别,那将是有用的。 For example, Perl's Test2 will produce diagnostics like this. 例如,Perl的Test2将产生这样的诊断信息。

use Test2::Bundle::Extended;

is { foo => 23, bar => 42, baz => 99 },
   { foo => 22, bar => 42, zip => 99 };

done_testing;

# +-------+------------------+---------+------------------+
# | PATH  | GOT              | OP      | CHECK            |
# +-------+------------------+---------+------------------+
# | {foo} | 23               | eq      | 22               |
# | {zip} | <DOES NOT EXIST> |         | 99               |
# | {baz} | 99               | !exists | <DOES NOT EXIST> |
# +-------+------------------+---------+------------------+

Then there's the question of maintenance, and again this comes down to how good your assertEquals is. 然后是维护问题,这又assertEquals您的assertEquals有多好。 A single assertEquals is easy to read and maintain. 单个assertEquals易于阅读和维护。

json = call_rest();
want = { ...whatever you expect... };
assertEquals( json.from_json(), want );

There's very little code, and want is very clear about what is expected. 有很少的代码,并且want是什么预期非常明确。

While multiple gets very wordy. 虽然倍数非常罗word。

json = call_rest();
have = json.from_json();
assertEquals( have['thiskey'], 23 );
assertEquals( have['thatkey'], 42 );
assertEquals( have['foo'], 99 );
...and so on...

It can be difficult to know which assert failed, you have to tell by line number or (if your test suite supports it) manually naming each assert which is more work and more maintenance and one more thing to get wrong. 可能很难知道哪个断言失败,您必须通过行号或(如果测试套件支持)行号来手动命名每个断言,这需要更多的工作,更多的维护和更多的出错。

OTOH individual assertEquals allow for more flexibility. OTOH个人assertEquals允许更大的灵活性。 For example, what if you only want to check certain fields? 例如,如果您只想检查某些字段怎么办? What if there are acceptable values in a range? 如果范围内有可接受的值怎么办?

# bar just has to contain a number
assert( have['bar'].is_number );

But some test suites support this via a single assert. 但是某些测试套件通过单个断言来支持这一点。

want = {
    thiskey: 23,
    thatkey: 42,
    foo:     99,
    bar:     is_number
}

assertEquals( json.from_json, want );

is_number is a special object that tells assert it's not a normal quality check, but to just check that the value is a number. is_number是一个特殊的对象,它告诉断言这不是正常的质量检查,而只是检查该值是否为数字。 If your test suite supports this style, it's generally superior to writing out a bunch of asserts. 如果您的测试套件支持这种样式,则通常优于编写一堆断言。 The declarative approach means less code to write, read, and maintain. 声明式方法意味着更少的代码编写,读取和维护。

The answer is: it depends on how good your testing tools are! 答案是:这取决于您的测试工具有多好!

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

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