Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

TechNobre/PowerUtils.xUnit.Extensions

PowerUtils.xUnit.Extensions

⚠️ DEPRECATED

This package has been discontinued because it never evolved, and the code present in this package does not justify its continuation. It is preferable to implement this code directly in the project if necessary.

Logo

Utils, helpers and extensions to create tests with xUnit

License: MIT

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

Dependencies

  • xunit.extensibility.core NuGet

How to use

Install NuGet package

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

Extensions

object.InvokeNonPublicMethod();

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");

object.SetNonPublicProperty(); and object.SetNonPublicField()

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");

Helpers

Sort tests by priority

[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public class Tests
{
    [Fact, TestPriority(2)]
    public void Test1()
    {
    }

    [Fact, TestPriority(1)]
    public void Test2()
    {
    }
}

InvokeStaticNonPublicMethod

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");

Factories

ObjectFactory

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);

Contribution

If you have any questions, comments, or suggestions, please open an issue or create a pull request

About

Utils, helpers and extensions to create tests with xUnit

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •