Setting up a test project in Litium
To make it easy to set up test projects we have created a bootstrapper package for Xunit. Use this for testing directly against Litium.
Read the system requirements for the development environment before installing.
Installation
- Open Visual Studio.
- Create an empty class library project application that uses .NET framework 4.7.2 or later.
- Open Package Manager Console from the Tools > NuGet Package Manager menu. If there are multiple projects in the solution, make sure that you select the correct project as Default project in Package Manager Console.
- Install Litium Xunit nuget-packages with the following command in Package Manager Console:
Install-Package Litium.Xunit.Bootstrapper
Install-Package Litium.Setup.Complete
- Install Xunit runner nuget-packages with the following command in Package Manager Console:
Install-Package xunit.runner.visualstudio
or
Install-Package xunit.runner.console
- Update the following settings in the application config (app.config):
- filesDirectory: Relative paths will work.
Usage
When the Litium.Xunit.Bootstrapper package is installed a class is created in the root of the project with the name ApplicationTestBase. The class will use the class library namespace. This class should be inherited in the tests that need the Litium application running.
The data population when the application starts is done in the constructor of ApplicationTestBase, which is executed once for all tests.
Example of test:
using Litium.Foundation;
using Xunit;
public class SolutionTests : LitiumApplicationTestBase
{
[Fact]
public void Exists_instance()
{
Assert.True(Solution.ExistsInstance);
}
[Fact]
public void Instance_not_null()
{
Assert.NotNull(Solution.Instance);
}
}
Troubleshooting
If the assembly references have not been set up properly, you may receive the following error message when building the test project:
Message: System.Exception : The project requires references that are missing: Litium.Web.Abstractions requires a reference to Litium.Web.Application, Litium.Web.Mvc.Abstraction requires a reference to Litium.Web.Application, Litium.Web.WebApi.Abstractions requires a reference to Litium.Web.Application. Add missing references via NuGet or turn off this validation by adding <add key="LitiumXunitAssemblyValidation" value="False" /> to appSettings in the config file. Note that disabling the validation without adding the reference may cause tests to fail.
This will most likely cause the tests to fail. Add the references according to the message.
It is not recommended to turn off validation as you will receive other error messages about missing services that are required for Litium to run. The validation is used by the LitiumApplicationTestBase class, which is inherited in the tests that need Litium running. If you do not want Litium to run as an application, the tests should not inherit the LitiumApplicationTestBase class, which means the validation will not be run.