Skip to content

Commit 1d50ae4

Browse files
authored
TUnit Templates (#1597)
* TUnit Templates * Fix * Fix * Tweak * Templates * GetPackageProjectsModule * EnableSourcyAttribute * Tweak * IncludeSource
1 parent d700e18 commit 1d50ae4

21 files changed

+321
-15
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ TUnit is designed to aid with all testing types:
1616
[![nuget](https://img.shields.io/nuget/v/TUnit.svg)](https://www.nuget.org/packages/TUnit/) [![NuGet Downloads](https://img.shields.io/nuget/dt/TUnit)](https://www.nuget.org/packages/TUnit/)
1717
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/thomhurst/TUnit/dotnet.yml) ![GitHub last commit (branch)](https://img.shields.io/github/last-commit/thomhurst/TUnit/main) ![License](https://img.shields.io/github/license/thomhurst/TUnit)
1818

19+
## Quick Start
20+
21+
Assuming you have the .NET SDK installed, simply run:
22+
23+
`dotnet new install TUnit.Templates`
24+
`dotnet new TUnit -n "YourProjectName"`
25+
26+
A new test project will be created for you with some samples of different test types and tips. When you're ready to get going, delete them and create your own!
27+
1928
## Documentation
2029

2130
See here: <https://thomhurst.github.io/TUnit/>

README_Template.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ TUnit is designed to aid with all testing types:
1616
[![nuget](https://img.shields.io/nuget/v/TUnit.svg)](https://www.nuget.org/packages/TUnit/) [![NuGet Downloads](https://img.shields.io/nuget/dt/TUnit)](https://www.nuget.org/packages/TUnit/)
1717
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/thomhurst/TUnit/dotnet.yml) ![GitHub last commit (branch)](https://img.shields.io/github/last-commit/thomhurst/TUnit/main) ![License](https://img.shields.io/github/license/thomhurst/TUnit)
1818

19+
## Quick Start
20+
21+
Assuming you have the .NET SDK installed, simply run:
22+
23+
`dotnet new install TUnit.Templates`
24+
`dotnet new TUnit -n "YourProjectName"`
25+
26+
A new test project will be created for you with some samples of different test types and tips. When you're ready to get going, delete them and create your own!
27+
1928
## Documentation
2029

2130
See here: <https://thomhurst.github.io/TUnit/>
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
using ModularPipelines.Context;
2-
using ModularPipelines.Extensions;
3-
using ModularPipelines.Git.Extensions;
42
using ModularPipelines.Modules;
53
using File = ModularPipelines.FileSystem.File;
64

75
namespace TUnit.Pipeline.Modules;
86

97
public class GetPackageProjectsModule : Module<List<File>>
108
{
11-
protected override Task<List<File>?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
9+
protected override async Task<List<File>?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
1210
{
13-
return context.Git().RootDirectory
14-
.GetFiles(x => x.Extension == ".csproj")
15-
.Where(x => !x.Name.Contains("Pipeline", StringComparison.OrdinalIgnoreCase))
16-
.Where(x => !x.Name.Contains("Analyzer", StringComparison.OrdinalIgnoreCase))
17-
.Where(x => !x.Name.Contains("Generator", StringComparison.OrdinalIgnoreCase))
18-
.Where(x => !x.Name.Contains("Sample", StringComparison.OrdinalIgnoreCase))
19-
.Where(x => !x.Name.Contains("Test", StringComparison.OrdinalIgnoreCase))
20-
.Where(x => !x.Name.Contains("Timer", StringComparison.OrdinalIgnoreCase))
21-
.Where(x => !x.Name.Contains("CodeFix", StringComparison.OrdinalIgnoreCase))
22-
.ToList()
23-
.AsTask<List<File>?>();
11+
await Task.CompletedTask;
12+
13+
return
14+
[
15+
Sourcy.DotNet.Projects.TUnit_Assertions,
16+
Sourcy.DotNet.Projects.TUnit_Core,
17+
Sourcy.DotNet.Projects.TUnit_Engine,
18+
Sourcy.DotNet.Projects.TUnit,
19+
Sourcy.DotNet.Projects.TUnit_Playwright,
20+
Sourcy.DotNet.Projects.TUnit_Templates
21+
];
2422
}
2523
}

TUnit.Pipeline/Modules/PackTUnitFilesModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ await context.DotNet()
3737
new KeyValue("AssemblyFileVersion", version.SemVer!),
3838
new KeyValue("IsPackTarget", "true")
3939
],
40-
IncludeSource = true,
40+
IncludeSource = project == Sourcy.DotNet.Projects.TUnit_Templates ? false : true,
4141
Configuration = Configuration.Release,
4242
}, cancellationToken);
4343

TUnit.Pipeline/TUnit.Pipeline.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<AssemblyAttribute Include="Sourcy.EnableSourcyAttribute">
10+
<_Parameter1>$(MSBuildProjectFullPath)</_Parameter1>
11+
</AssemblyAttribute>
12+
</ItemGroup>
713

814
<PropertyGroup>
915
<OutputType>Exe</OutputType>

TUnit.Templates/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TUnit.Templates
2+
3+
Some templates to help you get started with TUnit!
4+
5+
For more information, check out the repository at https://www.github.com/thomhurst/TUnit
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageId>TUnit.Templates</PackageId>
5+
<PackageVersion>1.0</PackageVersion>
6+
<Title>TUnit Templates</Title>
7+
<Authors>Tom Longhurst</Authors>
8+
<Description>Templates for getting started with TUnit</Description>
9+
<PackageTags>TUnit,test,testing,unit,integration,acceptance</PackageTags>
10+
<PackageProjectUrl>https://www.github.com/thomhurst/TUnit</PackageProjectUrl>
11+
12+
<PackageType>Template</PackageType>
13+
<TargetFramework>net8.0</TargetFramework>
14+
<IncludeContentInPack>true</IncludeContentInPack>
15+
<IncludeBuildOutput>false</IncludeBuildOutput>
16+
<ContentTargetFolders>content</ContentTargetFolders>
17+
<NoWarn>$(NoWarn);NU5128</NoWarn>
18+
<NoDefaultExcludes>true</NoDefaultExcludes>
19+
<PackageReadmeFile>README.md</PackageReadmeFile>
20+
</PropertyGroup>
21+
22+
<PropertyGroup>
23+
<LocalizeTemplates>false</LocalizeTemplates>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="Microsoft.TemplateEngine.Tasks" Version="*" PrivateAssets="all" IsImplicitlyDefined="true"/>
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Content Include="content\**\*" Exclude="content\**\bin\**;content\**\obj\**;content\Directory.Build.props;content\Directory.Build.targets;content\Directory.Packages.props" />
32+
<Compile Remove="**\*" />
33+
</ItemGroup>
34+
35+
<ItemGroup>
36+
<None Include="README.md" Pack="true" PackagePath="" />
37+
</ItemGroup>
38+
39+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<ItemGroup>
4+
<Using Include="TUnit.Core.HookType" Static="True" />
5+
<Using Include="TUnit.Core" />
6+
</ItemGroup>
7+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
4+
</PropertyGroup>
5+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"author": "Tom Longhurst",
4+
"description": "Templates for getting started with TUnit",
5+
"classifications": [
6+
"Test",
7+
"TUnit"
8+
],
9+
"name": "TUnit Test Project",
10+
"identity": "TUnit.Test.Project",
11+
"groupIdentity": "TUnit",
12+
"shortName": "TUnit",
13+
"tags": {
14+
"language": "C#",
15+
"type": "project"
16+
},
17+
"sourceName": "TestProject",
18+
"preferNameDirectory": true
19+
}
20+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using TUnit.Core.Interfaces;
2+
3+
namespace TestProject;
4+
5+
public class DataClass : IAsyncInitializer, IAsyncDisposable
6+
{
7+
public Task InitializeAsync()
8+
{
9+
return Console.Out.WriteLineAsync("Classes can be injected into tests, and they can perform some initialisation logic such as starting an in-memory server or a test container.");
10+
}
11+
12+
public async ValueTask DisposeAsync()
13+
{
14+
await Console.Out.WriteLineAsync("And when the class is finished with, we can clean up any resources.");
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace TestProject.Data;
2+
3+
public class DataGenerator : DataSourceGeneratorAttribute<int, int, int>
4+
{
5+
public override IEnumerable<Func<(int, int, int)>> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata)
6+
{
7+
yield return () => (1, 1, 2);
8+
yield return () => (1, 2, 3);
9+
yield return () => (4, 5, 9);
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TUnit.Core.Interfaces;
2+
3+
namespace TestProject;
4+
5+
public class DependencyInjectionClassConstructor : IClassConstructor
6+
{
7+
public T Create<T>(ClassConstructorMetadata classConstructorMetadata) where T : class
8+
{
9+
Console.WriteLine("You can also control how your test classes are new'd up, giving you lots of power and the ability to utilise tools such as dependency injection");
10+
11+
if (typeof(T) == typeof(AndEvenMoreTests))
12+
{
13+
return (new AndEvenMoreTests(new DataClass()) as T)!;
14+
}
15+
16+
throw new NotImplementedException();
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Here you could define global logic that would affect all tests
2+
3+
// You can use attributes at the assembly level to apply to all tests in the assembly
4+
[assembly: Retry(3)]
5+
6+
namespace TestProject;
7+
8+
public class GlobalHooks
9+
{
10+
[Before(TestSession)]
11+
public static void SetUp()
12+
{
13+
Console.WriteLine("Or you can define methods that do stuff before...");
14+
}
15+
16+
[After(TestSession)]
17+
public static void CleanUp()
18+
{
19+
Console.WriteLine("...and after!");
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<OutputType>Exe</OutputType>
7+
<TargetFramework>net8.0</TargetFramework>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="TUnit" Version="0.6.121" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using TestProject.Data;
2+
3+
namespace TestProject;
4+
5+
public class Tests
6+
{
7+
[Test]
8+
public void Basic()
9+
{
10+
Console.WriteLine("This is a basic test");
11+
}
12+
13+
[Test]
14+
[Arguments(1, 2, 3)]
15+
[Arguments(2, 3, 5)]
16+
public async Task DataDrivenArguments(int a, int b, int c)
17+
{
18+
Console.WriteLine("This one can accept arguments from an attribute");
19+
20+
var result = a + b;
21+
22+
await Assert.That(result).IsEqualTo(c);
23+
}
24+
25+
[Test]
26+
[MethodDataSource(nameof(DataSource))]
27+
public async Task MethodDataSource(int a, int b, int c)
28+
{
29+
Console.WriteLine("This one can accept arguments from a method");
30+
31+
var result = a + b;
32+
33+
await Assert.That(result).IsEqualTo(c);
34+
}
35+
36+
[Test]
37+
[ClassDataSource<DataClass>]
38+
[ClassDataSource<DataClass>(Shared = SharedType.PerClass)]
39+
[ClassDataSource<DataClass>(Shared = SharedType.PerAssembly)]
40+
[ClassDataSource<DataClass>(Shared = SharedType.PerTestSession)]
41+
public void ClassDataSource(DataClass dataClass)
42+
{
43+
Console.WriteLine("This test can accept a class, which can also be pre-initialised before being injected in");
44+
45+
Console.WriteLine("These can also be shared among other tests, or new'd up each time, by using the `Shared` property on the attribute");
46+
}
47+
48+
[Test]
49+
[DataGenerator]
50+
public async Task CustomDataGenerator(int a, int b, int c)
51+
{
52+
Console.WriteLine("You can even define your own custom data generators");
53+
54+
var result = a + b;
55+
56+
await Assert.That(result).IsEqualTo(c);
57+
}
58+
59+
public static IEnumerable<(int a, int b, int c)> DataSource()
60+
{
61+
yield return (1, 1, 2);
62+
yield return (2, 1, 3);
63+
yield return (3, 1, 4);
64+
}
65+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace TestProject;
2+
3+
[Arguments("Hello")]
4+
[Arguments("World")]
5+
public class MoreTests(string title)
6+
{
7+
[Test]
8+
public void ClassLevelDataRow()
9+
{
10+
Console.WriteLine(title);
11+
Console.WriteLine("Did I forget that data injection works on classes too?");
12+
}
13+
14+
// You can even inject in ClassDataSources as properties to avoid repetitive constructors if you're using inheritance!
15+
[ClassDataSource<DataClass>(Shared = SharedType.PerTestSession)]
16+
public required DataClass DataClass { get; init; }
17+
18+
[Test]
19+
public void Matrices(
20+
[Matrix(1, 2, 3)] int a,
21+
[Matrix(true, false)] bool b,
22+
[Matrix("A", "B", "C")] string c)
23+
{
24+
Console.WriteLine("A new test will be created for each data row, whether it's on the class or method level!");
25+
26+
Console.WriteLine("Oh and this is a matrix test. That means all combinations of inputs are attempted.");
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace TestProject;
2+
3+
[ClassDataSource<DataClass>]
4+
[ClassConstructor<DependencyInjectionClassConstructor>]
5+
public class AndEvenMoreTests(DataClass dataClass)
6+
{
7+
[Test]
8+
public void HaveFun()
9+
{
10+
Console.WriteLine(dataClass);
11+
Console.WriteLine("For more information, check out the documentation");
12+
Console.WriteLine("https://thomhurst.github.io/TUnit/");
13+
}
14+
}

0 commit comments

Comments
 (0)