如何测试逻辑树-10中有1个意外的测试结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何测试逻辑树-10中有1个意外的测试结果相关的知识,希望对你有一定的参考价值。
我已经提交了一个程序来满足业务需求声明,但是我得到的反馈是每10个输入中就有1个是错误的。我不确定哪些输入错误或如何识别它们。
Pepi打电话给您,并要求您编写一个在控制台上打印特定结果的程序。在几场足球比赛之后,他注意到球员在某些分钟内总是错过点球,得到黄牌,进球,得到任意球或错过角球。
登录
控制台读取两行:
- 第一行-分钟-区间[0…10000]中的正整数
- 第二行-玩家名称-文本
退出
在第一行中,打印:
- “比赛刚刚开始!” -如果分钟为0。
- “上半年。” -如果分钟数少于45。
- “第二个半场。” -如果分钟数为45分钟或更多。
在以下行中打印:
- 如果分钟数在1到10之间,包括:“ player name没受处罚。”
- 如果分钟数同时是偶数,请打印:
- “ 玩家名称受罚后受伤。”
- 如果分钟数大于10且小于或等于35:“ 玩家名称收到了一张黄牌。”
- 如果分钟数同时是一个奇数,请打印:
- “ 玩家名称得到了另一张黄牌。”
- 如果分钟数大于35且小于45:“ 玩家名称得分了!!”
- 如果分钟数大于45且小于或等于55。“ 玩家名称有一个任意球。”
- 如果分钟数同时是偶数,请打印:
- “ 玩家名称错过了任意球。”
- 如果分钟数大于55且小于或等于80。“ player name投丢了角球。”
- 如果分钟数同时是一个奇数,
- “ 玩家名称已被其他玩家更改。”
- 如果分钟数大于80且小于或等于90:“ 玩家名称从罚中获得进球!”
示例输入和输出
Login Exit Explanation
85
Harry Kane Second half time.
Harry Kane SCORED A GOAL FROM PENALTY !!! The minutes are over 45 and we print the first line, then we see that they are in the range of 80 to 90 and we print the second line.
10
Messi First half time.
Messi missed a penalty.
Messi was injured after the sentence.
25
Ronaldo First half time.
Ronaldo received a yellow card.
Ronaldo got another yellow card.
以下是我的程序,但是10个测试输入中有1个给出了错误的答案。我不知道输入内容本身,但是如果您有所帮助,那将意味着很多。
string name;
minutes = Convert.ToInt32(Console.ReadLine());
name = Console.ReadLine();
if (minutes == 0) Console.WriteLine("Match has just began!");
else if (minutes < 45) Console.WriteLine("First half time.");
else Console.WriteLine("Second half time.");
if (minutes == 0) placeholder = 0;
else if (minutes > 0 && minutes <= 10) Console.WriteLine(name + " missed a penalty."); if (minutes % 2 == 0) Console.WriteLine(name + " was injured after the penalty.");
else if (minutes <= 35) Console.WriteLine(name + " received yellow card."); if (minutes % 2 != 0) Console.WriteLine(name + " got another yellow card.");
else if (minutes <= 45) Console.WriteLine(name + " SCORED A GOAL !!!");
else if (minutes <= 55) Console.WriteLine(name + " got a freekick."); if (minutes % 2 == 0) Console.WriteLine(name + " missed the freekick.");
else if (minutes <= 80) Console.WriteLine(name + " missed a shot from corner."); if (minutes % 2 != 0) Console.WriteLine(name + " has been changed with another player.");
else if (minutes <= 90) Console.WriteLine(name + " SCORED A GOAL FROM PENALTY !!!");
欢迎您!而不是发布家庭作业,教程或作业中的确切问题,您应该专注于Minimum Reproducible Example,突出显示您想解决的特定问题。现实世界中的一个单一来源的问题可能会导致在SO上发布多个孤立的问题,隔离您的示例的过程通常会导致更多的理解和解决方案本身。
您的评论您知道“ 10个测试输入中的1个给出了错误的答案”,但是您不知道是哪个,这一事实表明您没有尝试自己运行此代码进行组合在示例文本之外。
起初,不清楚您的问题是什么,这就是为什么这个问题被否决这么多,所以我将通过一路分享我的想法来帮助您解决这个问题。
当我运行您的代码时,输出几乎与示例匹配,我认为您的示例虽然有一些错误,所以此输出看起来正确:
85
Harry Kane
Second half time.
Harry Kane SCORED A GOAL FROM PENALTY !!!
10
Messi
First half time.
Messi missed a penalty.
Messi was injured after the penalty.
25
Ronaldo
First half time.
Ronaldo received yellow card.
Ronaldo got another yellow card.
但是现在您需要发挥创造力,并遵循问题中指定的每个标准,并证明您可以获得正确的输出。
[您已经证明了自己知道如何从输入中捕获名称并将其用于控制台输出中,并且还证明了if
分支中的隔离性,所以唯一的问题实际上是如何提出一种测试逻辑中其他分支的好方法。
对于这样的逻辑场景,我总是尝试制定一个测试工具,或涵盖所有边界条件的单元测试。这可能是一个乏味的过程,但是使用大于,小于和等于进行比较是唯一可以100%确定您的逻辑正确的方法。]
Unit Testing
和Test Harnesses是可用于自动化测试的技术,它是代码或运行您的代码并提供特定输入并将应用程序的输出与预期输出进行比较的应用程序。让我们建立一个预期输出矩阵,但为简单起见,我们只关注时间输入。 (如上所述,您已经覆盖了名称注入)因此,确定时间的所有边界条件,包括最小值和最大值,并写出预期的输出。
要测试的边界是业务需求中打印的数字,要对其进行充分测试,您需要测试该数字的任一侧,以确保您具有正确的相等性声明:
“如果分钟数在1到10之间,包括首尾”
此要求有两个边界,1
和10
,inclusive
运算符告诉我们可能的范围包括边界值,无论哪种方式,我们都应测试以下值以确认我们具有正确的边界条件:0, 1,2,9,10,11在编程中很常见的是,意外地将其关闭1,因此这种形式的测试将解决这些问题。
我们还将测试分为三个结果矩阵,一个用于第一行,因为它的结果条件与其余行互斥,一个用于第二行,因为它是一个清晰的逻辑分支,它也产生与以下结果互斥的结果在其他条件下,最后一个矩阵覆盖第三行输出。这些可以全部组合成一个矩阵,但是这使得识别哪个代码分支不正确变得更加困难。
第一行输出测试用例
| Time | Expected First Line | First Line Output | Result | |------|-------------------------|-----------------------|--------| | 0 | Match has just started! | | | | 1 | First half time. | | | | 44 | First half time. | | | | 45 | Second half time. | | | | 46 | Second half time. | | | |10000 | Second half time. | | |
第二行输出测试用例
这些测试的播放器名称将为
"Px"
,这会使输出足够小以显示在此论坛中。| Time | Expected Second Line | Second Line Output | Result | |------|-----------------------------------|-----------------------|--------| | 0 | | | | | 1 | Px missed a penalty. | | | | 9 | Px missed a penalty. | | | | 10 | Px missed a penalty. | | | | 11 | Px received a yellow card. | | | | 34 | Px received a yellow card. | | | | 35 | Px received a yellow card. | | | | 36 | Px SCORED A GOAL !!! | | | | 44 | Px SCORED A GOAL !!! | | | | 45 | | | | | 46 | Px got a freekick. | | | | 54 | Px got a freekick. | | | | 55 | Px got a freekick. | | | | 56 | Px missed a shot from the corner. | | | | 79 | Px missed a shot from the corner. | | | | 80 | Px missed a shot from the corner. | | | | 81 | Px SCORED A GOAL FROM PENALTY !!! | | | | 89 | Px SCORED A GOAL FROM PENALTY !!! | | | | 90 | Px SCORED A GOAL FROM PENALTY !!! | | | | 91 | | | | |10000 | | | |
只要上述测试条件通过,我们就可以跳过第二行语句的已知边界测试,而仅测试影响第三行的边界。但是,由于第三行条件会影响第二个值(映射为奇数或偶数),因此请使用边界并在范围内遍历一个完整的序列,以使其更彻底,或者如果要使测试用例自动化,则应在范围。如果范围足够大,我可以在其中放入一些随机测试用例。
| Time | Expected Third Line | Second Line Output | Result | |------|------------------------------------------|-----------------------|--------| | 1 | | | | | 2 | Px was injured after the penalty. | | | | 3 | | | | | 8 | Px was injured after the penalty. | | | | 9 | | | | | 10 | Px was injured after the penalty. | | | | 11 | Px got another yellow card. | | | | 12 | | | | | 13 | Px got another yellow card. | | | | 22 | | | | | 27 | Px got another yellow card. | | | | 33 | Px got another yellow card. | | | | 34 | | | | | 35 | Px got another yellow card. | | | | 36 | | | | | 44 | | | | | 45 | | | | | 46 | Px missed the freekick. | | | | 47 | | | | | 50 | Px missed the freekick. | | | | 53 | | | | | 54 | Px missed the freekick. | | | | 55 | | | | | 56 | | | | | 57 | Px has been changed with another player. | | | | 58 | | | | | 62 | | | | | 65 | Px has been changed with another player. | | | | 74 | | | | | 79 | Px has been changed with another player. | | | | 80 | | | | | 81 | | | | |10000 | | | |
这是我对这些测试矩阵的评估,我分两步提供了这一过程以演示该过程,您甚至在编写任何代码之前,都要从业务需求中首先确定预期结果。预先了解所有事实将有助于您在开始编写任何代码之前确定分支逻辑。
第一行输出测试结果
仅当时间为零时,特定的文本值才与业务需求不同
| Time | Expected First Line | First Line Output | Result | |------|-------------------------|-----------------------|--------| | 0 | Match has just started! | Match has just began! | FAIL | | 1 | First half time. | First half time.
以上是关于如何测试逻辑树-10中有1个意外的测试结果的主要内容,如果未能解决你的问题,请参考以下文章
带有 CLion 的 Boost.TEST:“测试框架意外退出”