Skip to content

Add CIConditionAttribute to support CI-specific test execution control #5797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 17, 2025

This PR implements CIConditionAttribute to allow developers to conditionally run or skip tests based on whether they are executing in a Continuous Integration (CI) environment.

Summary

The new attribute addresses a common need in test suites where certain tests should behave differently in CI environments - either running only in CI or being temporarily disabled in CI (e.g., for flaky tests).

Usage

[TestMethod]
[CICondition(ConditionMode.Include)]  // Run only in CI
public void PerformanceTestOnlyInCI()
{
    // Resource-intensive test that should only run in CI infrastructure
}

[TestMethod]
[CICondition(ConditionMode.Exclude)]  // Skip in CI
public void FlakyTestTemporarilyDisabledInCI()
{
    // Temporarily disable flaky test in CI while keeping it for local development
}

Implementation Details

  • Follows existing patterns: Inherits from ConditionBaseAttribute like OSConditionAttribute

  • Comprehensive CI detection: Supports major CI systems including:

    • GitHub Actions (GITHUB_ACTIONS)
    • Azure Pipelines (TF_BUILD)
    • AppVeyor (APPVEYOR)
    • Travis CI (TRAVIS)
    • CircleCI (CIRCLECI)
    • Jenkins (BUILD_ID + BUILD_URL)
    • TeamCity (TEAMCITY_VERSION)
    • AWS CodeBuild (CODEBUILD_BUILD_ID + AWS_REGION)
    • Google Cloud Build (BUILD_ID + PROJECT_ID)
    • JetBrains Space (JB_SPACE_API_URL)
    • Generic CI (CI)
  • CI detection logic: Based on the well-documented detection from .NET SDK telemetry (https://learn.microsoft.com/dotnet/core/tools/telemetry#continuous-integration-detection)

  • Comprehensive testing: 20+ unit tests covering all CI detection scenarios and edge cases

  • API consistency: Updated PublicAPI files for proper API tracking

Use Cases

  1. Flaky test management: Temporarily disable problematic tests in CI while keeping them active for local development
  2. Resource-intensive tests: Run performance or integration tests only in CI environments with adequate resources
  3. CI-specific scenarios: Handle tests that depend on CI infrastructure or specific CI configurations

The attribute can be applied to both test methods and test classes, supporting standard MSTest condition attribute grouping behavior.

Fixes #5533.


💡 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.

Copilot AI and others added 3 commits June 17, 2025 08:46
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add CIConditionAttribute Add CIConditionAttribute to support CI-specific test execution control Jun 17, 2025
@Copilot Copilot AI requested a review from Evangelink June 17, 2025 08:50
Copilot finished work on behalf of Evangelink June 17, 2025 08:51
Comment on lines 32 to 39
public override bool ShouldRun
{
get
{
bool isCI = IsCIEnvironment();
return Mode == ConditionMode.Include ? isCI : !isCI;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public override bool ShouldRun
{
get
{
bool isCI = IsCIEnvironment();
return Mode == ConditionMode.Include ? isCI : !isCI;
}
}
public override bool ShouldRun => IsCIEnvironment();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed to public override bool ShouldRun => IsCIEnvironment(); to follow the same pattern as OSConditionAttribute. The framework handles the Mode logic in AttributeHelpers.cs. Commit: 7764d1e

…ework pattern

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 5rqvsblobprodcus385.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25316.2.txt (dns block)
    • Triggering command: dotnet build test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj --verbosity quiet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Youssef1313 June 17, 2025 09:01
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Linq;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Removed the using System.Linq; statement and replaced the LINQ All() method with a traditional foreach loop to eliminate the dependency. Commit: d3fc478


return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/TestFramework/TestFramework/Attributes/TestMethod/CIConditionAttribute.cs(119,2): error SA1518: (NETCORE_ENGINEERING_TELEMETRY=Build) File is required to end with a single newline character (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1518.md)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Added the missing newline character at the end of the file to resolve the StyleCop SA1518 error. Commit: 29c801e

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 June 17, 2025 14:13
…te.cs

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 5rqvsblobprodcus385.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25316.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Youssef1313 June 17, 2025 14:17
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.

Add CIConditionAttribute
3 participants