litt unit testing

This commit is contained in:
Geir Okkenhaug Jerstad 2025-01-02 13:09:14 +01:00
parent c00065c1bc
commit 3cbfff200e
6 changed files with 141 additions and 0 deletions

View file

@ -0,0 +1,11 @@
namespace UnitTesting.Test;
public class StatsTest
{
[Test]
public void TestWith3And4()
{
var stats = new Stats();
Assert.Pass();
}
}

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UnitTesting\UnitTesting.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "UnitTesting\UnitTesting.csproj", "{DB18A478-4E45-453D-9AB4-64D1DE10828D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting.Test", "UnitTesting.Test\UnitTesting.Test.csproj", "{0EE8BB76-7CF5-49F1-808D-5684B28D482A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DB18A478-4E45-453D-9AB4-64D1DE10828D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB18A478-4E45-453D-9AB4-64D1DE10828D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB18A478-4E45-453D-9AB4-64D1DE10828D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB18A478-4E45-453D-9AB4-64D1DE10828D}.Release|Any CPU.Build.0 = Release|Any CPU
{0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,22 @@
using System;
namespace UnitTesting
{
internal class Program
{
static void Main(string[] args)
{
var stats = new Stats();
while (true)
{
Console.WriteLine("Please enter a number:");
var numberStr = Console.ReadLine();
var number = Convert.ToInt32(numberStr);
stats.Add(number);
Console.WriteLine(stats.GetDescription());
}
}
}
}

View file

@ -0,0 +1,48 @@
using System.Security;
namespace UnitTesting;
public class Stats
{
public int Count {get; private set;}
public int Sum {get; private set;}
public int Max {get; private set;}
public int Min {get; private set;}
public float Mean => (float)Sum / Count;
public void Add(int number)
{
if (Max == -1 || number > Max) Max = number;
if (Min == -1 || number < Min) Min = number;
Count++;
Sum += number;
}
public string GetDescription()
{
return
Format("Antall tall", Count) +
Format("Sum", Sum) +
Format("Max", Max) +
Format("Min", Min) +
Format("Gjennomsnitt", Mean);
}
private static string Format(string label, float number)
{
return FormatImpl(label, number.ToString("####.##"));
}
private static string Format(string label, int number)
{
return FormatImpl(label, number.ToString("####"));
}
private static string FormatImpl(string label, string number)
{
return label.PadRight(12, ' ') + ": " + number + '\n';
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>