开发者

Unit Testing - Using Image as Resource in Test Project

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜