AutoName.xUnit 1.0.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
Install-Package AutoName.xUnit -Version 1.0.0
dotnet add package AutoName.xUnit --version 1.0.0
<PackageReference Include="AutoName.xUnit" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoName.xUnit --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoName.xUnit, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutoName.xUnit as a Cake Addin
#addin nuget:?package=AutoName.xUnit&version=1.0.0

// Install AutoName.xUnit as a Cake Tool
#tool nuget:?package=AutoName.xUnit&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

AutoName.xUnit

Usage

There are three ways to use this package:

  1. Use defaults
  2. Customise inline default behaviour: 3. Name - The name for the test 4. Splitter - The method to use to split your unit test method name 5. Joiner - The method to use to join your splitter
  3. Custom Implementation

1. Use Defaults

// Output:  `The Name Of Your Test Method`
[NamedFact]
public void TheNameOfYourTestMethod() 
{}

// Output:  `The Name Of Another Test Method`
[NamedTheory]
[InlineData(...)]
public void TheNameOfAnotherTestMethod(...) 
{}

Default rules are:

  • Split by uppercase
  • Join with single space

2. Customise Inline Default Behaviour

Customising inline data is useful for when you have a small number of tests that use a different naming stategy. If the defaults do not fit the majority of your scheme, then you should look at custom implementation.

// Output: `The Name Of Your Test Method`
[NamedFact(SplitBy.Underscore, JoinWith.SingleSpace)] 
public void The_Name_Of_Your_Test_Method()
{}

// Output: `The Name Of Your Test Method`
[NamedFact(NameIt.MethodName, SplitBy.Underscore, JoinWith.SingleSpace)] 
public void The_Name_Of_Your_Test_Method()
{}

// Output: `The Name Of Another Test Method`
[NamedTheory(SplitBy.Underscore, JoinWith.SingleSpace)]
[InlineData(...)]
public void The_Name_Of_Another_Test_Method(...)
{}

// Output: `The Name Of Another Test Method`
[NamedTheory(NameIt.MethodName, SplitBy.Underscore, JoinWith.SingleSpace)]
[InlineData(...)]
public void The_Name_Of_Another_Test_Method(...)
{}

NameIt and SplitBy are flags, enabling you to also handle mixed naming:

// Output: `The Name Of Your Test Method`
[NamedFact(SplitBy.Underscore | SplitBy.Uppercase, JoinWith.SingleSpace)] 
public void TheName_OfYour_TestMethod()
{}

// Output: `The Name Of Another Test Method`
[NamedTheory(SplitBy.Underscore | SplitBy.Uppercase, JoinWith.SingleSpace)]
[InlineData(...)]
public void The_Name_Of_Another_Test_Method(...)
{}

3. Custom Implementation

Configure the default NameIt, SplitBy and JoinWith enums. To create a custom attribute that will handle mixed uppercase and underscore naming:

    public class MyCustomAttribute : NamedFactAttribute
    {
		public MyCustomAttribute([CallerMemberName] string callerName = null, [CallerFilePath] string sourceFilePath = null)
		: base(NameIt.MethodName, SplitBy.Underscore | SplitBy.Uppercase, JoinWith.SingleSpace, callerName, sourceFilePath)
		{ }
    }

Usage:

// Output: This Is My Preferred Naming Style
[MyCustomAttribute]
public void ThisIsMy_Preferred_NamingStyle()
{}

Most common cases should be handled by the built in behaviour. However, should you need a more bespoke naming convention, then you can override the SetName method like shown below:

public class MyCustomAttribute : NamedFactAttribute
{
	public MyCustomAttribute([CallerMemberName] string callerName = null, [CallerFilePath] string sourceFilePath = null)
	: base(NameIt.MethodName, SplitBy.Underscore | SplitBy.Uppercase, JoinWith.SingleSpace, callerName, sourceFilePath)
	{ }

    
    public override void SetDisplayName()
    {
        // Customise the base name
        NameIt = NameIt.MethodName | NameIt.NameSpace;

        // Customise how you wish to split the name 
        Func<string, IEnumerable<string>> mySplitter = delegate(string name)
        {
            return name.Split("__");
        }; 

        // Supports multiple splitters
        var mySplitters = new [] { mySplitter };

        // Customise how you wish to join the name
        Func<IEnumerable<string>, string> myJoiner = delegate(IEnumerable<string> names)
        {
            return string.Join("#", names);
        };

		base.DisplayName = ResolveName(mySplitters, myJoiner);
    }
}

Usage:

// Output: My.Special.NameSpace#ThisIsMy#Preferred#NamingStyle
[MyCustomAttribute]
public void __ThisIsMy__Preferred__NamingStyle()
{}

If you want to access existing properties you could also do:

public override void SetDisplayName()
{
    var splitters = GetSplitters();
    var joiner = GetJoiner();
    var splitterMethods = LoadSplitters(splitters);
    var joinerMethod = LoadJoiner(joiner);
    base.DisplayName = ResolveName(splitterMethods, joinerMethod);
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 661 11/9/2018
1.1.0 675 5/23/2018
1.0.0 643 5/20/2018

Initial Release