提问人:Kishan Mistry 提问时间:11/16/2023 更新时间:11/16/2023 访问量:40
如何在 C 中为测试用例提供 json 文件的相对路径#
how to give relative path for json file for test cases in C#
问:
需要从项目结构中访问 json 文件。所以需要给亲戚。C# 这适用于将数据列表与 API 调用中的数据列表进行比较的测试用例。 string dataListJSONFilePath = “C:\Repo\Project\Admins\Data Folder\dataList.json”;
答:
1赞
Michał Turczyn
11/16/2023
#1
如果我理解正确,您可以将 json 文件添加到您的项目中,并在文件属性中将其“构建操作”设置为“无”,并将“复制到输出目录”设置为“始终”:
下面是 JSON 示例:
{
"ExampleProperty": "hello world"
}
下面是单元测试示例:
using System.Text.Json;
namespace UnitTestsProject;
public record JsonBodyForTest(string ExampleProperty);
public class UnitTest1
{
[Fact]
public void ReadPropertyFromJsonFile_ShouldPopulateCorrectly()
{
// Arrange
var jsonBody = File.ReadAllText("testfile.json");
// Act
var parsed = JsonSerializer.Deserialize<JsonBodyForTest>(jsonBody);
// Assert
Assert.False(string.IsNullOrEmpty(parsed.ExampleProperty));
}
}
评论