将图像上传到 Cloud UnitTest

发布于 2024-12-19 16:20:29 字数 1778 浏览 4 评论 0 原文

我正在尝试将图像上传到云端,并且希望能够对此进行测试, 这是我已经尝试过的,我不太明白我在做什么,所以如果有人能告诉我该怎么做,我将不胜感激。

到目前为止,我已经包含了该方法的主要方法以及该方法的测试。

public static String UploadToCloud(string fileName)
    {
        try
        {
            SetUpConnection();
            #region Upload a File from local storage to the Cloud
            // Get a reference to the blob.
            blob = blobContainer.GetBlobReference("Images/" + fileName.Substring(fileName.LastIndexOf('\\')));
            blob.UploadFile(fileName);
            return blob.Uri.ToString();
            #endregion
        }
        catch (StorageClientException e)
        {
            Console.WriteLine("Storage client error encountered: " + e.Message);
            return "Upload failed";
        }
    }

/// <summary>
    ///A test for UploadToCloud
    ///</summary>
    [TestMethod()]
    public void UploadToCloudTest()
    {
        string fileName = "https://kevin.blob.core.windows.net/cp300/Images//skin-mole.jpg";
        Image expected = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\skin-mole.jpg");
        string actual;
        actual = CloudConnection.UploadToCloud(fileName);

        //Compares to images and checks they are exactly the same
        MemoryStream ms = new MemoryStream();
        expected.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String expectedBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;
        actual.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String actualBitmap = Convert.ToBase64String(ms.ToArray());

        Assert.AreEqual(expectedBitmap, actualBitmap); 

        //Assert.AreEqual(expected, actual);
        //Assert.Inconclusive("Verify the correctness of this test method.");
    }

I am trying to upload an image to a cloud and i want to be able to test this,
here is what i have tried already, I dont really understand fully what im doing, so if somebody could tell me what to do, I would appreciate it.

I have included the main method for this and the test of that method so far.

public static String UploadToCloud(string fileName)
    {
        try
        {
            SetUpConnection();
            #region Upload a File from local storage to the Cloud
            // Get a reference to the blob.
            blob = blobContainer.GetBlobReference("Images/" + fileName.Substring(fileName.LastIndexOf('\\')));
            blob.UploadFile(fileName);
            return blob.Uri.ToString();
            #endregion
        }
        catch (StorageClientException e)
        {
            Console.WriteLine("Storage client error encountered: " + e.Message);
            return "Upload failed";
        }
    }

/// <summary>
    ///A test for UploadToCloud
    ///</summary>
    [TestMethod()]
    public void UploadToCloudTest()
    {
        string fileName = "https://kevin.blob.core.windows.net/cp300/Images//skin-mole.jpg";
        Image expected = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\skin-mole.jpg");
        string actual;
        actual = CloudConnection.UploadToCloud(fileName);

        //Compares to images and checks they are exactly the same
        MemoryStream ms = new MemoryStream();
        expected.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String expectedBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;
        actual.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String actualBitmap = Convert.ToBase64String(ms.ToArray());

        Assert.AreEqual(expectedBitmap, actualBitmap); 

        //Assert.AreEqual(expected, actual);
        //Assert.Inconclusive("Verify the correctness of this test method.");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-12-26 16:20:29

我想说这并不是真正的单元测试 - 您正在尝试将某些内容上传到您无法控制的外部服务,并且无法保证每次运行的结果都是相同的。

您编写的是集成测试,它是对两个或多个软件组件如何协同工作的测试。在这种情况下,这两个组件是

  • 您的代码
  • 云上传 API

集成测试没有任何问题,但它们往往会更慢(在这种情况下,由于将文件上传到云),而且它们往往更脆弱。例如,如果云服务不可用,您的集成测试就会中断。您的代码没有任何变化,您的测试也没有任何变化,但测试的结果不同。

如果您想对 UploadToCloud 方法进行单元测试,我建议您首先将“云上传”功能包装在实现接口的类中,例如 ICloudUploader。然后,您可以模拟实际与云服务通信的部分,并确保您的代码的功能在您想要测试的所有情况下(成功上传、服务不可用、上传由于文件太大而失败,无论如何)。

要模拟一个类,您可以推出自己的类(编写一个实现您的接口的类,例如 public class FakeCloudUploader : ICloudUploader,或者查看类似 MoqUploadToCloud 返回的

字符串。您期望的值。

I'd say that this isn't really a unit test -- you're trying to upload something to an external service that you have no control over and can't guarantee that the results are going to be the same from run to run.

What you've written is an integration test, which is a test of how two or more software components work together. In this case, the two components are

  • Your code
  • The cloud upload API

There's nothing wrong with integration tests, but they tend to be slower (in this case, due to uploading a file to the cloud), and they tend to be more brittle. Your integration test, for example, would break if the cloud service wasn't available. Nothing changed in your code, nothing changed in your test, but the test's results were different.

If you wanted to unit test your UploadToCloud method, I'd recommend that you start by wrapping your "cloud uploading" functionality in a class that implements an interface, e.g. ICloudUploader. Then you can mock out the pieces that actually communicate with your cloud service, and ensure that the functionality of your code is correct under all of the situations you want to test (successful upload, service is unavailable, upload fails due to file being too big, whatever).

For mocking out a class, you can either roll your own (write a class that implements your interface, for example public class FakeCloudUploader : ICloudUploader, or look into a mocking framework like Moq or RhinoMocks.

As for the test method you provided, it's not really testing the output of the method. It should validate that the string you get back from UploadToCloud is the value you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文