Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to create md based report #10

Closed
paulvanbladel opened this issue Aug 11, 2021 · 7 comments
Closed

Option to create md based report #10

paulvanbladel opened this issue Aug 11, 2021 · 7 comments

Comments

@paulvanbladel
Copy link

For smooth communication with the business stakeholders, it could be useful to be able to generate from the test output a .md file per scenario. A build server task could publish then the md material to a place where it has valuable meaning for stakeholders.

@cezarypiatek
Copy link
Owner

This feature should be available in v3.3.0. For more details please review readme section https://github.com/cezarypiatek/NScenario#exporting-scenario-transcription

@paulvanbladel
Copy link
Author

Brilliant. Thanks a lot.

@paulvanbladel
Copy link
Author

Cezary,
Works great.
I'm using XUnit and also outputting standard output to the test output window via

public class XUnitOutputAdapter : TextWriter
    {
        private readonly ITestOutputHelper _output;
        public XUnitOutputAdapter(ITestOutputHelper output) => _output = output;
        public override void WriteLine(string? value) => _output.WriteLine(value);
        public override Encoding Encoding { get; }
    }

I created also a IClassFixture for setting up the plumbing for the md reporting as follows

 public class MarkdownReportSetupCode : IDisposable
    {
        private readonly MarkdownFormatterOutputWriter _reportWriter = new MarkdownFormatterOutputWriter(title: "Sample tests with NScenario");
        public MarkdownReportSetupCode()
        {
            TestScenarioFactory.DefaultScenarioOutputWriter = new ComposeScenarioOutputWriter(new IScenarioOutputWriter[]
               { 
                    //INFO: Configure live reporting to console with NUnit
                    // new StreamScenarioOutputWriter(TestContext.Progress),
                    //INFO: Configure collecting transcription as markdown
                    _reportWriter, 


               });
            
        }

        public void Dispose()
        {
            // clean-up code
            _reportWriter.Save("Report.md");
            _reportWriter.ExportToHtml("Report.html");
        }
    }

So, my test class derives uses that fixture:

public partial class MyTestClass: IClassFixture<MarkdownReportSetupCode>
    {
        private readonly TestApplication _testApplication;
        private readonly ITestOutputHelper _output;
        private readonly MarkdownReportSetupCode _mdSetup;


        public MyTestClass(ITestOutputHelper output, MarkdownReportSetupCode mdSetup)
        {

            this._testApplication = new TestApplication(); // specific for asp.Net core testing...
            this._output = output;
            this._mdSetup= mdSetup;

        }


        [Fact]
        public async Task Happy_Flow_Create_Package_List_And_Retrieve_Details()
        {
            var scenario = TestScenarioFactory.Default(new XUnitOutputAdapter(_output));

Now, unfortunately I cannot combine both output in console and output in md.
When I change to var scenario = TestScenarioFactory.Default();
then the md file is generated, but no longer output to console.

I guess it must be something trivial, but I cannot find it :)

All the best to you and our Ukrainian friends.

@cezarypiatek
Copy link
Owner

I'm not an expert in XUnit but I think you cannot use IClassFixture as it's executed per test class (if you have more than one test class it will be executed multiple times). Probably you need to use Collection FIxtures https://xunit.net/docs/shared-context

I've added overload to TestScenarioFactory that should help with this problem. I've also updated sample project for XUnit.

Please try the following solution:

[CollectionDefinition("NScenarioCollection")]
public class NScenarioCollection : ICollectionFixture<NScenarioReporting>
{
    // This class has no code, and is never created. Its purpose is simply
    // to be the place to apply [CollectionDefinition] and all the
    // ICollectionFixture<> interfaces.
}

public class NScenarioReporting : IDisposable
{
    public MarkdownFormatterOutputWriter ReportWriter { get; } = new MarkdownFormatterOutputWriter(title: "Sample tests with NScenario");

    public NScenarioReporting()
    {
    }

    public void Dispose()
    {
        // clean-up code
        ReportWriter.Save("Report.md");
        ReportWriter.ExportToHtml("Report.html");
    }
}

[Collection("NScenarioCollection")]
public class Tests 
{
    private readonly IScenarioOutputWriter scenarioOutputWriter;
    public Tests(ITestOutputHelper output, NScenarioReporting mdSetup)
    {
        scenarioOutputWriter = new ComposeScenarioOutputWriter(new IScenarioOutputWriter[]
        {
            mdSetup.ReportWriter,
            new StreamScenarioOutputWriter(new XUnitOutputAdapter(output))
        });
    }


    [Fact]
    public async Task should_present_basic_scenario()
    {
        var scenario = TestScenarioFactory.Default(scenarioOutputWriter);

        await scenario.Step("This is the first step", () =>
        {
            // Here comes the logic
        });
        
        await scenario.Step("This is the second step", () =>
        {
            // Here comes the logic
        });
        
        await scenario.Step("This is the third step", () =>
        {
            // Here comes the logic
        });
    }
}

Not sure how this behaves when tests are run in parallel....

@paulvanbladel
Copy link
Author

If i understand you correctly, combining both outputs (md and console) works seamless when using NUnit?
I'm not married with Xunit and anyhow these type of scenariotests have their own assembly, so i guess i'll move to nunit.

@cezarypiatek
Copy link
Owner

I'm using NUnit+NScenario on a daily basis and it works fine. Test scenario transcriptions exported to HTML are later referenced in TeamCity as Report Tab https://www.jetbrains.com/help/teamcity/including-third-party-reports-in-the-build-results.html - very useful ;)

@paulvanbladel
Copy link
Author

HI Cezary,
I trust you are fine.
Imagine you have a set of tests structured in folder and subfolders, which could provide together with the test class names semantic meaning to the report.
What would be the way to proceed to incorporate these structural elements in the md report?
Thanks !
paul.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants