red888
red888

Reputation: 31560

How are unit tests compiled in c# test projects?

I've gone through tutorials on writing unit tests with a "Test" project for my .net 4.5 app but I don't understand how they are running.

When I run unit tests that test specific methods of classes in my projects are the entire projects that the methods are recompiled for each test?

I'm confused about how the tests interact with the methods they are testing. Are the tests themselves compiled? I guess they would have to be because it's c#. Is there a separate binary for the tests?

Upvotes: 1

Views: 1333

Answers (1)

John Wu
John Wu

Reputation: 52250

You actually can't run unit tests independently. Typically they will be executed by a "test runner," e.g. the built-in MSTest test running or an NUnit test runner adapter. The process works like this:

  1. You write the test, marking test classes with a specific attribute, e.g. [TestClass] or [TestFixture].

  2. The test classes are compiled into a DLL.

  3. You start your test runner using menu items in Visual Studio (or it is started as part of the automated build process).

  4. The test runner loads the unit test DLL.

  5. The test runner loads all of the types in the DLL and uses reflection to locate the types (classes) that have the magic attibute that indicates that it contains tests.

  6. The test runner instantiates the test class/fixture.

  7. The test runner iterates over the methods in the DLL that have a [Test] or [TestMethod] attribute.

  8. The test runner invokes each method using Invoke().

  9. The test runner displays the results of its test in the test runner UI, or outputs it to a report.

Upvotes: 5

Related Questions