Using bUnit, xUnit and an In-Process Generic Host For Full Stack Functional Test of Blazor Page #286
dcs3spp
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As requested, following a recent discussion with @egil, I am providing details of how I have been using bUnit to perform a full stack functional test, using some abstractions that I have created.
I have implemented a Blazor Server architecture that is using signalR and the following external dependencies:
I am using xUnit and bUnit to test that motion detection notifications appear on a Blazor Server dashboard page upon receipt of an MQTT message at the server.
I orignally started with using a WebApplicationFactory for full stack functional test. However, the WebApplicationFactory is in-memory process only and I experienced problems connecting signalR client to the signalR hub. From what I understand there is no notion of websockets or network sockets with
WebApplicationFactory
?. Subsequently, I started to use a generic in-process host.I am using two docker-compose stacks to host the required external dependencies for use with functional tests of my Blazor Server Page:
Local Testing (external dependencies in docker-compose, SUT + tests on local machine)
External dependencies (kafka, mqtt, s3) run within a docker-compose stack. This is started automatically via VS tasks before testing begins. Secrets are initialised from .env file. xUnit/bUnit tests run on local development machine.
An in-process generic host automatically starts before the test collection begins. This host is initialised with the services (clients) required to connect to the external dependencies hosted within the docker-compose stack. The generic host itself does not contain the xUnit/bUnit tests.
The xUnit tests run locally. Before each test begins a bUnit test context is created to represent the Blazor Server page under test (PUT). The service dependencies are injected into this test context, taken from the in-process generic host. In this example the service clients for Kafka, Mqtt and S3 Minio are injected.
The test runs and once completed the bUnit test context associated with that test is disposed.
I am using GitLab Continuous Integration, which is docker based. I experienced problems running the above setup on the CI server, i.e. the tests are not running on the same docker network as the external dependencies. Subsequently, this led to refactoring and adding the tests to the docker-compose stack so that everything is in the same network, as explained below.
CI Server (external dependencies and SUT + tests run in docker-compose)
External dependencies (kafka, mqtt, s3) and SUT+tests run within docker-compose. Everything is on the same docker network. This allows me to run the tests on local development machine and on the Gitlab CI server. The environment is replicable. Secrets are initialised from environment variables.
Everything else is the same as before, except the in-process generic host and xUnit/bUnit tests are now running within a docker-compose service on the same network as the external dependencies.
Test Abstractions
I have included a small repository of test abstractions for using an in-process generic host with xUnit. The abstractions include:
Hopefully, this may help others getting started for performing some form of full stack functional / server end-to-end testing with bUnit, where a WebApplicationFactory cannot be used, e.g. in architectures that use signalR websockets.
The repository includes a README explaining use of the abstractions.
Beta Was this translation helpful? Give feedback.
All reactions