ManagedCode.EnvironmentDetector
1.0.4
Prefix Reserved
See the version list below for details.
dotnet add package ManagedCode.EnvironmentDetector --version 1.0.4
NuGet\Install-Package ManagedCode.EnvironmentDetector -Version 1.0.4
<PackageReference Include="ManagedCode.EnvironmentDetector" Version="1.0.4" />
paket add ManagedCode.EnvironmentDetector --version 1.0.4
#r "nuget: ManagedCode.EnvironmentDetector, 1.0.4"
// Install ManagedCode.EnvironmentDetector as a Cake Addin #addin nuget:?package=ManagedCode.EnvironmentDetector&version=1.0.4 // Install ManagedCode.EnvironmentDetector as a Cake Tool #tool nuget:?package=ManagedCode.EnvironmentDetector&version=1.0.4
EnvironmentDetector
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 MSTest, NUnit, and xUnit test frameworks.
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
{
app.AddReal();
}
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 | Versions 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 was computed. 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. |
-
.NETStandard 2.1
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
-
net6.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
-
net7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.