Внедрение NUnit-тестов в проект - C#
Формулировка задачи:
Добрый день. Написал программу, теперь интересует как внедрить в нее NUnit тесты. Прочитал статей, ничего толком не понял. Цель: добавить unit тесты class equation: 1к, 2к вещ, 2к комп, а=0, ввод чисел
Прошу помочь разобраться новичку.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using System.Globalization;//system.globalization - для конверт ту сингл, иначе точку не распознает
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
equation eq = new equation();
eq.print();
eq.calculate();
Console.ReadKey();
}
}//class program
[TestFixture]
class equation
{
private float a;
private float b;
private float c;
public equation()
{
a = input("a", "Введите первый коэффициент, не равный нулю:");
b = input("b", "Введите второй коэффициент, не равный нулю:");
c = input("c", "Введите третий коэффициент:");
}
public void print()
{
Console.WriteLine("Полученное уравнение: {0}a^2+{1}b+{2}",a,b,c);
}
public void calculate()
{
double d;
d = b * b - 4 * a * c;
Console.WriteLine("Дискриминант равен {0}",d);
if (d<0)// с комплексными - сделано
{
Console.WriteLine("x1 = ({0}+({1}^(1/2))*i)/(2*{2})", -b, d, a);
Console.WriteLine("x2 = ({0}-({1}^(1/2))*i)/(2*{2})", -b, d, a);
}
else if (d==0)
{
if(-b/(2*a) % 1 == 0)
Console.WriteLine("x = {0}", -b/(2*a));
else
Console.WriteLine("x = {0}/{2}",-b,2*a);
}
else if (d > 0)
{
if(((-b+Math.Sqrt(d))/(2*a)) % 1 == 0)
Console.WriteLine("x1 = {0}",(-b+Math.Sqrt(d))/(2*a));
else
Console.WriteLine("x1 = ({0})/({1})", (-b + Math.Sqrt(d)), a * 2);
if(((-b-Math.Sqrt(d))/(2*a)) % 1 == 0)
Console.WriteLine("x2 = {0}",(-b-Math.Sqrt(d))/(2*a));
else
Console.WriteLine("x2 = ({0})/({1})", (-b - Math.Sqrt(d)), a * 2);
}
}
//проверяет коэффициенты на соответствие требованиям.
private float input(string name, string msg)
{
Console.WriteLine(msg);
string temp = Console.ReadLine();
const string regexp = @"[0-9]*([\.,]?[0-9]*)?";
const string regexp2 = @"0*([\.,]?0*)?";
while ((name != "c" && (regCheck(temp, regexp2) || !regCheck(temp, regexp))) ||
(name == "c" && !regCheck(temp, regexp)))
{
Console.WriteLine("Ошибка!\nЗначение {0} введено неверно!\n" +
"Введите верное число, или q для выхода:",name);
if(temp == "q")
Environment.Exit(0);
temp = Console.ReadLine();
}
return Convert.ToSingle(temp, new CultureInfo("en-US"));
}
//Костыль на регулярки.длина совпадения будет не равна длине переданной строки, вернет фалс
private bool regCheck(string str, string regexp)
{
Match m = Regex.Match(str, regexp);
if(m.Length != str.Length)
return false;
else
return true;
}
}
}Решение задачи: «Внедрение NUnit-тестов в проект»
textual
Листинг программы
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ArgZero()
{
equation eq = new equation(0f, 3f, 1f);
var test = eq.calculate();
}