Skip to content
Tags

Creating a test report

by on August 12, 2012

So one of the other editors recently posted about creating a report file that would allow you to append text to it as your tests ran. Excellent. It worked too. However, me being me, I saw a way to reduce the amount of lines of code it was using. I’m all about code economy.

So last night I came up with this. I have two functions. One that creates and logs entries to a file. The other will archive any existing report files to an archive folder.

I used references to my resource file, but I’ll change it here so that it’s using strings declared in the code itself. That’ll make it easier to work with for anyone who wants to give it a try. Ideally, you would have the strings declared once outside of the functions, but the functions would still be able to use them. This would save needing to set them in each function. But for ease of use, i’ve set them in both functions so you have them running quicker.

The strings LogFolder and LogFile could be declared outside of the two functions to allow them to be used by both functions without defining them in each. This would be better to do. And is how i have mine in the class i created for these functions.

public void writeToLogFile(string Text, bool Pass)
{
string LogFolder = @"logging\path"; \\This is your logging folder path
string LogFile = "filename.html"; \\Filename like log.html
// Create a writer and open the file:
StreamWriter log;
// Check file and directory exist
if (!File.Exists(LogFolder + @"\" + LogFile))
{
try
{
Directory.CreateDirectory(LogFolder);
}
finally
{
log = new StreamWriter(LogFolder + @"\" + LogFile);
}
}
// append the file if it exists
else
{
log = File.AppendText(LogFolder + @"\" + LogFile);
}
// Text to log to the file
if (!Pass)
{
// Write to the file:
log.WriteLine("<li><b>" + DateTime.Now + ": <font color='red'>" + Text + "</font></b></li>");
}
else
{
// Write to the file:
log.WriteLine("<li><b>" + DateTime.Now + ":</b> <font color='black'>" + Text + "</font></li>");
}
// Close the stream:
log.Close();
}

And here’s a little archiving function I wrote to copy files with a date/time so they can all be easily referenced from when they were run.

public void archiveLogFiles()
{
#region Archive old log files
string LogFolder = @"logging\path"; \\This is your logging path
string LogFile = "filename.html"; \\Filename like log.html
//format the date in a string that can be used in windows filenames
string createDate = File.GetLastWriteTime(LogFolder + @"\" + LogFile).ToString("yyyy-MM-dd HH_mm_ss");
string SourceFile = LogFolder + @"\" + LogFile;
string DestinationFile = LogArchive + @"\" + createDate + "_" + LogFile;
if (File.GetCreationTime(LogFolder + @"\" + LogFile) != DateTime.Now)
{
try
{
if (!Directory.Exists(LogArchive))
{
Directory.CreateDirectory(LogArchive);
}
}
finally
{
if (!File.Exists(DestinationFile))
{
File.Move(SourceFile, DestinationFile);
}
}
}

I hope this is of some use. I’m going to continue working with this to provide a much nicer looking log file. But for now, this works perfectly

The usage of the writeToLogFile function:
writeToLogFile("text here", true/false);

Leave a Comment

Leave a comment

NE1 Atoll

The Official blog of NE1 Games

Selenium for .Net using C# language

Adventures in Coding, gaming and other fun things in my life

Coded UI 101 - Understanding Coded UI Tests

Adventures in Coding, gaming and other fun things in my life