如何确定单元测试的测试用例? [英] How to decide test cases for unit tests?

查看:35
本文介绍了如何确定单元测试的测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始进行单元测试,并编写了一些简短的测试来检查名为 isPrime() 的函数是否正常工作.

I'm just getting into unit testing, and have written some short tests to check if function called isPrime() works correctly.

我有一个测试来检查函数是否有效,并且有一些数字形式的测试数据和预期的返回值.

I've got a test that checks that the function works, and have some test data in the form of some numbers and the expected return value.

我应该测试多少?我如何决定测试哪个?这里的最佳做法是什么?

How many should I test? How do I decide on which to test? What's the best-practices here?

一种方法是生成 1000 个素数,然后循环遍历它们,另一种方法是只选择 4 或 5 并测试它们.正确的做法是什么?

One approach would be to generate 1000 primes, then loop through them all, another would be to just select 4 or 5 and test them. What's the correct thing to do?

推荐答案

您想检查边缘情况.您的方法应该能够处理多大的素数?这将取决于您使用的表示(类型).如果您只对小(在数论中使用时真正 相对术语)素数感兴趣,您可能正在使用 int 或 long.在您选择的表示中测试一些最大的素数.确保你也检查一些非质数.(这些更容易独立验证.)

You'd want to check edge cases. How big a prime number is your method supposed to be able to handle? This will depend on what representation (type) you used. If you're only interested in small (really relative term when used in number theory) primes, you're probably using int or long. Test a handful of the biggest primes you can in the representation you've chosen. Make sure you check some non-prime numbers too. (These are much easier to verify independently.)

当然,您还需要测试一些小数(质数和非质数)和范围中间的一些数.一把就足够了.还要确保为超出有效输入范围的数字抛出异常(或返回错误代码,以您的偏好为准).

Naturally, you'll also want to test a few small numbers (primes and non-primes) and a few in the middle of the range. A handful of each should be plenty. Also make sure you throw an exception (or return an error code, whichever is your preference) for numbers that are out of range of your valid inputs.

这篇关于如何确定单元测试的测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆