AutoName.xUnit
1.0.0
See the version list below for details.
dotnet add package AutoName.xUnit --version 1.0.0
NuGet\Install-Package AutoName.xUnit -Version 1.0.0
<PackageReference Include="AutoName.xUnit" Version="1.0.0" />
paket add AutoName.xUnit --version 1.0.0
#r "nuget: AutoName.xUnit, 1.0.0"
// 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
AutoName.xUnit
Usage
There are three ways to use this package:
- Use defaults
- 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
- 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 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
- xunit (>= 2.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial Release