https://github.com/technobre/powerutils.xunit.extensions
Utils, helpers and extensions to create tests with xUnit
https://github.com/technobre/powerutils.xunit.extensions
c-sharp csharp dotnet dotnet-core test xunit
Last synced: 12 months ago
JSON representation
Utils, helpers and extensions to create tests with xUnit
- Host: GitHub
- URL: https://github.com/technobre/powerutils.xunit.extensions
- Owner: TechNobre
- License: mit
- Created: 2021-07-22T23:27:55.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-06-16T07:25:45.000Z (about 1 year ago)
- Last Synced: 2025-07-05T11:18:50.103Z (12 months ago)
- Topics: c-sharp, csharp, dotnet, dotnet-core, test, xunit
- Language: C#
- Homepage: https://www.nuget.org/packages/PowerUtils.xUnit.Extensions/
- Size: 232 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# PowerUtils.xUnit.Extensions

***Utils, helpers and extensions to create tests with xUnit***

[](https://dashboard.stryker-mutator.io/reports/github.com/TechNobre/PowerUtils.xUnit.Extensions/main)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.xUnit.Extensions)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.xUnit.Extensions)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.xUnit.Extensions)
[](https://www.nuget.org/packages/PowerUtils.xUnit.Extensions)
[](https://www.nuget.org/packages/PowerUtils.xUnit.Extensions)
[](https://github.com/TechNobre/PowerUtils.xUnit.Extensions/blob/main/LICENSE)
- [Support to](#support-to)
- [Dependencies](#dependencies)
- [How to use](#how-to-use)
- [Install NuGet package](#install-nuget-package)
- [Extensions](#extensions)
- [object.InvokeNonPublicMethod();](#objectinvokenonpublicmethod)
- [object.SetNonPublicProperty(); and object.SetNonPublicField()](#objectsetnonpublicproperty-and-objectsetnonpublicfield)
- [Helpers](#helpers)
- [Sort tests by priority](#sort-tests-by-priority)
- [InvokeStaticNonPublicMethod](#invokestaticnonpublicmethod)
- [Factories](#factories)
- [ObjectFactory](#objectfactory)
- [Contribution](#contribution)
## Support to
- .NET 9.0
- .NET 8.0
- .NET 7.0
- .NET 6.0
- .NET 5.0
- .NET 3.1
- .NET Standard 2.1
- .NET Framework 4.6.2 or more
- xunit.extensibility.core [NuGet](https://www.nuget.org/packages/xunit.extensibility.core/)
### Install NuGet package
This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.xUnit.Extensions
**Nuget**
```bash
Install-Package PowerUtils.xUnit.Extensions
```
**.NET CLI**
```
dotnet add package PowerUtils.xUnit.Extensions
```
#### object.InvokeNonPublicMethod();
Extension to invoke non-public methods
```csharp
public class SampleClass
{
private bool _methodName1(int value)
{
return false;
}
private bool _methodName2()
{
return true;
}
private void _methodName3(int value)
{
}
private void _methodName4()
{
}
private Task _methodName5Async()
{
}
private Task _methodName6Async()
{
}
}
```
```csharp
var sampleClass = new SampleClass();
var result1 = sampleClass.InvokeNonPublicMethod("_methodName1", 1);
var result2 = sampleClass.InvokeNonPublicMethod("_methodName2");
sampleClass.InvokeNonPublicMethod("_methodName3", 532);
sampleClass.InvokeNonPublicMethod("_methodName4");
var result3 = await sampleClass.InvokeNonPublicMethodAsync("_methodName5Async", 1);
await sampleClass.InvokeNonPublicMethodAsync("_methodName6Async");
```
#### object.SetNonPublicProperty(); and object.SetNonPublicField()
Extensions to set non-public properties and fields
```csharp
public class SampleClass
{
public string PropSetPrivate { get; private set; }
private string _propPrivate { get; set; }
private string _privateField;
}
```
```csharp
var sampleClass = new SampleClass();
sampleClass.SetNonPublicProperty(p => p.PropSetPrivate, "Value");
sampleClass.SetNonPublicProperty("_propPrivate", "Value");
sampleClass.SetNonPublicField("_privateField", "Value");
```
```csharp
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public class Tests
{
[Fact, TestPriority(2)]
public void Test1()
{
}
[Fact, TestPriority(1)]
public void Test2()
{
}
}
```
#### InvokeStaticNonPublicMethod
```csharp
public static class SampleClass
{
private static bool _methodName1(int value)
{
return false;
}
private static bool _methodName2()
{
return true;
}
private static void _methodName3(int value)
{
}
private static void _methodName4()
{
}
private static Task _methodName5Async()
{
}
private static Task _methodName6Async()
{
}
}
```
```csharp
var result1 = ObjectInvoker.Invoke(typeof(SampleClass), "_methodName1", 1);
var result2 = ObjectInvoker.Invoke(typeof(SampleClass), "_methodName2");
ObjectInvoker.Invoke(typeof(SampleClass), "_methodName3", 532);
ObjectInvoker.Invoke(typeof(SampleClass), "_methodName4");
var result3 = await ObjectInvoker.InvokeAsync(typeof(SampleClass), "_methodName5Async", 1);
await ObjectInvoker.InvokeAsync(typeof(SampleClass), "_methodName6Async");
```
#### ObjectFactory
Factory to create an object by non public constructor
```csharp
public class TestObject
{
public string Name { get; private set; }
public int Age { get; private set; }
private TestObject()
{
this.Name = "Example name";
this.Age = 21;
}
protected TestObject(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
var obj1 = ObjectFactory.Create();
var obj2 = ObjectFactory.Create("My name", 50);
```
If you have any questions, comments, or suggestions, please open an [issue](https://github.com/TechNobre/PowerUtils.xUnit.Extensions/issues/new/choose) or create a [pull request](https://github.com/TechNobre/PowerUtils.xUnit.Extensions/compare)