[英]Writing a unit test for an if else statement
I am new to unit testing and fairly new to c# and would really appreciate some help with how to write a unit test to ensure the logic in the AddGrade method is working.我是单元测试的新手,对 c# 很陌生,非常感谢有关如何编写单元测试以确保 AddGrade 方法中的逻辑正常工作的一些帮助。 So basically if the grade is >=0 and <=100 then the grade is valid and it will be added, anything else isn't and it should display an error in the console.所以基本上如果等级是 >=0 并且 <=100 则等级是有效的并且将被添加,其他任何东西都不是并且它应该在控制台中显示一个错误。
I have seen another post about unit testing an if-else statement in c# and tried working this out from that but It confused me if I am honest.我在 c# 中看到了另一篇关于对 if-else 语句进行单元测试的帖子,并尝试从中解决这个问题,但如果我诚实的话,这让我感到困惑。 I have looked around online etc and tried a lot of different things to try and figure this out but I find it hard sometimes to apply peoples example to my code so I thought it best just to post my code and get some help, any help would be greatly appreciated:)我在网上四处寻找并尝试了很多不同的方法来尝试解决这个问题,但我发现有时很难将人们的示例应用到我的代码中,所以我认为最好只是发布我的代码并获得一些帮助,任何帮助都会不胜感激:)
I am using Xunit for the unit tests.我正在使用 Xunit 进行单元测试。 This project runs in the console.该项目在控制台中运行。
This is the program class with the main method这是带有main方法的程序class
using System;
using System.Collections.Generic;
namespace GradeBook
{
class Program
{
static void Main(string[] args)
{
var book = new Book("Dave's");
//add grade to the book
book.AddGrade(90.5);
book.AddGrade(80.5);
book.AddGrade(70.5);
book.AddGrade(60.5);
var stats = book.GetStatistics();
Console.WriteLine($"This Gradebooks name is {book.Name}");
Console.WriteLine($"The highest grade is {stats.High:N1}");
Console.WriteLine($"The lowest grade is {stats.Low:N1}");
Console.WriteLine($"The average grade is {stats.Average:N1}");//N1,N2 etc. is number of decimal places
}
}
}
This is the book class where the AddGrade method is I want to write the unit test for这是本书 class 我想写单元测试的 AddGrade 方法
using System;
using System.Collections.Generic;
namespace GradeBook
{
public class Book
{
public Book(string name)
{
grades = new List<double>();
Name = name;
}
public void AddGrade(double grade)
{
if (grade >= 0 && grade <= 100)
{
grades.Add(grade);
}
else
{
Console.WriteLine("Please enter a value between 0 and 100");
}
}
public Statistics GetStatistics()
{
var result = new Statistics();
result.Average = 0.0;
result.High = double.MinValue;
result.Low = double.MaxValue;
foreach (var grade in grades)
{
Console.WriteLine($"{Name} grade is: {grade}");
result.Low = Math.Min(grade, result.Low);
result.High = Math.Max(grade, result.High);
result.Average += grade;
}
result.Average /= grades.Count;
return result;
}
private List<double> grades = new List<double>() { };
public string Name;
}
}
This is the Statistics class这是统计数据 class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBook
{
public class Statistics
{
public double Average;
public double High;
public double Low;
}
}
This is where I am writing the unit test for this这是我为此编写单元测试的地方
using System;
using System.Collections.Generic;
using Xunit;
namespace GradeBook.Tests
{
public class BookTests
{
[Fact]
public void BookGradeIsValid()
{
var book = new Book("");
book.AddGrade(105);
}
}
}
I recommend that you modify your AddGrade()
method to throw an exception if an invalid grade is added:如果添加了无效成绩,我建议您修改AddGrade()
方法以引发异常:
public void AddGrade(double grade)
{
if (grade >= 0 && grade <= 100)
{
grades.Add(grade);
}
else
{
throw new ArgumentException("grade must be between 0 and 100");
}
}
You can test it like this:你可以像这样测试它:
[Fact]
public void BookGradeIsValid()
{
var book = new Book("");
Assert.Throws<ArgumentExeception>(() => book.AddGrade(101)); // should throw
Assert.Throws<ArgumentExeception>(() => book.AddGrade(-1)); // should throw
book.AddGrade(0); // should not throw
book.AddGrade(100); // should not throw
}
As a hint, don't use 105 for testing.作为提示,不要使用 105 进行测试。 Use a value directly at the boundary.直接在边界处使用值。 If you had a typo and you wrote if (grade >= 0 && grade <= 103)
instead of 100, you would not realize it with your test.如果你有一个错字并且你写的if (grade >= 0 && grade <= 103)
而不是 100,你不会在你的测试中意识到它。
Using the exception has several advantages.使用异常有几个优点。 First, you can use the Book
class also in a non-console app.首先,您也可以在非控制台应用程序中使用Book
class。 Second, you can do a custom error handling if something went wrong:其次,如果出现问题,您可以进行自定义错误处理:
int grade = 300;
try
{
book.AddGrade(grade)
}
catch(ArgumentException)
{
// so many possibilities: log to console or display a MessageBox or
// send an e-mail to system admin or add grade with a default value or ...
}
Also, you can't ignore the exception.此外,您不能忽略异常。 If you add a grade with an invalid value, your program will stop there, and you don't wonder 200 lines later in the code, why one grade is missing.如果你添加了一个无效值的成绩,你的程序将停在那里,你不会想知道代码后面的 200 行,为什么缺少一个成绩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.