Skip to content

Commit

Permalink
add msUnit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fangdatto committed Jun 9, 2024
1 parent 7e1a6e2 commit fc5f053
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 59 deletions.
File renamed without changes.
33 changes: 33 additions & 0 deletions Solution1/TestLibrary/MyCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestLibrary
{
public class MyCalculator
{
public int Add(int a, int b)
{
return a + b;
}

public int Subtract(int a, int b)
{
return a - b;
}

public int Multiply(int a, int b)
{
return a * b;
}

public int Divide(int a, int b)
{
if (b == 0)
throw new DivideByZeroException("Cannot divide by zero.");
return a / b;
}
}
}
File renamed without changes.
14 changes: 7 additions & 7 deletions Solution1/UnitTest.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestLibrary", "ClassLibrary1\TestLibrary.csproj", "{26D6D1D3-EC61-491B-A460-264E3AB6C143}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "nUnitTestProject", "UnitTestProject\nUnitTestProject.csproj", "{E1317B48-A1F1-4C66-A546-465BB0FB92AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{E1317B48-A1F1-4C66-A546-465BB0FB92AE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestRWCore", "TestRWCore\TestRWCore.csproj", "{9989925A-0944-43F3-BD45-EFA7A401C671}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestLibrary", "TestLibrary\TestLibrary.csproj", "{3447D62F-9860-420B-B5BA-92CAD42F7363}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26D6D1D3-EC61-491B-A460-264E3AB6C143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26D6D1D3-EC61-491B-A460-264E3AB6C143}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26D6D1D3-EC61-491B-A460-264E3AB6C143}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26D6D1D3-EC61-491B-A460-264E3AB6C143}.Release|Any CPU.Build.0 = Release|Any CPU
{E1317B48-A1F1-4C66-A546-465BB0FB92AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1317B48-A1F1-4C66-A546-465BB0FB92AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1317B48-A1F1-4C66-A546-465BB0FB92AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -27,6 +23,10 @@ Global
{9989925A-0944-43F3-BD45-EFA7A401C671}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9989925A-0944-43F3-BD45-EFA7A401C671}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9989925A-0944-43F3-BD45-EFA7A401C671}.Release|Any CPU.Build.0 = Release|Any CPU
{3447D62F-9860-420B-B5BA-92CAD42F7363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3447D62F-9860-420B-B5BA-92CAD42F7363}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3447D62F-9860-420B-B5BA-92CAD42F7363}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3447D62F-9860-420B-B5BA-92CAD42F7363}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
89 changes: 89 additions & 0 deletions Solution1/UnitTestProject/TestCalculatorMS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestLibrary;
using System;

namespace UnitTestProject
{
[TestClass]
public class TestCalculatorMS
{
private MyCalculator _calculator;

Check warning on line 10 in Solution1/UnitTestProject/TestCalculatorMS.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Non-nullable field '_calculator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 10 in Solution1/UnitTestProject/TestCalculatorMS.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Non-nullable field '_calculator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

[TestInitialize]
public void Setup()
{
_calculator = new MyCalculator();
}

[TestMethod]
public void Add_ShouldReturnCorrectSum()
{
// Arrange
int a = 5;
int b = 3;

// Act
int result = _calculator.Add(a, b);

// Assert
Assert.AreEqual(8, result);
}

[TestMethod]
public void Subtract_ShouldReturnCorrectDifference()
{
// Arrange
int a = 5;
int b = 3;

// Act
int result = _calculator.Subtract(a, b);

// Assert
Assert.AreEqual(2, result);
}

[TestMethod]
public void Multiply_ShouldReturnCorrectProduct()
{
// Arrange
int a = 5;
int b = 3;

// Act
int result = _calculator.Multiply(a, b);

// Assert
Assert.AreEqual(15, result);
}

[TestMethod]
[ExpectedException(typeof(DivideByZeroException))]
public void Divide_ShouldThrowDivideByZeroException_WhenDividingByZero()
{
// Arrange
int a = 5;
int b = 0;

// Act
_calculator.Divide(a, b);

// Assert is handled by ExpectedException
}

[TestMethod]
public void Divide_ShouldReturnCorrectQuotient()
{
// Arrange
int a = 6;
int b = 3;

// Act
int result = _calculator.Divide(a, b);

// Assert
Assert.AreEqual(2, result);
}
}
}

Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace UnitTestProject
{
public class TestChrome
{

[SetUp]
public void Setup()
{
//
}
[TearDown]
public void TearDown()
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace UnitTestProject
{
public class TestChromeN
{

[SetUp]
public void Setup()
{
//
}

[Test]
public void goItalle()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
var driver = new ChromeDriver(chromeOptions);
Console.WriteLine("open rw.italle.dk");
driver.Navigate().GoToUrl("https://rw.italle.dk");
Console.WriteLine(driver.Title);
driver.FindElement(By.Name("username")).SendKeys("fang");
Assert.AreEqual(driver.Title, "HB System Login");
driver.Quit();
}

[Test]
public void goSoonr()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
var driver = new ChromeDriver(chromeOptions);
Console.WriteLine("open workplace");
driver.Navigate().GoToUrl("https://us.workplace.datto.com/login");
Console.WriteLine(driver.Title);
driver.FindElement(By.Name("userName")).SendKeys("fang");
Assert.AreEqual(driver.Title, "Workplace | Sign In");
driver.Quit();
}

}
}
}
[TearDown]
public void TearDown()
{
//
}

[Test]
public void goItalle()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
var driver = new ChromeDriver(chromeOptions);
Console.WriteLine("open rw.italle.dk");
driver.Navigate().GoToUrl("https://rw.italle.dk");
Console.WriteLine(driver.Title);
driver.FindElement(By.Name("username")).SendKeys("fang");
Assert.AreEqual(driver.Title, "HB System Login");
driver.Quit();
}

[Test]
public void goSoonr()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
var driver = new ChromeDriver(chromeOptions);
Console.WriteLine("open workplace");
driver.Navigate().GoToUrl("https://us.workplace.datto.com/login");
Console.WriteLine(driver.Title);
driver.FindElement(By.Name("userName")).SendKeys("fang");
Assert.AreEqual(driver.Title, "Workplace | Sign In");
driver.Quit();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace UnitTestProject
{
public class TestsCalculator
public class TestsCalculatorN
{
Calculator calculator;

Check warning on line 8 in Solution1/UnitTestProject/TestsCalculatorN.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Non-nullable field 'calculator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 8 in Solution1/UnitTestProject/TestsCalculatorN.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Non-nullable field 'calculator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<PackageReference Include="Selenium.Support" Version="4.19.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="125.0.6422.14100" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\TestLibrary.csproj" />
<ProjectReference Include="..\TestLibrary\TestLibrary.csproj" />
</ItemGroup>

</Project>

0 comments on commit fc5f053

Please sign in to comment.