? QA Design Gurus: Storing Test execution log to an external file using TTS

Sep 30, 2015

Storing Test execution log to an external file using TTS



Whenever we run/debug Telerik Tests normally (without using scheduler or test list), our execution log will not be stored on disk.
Execution log is cached in-memory and will persist until you rerun the test case or close studio. So if we use our own frameworks to run telerik cases then we will not have logs stored on our disk.

Similarly if you have 3 tests running in a test list, result will not be shown until the execution of list completes.
So to look at the result of test1 we have to wait until the complete test result is executed.

To overcome these challenge, TTS suggest to override OnAfterTestCompleted method for individual tests in order to store result in logfile.
Below is the sample working code to achieve it.

public IList<AutomationStepResult> stepResults { get; set; }
        public override void OnAfterTestCompleted(TestResult result)
        {
            stepResults = result.StepResults;
            string passed = Convert.ToString(result.TotalPassedSteps);
            string notRunSteps = Convert.ToString(result.TotalNumberOfNotRunSteps);
            string overall = Convert.ToString(result.Result);
            string resultMessage = Convert.ToString(result.Message);
            string startTime = Convert.ToString(result.StartTime);
            string endTime = Convert.ToString(result.EndTime);
            string totalTime = Convert.ToString(result.Duration);
            string s = Convert.ToString(DateTime.Now.Date.ToShortDateString()) +    Convert.ToString(DateTime.Now.Hour);
            string dateOfExecution = s.Replace("/", "");

            var filePath = this.ExecutionContext.DeploymentDirectory + " \\TestResults\\" + result.TestName + dateOfExecution + ".txt";

            Log.WriteLine(filePath + "*********");
            if (!File.Exists(filePath))
            {
                File.Create(filePath).Dispose();
            }

            var file = new StreamWriter(filePath, true);
            file.WriteLine(resultMessage);
            file.WriteLine("Number of passed steps: " + passed);
            file.WriteLine("Number of not run steps: " + notRunSteps);
            file.WriteLine("Total test result: " + overall);
            file.Close();
        }
But still drawback of this method is we need to write this piece of code in each and every test if you want for each test.

No comments:

Post a Comment