Xunit中类似于Visual Studio测试中的TestContext的属性是什么?

14
我们正在从Visual Studio Tests迁移到xUnit。在VStests中,我们可以使用TestContext访问运行时测试参数。我想要从命令行使用msbuild在运行时为测试设置全局变量。能否帮助找出xunit中TestContext的等效方法?

3
99%确定这不是xUnit拥有或想要的功能 - 通过环境变量传递(或在程序集中添加嵌入资源,并在测试体中读取,如果为静态)。 - Ruben Bartelink
3
请参见:https://xunit.github.io/docs/shared-context.html - robi-y
@RubenBartelink:感谢您的输入,我已经通过使用环境变量解决了问题... - swathi_reddy
嗨,Swathi,你能告诉我如何使用环境变量吗?由于我是C#的新手,如果你能提供一个例子就太好了。 - Balaji Singh .Y
1个回答

4

XUnit中没有TestContext

当运行测试时,我找不到一种处理环境参数的规范方式,因此我依赖于一个JSON文件。例如:

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

C#代码:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "environment.json");
string json = File.ReadAllText(path);
Configuration = JsonConvert.DeserializeObject<TestingConfiguration>(json);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接