SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.25k stars 754 forks source link

Colorize test output #2605

Open Socolin opened 2 years ago

Socolin commented 2 years ago

Link to Feature Request

No response

Implementation Idea

Hello,

I would like to add some colors in the specflow output, it'll make it simpler and easier to find what step failed when reading the test output. Since Visual Studio does not support it this feature would impact only Rider users and test output on build servers (dotnet test)

Here a preview of what it can look like image

By default this feature would be disabled (so it does not break anything) then people could enable it with the config trace.coloredOutput

If the color is problematic in some context it's possible to disable it with NO_COLOR environment variable (https://no-color.org/).

For teams using Rider and VS I would need a bit of help to find a way to detect if the execution is done through VS (I don't have VS so I cannot do test, but maybe there is an environment variables set we can use to detect it, otherwise since Rider allow to provide custom env variable in test runner we could add a FORCE_COLOR too, ignoring the configuration)

I have a WIP so you can test it already, I'll wait to get some feedback before writting test & co https://github.com/Socolin/SpecFlow/commits/color-output

The colors can be configure by injecting IColorOutputTheme so users can decide programatically the colors and adjust if needed (or add Bold / underline etc...) I don't know if we want to allow the color to be configure with specflow.json too

[Binding]
public class Hooks
{
    [BeforeTestRun]
    public static void ConfigureColor(IColorOutputTheme colorOutputTheme)
    {
        colorOutputTheme.Keyword = AnsiColor.Reset;
        colorOutputTheme.Error = AnsiColor.Composite(AnsiColor.Bold, AnsiColor.Foreground(TerminalRgbColor.FromHex("FF8EF3")));
        colorOutputTheme.Done = AnsiColor.Foreground(TerminalRgbColor.FromHex("3A86FF"));
    }
}

Would give an output like image

Notes

in dotnet test NUnit and xUnit are currently escaping color codes so to test without rider I would recommend to use MSTest for now. I'm working on a fix for NUnit and I opened an issue for xUnit

SabotageAndi commented 2 years ago

@gasparnagy your two cents to this?

I am fine with this.

gasparnagy commented 2 years ago

Looks interesting. We can try in an opt-in fession without too much risk... @Socolin could you please try to make a PR for this so that it is easier to review/test the changes?