开发者

Unit Testing - Using Image as Resource in Test Project

开发者 https://www.devze.com 2023-02-04 21:39 出处:网络
One part of a project that I am writing unit tests for extracts data from a jpeg header. I would like to create a unit test that tests this extraction, using a known image (so I can test the known pr

One part of a project that I am writing unit tests for extracts data from a jpeg header.

I would like to create a unit test that tests this extraction, using a known image (so I can test the known properties of that image).

It seems logical to me that this sample image be kept in the testing project as a Resource, but I am not sure how to do so. If it was a WPF project, I'd simply use the pack syntax.

Is there a Best Practice for something like thi开发者_如何学编程s (or, even, something that would work)?

Thanks, wTs


You can include the image in your test project as an embedded resource.
Then you can use the following code to get the stream of that image:

Assembly.GetExecutingAssembly().GetManifestResourceStream("TestProject.TestImages.myImage.png");

For more information see this link


This is based on @Matthew Manela's posted answer, which pointed me in the (or, perhaps more appropriately, one) right direction. It feels a bit kludgey to me, but it works.

First, set the image as a Content resource, with Copy Always.

Then, use the following helper function (used by different tests):

private string GetValidFileName()
{
    FileInfo fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
    return Path.Combine(fileInfo.DirectoryName, @"Resources\Sample.jpg");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号