Skip to content

Improve test assertions for clearer failure messages and MSTest 4.x compatibility#292

Merged
samsmithnz merged 3 commits intomainfrom
copilot/update-test-assertions
Dec 4, 2025
Merged

Improve test assertions for clearer failure messages and MSTest 4.x compatibility#292
samsmithnz merged 3 commits intomainfrom
copilot/update-test-assertions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 4, 2025

Refactors test assertions to provide better failure diagnostics and updates exception testing to use MSTest 4.x APIs.

Assertion improvements

  • Replace Assert.IsTrue(x == y) with Assert.AreEqual(expected, actual) for meaningful failure messages
  • Extract magic numbers into named constants with delta parameters
// Before
Assert.IsTrue(System.Math.Round(result, 0) == 181f);

// After
Assert.AreEqual(181f, System.Math.Round(result, 0));

Test method naming

  • ControllerCountPeanutMandMsInAUSCupTestControllerCountPeanutMandMsInAmericanCupTest
  • ControllerCountPeanutMandMsInA1CubicCMTestControllerCountPeanutMandMsInA1000CubicCMTest (test uses 10x10x10)

Shared test setup

  • Add [TestInitialize] in SkittleControllerTests to reduce controller instantiation duplication

Exception testing (MSTest 4.x)

  • Replace try-catch blocks with Assert.Throws<Exception>() (Assert.ThrowsException was removed in MSTest 4.x)
// Before
try { Calculator.CountMandMs(null, 1); }
catch (Exception ex) { Assert.IsTrue(ex != null); }

// After
Assert.Throws<Exception>(() => Calculator.CountMandMs(null, 1f));

Type consistency

  • Fix float quantity = 1float quantity = 1f for consistency across test files
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"Use Assert.AreEqual instead of Assert.IsTrue with equality comparison for clearer test failure messages. Replace with Assert.AreEqual(181f, System.Math.Round(result, 0));","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n--- a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n+++ b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n@@ -22,7 +22,7 @@\n             float result = controller.GetDataForUnit(unit, quantity);\n \n             //Assert\n-            Assert.IsTrue(System.Math.Round(result, 0) == 181f);\n+            Assert.AreEqual(181f, System.Math.Round(result, 0));\n         }                \n \n         #endregion\n"}]},{"message":"Use Assert.AreEqual instead of Assert.IsTrue with equality comparison for clearer test failure messages. Replace with Assert.AreEqual(764, (int)System.Math.Round(result, 0));","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n--- a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n+++ b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n@@ -43,7 +43,7 @@\n             float result = controller.GetDataForRectangle(unit, height, width, length);\n \n             //Assert\n-            Assert.IsTrue((int)System.Math.Round(result, 0) == (int)764f);\n+            Assert.AreEqual(764, (int)System.Math.Round(result, 0));\n         }      \n \n         #endregion\n"}]},{"message":"Use Assert.AreEqual instead of Assert.IsTrue with equality comparison for clearer test failure messages. Replace with Assert.AreEqual(600f, System.Math.Round(result, 0));","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n--- a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n+++ b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n@@ -63,7 +63,7 @@\n             float result = controller.GetDataForCylinder(unit, height, radius);\n \n             //Assert\n-            Assert.IsTrue(System.Math.Round(result, 0) == 600f);\n+            Assert.AreEqual(600f, System.Math.Round(result, 0));\n         }\n \n         #endregion\n"}]},{"message":"[nitpick] Test method name contains 'ACUS' which is unclear. Consider renaming to 'ControllerCountPeanutMandMsInAmericanCupTest' or similar for clarity.","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n--- a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n+++ b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n@@ -11,7 +11,7 @@\n         #region \"Testing units\"       \n \n         [TestMethod]\n-        public void ControllerCountPeanutMandMsInAUSCupTest()\n+        public void ControllerCountPeanutMandMsInAmericanCupTest()\n         {\n             //Arrange\n             string unit = \"Cup\";\n"}]},{"message":"Test method name contains 'A1CubicCM' which is unclear. The test uses 1000 cubic cm (10x10x10). Consider renaming to reflect the actual volume being tested.","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n--- a/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n+++ b/src/MandMCounter.Tests/Controllers/PeanutMandMControllerTests.cs\n@@ -30,7 +30,7 @@\n         #region \" Testing volume in a rectangle\" \n \n         [TestMethod]\n-        public void ControllerCountPeanutMandMsInA1CubicCMTest()\n+        public void ControllerCountPeanutMandMsInA1000CubicCMTest()\n         {\n             //Arrange\n             string unit = \"cm\";\n"}]},{"message":"Controller instance is created in each test method. Consider creating a shared instance in a setup method or using dependency injection to reduce code duplication.","fixFiles":[{"filePath":"src/MandMCounter.Tests/Controllers/SkittleControllerTests.cs","diff":"diff --git a/src/MandMCounter.Tests/Controllers/SkittleControllerTests.cs b/src/MandMCounter.Tests/Controllers/SkittleControllerTests.cs\n--- a/src/MandMCounter.Tests/Contro...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 4, 2025

Pull Request Test Coverage Report for Build 19936417426

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.366%

Totals Coverage Status
Change from base Build 19936079754: 0.0%
Covered Lines: 102
Relevant Lines: 102

💛 - Coveralls

@samsmithnz samsmithnz marked this pull request as ready for review December 4, 2025 16:32
Copilot AI and others added 2 commits December 4, 2025 16:35
- Replace Assert.IsTrue with Assert.AreEqual for clearer failure messages
- Rename test methods for clarity (ACUS -> AmericanCup, 1CubicCM -> 1000CubicCM)
- Add shared controller instance using TestInitialize in SkittleControllerTests
- Use Assert.Throws<Exception> for exception testing (MSTest 4.x compatible)
- Fix quantity type consistency (1 -> 1f)

Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
@samsmithnz samsmithnz enabled auto-merge December 4, 2025 16:38
Copilot AI changed the title [WIP] Use Assert.AreEqual for clearer test failure messages Improve test assertions for clearer failure messages and MSTest 4.x compatibility Dec 4, 2025
Copilot AI requested a review from samsmithnz December 4, 2025 16:40
@samsmithnz samsmithnz merged commit 835188c into main Dec 4, 2025
7 checks passed
@samsmithnz samsmithnz deleted the copilot/update-test-assertions branch December 4, 2025 16:41
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