Wonko the Sane
Wonko the Sane

Reputation: 10813

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 this (or, even, something that would work)?

Thanks, wTs

Upvotes: 3

Views: 2492

Answers (2)

Wonko the Sane
Wonko the Sane

Reputation: 10813

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");
}

Upvotes: 4

Matthew Manela
Matthew Manela

Reputation: 16752

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

Upvotes: 7

Related Questions