Custom Tests with C#Bot
By default, C#Bot writes tests for the client-side, server-side and test target.
- Client-side: These tests reside in the
clientside
in form of*.tsx
files. React uses Jest by default. - Server-side: These tests reside in the
testtarget/serverside
directory. C# uses XUnit bundled with its test framework for unit and integration tests. - End user: These tests reside in the
testtarget/selenium
directory. Unlike the server-side tests, these tests focus on end-to-end or user acceptance testing with Selenium, XUnit and SpecFlow 3. - API: These tests reside in the
testtarget/api
directory. These tests focus on testing API endpoints using Restsharp and XUnit.
We will be making a simple Test Target
Specflow test. The test will be a custom invalid login test that uses a mixture of C# bot-written steps and a custom step.
Setting Up The Solution
Navigate to your root project directory and run the following commands to setup the solution:
dotnet new sln
dotnet sln add ./testtarget/TestDataLib/TestDataLib.csproj
dotnet sln add ./testtarget/Selenium/SeleniumTests.csproj
dotnet sln add ./testtarget/API/APITests.csproj
dotnet sln add ./testtarget/Serverside/ServersideTests.csproj
dotnet sln add ./serverside/src/[PROJECTNAME].csproj
You should now be able to open the solution in your IDE.
Feature Files
The first step is to setup a Specflow Feature file. To do this, create a file in the /testtarget/Selenium/Tests/Manual/
folder called CustomFishnaticsLoginTests.feature
. Open this file in your editor of choice, and paste in the following:
Feature: CustomFishnaticsLoginTests
Custom login tests
@new
Scenario: I attempt to login to fishnatics with invalid credentials
Given I login to the site with username notreal@example.com and password notreal then I expect login failure
Then I want to print a simple message "notreal@example.com cannot be logged in" to the test output
The first Given
step is already defined as a bot-written step in the Fishnatics
project. The second Then
step is undefined and will need to be defined.
Step Definition
Navigate to the /testtarget/Selenium/Steps/Manual/
folder and create new file called CustomFishnaticsLoginSteps.cs
. Open the file and paste in the following code.
using SeleniumTests.Setup;
using TechTalk.SpecFlow;
namespace SeleniumTests.Steps.Manual
{
[Binding]
public class CustomFishnaticsLoginSteps
{
private readonly ContextConfiguration _context;
public CustomFishnaticsLoginSteps(ContextConfiguration contextConfiguration)
{
_context = contextConfiguration;
}
}
}
The contextConfiguration
gives access to all the important tools for building integrations tests (e.g. webDriver, testOutputHelper). We will use it in our step to print a simple message to the test output.
Add the following step definition to the CustomFishnaticsLoginSteps
class.
[Then(@"I want to print a simple message ""([^""]*)"" to the test output")]
public void ThenIWantToPrintASimpleMessageToTheTestOutput(string message)
{
_context.TestOutputHelper.WriteLine(message);
}
Running Tests
Tests can be run through the command line, or through an IDE. Before running any Specflow tests, ensure that both server and client are running.
Command Line
Navigate to /testtarget/selenium/
and run the command:
dotnet test --filter Category=new
We use the @new
tag added in the .feature
file to run specific tests or groups of tests, by using the --filter Category=[tag]
.
Visual Studio
In order to run the tests through Visual Studio, the Specflow package needs to be installed. Follow the steps in this guide from Specflow to install Specflow for Visual Studio.
Open up the test explorer by going to Test -> Test Explorer
in the Visual Studio navigation bar. Now find the CustomFishnaticsLoginTestsFeature
in the test explorer, right click on it, and select Run
.

Was this article helpful?