{"_id":"54cd1967380ecd0d00ca1963","user":"54c4ca54686bce0d00efe1b9","project":"54c51af02418480d0028a2b4","version":{"_id":"54c51af02418480d0028a2b7","project":"54c51af02418480d0028a2b4","__v":3,"createdAt":"2015-01-25T16:33:52.715Z","releaseDate":"2015-01-25T16:33:52.715Z","categories":["54c51af12418480d0028a2b8","54cd18ea380ecd0d00ca1961","54cd18f0380ecd0d00ca1962"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"54cd18ea380ecd0d00ca1961","__v":4,"pages":["54cd193f3be4370d0082ad26","54cd1967380ecd0d00ca1963","54cd19841097810d0009ab88","54cd199b1097810d0009ab8a"],"project":"54c51af02418480d0028a2b4","version":"54c51af02418480d0028a2b7","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-01-31T18:03:22.937Z","from_sync":false,"order":2,"slug":"customization","title":"Customization"},"__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-01-31T18:05:27.014Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"This post looks at how to customize the HTML Report and how to create your own custom reports. You can customize the HTML report via configuration or by applying custom CSS or JavaScript. You can create custom reports either by implementing a new Processor or Batch Processor and adding them into their respective pipelines (see the [Architecture Overview](/BDDfy/Customizing/ArchitectureOverview.html) for details about the differences between these).\n\n## Customizing the HTML Report ##\nThe HTML report is the most sophisticated report in [BDDfy](http://teststack.github.io/pages/BDDfy.html) and therefore provides a lot more things that you can configure. Its configuration is defined by the IHtmlReportConfiguration interface.\n\n\tpublic interface IHtmlReportConfiguration\n\t{\n\t    string ReportHeader { get; }\n\t    string ReportDescription { get; }\n\t    string OutputPath { get; }\n\t    string OutputFileName { get; }\n\t    bool RunsOn(Story story);\n\t}\n\nYou can create a new configuration by implementing that interface or you can inherit from the  DefaultHtmlReportConfiguration class, used to configure the standard HTML Report, and just override specific properties.  Here is an example of a custom configuration, taken from the ATM sample, available on github [here](https://github.com/TestStack/TestStack.BDDfy/tree/master/TestStack.BDDfy.Samples/Atm).\n\n    public class HtmlReportConfig : DefaultHtmlReportConfiguration\n    {\n        public override bool RunsOn(Core.Story story)\n        {\n            return story.MetaData.Type.Namespace != null && story.MetaData.Type.Namespace.EndsWith(\"Atm\");\n        }\n\n        /// <summary>\n        /// Change the output file name\n        /// </summary>\n        public override string OutputFileName\n        {\n            get\n            {\n                return \"ATM.html\";\n            }\n        }\n\n        /// <summary>\n        /// Change the report header to your project\n        /// </summary>\n        public override string ReportHeader\n        {\n            get\n            {\n                return \"ATM Solutions\";\n            }\n        }\n\n        /// <summary>\n        /// Change the report description\n        /// </summary>\n        public override string ReportDescription\n        {\n            get\n            {\n                return \"A reliable solution for your offline banking needs\";\n            }\n        }\n    }\n\nwhich produces the following customised report, which you will find in your bin directory named ATM.html:\n\n![BDDfy functional decomposition](/img/BDDfy/Customizing/bddfy-sample-atm-html-custom.png)\n\n\nThe HTML report is a [Batch Processor](/BDDfy/Customizing/ArchitectureOverview.html) and is implemented by the HtmlReporter class. To plug the new report into BDDfy you need to create a new HtmlReporter and pass the custom configuration into its constructor. As I explained in the [Reports post](/BDDfy/Usage/Reports.html), the place to apply that configuration to BDDfy is the Configurator class, which is called before the tests run. \n\n\tConfigurator.BatchProcessors.Add(new HtmlReporter(new HtmlReportConfig()));\n\nThe use of the Add method means this is adding a second HTML Report processor into the Batch Processor pipeline, so the default report runner will still run. If you actually want the new report to replace the default report, then you will also need to disable the default report.\n\n\tConfigurator.BatchProcessors.HtmlReport.Disable();\n\n## Custom CSS and JavaScript ##\nYou can customize a lot more about the HTML report. BDDfy uses the BDDfy.css file to style the report and BDDfy.js and jQuery to add interactivity to it. You will find these files in the bin directory alongside the HTML report. You can customise the styles by adding a bddifyCustom.css class and the behaviour by adding a bddifyCustom.js file. These files also need to be in the same directory as the HTML report file. This will affect all the reports in the project.\n\n\n## Create a custom report by creating a new Processor  ##\nOne way to create a custom report is to implement a new Processor and plug it into the Processor pipeline. You just have to implement the one Process() method and set the Process Type to Report.\n\nAn example of doing this is provided in the BDDfy Tic Tac Toe sample project with the Custom Text Reporter. The sample is available on [github](https://github.com/TestStack/TestStack.BDDfy/tree/master/TestStack.BDDfy.Samples) or [nuget](http://nuget.org/packages/TestStack.BDDfy.Samples/).\n\n    /// <summary>\n    /// This is a custom reporter that shows you how easily you can create a custom report.\n    /// Just implement IProcessor and you are done\n    /// </summary>\n    public class CustomTextReporter : IProcessor\n    {\n        private static readonly string Path;\n\n        private static string OutputDirectory\n        {\n            get\n            {\n                string codeBase = typeof(CustomTextReporter).Assembly.CodeBase;\n                var uri = new UriBuilder(codeBase);\n                string path = Uri.UnescapeDataString(uri.Path);\n                return System.IO.Path.GetDirectoryName(path);\n            }\n        }\n\n        static CustomTextReporter()\n        {\n            Path = System.IO.Path.Combine(OutputDirectory, \"BDDfy-text-report.txt\");\n            \n            if(File.Exists(Path))\n                File.Delete(Path);\n\n            var header = \n                \" A custom report created from your test assembly with no required configuration \" + \n                Environment.NewLine + \n                Environment.NewLine + \n                Environment.NewLine + \n                Environment.NewLine;\n            File.AppendAllText(Path, header);\n        }\n\n        public void Process(Story story)\n        {\n            // use this report only for tic tac toe stories\n            if (!story.MetaData.Type.Name.Contains(\"TicTacToe\"))\n                return;\n\n            var scenario = story.Scenarios.First();\n            var scenarioReport = new StringBuilder();\n            scenarioReport.AppendLine(string.Format(\" SCENARIO: {0}  \", scenario.Title));\n\n            if (scenario.Result != StepExecutionResult.Passed && scenario.Steps.Any(s => s.Exception != null))\n            {\n                scenarioReport.Append(string.Format(\"    {0} : \", scenario.Result));\n                scenarioReport.AppendLine(scenario.Steps.First(s => s.Result == scenario.Result).Exception.Message);\n            }\n\n            scenarioReport.AppendLine();\n\n            foreach (var step in scenario.Steps)\n                scenarioReport.AppendLine(string.Format(\"   [{1}] {0}\", step.StepTitle, step.Result));\n\n            scenarioReport.AppendLine(\"--------------------------------------------------------------------------------\");\n            scenarioReport.AppendLine();\n\n            File.AppendAllText(Path, scenarioReport.ToString());\n        }\n\n        public ProcessType ProcessType\n        {\n            get { return ProcessType.Report; }\n        }\n    }\n\nThis produces the BDDfy-text-report.txt text file report which is output to the bin directory:\n\n![BDDfy custom text report](/img/BDDfy/Customizing/bddfy-custom-text-report.png)\n\n## Create a custom report by creating a new Batch Processor  ##\nWhile that is one way that you can create a custom report, probably the better way to do it is to implement a new Batch Processor. The Processor runs as each test is being executed and allows you to build up the report, whereas a Batch Processor has the advantage of running after all of the tests have finished, meaning that you have access to total and summary information, such as diagnostics.\n\nAs an example of creating a custom report by creating a new Batch Processor, I was recently messing around with running tests in parallel and, as you might expect, the normal console report was quite jumbled. Multiple console report Processors were writing to the console at the same time and different test results were overlapping. The solution was to run the console report after all of the tests had run by creating a new Console Reporter as a Batch Processor rather than a Processor.\n\n    public class MyConsoleReporter : IBatchProcessor\n    {\n        public void Process(IEnumerable<Story> stories)\n        {\n            var reporter = new ConsoleReporter();\n            stories\n                .ToList()\n                .ForEach(story => reporter.Process(story));\n        }\n    }\n\nThen I just needed to add it to the Batch Processor pipeline and disable the built-in console report:\n\n    Configurator.Processors.ConsoleReport.Disable();\n    Configurator.BatchProcessors.Add(new MyConsoleReporter());\n\n\nThat's a bit of a hack for demo purposes. The HTML, MarkDown, and Diagnostics reports are all implemented as Batch Processors and I would recommend checking them out for examples of how to create a new report.","excerpt":"","slug":"customizing-reports","type":"basic","title":"Customizing Reports"}

Customizing Reports


This post looks at how to customize the HTML Report and how to create your own custom reports. You can customize the HTML report via configuration or by applying custom CSS or JavaScript. You can create custom reports either by implementing a new Processor or Batch Processor and adding them into their respective pipelines (see the [Architecture Overview](/BDDfy/Customizing/ArchitectureOverview.html) for details about the differences between these). ## Customizing the HTML Report ## The HTML report is the most sophisticated report in [BDDfy](http://teststack.github.io/pages/BDDfy.html) and therefore provides a lot more things that you can configure. Its configuration is defined by the IHtmlReportConfiguration interface. public interface IHtmlReportConfiguration { string ReportHeader { get; } string ReportDescription { get; } string OutputPath { get; } string OutputFileName { get; } bool RunsOn(Story story); } You can create a new configuration by implementing that interface or you can inherit from the DefaultHtmlReportConfiguration class, used to configure the standard HTML Report, and just override specific properties. Here is an example of a custom configuration, taken from the ATM sample, available on github [here](https://github.com/TestStack/TestStack.BDDfy/tree/master/TestStack.BDDfy.Samples/Atm). public class HtmlReportConfig : DefaultHtmlReportConfiguration { public override bool RunsOn(Core.Story story) { return story.MetaData.Type.Namespace != null && story.MetaData.Type.Namespace.EndsWith("Atm"); } /// <summary> /// Change the output file name /// </summary> public override string OutputFileName { get { return "ATM.html"; } } /// <summary> /// Change the report header to your project /// </summary> public override string ReportHeader { get { return "ATM Solutions"; } } /// <summary> /// Change the report description /// </summary> public override string ReportDescription { get { return "A reliable solution for your offline banking needs"; } } } which produces the following customised report, which you will find in your bin directory named ATM.html: ![BDDfy functional decomposition](/img/BDDfy/Customizing/bddfy-sample-atm-html-custom.png) The HTML report is a [Batch Processor](/BDDfy/Customizing/ArchitectureOverview.html) and is implemented by the HtmlReporter class. To plug the new report into BDDfy you need to create a new HtmlReporter and pass the custom configuration into its constructor. As I explained in the [Reports post](/BDDfy/Usage/Reports.html), the place to apply that configuration to BDDfy is the Configurator class, which is called before the tests run. Configurator.BatchProcessors.Add(new HtmlReporter(new HtmlReportConfig())); The use of the Add method means this is adding a second HTML Report processor into the Batch Processor pipeline, so the default report runner will still run. If you actually want the new report to replace the default report, then you will also need to disable the default report. Configurator.BatchProcessors.HtmlReport.Disable(); ## Custom CSS and JavaScript ## You can customize a lot more about the HTML report. BDDfy uses the BDDfy.css file to style the report and BDDfy.js and jQuery to add interactivity to it. You will find these files in the bin directory alongside the HTML report. You can customise the styles by adding a bddifyCustom.css class and the behaviour by adding a bddifyCustom.js file. These files also need to be in the same directory as the HTML report file. This will affect all the reports in the project. ## Create a custom report by creating a new Processor ## One way to create a custom report is to implement a new Processor and plug it into the Processor pipeline. You just have to implement the one Process() method and set the Process Type to Report. An example of doing this is provided in the BDDfy Tic Tac Toe sample project with the Custom Text Reporter. The sample is available on [github](https://github.com/TestStack/TestStack.BDDfy/tree/master/TestStack.BDDfy.Samples) or [nuget](http://nuget.org/packages/TestStack.BDDfy.Samples/). /// <summary> /// This is a custom reporter that shows you how easily you can create a custom report. /// Just implement IProcessor and you are done /// </summary> public class CustomTextReporter : IProcessor { private static readonly string Path; private static string OutputDirectory { get { string codeBase = typeof(CustomTextReporter).Assembly.CodeBase; var uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return System.IO.Path.GetDirectoryName(path); } } static CustomTextReporter() { Path = System.IO.Path.Combine(OutputDirectory, "BDDfy-text-report.txt"); if(File.Exists(Path)) File.Delete(Path); var header = " A custom report created from your test assembly with no required configuration " + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine; File.AppendAllText(Path, header); } public void Process(Story story) { // use this report only for tic tac toe stories if (!story.MetaData.Type.Name.Contains("TicTacToe")) return; var scenario = story.Scenarios.First(); var scenarioReport = new StringBuilder(); scenarioReport.AppendLine(string.Format(" SCENARIO: {0} ", scenario.Title)); if (scenario.Result != StepExecutionResult.Passed && scenario.Steps.Any(s => s.Exception != null)) { scenarioReport.Append(string.Format(" {0} : ", scenario.Result)); scenarioReport.AppendLine(scenario.Steps.First(s => s.Result == scenario.Result).Exception.Message); } scenarioReport.AppendLine(); foreach (var step in scenario.Steps) scenarioReport.AppendLine(string.Format(" [{1}] {0}", step.StepTitle, step.Result)); scenarioReport.AppendLine("--------------------------------------------------------------------------------"); scenarioReport.AppendLine(); File.AppendAllText(Path, scenarioReport.ToString()); } public ProcessType ProcessType { get { return ProcessType.Report; } } } This produces the BDDfy-text-report.txt text file report which is output to the bin directory: ![BDDfy custom text report](/img/BDDfy/Customizing/bddfy-custom-text-report.png) ## Create a custom report by creating a new Batch Processor ## While that is one way that you can create a custom report, probably the better way to do it is to implement a new Batch Processor. The Processor runs as each test is being executed and allows you to build up the report, whereas a Batch Processor has the advantage of running after all of the tests have finished, meaning that you have access to total and summary information, such as diagnostics. As an example of creating a custom report by creating a new Batch Processor, I was recently messing around with running tests in parallel and, as you might expect, the normal console report was quite jumbled. Multiple console report Processors were writing to the console at the same time and different test results were overlapping. The solution was to run the console report after all of the tests had run by creating a new Console Reporter as a Batch Processor rather than a Processor. public class MyConsoleReporter : IBatchProcessor { public void Process(IEnumerable<Story> stories) { var reporter = new ConsoleReporter(); stories .ToList() .ForEach(story => reporter.Process(story)); } } Then I just needed to add it to the Batch Processor pipeline and disable the built-in console report: Configurator.Processors.ConsoleReport.Disable(); Configurator.BatchProcessors.Add(new MyConsoleReporter()); That's a bit of a hack for demo purposes. The HTML, MarkDown, and Diagnostics reports are all implemented as Batch Processors and I would recommend checking them out for examples of how to create a new report.