With the old version of Specflow (3.0.0.0, and with .NET Framework 4.7.2), I could add Console.Writeline() into the step methods, and I ran the tests via Azure pipeline, on the Test tab I can get to the failed test cases, and with each of them, a file named Standard_Console_Output.log is attached. In that file, I can see my logs emitted with Console.Writeline(). It looks like:
When I do something
My custom logs
-> done ...
Now, with NET 6.0 and Specflow 3.9, this approach no longer works, Console.Writeline() emits nothing in that file.
How to add log to that console output file? Thank you.
I put Console.Writeline()
in my steps, and nothing was added to the Standard_Console_Output.log file.
ISpecFlowOutputHelper never works. I followed the document's instructions and received error: System.InvalidOperationException : Unable to resolve service for type 'TechTalk.SpecFlow.Infrastructure.ISpecFlowOutputHelper'
If I register ISpecFlowOutputHelper myself with
services.AddSingleton<ISpecFlowOutputHelper, SpecFlowOutputHelper>();
I received error complaining another dependency class cannot be resolved. It never ends...
It appears to me Specflow's document is outdated.
I use VS Professional 2022 (64-bit) 17.12.1 on Windows 10, and with SpecFlow for VS 2022 extension (ver: 2022.1.91.26832).
I created this example project with the SpecFlow extension, the template only supports .NET 6, but I changed it to 8.0 manually by editing the .csproj file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
<PackageReference Include="SpecFlow.xUnit" Version="3.9.74" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
</Project>
./features/Calculator.feature
Feature: Calculator
@experimental
Scenario: Add two numbers
Given the first number is 50
Then the result should be 120
./StepDefinitions/CalculatorStepDefinitions.cs
using TechTalk.SpecFlow;
using System;
using Xunit;
namespace SpecFlowLogTest.StepDefinitions
{
[Binding]
public sealed class CalculatorStepDefinitions
{
[Given("the first number is (.*)")]
public void GivenTheFirstNumberIs(int number)
{
Console.WriteLine("The first number is " + number);
Assert.Equal(1, 2); // the Standard_Console_Output.log file is only attached when the test fails
}
[Then("the result should be (.*)")]
public void ThenTheResultShouldBe(int result)
{
//TODO: implement assert (verification) logic
throw new PendingStepException();
}
}
}
I have tested this example project in Azure pipeline, and in the Standard_Console_Output.log file, the content reads:
Given the first number is 50
-> error: Assert.Equal() Failure: Values differ
Expected: 1
Actual: 2 (0.0s)
Then the result should be 120
-> skipped because of previous errors
I expect "The first number is 50". Please note that this log doesn't even show up when running on my PC locally.
The output API is something you get for free with SpecFlow. All you need to do is declare a constructor parameter:
namespace SpecFlowLogTest.StepDefinitions
{
[Binding]
public sealed class CalculatorStepDefinitions
{
private readonly ISpecFlowOutputHelper output;
public CalculatorStepDefinitions(ISpecFlowOutputHelper output)
{ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this.output = output;
}// ^^^^^^^^^^^^^^^^^^^^
[Given("the first number is (.*)")]
public void GivenTheFirstNumberIs(int number)
{
output.WriteLine("The first number is " + number);
// ^^^^^^
Assert.Equal(1, 2); // the Standard_Console_Output.log file is only attached when the test fails
}
[Then("the result should be (.*)")]
public void ThenTheResultShouldBe(int result)
{
//TODO: implement assert (verification) logic
throw new PendingStepException();
}
}
}
If this isn't working then consider upgrading to the latest SpecFlow, and possibly upgrading to .NET 8.