Noren
Noren

Reputation: 793

Visual Studio 2008 unit testing using the same file resource

I am using the built-in unit testing framework of Visual Studio 2008 to get my feet wet with TDD of a C# WinForms application. The application works with proprietary image data formats and I've broken up sample files into their component sections (header, metadata, single frames, all frames, etc) to allow for testing. Several of my test methods use the same data chunk and when I choose the 'Run All Tests in Solution' option, the tests using the same data chunk fail with "System.IO.IOException: The process cannot access the file 'datachunk.dat' because it is being used by another process." These tests pass when they are run individually. Is there a way I can tell Visual Studio not to try and run these tests at the same time or am I going about this the wrong way?

Upvotes: 1

Views: 297

Answers (3)

Schultz9999
Schultz9999

Reputation: 8916

I can suggest several things:

  1. Check if you can change the flags when the file is opened. Effectively you should open it as shared for reading.
  2. Read the complete content of the file into a byte array and use that one as a source in your tests. That doesn't really change much.
  3. Use shared resource and TestInitialize.

Upvotes: 0

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

Use the intialize method to load the "chunk" before the tests need it. You can then reuse the same "chunk" over and over, across tests.

You can also encapsulate the logic into its own method and ensure you destroy the reader between hits on the method.

Peace and Grace, Gregory A. Beamer

Upvotes: 1

Oded
Oded

Reputation: 498914

Read it once in a TestInitialize function, reading into a field, so it is available to all the tests in the class.

Upvotes: 0

Related Questions