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?
Asked
Active
Viewed 1.0k times
14
-
399% sure it's not a faciility xUnit has or wants - pass via env vars (or add an embedded resource to the assembly and read in body of test if static) – Ruben Bartelink Oct 19 '16 at 05:41
-
3See: https://xunit.github.io/docs/shared-context.html – robi-y Oct 19 '16 at 09:15
-
@RubenBartelink: thank you for your inputs figured out by using env vars... – swathi_reddy Oct 20 '16 at 04:54
-
Hi Swathi , Can you please let me know how did you do using Env Variables. As i am new to C# it would be good if you could provide me with an example. – Balaji Singh .Y Dec 11 '18 at 17:51
1 Answers
4
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);

Alexei - check Codidact
- 22,016
- 16
- 145
- 164