Skip to content

Hw3 tests for stack#5

Open
AnNyiiik wants to merge 12 commits intomainfrom
HW3_TestsForStack
Open

Hw3 tests for stack#5
AnNyiiik wants to merge 12 commits intomainfrom
HW3_TestsForStack

Conversation

@AnNyiiik
Copy link
Copy Markdown
Owner

No description provided.

@@ -0,0 +1,53 @@
namespace HW2_Calculator.Tests;

public class Tests
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Файл принято именовать так же как и класс, который в нем расположен.

Название Tests для класса не очень отражает суть содержимого. Лучше StackImplementationTests для тестов реализации стека или StackCalculatorTests для тестов калькулятора.

Тесты для реализаций стека и для калькулятора лучше разместить в разных классах. Так если тест упадет, можно будет быстрее понять, баг в реализации стека или калькулятора

private IStack stack;
private StackCalculator stackCalculator;

private static readonly string[] testCasesTrue = { "12 8 - 3 *", "-4 9 + 8 * 4 /", "13 7 / 9 +" };
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В NUnit есть такая штука как TestCase, стоит ей воспользоваться

{
private IStack stack;
private StackCalculator stackCalculator;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ваш стек может не только c int-ами, но и с double-ами работать, такие тесты тоже нужны. Еще нужны тесты с отрицательными значениями и нулем в качестве операндов

private static readonly string[] testCasesTrue = { "12 8 - 3 *", "-4 9 + 8 * 4 /", "13 7 / 9 +" };
private static readonly double[] correctAnswers = { 12.0, 10.0, 10.85714};

private static readonly string[] testCasesFalse = { "", "10 8 - 0 /", "10 8 - 5", "+" };
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А еще нужны тесты с отрицательными значениями, null в качестве аргумента для метода Calculate калькулятора

[Test]
public void TestTrue()
{
for (var i = 0; i < 2; ++i)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Магических чисел вроде 2 в коде быть не должно - это известный антипаттерн

var result = stackCalculator.Calculate(testCasesTrue[j]);
Assert.True(result.Item1);
Assert.True(Math.Abs((double)result.Item2 - correctAnswers[j]) < delta);
stack.Clear();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

После вызова этого метода хорошо бы проверить, что стек действительно стал пустым

for(var j = 0; j < testCasesTrue.Length; ++j)
{
var result = stackCalculator.Calculate(testCasesTrue[j]);
Assert.True(result.Item1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Разработчики NUnit рекомендуют использовать Assert.That вместо Assert.True и Assert.False

}

[Test]
public void TestTrue()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test обычно добавляют как постфикс к названию теста, например, PossibleToPushToEmptyStackTest. Тест должен тестировать какой-то конкретный case, например, получается ли добавить элемент в пустой стек

foreach(var expression in testCasesFalse)
{
var result = stackCalculator.Calculate(expression);
Assert.False(result.Item1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используйте Assert.That вместо Assert.False

var result = stackCalculator.Calculate(expression);
Assert.False(result.Item1);
stack.Clear();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У вас еще в этом файле 3 warnings, nullability-анализ на вас ругается. Поправьте, пожалуйста :)
В сданной задаче warnings быть не должно

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants