Advertisement
Levi0227

2. félév 4. óra NUnit Tests

Mar 5th, 2024 (edited)
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | Source Code | 0 0
  1. namespace ConsoleApp1
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.  
  8.         }
  9.     }
  10. }
  11.  
  12. ---------------------------
  13. Osztáylok
  14. ---------------------------
  15.  
  16. <Project Sdk="Microsoft.NET.Sdk">
  17.  
  18.   <PropertyGroup>
  19.     <TargetFramework>net6.0</TargetFramework>
  20.     <ImplicitUsings>enable</ImplicitUsings>
  21.     <Nullable>enable</Nullable>
  22.  
  23.     <IsPackable>false</IsPackable>
  24.   </PropertyGroup>
  25.  
  26.   <ItemGroup>
  27.     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
  28.     <PackageReference Include="NUnit" Version="3.13.3" />
  29.     <PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
  30.     <PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
  31.     <PackageReference Include="coverlet.collector" Version="3.1.2" />
  32.   </ItemGroup>
  33.  
  34.   <ItemGroup>
  35.     <ProjectReference Include="..\ConsoleApp1\ConsoleApp1.csproj" />
  36.   </ItemGroup>
  37.  
  38. </Project>
  39.  
  40. ---------------------------
  41.  
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. using System.Text;
  46. using System.Threading.Tasks;
  47.  
  48. namespace ConsoleApp1
  49. {
  50.     public class BMath
  51.     {
  52.         public int SumOfTwoNumbers(int x, int y)
  53.         {
  54.             return x + y;
  55.         }
  56.        
  57.         public int SubtractFromX(int x, int y)
  58.         {
  59.             return x - y;
  60.         }
  61.     }
  62. }
  63.  
  64. ------------------------
  65.  
  66. using System;
  67. using System.Collections.Generic;
  68. using System.Linq;
  69. using System.Text;
  70. using System.Threading.Tasks;
  71.  
  72. namespace ConsoleApp1
  73. {
  74.     public class PrimeTool
  75.     {
  76.         private int number;
  77.  
  78.         public PrimeTool(int number)
  79.         {
  80.             this.number = number;
  81.         }
  82.  
  83.         public bool IsPrime()
  84.         {
  85.             if (this.number == 1) return false;
  86.             if (this.number == 2) return true;
  87.  
  88.             for (int i = 2; i <= Math.Ceiling(Math.Sqrt(number)); ++i)
  89.             {
  90.                 if (number % i == 0)
  91.                 {
  92.                     return false;
  93.                 }
  94.             }
  95.  
  96.             return true;
  97.         }
  98.     }
  99. }
  100.  
  101.  
  102. -------------------------------------------
  103.  
  104.  
  105. using ConsoleApp1;
  106. using NUnit.Framework;
  107.  
  108. namespace TestProject1
  109. {
  110.     public class Tests
  111.     {
  112.         [TestCase(3, 3)]
  113.         [TestCase(2, 2)]
  114.         [TestCase(1, 1)]
  115.         public void TestSumOfNumbers(int x, int y)
  116.         {
  117.             BMath bMath = new BMath();
  118.  
  119.             Assert.AreEqual(x + y, bMath.SumOfTwoNumbers(x, y));
  120.         }
  121.        
  122.         [TestCase(3, 3)]
  123.         [TestCase(2, 2)]
  124.         [TestCase(1, 1)]
  125.         public void TestSubtractFromX(int x, int y)
  126.         {
  127.             BMath bMath = new BMath();
  128.  
  129.             Assert.AreEqual(x - y, bMath.SubtractFromX(x, y));
  130.         }
  131.        
  132.         [TestCase(3, true)]
  133.         [TestCase(2, true)]
  134.         [TestCase(1, false)]
  135.         [TestCase(9, false)]
  136.         [TestCase(17, true)]
  137.         public void TestIsPrime(int number, bool expectedResults)
  138.         {
  139.             PrimeTool primeTool = new PrimeTool(number);
  140.  
  141.             Assert.AreEqual(expectedResults, primeTool.IsPrime());
  142.         }
  143.  
  144.         [TestCase(3)]
  145.         [TestCase(2)]
  146.         [TestCase(17)]
  147.         public void TestIsPrimeTrue(int number)
  148.         {
  149.             PrimeTool primeTool = new PrimeTool(number);
  150.  
  151.             Assert.True(primeTool.IsPrime());
  152.         }
  153.  
  154.         [TestCase(1)]
  155.         [TestCase(9)]
  156.         [TestCase(4)]
  157.         public void TestIsPrimeFalse(int number)
  158.         {
  159.             PrimeTool primeTool = new PrimeTool(number);
  160.  
  161.             Assert.False(primeTool.IsPrime());
  162.         }
  163.        
  164.          [TestCase(new int[] {2, 3, 5}, 10)]
  165.         [TestCase(new int[] {2, 2, 2}, 6)]
  166.         [TestCase(new int[] {1, 3, 4}, 8)]
  167.         [TestCase(new int[] {5, 5, 5}, 15)]
  168.         [TestCase(new int[] {0, 1, 6}, 7)]
  169.         public void TestTotal(int[] numbers, int expectedResults)
  170.         {
  171.             ArrayStatistics arrayStatistics = new ArrayStatistics(numbers);
  172.  
  173.             Assert.AreEqual(expectedResults, arrayStatistics.Total());
  174.         }
  175.     }
  176. }
  177.  
  178. ----------------------------------------
  179.  
  180. using System;
  181. using System.Collections.Generic;
  182. using System.Linq;
  183. using System.Text;
  184. using System.Threading.Tasks;
  185.  
  186. namespace ConsoleApp1
  187. {
  188.     public class ArrayStatistics
  189.     {
  190.         private int[] array;
  191.  
  192.         public ArrayStatistics(int[] array)
  193.         {
  194.             this.array = array;
  195.         }
  196.  
  197.         public int Total()
  198.         {
  199.             int sum = 0;
  200.             for (int i = 0; i < array.Length; i++)
  201.             {
  202.                 sum += array[i];
  203.             }
  204.  
  205.             return sum;
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement