-4

I am using c# for writing automation result. I have requirement where i have to write result in different sheet of same excel while running automation end to end flow. When my testmethod gives result as pass then i have to write result of that testmethods in sheet1 in below format

Testclassname | testmethodname | pass(yes/No) | time 

When testmethod gets failed then write like

Testclassname | testmethodname | Fail(yes/No) | time | exception

How to achieve this in c#??

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • What have you tried? Please show some code and point out which part specifically are you having problems with. Have you taken a look at Excel Interop? – jmc Apr 14 '15 at 12:24
  • i have not used interop...i have not written code ...actually i want code which will give me above result – prakash pandramise Apr 14 '15 at 13:34
  • 2
    ok, just wait for some time as someone might be writing that code right now. plot twist: no one's gonna write the code for you. at least do some research, write some code, try something and show us what you have done so far. someone might be able to help you. – jmc Apr 15 '15 at 01:10

2 Answers2

3

It's a bad idea to have your Test methods themselves write to Excel files. I personally don't see much value in writing to excel files at all, but you may have your reasons.

Depending on how you run your tests, there may be better ways to achieve what you want:

  1. When using the commandline vstest.console.exe to run the tests, you can specify a Logger. The /Logger:trx will create an XML file with all the test results. The XML file is easy to parse. Either XSLT to an HTML table or use a package like Excel Package Plus or using the OleDB driver for ADO.NET.

  2. You can link your test results to a Build (either when tests are executed through Team Build or MTM as well as from the commandline using the /Logger:TfsPublisher (see: MSDN). You can then use the Data warehouse to create your excel file using the Excel Powerpivot features.

If you need more details, experiment, post your findings as a new question and ask specific questions.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
-1

You can use the TestContext to check the current test method name and also the test results:

Test name: TestContext.TestName;

Test result: TestContext.CurrentTestOutcome == UnitTestOutcome.Failed

Now you should be able to create an if-else in the TestCleanup that checks if the test failed or not, get the test-method name. Depending on the test result write it either to one or the other Excel file.

For writing to Excel files in C# I would Google and try some examples: https://www.google.com/#q=writing+to+an+excel+file+in+c%23

Update

Keep in mind that in the future as your testsuite becomes larger, you might want to run the tests in parallel. So anything in the TestCleanup has to be parallel proof. Writing to the same Excel file will lead to issues. Its better to process the test results after the run as Jesse suggested.

Community
  • 1
  • 1
Niels van Reijmersdal
  • 2,038
  • 1
  • 20
  • 36