public void RunTestsAndCheckOutput(string typeName, string arguments, string testCaseName) { string goldenFile = Path.Combine(_goldenFilesDir, TestResources.GetGoldenFileName(typeName, testCaseName, ".xml")); string resultsFile = RunExecutableAndGetResultsFile(arguments); if (resultsFile == null) { goldenFile.AsFileInfo().Should().NotExist("Test run did not produce result trx file, therefore no golden file should exist"); return; } string transformedResultFile = TransformResultsFile(resultsFile); CheckIfFileIsParsable(transformedResultFile); if (!File.Exists(goldenFile)) { // ReSharper disable once AssignNullToNotNullAttribute File.Copy(transformedResultFile, goldenFile, true); Assert.Inconclusive($"First time this test runs, created golden file - check for correctness! File: {goldenFile}"); } CheckIfFileIsParsable(goldenFile); string diffFile; if (!CompareXmlFiles(goldenFile, transformedResultFile, out diffFile)) { if (!Directory.Exists(_diffFilesDir)) { Directory.CreateDirectory(_diffFilesDir); } string htmlDiffFile = Path.Combine(_diffFilesDir, TestResources.GetGoldenFileName(typeName, testCaseName, ".html")); string resultFile = Path.ChangeExtension(htmlDiffFile, ".xml"); string htmlContent = CreateDiffAsHtml(goldenFile, diffFile); File.WriteAllText(htmlDiffFile, htmlContent); File.Copy(transformedResultFile, resultFile, true); if (TestMetadata.OverwriteTestResults && !CiSupport.IsRunningOnBuildServer) { File.Copy(transformedResultFile, goldenFile, true); Assert.Inconclusive($"Updated golden file file:///{goldenFile}, test should now pass"); } Assert.Fail($@"Files differ, see file:///{htmlDiffFile}"); } }
public void CheckResults(string testResults, string typeName, [CallerMemberName] string testCaseName = null) { string expectationFile = Path.Combine(_goldenFilesDirectory, TestResources.GetGoldenFileName(typeName, testCaseName, _fileExtension)); string resultFile = Path.Combine(_testErrorsDirectory, TestResources.GetGoldenFileName(typeName, testCaseName, _fileExtension)); if (!File.Exists(expectationFile)) { File.WriteAllText(expectationFile, testResults); Assert.Inconclusive("This is the first time this test runs."); } string expectedResult = File.ReadAllText(expectationFile); string msg; bool stringsAreEqual = AreEqual(expectedResult, testResults, out msg); if (!stringsAreEqual) { #pragma warning disable CS0162 // Unreachable code (because overwriteTestResults is compile time constant) // ReSharper disable HeuristicUnreachableCode // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (TestMetadata.OverwriteTestResults) { File.WriteAllText(expectationFile, testResults); Assert.Inconclusive("Test results changed and have been overwritten. Differences: " + msg); } else { // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(Path.GetDirectoryName(resultFile)); File.WriteAllText(resultFile, testResults); Assert.Fail("Test result doesn't match expectation. Result written to: " + resultFile + ". Differences: " + msg); } // ReSharper restore HeuristicUnreachableCode #pragma warning restore CS0162 } else if (File.Exists(resultFile)) { File.Delete(resultFile); } }