Search code examples
c#.net-6.0specflowgherkin

Using Specflow without the unit testing frameworks


So for my current project I need to perform several tests on a (physical) system. Within my company we have this testing framework, which provides several features out of the box, such as writing my results to a database and such.

Now I've heard about BDD and I found that a gherkin way of writing these tests would be really nice and clean. So I started looking into Specflow, which looks really good. Only problem with Specflow is that you are required to use a Unit Testing framework. This is not at all what I want, since my own framework already provides all this stuff. Starting a test for example needs to be done by an operator from a GUI. So the unit testing framework is not needed at all.

Now my question is whether I can use Specflow simply to generate these tests (within their own test class or something) and keep using my own framework.

So as an example, what I'm looking for is something along these lines:

Given that the machine is in base state
When we open the valve
Then the valve sensor should have value higher than 450

Then have a StepDefinition file, which will contain the actual machine interface (sample code ofc)

public void MachineInBaseState()
{
    machine.GoToBaseState();
}
public void ValvePosition(bool open)
{
    machine.SetValve(open);
}
public void CheckValveSensorPosition(int valueToCheckAgainst)
{
    testResults.Add(new ValueResult(value:machine.GetSensorValue(),min:valueToCheckAgainst,max:double.NaN));
}

Then during build of the project, to have the gherkin file be generated in such a way that my own testing framework can pick up the generated testresults.

I know that this may not be something that a lot of people do, but maybe someone has seen something like it before.

Another way of course is to write a source generator, but that will take a lot more time.


Solution

  • You would need to build your own UnitTestProvider for your framework.

    One for compile time (when the code behind files are generated): https://github.com/SpecFlowOSS/SpecFlow/tree/master/TechTalk.SpecFlow.Generator/UnitTestProvider

    This is what the content of the feature.cs files generates.

    And one for runtime: like the xUnit one https://github.com/SpecFlowOSS/SpecFlow/blob/master/Plugins/TechTalk.SpecFlow.xUnit.SpecFlowPlugin/XUnitRuntimeProvider.cs

    All this can be plugged into SpecFlow via Plugins, but it is not the easiest to do.