1

we are working with MSTest and Selenium to run web tests. Right now we are trying to make the log more understandable and for that we would like to log each method name that the test has executed.

We are using VS2015 update 3 and MSTest 1.4

[TestClass]
    public class Test1: TestBase
    {
        [TestMethod]
        public void TEST_Method1()
        {
                driver.Url = targetURL
                loginPage.Login(USER, PASSWORD);

                homePage.GoToGraphics();
                homePage.GoToUserSearch();

                searchBI.Click_Calendar();
                searchBI.Select_November();

                ...
        }
    }

We would like the log to print

Starting TEST_Method1()
Executing loginPage.Login()
Executing homePage.GoToGraphics()
Executing homePage.GoToUserSearch()
Executing searchBI.Click_Calendar()
Executing searchBI.Select_November()
...
elgato
  • 506
  • 1
  • 5
  • 20
  • 2
    You should have a look at AOP (Aspect-oriented programming) frameworks. Logging as you described is **the** example for AOP .https://stackoverflow.com/questions/633710/what-is-the-best-implementation-for-aop-in-net – Robin B Nov 07 '19 at 12:34
  • Hi Robin B, sometime ago I checked PostSharp but I saw it requires a license. Do you recommend any small framework for this task? Thank you! – elgato Nov 07 '19 at 12:38
  • @elgato, why would you want to do this? Who is going to be reading these logs? – Anton Gogolev Nov 07 '19 at 13:04
  • @AntonGogolev the automation team and probably the tester that will run the build. We want this so we have a clearer picture of when the test failed – elgato Nov 07 '19 at 13:43

1 Answers1

1

The code snippet below captures the current method name and stores it in the methodName variable:

var currentMethod = MethodBase.GetCurrentMethod();

The currentMethod variable contains several properties, including the name of the current method.

You can encapsulate this in another class to expose this to your test execution only, or use a regular Logging library to log it to a console or the output window.

Fabricio
  • 532
  • 1
  • 6
  • 21
  • Hello @Fabricio, thanks for your suggestion. One question, would it be possible to create an event that would be triggered when the method changes? Thank you – elgato Nov 07 '19 at 15:42
  • 1
    I'm not sure what do you meant by "when the method changes". Could you elaborate a bit more? – Fabricio Nov 07 '19 at 16:30
  • Yes, I was referring to when we finish executing for example loginPage.Login(USER, PASSWORD); and jump to homePage.GoToGraphics(); – elgato Nov 07 '19 at 17:15
  • 1
    I don't think it would be possible without wrapping your calls on another object. If that's a viable approach, then using a Dynamic proxy would do the trick (through AOP), like the one suggested by @robin-b in your question. – Fabricio Nov 07 '19 at 17:19