I've got a set of Specflow tests that I'm running and I'm trying to write the results so far to a file after each scenario finishes, and then I write again at the end of the entire test run. If I run the tests in debug mode then this code is hit after each scenario, but the file only appears in Windows Explorer after all the tests finish (or I force the tests to stop). The code below, that writes to the file is in a separate project to the Specflow test project.
I originally was not flushing and had this in place of StreamWriter:
using (var file = new FileInfo(filePath).AppendText())
But that wasn't working, so after looking up various examples on the internet I added extra Flush, Close, then Process.Start, changed to StreamWriter and none of them helped. The current code that still isn't working is:
private string rootFolderPath = Directory.GetParent(@"..\..\..\..") +@"\";
public void WriteAllTestScenarioNames(List<ScenarioResult> results, string fileName, string directoryName)
{
results.Sort();
Directory.CreateDirectory(rootFolderPath + directoryName);
string filePath = rootFolderPath + directoryName + @"\" + fileName;
using (var file = new System.IO.StreamWriter(filePath))
{
file.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
foreach (var scenarioResult in results)
{
file.WriteLine(scenarioResult.ToString());
}
file.Flush();//flush the file to ensure that it's written now
file.Close();//flush the file to ensure that it's written now
}
Process.Start(filePath); //flush the file to ensure that it's written now
}
In the example I'm trying there are only 3 lines in the output file when it finally finishes and the Created, Modified and Accessed dates in the file properties in Windows Explorer are identical.
I am using Windows 7 Enterprise SP1 64-bit with .NET Framework 4.5.2
It seems that the file is being written, the issue is that it wasn't showing up in Windows Explorer. I stepped through again in debug mode, checked in Explorer, the file wasn't there. I copied the file path from the variable's value and opened that exact path in Notepad++ and the file was there. Following that the file now is showing in Explorer and updating after each test scenario finishes.
Edit: It seems that the issue was
private string rootFolderPath = Directory.GetParent(@"..\..\..\..") +@"\";
was returning a different result after each scenario to what it was after the test run. One was in the root of my user folder (correct) and the other 3 directories above the Specflow project folder. In the first folder it's only outputting at the end, but in the second it's updating after every scenario.
Try this:
public void WriteAllTestScenarioNames(List<ScenarioResult> results, string fileName, string directoryName)
{
results.Sort();
Directory.CreateDirectory(rootFolderPath + directoryName);
string filePath = rootFolderPath + directoryName + @"\" + fileName;
FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fs))
{
file.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
foreach (var scenarioResult in results)
{
file.WriteLine(scenarioResult.ToString());
}
}
fs.Close();
}
Also check if the filePath is getting formed properly and whether you have permission to write in that area - I suspect it may not be getting formed properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.