Advertisement
Old_But_Gold

C# Unit Tests

Sep 1st, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. NUnit
  2.  
  3. public class Task1Tests
  4. {
  5. Calculator _calculator;
  6.  
  7. [SetUp]
  8. public void SetUp() => _calculator = new();
  9.  
  10. [Test]
  11. [TestCase(2, 3, 5)]
  12. [TestCase(0, 0, 0)]
  13. [TestCase(-2, 3, 1)]
  14. public void AddTest(int num1, int num2, int expectedValue)
  15. {
  16. var result = _calculator.Add(num1, num2);
  17. Assert.AreEqual(expectedValue, result);
  18. }
  19.  
  20. [Test]
  21. [TestCase(2, 3, -1)]
  22. [TestCase(0, -1, 1)]
  23. [TestCase(1000, 200, 800)]
  24. public void SubTest(int num1, int num2, int expectedValue)
  25. {
  26. var result = _calculator.Sub(num1, num2);
  27. Assert.AreEqual(expectedValue, result);
  28. }
  29.  
  30. [Test]
  31. [TestCase(2, 3, 6)]
  32. [TestCase(0, 100, 0)]
  33. [TestCase(-2, -20, 40)]
  34. public void MultiplyTest(int num1, int num2, int expectedValue)
  35. {
  36. var result = _calculator.Multiply(num1, num2);
  37. Assert.AreEqual(expectedValue, result);
  38. }
  39.  
  40. [Test]
  41. [TestCase(2, 3, 0)]
  42. [TestCase(0, -1, 0)]
  43. [TestCase(1000, 200, 5)]
  44. public void DivideTest(int num1, int num2, int expectedValue)
  45. {
  46. var result = _calculator.Sub(num1, num2);
  47. Assert.AreEqual(expectedValue, result);
  48. }
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. XUnit
  59.  
  60. public class Task1Tests
  61. {
  62. private Calculator _calculator = new();
  63.  
  64. [Fact]
  65. public void AddTest()
  66. {
  67. Assert.Equal(5, _calculator.Add(2, 3));
  68. Assert.Equal(0, _calculator.Add(0, 0));
  69. Assert.Equal(1, _calculator.Add(-2, 3));
  70. }
  71.  
  72. [Fact]
  73. public void SubTest()
  74. {
  75. Assert.Equal(-1, _calculator.Sub(2, 3));
  76. Assert.Equal(1, _calculator.Sub(0, -1));
  77. Assert.Equal(800, _calculator.Sub(1000, 200));
  78. }
  79.  
  80. [Fact]
  81. public void MultiplyTest()
  82. {
  83. Assert.Equal(6, _calculator.Multiply(2, 3));
  84. Assert.Equal(0, _calculator.Multiply(0, 100));
  85. Assert.Equal(40, _calculator.Multiply(-2, -20));
  86. }
  87.  
  88. [Fact]
  89. public void DivideTest()
  90. {
  91. Assert.Equal(0, _calculator.Divide(2, 3));
  92. Assert.Equal(0, _calculator.Divide(0, -1));
  93. Assert.Equal(5, _calculator.Divide(1000, 200));
  94. }
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. MSUnit
  103.  
  104. [TestClass]
  105. public class Task1Tests
  106. {
  107. Calculator _calculator;
  108.  
  109. [TestInitialize]
  110. public void TestInitialize() => _calculator = new();
  111.  
  112. [TestMethod]
  113. [DataRow(2, 3, 5)]
  114. [DataRow(0, 0, 0)]
  115. [DataRow(-2, 3, 1)]
  116. public void AddTest(int num1, int num2, int expectedValue)
  117. {
  118. var result = _calculator.Add(num1, num2);
  119. Assert.AreEqual(expectedValue, result);
  120. }
  121.  
  122. [TestMethod]
  123. [DataRow(2, 3, -1)]
  124. [DataRow(0, -1, 1)]
  125. [DataRow(1000, 200, 800)]
  126. public void SubTest(int num1, int num2, int expectedValue)
  127. {
  128. var result = _calculator.Sub(num1, num2);
  129. Assert.AreEqual(expectedValue, result);
  130. }
  131.  
  132. [TestMethod]
  133. [DataRow(2, 3, 6)]
  134. [DataRow(0, 100, 0)]
  135. [DataRow(-2, -20, 40)]
  136. public void MultiplyTest(int num1, int num2, int expectedValue)
  137. {
  138. var result = _calculator.Multiply(num1, num2);
  139. Assert.AreEqual(expectedValue, result);
  140. }
  141.  
  142. [TestMethod]
  143. [DataRow(2, 3, 0)]
  144. [DataRow(0, -1, 0)]
  145. [DataRow(1000, 200, 5)]
  146. public void DivideTest(int num1, int num2, int expectedValue)
  147. {
  148. var result = _calculator.Divide(num1, num2);
  149. Assert.AreEqual(expectedValue, result);
  150. }
  151. }
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement