问题
I couldn't find a post similar to this, so I hope this isn't a duplicate.
I have a c# class library that I'm trying to run unit tests on in Visual Studio 2012. I've added a new Unit Test Project to my solution, and added my main project as a reference there. I've set my unit test project as the Startup Project. When I try to debug, I get an error message
A project with an Output Type of Class Library cannot be started directly.
In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.
According to the walkthrough at msdn, it should be running the tests when I hit debug. Any thoughts? Here is my unit test code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Common;
using Messages;
namespace MessageUnitTests
{
[TestClass]
class RegistrationTester
{
[TestMethod]
public void RegistrationRequest_TestConstructorsAndFactories()
{
RegistrationRequest rr1 = new RegistrationRequest("myhandle");
Assert.AreEqual("myhandle", rr1.Handle);
rr1 = new RegistrationRequest("longHandle-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789'|;:',.=-_+!@#$%^&*()");
Assert.AreEqual("longHandle-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789'|;:',.=-_+!@#$%^&*()", rr1.Handle);
rr1 = new RegistrationRequest("");
Assert.AreEqual("", rr1.Handle);
rr1 = new RegistrationRequest(null);
Assert.AreEqual(null, rr1.Handle);
rr1 = new RegistrationRequest("myhandle");
ByteList bytes = new ByteList();
rr1.Encode(bytes);
RegistrationRequest rr2 = RegistrationRequest.Create(bytes);
Assert.IsNotNull(rr2);
Assert.AreEqual(rr1.IsARequest, rr2.IsARequest);
Assert.AreEqual(rr1.MessageNr.ProcessId, rr2.MessageNr.ProcessId);
Assert.AreEqual(rr1.MessageNr.SeqNumber, rr2.MessageNr.SeqNumber);
Assert.AreEqual(rr1.ConversationId.ProcessId, rr2.ConversationId.ProcessId);
Assert.AreEqual(rr1.ConversationId.SeqNumber, rr2.ConversationId.SeqNumber);
Assert.AreEqual(rr1.RequestType, rr2.RequestType);
Assert.AreEqual(rr1.SessionId, rr1.SessionId);
Assert.AreEqual(rr1.Handle, rr2.Handle);
}
//[TestMethod]
//public void RegistrationRequest_EncodingDecoding()
//{
// Message m1 = new RegistrationRequest("myhandle");
// m1.MessageNr = MessageNumber.Create(10, 14);
// m1.ConversationId = MessageNumber.Create(10, 12);
// ByteList bytes = new ByteList
//}
}
}
回答1:
You'll want to debug it a different way:

回答2:
Ensure that you used the "unit test project" template when creating the visual studio project which contains your test. Visual studio needs some metadata in the csproj markup to now how to execute a class library.
You can add it or confirm it is present by editing the csproj file in notepad:
<Project>
<PropertyGroup>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
</Project>
Details @ http://onlinecoder.blogspot.ca/2009/09/visual-studio-projects-project-type.html
Now it should work with F5 in Visual Studio. If it still doesn't work, right click on the test and click 'run tests' or use the test explorer (Test > Windows > Test Explorer)
来源:https://stackoverflow.com/questions/12594018/cannot-debug-a-unit-testing-project-in-visual-studio-2012