xUnit.UserContext
1.0.1
See the version list below for details.
dotnet add package xUnit.UserContext --version 1.0.1
NuGet\Install-Package xUnit.UserContext -Version 1.0.1
<PackageReference Include="xUnit.UserContext" Version="1.0.1" />
paket add xUnit.UserContext --version 1.0.1
#r "nuget: xUnit.UserContext, 1.0.1"
// Install xUnit.UserContext as a Cake Addin #addin nuget:?package=xUnit.UserContext&version=1.0.1 // Install xUnit.UserContext as a Cake Tool #tool nuget:?package=xUnit.UserContext&version=1.0.1
xUnit.UserContext
xUnit.UserContext is an attribute based extension for xUnit that allows tests to run under the context of a given Windows user. Impersonation can be useful for running integration tests that use resources that require windows authentication, such as connecting to a database.
The aim of this library is to allow easy usage in xUnit tests with minimal code. This library utilises the wrapper library SimpleImpersonation to use the Win32 LogonUser API for Impersonation. Additionally, because storing credentials in code is not ideal, this library also supports retrieval of credentials stored using ASP.NET UserSecrets.
Installation
Using the dotnet cli:
dotnet add package xunit.usercontext
Usage
To use this library use a UserFact or UserTheory in place of Fact or Theory
UserFactAttribute
using Xunit.UserContext;
...
[UserFact("UserName", "Password")]
public void TestedMethodName_StateUnderTest_ExpectedBehavior()
{
// Test case that uses resource restricted to a windows user
// e.g. database connection using windows auth
}
...
UserTheoryAttribute
using Xunit.UserContext;
...
[UserTheory("TestUserSecretsId")]
[InlineData("TestValue")]
public void TestedMethodName_StateUnderTest_ExpectedBehavior(string value)
{
// Test case that uses resource restricted to a windows user
// e.g. database connection using windows auth
}
...
Attribute Constructors
Both UserFact and UserTheory follow the same constructor patterns, so the all the following overloads are applicable to both. Additionally, all constructors have the following optional parameters with default values.
- logonType - default value of Network. The type of logon operation to perform (as per the underlying API), this should be set according to your usage, more information on the different logon types is available here.
- displayNameOnTest - default value of true. If true, this appends the username to the test name in the following format
(user: username)
Static Username and Password
[UserFact("UserName", "Password")]
[UserTheory("UserName","Password")]
- username - windows username
- password - windows password
Static Username, Password and Domain
[UserFact("UserName", "Password","Domain")]
[UserTheory("UserName","Password","Domain")]
- username - windows username
- password - windows password
- domain - windows domain
User Secrets Id
[UserFact("TestUserSecretsId")]
[UserTheory("TestUserSecretsId")]
- userSecretsId - user secrets id, used when setting secrets
To set the credentials for this option, use the dotnet cli to set the values. The library will read values for the following keys: Username,Password, and optionally Domain. Below is an example of setting these values:
dotnet user-secrets set "Username" MyUsername --id TestUserSecretsId
dotnet user-secrets set "Password" MyPassword --id TestUserSecretsId
dotnet user-secrets set "Domain" Domain --id TestUserSecretsId
Note: the secrets id used to set these values must be the same as what is provided to the attribute
Issues & Contributions
If you find a bug or have a feature request, please report them in this repository's issues section. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Note: Impersonation does not work with async
License
Acknowledgments
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- Microsoft.Extensions.Configuration.UserSecrets (>= 2.2.0)
- SimpleImpersonation (>= 3.0.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- xunit (>= 2.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.