A unit test framework for project templates built using dotnet new.
- Install dotnet new based project templates from a directory.
- Run
dotnet restore
,dotnet build
anddotnet publish
commands. - For ASP.NET Core project templates you can run
dotnet run
which gives you aHttpClient
that you can use to call the app and run further tests.
public class ApiTemplateTest
{
public ApiTemplateTest() => DotnetNew.Install<ApiTemplateTest>("ApiTemplate.sln").Wait();
[Theory]
[InlineData("StatusEndpointOn", "status-endpoint=true")]
[InlineData("StatusEndpointOff", "status-endpoint=false")]
public async Task RestoreAndBuild_CustomArguments_IsSuccessful(string name, params string[] arguments)
{
using (var tempDirectory = TempDirectory.NewTempDirectory())
{
var dictionary = arguments
.Select(x => x.Split('=', StringSplitOptions.RemoveEmptyEntries))
.ToDictionary(x => x.First(), x => x.Last());
var project = await tempDirectory.DotnetNew("api", name, dictionary);
await project.DotnetRestore();
await project.DotnetBuild();
}
}
[Fact]
public async Task Run_DefaultArguments_IsSuccessful()
{
using (var tempDirectory = TempDirectory.NewTempDirectory())
{
var project = await tempDirectory.DotnetNew("api", "DefaultArguments");
await project.DotnetRestore();
await project.DotnetBuild();
await project.DotnetRun(
@"Source\DefaultArguments",
async (httpClient, httpsClient) =>
{
var httpResponse = await httpsClient.GetAsync("status");
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
});
}
}
}