SpecFlow tests for .NET Core Web Apps with Playwright

Specflow is a fantastic framework for codifying your requirements. It allows writing human-readable scenarios which map to C# code. This, coupled with a browser automation tool like playwright, puppeteer, or selenium can create an incredibly powerful and versatile testing framework.

In this article, I’ll show you how to set up a basic test framework for the default MVC project (though the underlying principles apply to any .NET project really).

As a starting point, I've created a very simple MVC application (the default one from VS2019). It had the following structure:

Structure of your main application

And looked like this:

Then we'll need to setup a test project, that I have called TestSpecflowApplication.Test:

The structure of your test project

You will need the following NuGet packages installed in your project:

For our testing, we'll need 3 files, the feature file with the SpecFlow test, the SpecFlow implementation behind each step, and the Kestrel server setup.

For our SpecFlow feature called HomeScreen.feature, will check for the Welcome header on the home page:

The next step is to tie the scenario steps to step definitions

Finally, before we run any tests, we need to make sure Kestrel server is up and running:

Fingers Crossed:

Passing Test using Specflow and Kestrel

Conclusion

This is obviously a very simple example. The handling of Playwright was a little bit hacked, perhaps use it with BoDi dependency injection.

In this code, we also don't have a database. In memory DB could be an interesting use case here, but perhaps setting up a local instance of SQL db could go a long way, could be deleted afterwards.

The benefit of running the tests also could be docerized and don't interferrer with any production code.

There is a also the question of the environment (you want to run your tests against prod environment right?).