XUnit Testing Tutorial: Environment Setup For Selenium Testing
Tags: xunit, testing, tutorial, programming
Published: 2020-06-12 11:00:02

xUnit.net (also referred to as xUnit) framework is a popular open-source unit testing framework for the .Net platform. The framework is built with a community focus. Since there is a focus on the community, it is easier to expand upon than other popular [Selenium testing](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=webpage) frameworks.

As xUnit is written by the creators of the NUnit framework, you would find a lot of similarities between the two frameworks. The intention of creating a new [unit testing framework ](https://www.lambdatest.com/blog/11-best-unit-testing-frameworks-for-selenium-automation/?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=blog)was to build a much better framework from ground-up.

Unlike other popular frameworks for automation testing with Selenium C#, like NUnit and MSTest, this test framework follows a more unique style of attributes/annotations.

The xUnit framework has fewer attributes in comparison to the NUnit and MSTest frameworks. The other advantage of using xUnit is that the Selenium test automation framework creates a new instance of a test class for every test. At the time of writing this blog, the latest version of xUnit was 2.4.1.

In this xUnit testing tutorial, I’ll take a detailed look at setting up the xUnit framework (or xUnit setup example) which can help you get started with xUnit (or xUnit.net) on Visual Studio.

**_In this [System testing](https://www.lambdatest.com/learning-hub/system-testing?utm_source=devto&utm_medium=group&utm_campaign=mar29_bh&utm_term=bh&utm_content=learning_hub) tutorial, learn why System testing is important and all the intricacies of the System testing process._**

**_Run your [Selenium Automation Testing](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=webpage) scripts on the LambdaTest cloud grid. Test on 3000+ desktop & mobile environments._**

## Setting Up Visual Studio For Development And Testing

For development, I’ll use the Visual Studio 2019 (VS 2019) IDE. Before looking into the xUnit setup example, I’ll take a brief look at setting up Visual Studio for automation testing with Selenium C#.

**Step 1:** Download the latest version of Visual Studio 2019. You can choose from the Community/Professional/Enterprise versions. In this xUnit testing tutorial, I have used the Community Edition of Visual Studio 2019.

![](https://cdn-images-1.medium.com/max/2000/0*OvD_CEAr4a5g5grf.png)



**Step 2:**In this xUnit testing tutorial, the xUnit test framework will be used along with Selenium for test automation. Hence, I’ll install the necessary packages that aid in automation testing with Selenium C# on the Windows platform.

Launch the Visual Studio:

![](https://cdn-images-1.medium.com/max/2562/0*PhOh-uzySCvGY420.png)

Install the necessary packages:

![](https://cdn-images-1.medium.com/max/2634/0*NegCL7nj8qG2d-GD.png)

**Step 3:** In case you are also installing the Community Edition of Visual Studio, it is important that you sign-in to the IDE else you would not be able to use the IDE after the trial period (approximately 30 days). Once you sign-in, you can also utilize other powerful features such as pushing source code to private Git, syncing Visual Studio settings, and more.

In the next section of the xUnit testing tutorial, I’ll take a look at the steps to install the mandatory packages for performing automation testing with Selenium C# using xUnit framework.

This xUnit Tutorial for beginners and professionals will help you learn how to use xUnit framework with Selenium C# for performing Selenium automation testing.
{% youtube gOC_lCz2zXs %}
## Installing The xUnit Framework & xUnit.net Runner

For executing tests that make use of the xUnit framework, you’ll have to install the corresponding test runner i.e. xUnit.net runner. Installation of the xUnit framework and its runner can be done by installing the packages using the package manager from GUI or executing the equivalent package manager (PM) commands on the terminal.

For this xUnit testing tutorial, I’ll explore both these options for installing the xUnit framework. Before the installation of the required packages, you’ll have to perform the following steps:

**Step 1:** Create a new project of the type ‘xUnit Test Project (.Net Core)’ in Visual Studio.

![](https://cdn-images-1.medium.com/max/2000/0*vbeneIlnLouAq_H1.png)

**Step 2:** Since I have created the project based on the xUnit.net framework, the default .cs file contains the [Fact] attribute in it. Also, the xUnit package is also included by default in the source code.

![](https://cdn-images-1.medium.com/max/2000/0*AXh0GhuVdCCCv_3O.png)

In case you have not created a project of the type mentioned above, you can still use the xUnit framework for performing [automation testing with Selenium C#](https://www.lambdatest.com/blog/nunit-testing-tutorial-for-selenium-csharp/). The xUnit framework and the runner can be installed using the package manager from GUI or the terminal.

Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.

Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:
{% youtube ZU85Mjwgv54 %}
**_Automate [Cypress testing ](https://www.lambdatest.com/cypress-testing?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=webpage)and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop & mobile environments._**

## Using Visual Studio IDE

For installing the xUnit test framework and other required packages for performing Selenium test automation with xUnit, perform the following steps:

**Step 1:** Go to **Tools -> NuGet Package Manager -> Manager NuGet Packages for Solution** for opening NuGet Package Manager in Visual Studio.

![](https://cdn-images-1.medium.com/max/2000/0*i6RDnkV5bzEjYcb2.png)

**Step 2:** Search for the following packages in the Browse tab and click **Install**

* xunit

* xunit.runner.visualstudio

* Microsoft.NET.Test.Sdk

![](https://cdn-images-1.medium.com/max/2114/0*SR2DDJbrt80YUde-.png)

## Using Package Manager (PM) Commands

If you are comfortable using the terminal, then you should look at the option of installing the xUnit framework using the Package Manager (PM) commands.

**Step 1:** For executing commands from the PM console, navigate to **Tools -> NuGet Package Manager -> Package Manager Console.**

![](https://cdn-images-1.medium.com/max/2000/0*vshSmOXxVqm6dfvx.png)

**Step 2:** For installation of the xUnit packages, we use Install-Package command with the required as the argument to the command.

    Install-Package xunit
    Install-Package xunit.runner.visualstudio
    Install-Package Microsoft.NET.Test.Sdk
    

Shown below are the package installation snapshots:



![](https://cdn-images-1.medium.com/max/2000/0*8sclNE21N8on0BKN.png)

![](https://cdn-images-1.medium.com/max/2038/0*sllKHbvO9O6zV-eb.png)

**Step 3: **To check whether the packages have been installed successfully, execute the Get-Package command on the PM console. Shown below is the execution output:

    PM> Get-Package
     
    Id                                  Versions
    --                                  --------
    xunit                               {2.4.1}
    xunit.runner.visualstudio           {2.4.1}
    Microsoft.NET.Test.Sdk              {16.2.0}

**_This [Cypress test automation](https://www.lambdatest.com/blog/cypress-test-automation-framework/?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=blog) tutorial will help you learn the benefits of Cypress automation, and how to install Cypress and execute Cypress automation testing over scores of browsers and operating systems online._**

## Wrapping It Up!

With this crisp xUnit setup example for [Selenium test automation](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=webpage), I demonstrated how to get started with the xUnit (or xUnit.net) framework for Visual Studio. Steps shown in the xUnit testing tutorial will be instrumental in performing automation testing with[ Selenium C# frameworks](https://www.lambdatest.com/blog/top-selenium-csharp-framework/?utm_source=devto&utm_medium=organic&utm_campaign=mar21_bh&utm_term=bh&utm_content=blog).

This brings us to the conclusion of this article. You can go ahead and set up the xUnit environment for yourself. We’d have covered a lot of articles before on automation testing with Selenium C#. Feel free to check them out, and do help us to reach out to more people by sharing and retweeting our article. That’s all folks. Happy Testing!!!?

**_A complete [Manual testing](https://www.lambdatest.com/learning-hub/manual-testing?utm_source=devto&utm_medium=group&utm_campaign=mar29_bh&utm_term=bh&utm_content=learning_hub) tutorial covering all aspects of Manual testing, including strategies and best practices._**