question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way of accessing TestContext.WriteLine from classes outside TestClass?

See original GitHub issue

Hello,

I am using MSTest with selenium to run web tests.

Web testing promotes the Page Object Model to model a web page, abstracting what the test does from how it is done and promoting re-usability.

When running test serially, we can make use of Console.WriteLine from a PageObject class to let the test writer know important information about the test like variable values used and test flow, however the same approach cannot be taken when running in parallel as Console.WriteLine is redirected to the first test running.

A solution to this would be to pass TestContext to the PageObject class but this means that PageObject classes which normally only use a WebDriver parameter in their constructor would have to be refactored.

Another problem that arises is that a reference to MSTest has to be added on the project containing the PageObject classes to make use of TestContext.

We could try to avoid logging from PageObject and doing it only from test but if we have 100 tests and all of them have to log in the same page one would be tempted to avoid logging in each test and have the PageObject log in method do it for us.

Having said that my question is this: is passing TestContext as an argument the proper way to have access to TestContext from classes outside the TestClass? Is there another way to achieve this?

Thanks for your time.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
martsvecommented, Apr 21, 2020

You probably have to refactor your PageObjects. You could get around it with a static/singleton logger that uses the last used TestContext (set in the test initialization), but that would “break” for multi-threaded tests.

You should probably create a Logger/LoggerInterface that takes TestContext as a constructor-parameter. The logger/Interface can then be set in the constructor of your PageObjects. That way, your PageObjects are not dependent on the TestContext, and doesn’t need to know how it logs the data.

1reaction
nohwndcommented, Jun 18, 2020

I would do what @martsve suggests. Injecting an implementation of an interface into the SUT. That way the dependencies on test framework would stay in the test project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get an instance of the MSTest V2 TestContext in ...
I'm migrating an existing codebase to MSTest V2 and running into a problem with the TestContext in the TestCleanup method. In MSTest V1...
Read more >
TestContext.WriteLine Method
Used to write trace messages while the test is running.
Read more >
Tag: Unit Testing - somewhat abstract
By default, each test class is its own collection. Test classes can be combined into collections using the Collection attribute.
Read more >
MSTest - DTan's Blog
In your Test Method, all you have to do is just call one line of code: ? 1. TestContext.PrintInput();. And ...
Read more >
How to Write to Console in Unit Test in Visual Studio 2022
The easiest way to write output from the test is to use the Console.WriteLine method. ... Starting with NUnit 3, you have access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found