Utils, helpers and extensions to create tests with xUnit
- .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
This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.xUnit.Extensions
Nuget
Install-Package PowerUtils.xUnit.Extensions
.NET CLI
dotnet add package PowerUtils.xUnit.Extensions
Extension to invoke non-public methods
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<bool> _methodName5Async()
{
}
private Task _methodName6Async()
{
}
}
var sampleClass = new SampleClass();
var result1 = sampleClass.InvokeNonPublicMethod<bool>("_methodName1", 1);
var result2 = sampleClass.InvokeNonPublicMethod<bool>("_methodName2");
sampleClass.InvokeNonPublicMethod("_methodName3", 532);
sampleClass.InvokeNonPublicMethod("_methodName4");
var result3 = await sampleClass.InvokeNonPublicMethodAsync<bool>("_methodName5Async", 1);
await sampleClass.InvokeNonPublicMethodAsync("_methodName6Async");
Extensions to set non-public properties and fields
public class SampleClass
{
public string PropSetPrivate { get; private set; }
private string _propPrivate { get; set; }
private string _privateField;
}
var sampleClass = new SampleClass();
sampleClass.SetNonPublicProperty(p => p.PropSetPrivate, "Value");
sampleClass.SetNonPublicProperty("_propPrivate", "Value");
sampleClass.SetNonPublicField("_privateField", "Value");
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public class Tests
{
[Fact, TestPriority(2)]
public void Test1()
{
}
[Fact, TestPriority(1)]
public void Test2()
{
}
}
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<bool> _methodName5Async()
{
}
private static Task _methodName6Async()
{
}
}
var result1 = ObjectInvoker.Invoke<bool>(typeof(SampleClass), "_methodName1", 1);
var result2 = ObjectInvoker.Invoke<bool>(typeof(SampleClass), "_methodName2");
ObjectInvoker.Invoke(typeof(SampleClass), "_methodName3", 532);
ObjectInvoker.Invoke(typeof(SampleClass), "_methodName4");
var result3 = await ObjectInvoker.InvokeAsync<bool>(typeof(SampleClass), "_methodName5Async", 1);
await ObjectInvoker.InvokeAsync(typeof(SampleClass), "_methodName6Async");
Factory to create an object by non public constructor
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<TestObject>();
var obj2 = ObjectFactory.Create<TestObject>("My name", 50);
If you have any questions, comments, or suggestions, please open an issue or create a pull request