Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the attribute in Xunit that's similar to TestContext in Visual studio tests?

We are migrating from visual studio tests to xunit.. In VStests we can access run time test parameters using TestContext. I am looking to set a global variable in the tests supplied at run time from command line using msbuild. Can someone help in finding out the TestContext equivalent in xunit?

like image 492
swathi_reddy Avatar asked Oct 19 '16 02:10

swathi_reddy


People also ask

Which attribute is used to mark a method as test method in xUnit?

This method is decorated with the Fact attribute, which tells xUnit that this is a test.

What is Fact attribute in xUnit?

xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.

What is IClassFixture in xUnit?

Important note: xUnit.net uses the presence of the interface IClassFixture<> to know that you want a class fixture to be created and cleaned up. It will do this whether you take the instance of the class as a constructor argument or not.

Which of the following is an ideal replacement for the SetUp attribute in xUnit?

Instead, intelligence is built in the xUnit framework so that it can locate the test methods, irrespective of the location of the tests. Verify the raise and raise assert, irrespective of the place in the code where the problem occurs. This is not an attribute but is an ideal replacement for the [SetUp] attribute.


1 Answers

There is no TestContext in XUnit.

I could not find a canonical way to deal with environment parameters when running the tests, so I relied on a JSON file. E.g.:

{
  "Browser": "Chrome",
  "BasePath": "localhost:4200",
  "BaseApiPath": "http://localhost:50204/"
} 

C# code:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "environment.json");
string json = File.ReadAllText(path);
Configuration = JsonConvert.DeserializeObject<TestingConfiguration>(json);
like image 142
Alexei - check Codidact Avatar answered Sep 23 '22 17:09

Alexei - check Codidact