Speed Up Automated Tests Writing With .NET Core Project Templates
Learn how to speed up development of .NET Core tests by automating test project creation.
Join the DZone community and get the full member experience.
Join For FreeIn the article Defining the Primary Problems that Test Automation Frameworks Solve, I defined 40+ problems that test automation and frameworks should be able to solve. One of them was " Tests Speed Creation." Here, I will quote the definition of the problem again:
"Time spent on maintaining the existing tests is essential. It can take significant time from the capacity of the QA team. However, for people to be motivated, it should be relatively easy to create new tests. For sure, QA engineers will be frustrated if for the whole sprint can produce only a few tests. It shouldn't be a rocket science to create a simple test. Even if the scenario is more complicated, it shouldn't be so hard to understand what needs to be done to automate it."
In the series, I already wrote about how you can create standardized page objects fast with the help of VS item templates, in Standardize Page Objects with Visual Studio Item Templates and Create Multiple Files Page Objects with Visual Studio Item Templates. In this article, I am going to share another way you can speed up test development — automating test project creation. Sometimes depending on the tools and frameworks, you use in your project it might cost more than an hour or configure correctly a project containing automated tests. In this case, the project templates are invaluable.
In the last article from the series, I showed you how to create Visual Studio project templates. However, as you may guess, they are working only for Visual Studio on Windows. In this publication, I am going to show you how to create cross-platform .NET Core project templates via NuGet packages. If you target .NET framework, you should use the VS project templates.
History
While working on the Bellatrix project, we wanted to find ways to solve all 40+ problems in the best possible way. There were a couple of things that we had to address- distribute the getting-started projects (the so-called starter-kits) across all platforms and give people a fast way to create new Bellatrix test projects.
So, we used 3 different solutions for test project creation: Visual Studio project templates, .NET Core templates, and custom made cross-platform templates.
How Do Project Templates Help You?
Without the templates, each one of our users had to spend 20-30 minutes to read how to configure every little detail. The templates hide all nitty-gritty configuration details. Even if your projects are much simple and you save 10 minutes each week or two per QA, it is something. Also, templates give you a way to standardize dependent library versions and not forgetting adding some of them or some necessary file.
Prepare a Test Project Template
Let's imagine that we want to have a way to create fast a project for WebDriver tests using MSTest framework. Additionally, we want our colleagues to follow all team standards about writing high-quality tests, so we want to incorporate in the project: StyleCop and EditorConfig.
Before using some of the methods for creation of project templates, we need to configure a sample test project.
Here is what the project contains.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>StyleCopeRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0-preview-20180807-05" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.21.0" />
<PackageReference Include="Selenium.Support" Version="3.14.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.14.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="2.41.0" />
<PackageReference Include="Selenium.WebDriver.IEDriver" Version="3.14.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta008">
<NoWarn>NU1701</NoWarn>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Update=".editorconfig">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="stylecop.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="StyleCopeRules.ruleset">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
All required NuGet packages are installed: WebDriver and MSTest. Additionally, StyleCop packages are included. For the static analysis to work: .editorconfig and StyleCopRules.ruleset files are included.
We even created an example test in the SampleTest.cs file.
Create .NET Core Project Template
1. Create a folder content under your project
2. Move all project files that should be part of the template under the content folder
3. Under the content folder, create a folder named .template.config
4. Under .template.config create a new file template.json
5. Populate the template's information
{
"$schema": "http://json.schemastore.org/template",
"author": "Anton Angelov",
"classifications": [ "WebDriver", "Test", "UITest" ],
"identity": "WebDriverTestsProject",
"name": "MSTest WebDriver Test Project",
"shortName": "mstest-webdriver-test-proj"
}
Find more information about all fields in the official documentation.
6. Next to the content folder, add a new nuspec file
7. Populate project template's data in the nuspec file
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>WebDriverTestsProject</id>
<version>1.0.0</version>
<description>
Creates the WebDriver MSTest test project.
</description>
<authors>Anton Angelov</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
8. Create the project package using nuget.exe
nuget pack <pathToProject>WebDriverTestsProjectTemplate.nuspec
Make sure you download the latest version of nuget.exe.
After you run the command in the folder will be generated a file called WebDriverTestsProject.1.0.0.nupkg. Later, we can use this NuGet package to create new projects.
Create a New Project Using NuGet Package
You can create a new project from a local nupkg file. To do so, follow the instructions.
1. Navigate to the folder where you want to create the new project
2. Open the command prompt
3. Use the dotnet new
command
dotnet new -i <pathToPackage>WebDriverTestsProject.1.0.0.nupkg
A better way to distribute your nupkg project templates is to publish them to nuget.org. To publish a NuGet package, follow the instructions in the Create and publish a package topic.
If you wish to install a template from a NuGet package stored at nuget.org, use the dotnet new
command with the -i|--install
option and supply the name of the NuGet package.
dotnet new -i WebDriverTestsProject.1.0.0.nupkg
Summary
In these articles, we talked about some of the problems that project templates solve and some of their benefits. I told you a short story about why I had to read about them in the first place. After that, I showed you how to create and use .NET Core project templates.
Published at DZone with permission of Anton Angelov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments