Skip to content

[Code Quality] Helper.IsRunningInUnitTest — only detects xUnit; NUnit/MSTest assemblies fall through to production code paths #830

@Christophe-Rogiers

Description

@Christophe-Rogiers

Severity: Info
File: src/Servy.Core/Helpers/Helper.cs
Lines: 327-331

public static bool IsRunningInUnitTest()
{
    // Checks if common test runners are loaded in the process
    return AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName != null && a.FullName.StartsWith(\"xunit\"));
}

The XML comment claims "common test runners" but the implementation only matches assemblies whose FullName starts with xunit. NUnit (nunit.framework), MSTest (Microsoft.VisualStudio.TestPlatform, MSTest.TestFramework), and TUnit are not detected.

Callers (DependenciesViewModel.cs:283, ServiceSearchViewModelBase.cs:130) use this guard to skip Dispatcher invocation in unit tests. Anyone running these ViewModels under NUnit/MSTest will hit Application.Current.Dispatcher calls outside a Dispatcher pump, producing flaky test failures or null-ref warnings.

Suggested fix: match any of the common test framework prefixes:

private static readonly string[] TestFrameworkPrefixes =
{
    \"xunit\", \"nunit.framework\", \"Microsoft.VisualStudio.TestPlatform\",
    \"Microsoft.TestPlatform\", \"MSTest.TestFramework\", \"TUnit\"
};

public static bool IsRunningInUnitTest()
{
    return AppDomain.CurrentDomain.GetAssemblies().Any(a =>
        a.FullName != null && TestFrameworkPrefixes.Any(prefix =>
            a.FullName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)));
}

The current Servy test suite is xUnit-only, so this is dormant; rename to IsRunningUnderXunit if you want to keep the narrow scope, or expand the detection to match the doc comment.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions