简体   繁体   English

单元测试-这里有多少个测试用例

[英]Unit testing - how many test cases here

I have a method that takes an array as an argument, and returns true or false depending on the presence of a particular value. 我有一个方法,将数组作为参数,并根据特定值的存在返回true或false。

In this scenario how many test cases should be written? 在这种情况下,应该编写多少个测试用例?

I think 3: 我认为3:

  1. If the value is present 如果值存在
  2. If the value is not present 如果该值不存在
  3. If the array is empty (could be covered by 2 though?? ) 如果数组为空(尽管可以被2覆盖???)

I can think of 3 test cases: 我可以想到3个测试用例:

  1. If the array is not empty (or not null) 如果数组不为空(或不为null)
  2. If the value is valid or not (I can pass an object where it expects a string :) ) 如果值有效或无效(我可以在需要字符串的地方传递一个对象:)
  3. If the value is present in array 如果值存在于数组中

It is the code of the function you want to test, so you cannot tell how many test cases are useful. 它是您要测试的功能的代码,因此您无法确定有多少测试用例有用。 Think again what your code does, how will the value be found? 再想一想您的代码做什么,将如何找到该值?

An example: If your code tries to find a value with a certain name, and you make a string comparison, then think of the problems that can arise with string comparisons -> should the key be found case (in)sensitive, is null equal to an empty string, how does it handle duplicates and are other types converted correctly to strings (type juggling)? 示例:如果您的代码尝试查找具有特定名称的值,并且进行了字符串比较,那么请考虑一下字符串比较可能引起的问题->如果发现键区分大小写(不区分大小写),则为null等于转换为空字符串,它如何处理重复项,其他类型是否正确转换为字符串(类型变戏法)?

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

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