Skip to content

[Test Improver] Add unit tests for TimeSpanParser #7852

Description

@github-actions

🤖 This PR was created by Test Improver, an automated AI assistant focused on improving tests.

Goal and Rationale

TimeSpanParser is a utility used by the testing platform to parse human-friendly time strings (e.g., 5400s, 90m, 2h) in configuration and timeout settings. It had zero unit tests despite containing non-trivial parsing logic: regex matching, multiple suffix branches, decimal value support, and two public methods (Parse/TryParse).

Approach

Added TimeSpanParserTests.cs in test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/ covering:

Scenario Result
null / "" / " "TryParse Returns false, output = TimeSpan.Zero
null / "" / " "Parse FormatException
"invalid", "abc", "-1s"TryParse Returns false
"invalid", "abc", "-1s"Parse FormatException
"0", "1000", "5400000" (no suffix) Parsed as milliseconds
"500ms", "500MS", "500mil", "500milliseconds" Milliseconds (case-insensitive)
"1s", "5400s", "1sec", "1seconds" Seconds
"1m", "90m", "1min", "1minutes" Minutes
"1h", "24h", "1hour", "1hours" Hours
"1d", "7d", "1day", "1days" Days
"1.5s", "2.5m", "0.5d" Decimal values handled
"5 s", "5 ms" (space before suffix) Handled (regex allows \s*)
Parse vs TryParse consistency All valid inputs agree

Note: s, m, h, d suffix checks use StringComparison.Ordinal (lowercase only), while ms/mil are checked with OrdinalIgnoreCase. Tests match actual behavior.

Test Status

✅ All 116 new tests pass. 1150 existing tests still pass (1266 total across net8.0 + net9.0).

Test run summary: Passed!
  total: 1266
  failed: 0
  succeeded: 1262
  skipped: 4

Reproducibility

export PATH="$PATH:.dotnet"
dotnet test test/UnitTests/Microsoft.Testing.Platform.UnitTests/Microsoft.Testing.Platform.UnitTests.csproj \
  -c Debug --filter "TimeSpanParser"

Trade-offs

Low maintenance burden: tests are data-driven ([DataRow]), follow the MSTest assertion style established in this project, and directly test the public API surface without mocking.

Generated by Daily Test Improver · ● 5.1M ·


Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch test-assist/timespan-parser-unit-tests-eabf1f50849ca30c.

Click here to create the pull request

To fix the permissions issue, go to SettingsActionsGeneral and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (219 of 219 lines)
From 3d3b48f09c4dc368934b097b0ae8dc5348834165 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Mon, 27 Apr 2026 00:11:25 +0000
Subject: [PATCH] Add unit tests for TimeSpanParser

Add 116 tests covering TimeSpanParser.TryParse and Parse methods:
- Null/empty/whitespace inputs return false / throw FormatException
- No suffix parses as milliseconds (e.g. '5400000' = 5400 seconds)
- ms/mil suffix (case-insensitive) parses as milliseconds
- s/sec/secs/second/seconds suffix parses as seconds
- m/min/mins/minute/minutes suffix parses as minutes
- h/hour/hours suffix parses as hours
- d/day/days suffix parses as days
- Decimal values (e.g. '1.5s')
- Space between value and suffix (regex allows \s*)
- TryParse and Parse are consistent for valid inputs
- Invalid inputs (non-numeric, no leading digit) return false / throw

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 .../Helpers/TimeSpanParserTests.cs            | 185 ++++++++++++++++++
 1 file changed, 185 insertions(+)
 create mode 100644 test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TimeSpanParserTests.cs

diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TimeSpanParserTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TimeSpanParserTests.cs
new file mode 100644
index 0000000..a681b63
--- /dev/null
+++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TimeSpanParserTests.cs
@@ -0,0 +1,185 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using Microsoft.Testing.Platform.Helpers;
+
+namespace Microsoft.Testing.Platform.UnitTests;
+
+[TestClass]
+public sealed class TimeSpanParserTests
+{
+    [TestMethod]
+    [DataRow(null)]
+    [DataRow("")]
+    [DataRow("   ")]
+    public void TryParse_NullOrWhitespace_ReturnsFalse(string? input)
+    {
+        bo
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/agentic-workflowsGitHub agentic workflow definitions under .github/workflows/*.md.type/automationCreated or maintained by an agentic workflow.type/test-gapMissing or insufficient tests.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions