ManagedCode.EnvironmentDetector 1.0.8

Prefix Reserved
dotnet add package ManagedCode.EnvironmentDetector --version 1.0.8                
NuGet\Install-Package ManagedCode.EnvironmentDetector -Version 1.0.8                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ManagedCode.EnvironmentDetector" Version="1.0.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ManagedCode.EnvironmentDetector --version 1.0.8                
#r "nuget: ManagedCode.EnvironmentDetector, 1.0.8"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ManagedCode.EnvironmentDetector as a Cake Addin
#addin nuget:?package=ManagedCode.EnvironmentDetector&version=1.0.8

// Install ManagedCode.EnvironmentDetector as a Cake Tool
#tool nuget:?package=ManagedCode.EnvironmentDetector&version=1.0.8                

EnvironmentDetector

.NET Coverage Status nuget CodeQL NuGet Package

EnvironmentDetector is a library that provides a simple way to detect whether your code is running in a test environment or not. It currently supports detecting Dapr, Kubernetes, Docker, MSTest, NUnit, and xUnit.

Installation

To use EnvironmentDetector in your project, you'll need to install the NuGet package. You can do this by running the following command in the Package Manager Console:

Install-Package ManagedCode.EnvironmentDetector

Alternatively, you can search for "ManagedCode.EnvironmentDetector" in the NuGet Package Manager in Visual Studio.

Usage

EnvironmentDetector provides two ways to detect whether your code is running in a test environment or not.

The first way is to use the provided extension method IsTests on an instance of IHostEnvironment. Here's an example of how you might use this method in a Razor page:

if (HostEnvironment.IsTests())
{
    app.AddFake();
}
else if (HostEnvironment.IsKubernetes())
{
    app.AddKubernetes();
}
else if (HostEnvironment.IsDapr())
{
    app.AddDapr();
}
else if (HostEnvironment.IsDocker())
{
    app.AddDocker();
}

The second way to use EnvironmentDetector is to use the MSTestDetector.IsInTest static property. This can be useful if you don't have access to an instance of IHostEnvironment, or if you want to check whether you're running in a test environment in a static context. Here's an example of how you might use this property:

using ManagedCode.TestsDetector;

if (MSTestDetector.IsInTest)
{
    Console.WriteLine("Running in a test environment");
}
else
{
    Console.WriteLine("Running in a production environment");
}

Motivation

When working on a software project, it's common to have both production code and test code in the same repository. However, it can sometimes be useful to be able to distinguish between the two, especially when working with dependency injection.

The TestsDetector project aims to provide a simple and convenient way to detect whether your code is running in a test environment or not. By using TestsDetector, you'll be able to easily check whether you're running in a test environment or not, and configure your dependency injection container accordingly. TestsDetector currently supports MSTest, NUnit, and xUnit test frameworks.

Limitations

TestsDetector uses reflection to detect the presence of test frameworks. This means that it may not work in all cases, especially if the test framework is being used in an unusual way. However, it should work in most cases.

Contributions

We welcome contributions to TestsDetector! If you have an idea for a new feature or have found a bug, please open an issue on GitHub. If you'd like to contribute code, please fork the repository and submit a pull request.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 2,124 5/27/2024
1.0.5 4,990 3/15/2023
1.0.4 597 1/2/2023
1.0.3 624 1/2/2023
1.0.2 578 1/2/2023