You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Azure文件存储的文件上传功能编写功能测试

为Azure文件存储上传功能编写功能测试

我是测试新手,想了解如何编写功能测试,实现将文件上传至Azure File Storage后,读取文件并对比结果。我已编写了如下用于上传文件到存储的Azure函数,现需要编写功能测试或集成测试。

现有Azure上传函数代码

[FunctionName("FileServiceUpload")]
public async Task<IActionResult> FileServiceUpload([HttpTrigger(AuthorizationLevel.Anonymous, "post", "put", Route = "FileService/{sharename}/{directory}/{filename}")] HttpRequest req, string sharename, string directory, string filename, ILogger log)
{
    var bodyContent = await new StreamReader(req.Body).ReadToEndAsync();
    var fileByteArray = Convert.FromBase64String(bodyContent);

    var azureObjectsResource = new AzureObjectsResource(_connectionString, sharename, directory, filename, fileByteArray);

    var returnObject = await _fileServiceApplication.UploadFile(azureObjectsResource);

    return new OkObjectResult(returnObject);
}

现有测试代码(FunctionalSpec.cs)

public class FunctionalSpec :IClassFixture<FileServiceApplication>
{
    private readonly IFileServiceApplication _fileServiceApplication;
    private readonly ILogger _log;

    public FunctionalSpec(IFileServiceApplication fileServiceApplication, ILogger log)
    {
        _fileServiceApplication = fileServiceApplication;
        _log = log;
    }

    [Fact]
    public async Task UploadFileSuccess()
    {
        // Arrange
        string contents = "test file contents!";
        string shareDirectory = "test1";
        string shareName = "teststorage";
        string fileName = "Test";
        string apiURL = "https://testfileshare.file.core.windows.net/testfileshare";
        string connectionString =
            "";

        byte[] byteArray = Encoding.UTF8.GetBytes(contents);

        var azureResource = new AzureObjectsResource(connectionString, shareName, shareDirectory, fileName, byteArray);

        await _fileServiceApplication?.UploadFile(azureResource)!;
    }
}

完善后的测试方案

你的现有测试仅完成了上传操作,缺少上传后读取文件并验证内容一致性的核心逻辑。以下是完整的实现步骤:

1. 引入Azure存储读取依赖

确保测试项目安装Azure.Storage.Files.Shares NuGet包,用于直接访问Azure文件存储。

2. 完善测试方法逻辑

修改UploadFileSuccess方法,添加上传后的读取和断言步骤:

public class FunctionalSpec :IClassFixture<FileServiceApplication>
{
    private readonly IFileServiceApplication _fileServiceApplication;
    private readonly ILogger<FunctionalSpec> _log;
    // 从环境变量读取连接字符串,避免硬编码敏感信息
    private readonly string _connectionString = Environment.GetEnvironmentVariable("AzureStorageConnectionString");

    public FunctionalSpec(IFileServiceApplication fileServiceApplication, ILogger<FunctionalSpec> log)
    {
        _fileServiceApplication = fileServiceApplication;
        _log = log;
    }

    [Fact]
    public async Task UploadFile_ShouldStoreCorrectContent()
    {
        // Arrange
        string testContent = "test file contents!";
        string shareName = "teststorage";
        string shareDirectory = "test1";
        string fileName = "Test.txt";
        byte[] testBytes = Encoding.UTF8.GetBytes(testContent);

        var azureResource = new AzureObjectsResource(_connectionString, shareName, shareDirectory, fileName, testBytes);

        // Act
        await _fileServiceApplication.UploadFile(azureResource);

        // Assert:读取存储中的文件并对比内容
        var shareClient = new ShareClient(_connectionString, shareName);
        var directoryClient = shareClient.GetDirectoryClient(shareDirectory);
        var fileClient = directoryClient.GetFileClient(fileName);

        using var memoryStream = new MemoryStream();
        await fileClient.DownloadToAsync(memoryStream);
        memoryStream.Position = 0;
        var storedContent = Encoding.UTF8.GetString(memoryStream.ToArray());

        Assert.Equal(testContent, storedContent);
    }

    // 可选:测试前清理测试目录,避免旧数据干扰结果
    [Fact]
    public async Task CleanupTestDirectory()
    {
        var shareClient = new ShareClient(_connectionString, "teststorage");
        var directoryClient = shareClient.GetDirectoryClient("test1");
        await foreach (var item in directoryClient.GetFilesAndDirectoriesAsync())
        {
            if (item.IsFile)
            {
                await directoryClient.DeleteFileAsync(item.Name);
            }
        }
    }
}

关键注意事项

  • 连接字符串管理:禁止硬编码连接字符串,使用环境变量或测试配置文件注入,防止敏感信息泄露。
  • 测试独立性:测试前/后清理测试用的文件或目录,避免旧数据影响测试结果。
  • 权限配置:确保测试用的存储账户拥有上传、读取、删除文件的权限。
  • 异常场景覆盖:除了成功上传的场景,还需补充测试上传空内容、无效路径、无权限访问等异常情况。

内容的提问来源于stack exchange,提问作者Deryn Johnstone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 18:40:39