16

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.

enter image description here

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
        //}
    }
}
Community
  • 1
  • 1
Matt
  • 161
  • 1
  • 1
  • 3

3 Answers3

24

You'll want to debug it a different way:

enter image description here

Gromer
  • 9,861
  • 4
  • 34
  • 55
  • I see the test explorer, it isn't showing me any tests. Is there something else I need to do to get the tests to appear in the test explorer? – Matt Sep 26 '12 at 03:20
  • I wouldn't think so, since you used a Unit test template when adding the project. Waht about if you right click in the test? Do you get an option to run the selected test? I haven't used VS2012 much, so I'm not 100% familiar with it. – Gromer Sep 26 '12 at 03:22
  • Not exactly - here is what I am seeing. http://i.imgur.com/cyAoa.png edit: Apparently I fail at posting images. When I click Run Tests, I get a "Build Succeeded" message at the bottom status bar and nothing further. – Matt Sep 26 '12 at 03:27
  • Clicking that gives me the same outcome as Run Tests. Here is a shot that actually shows the test explorer as well. http://i.imgur.com/mmtX8.png – Matt Sep 26 '12 at 03:40
  • Click the test runs tab at the bottom. – Gromer Sep 26 '12 at 03:47
  • Got that to show up by enabling windows in the View menu that looked promising. Doesn't seem to be useful at this point. http://i.imgur.com/9ZDiZ.png – Matt Sep 26 '12 at 03:50
  • I installed VS2010 and recreated the projects. On debug, the unit test runs and passes. Guess I'll chalk it up to VS2012 being buggy for now. Thanks for your help! – Matt Sep 26 '12 at 04:14
  • I'm having the same issue - tests simply don't show up in the Test List – Doguhan Uluca Sep 30 '12 at 22:29
  • 1
    I figured my problem out, my method was async void, but it needs to be async Task for it to be a valid test method. – Doguhan Uluca Sep 30 '12 at 22:46
  • Someone should mark this answer as the correct answer for this question. – Eido95 Aug 10 '16 at 12:55
0

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)

Kenn
  • 2,379
  • 2
  • 29
  • 38
0

Right click on the project and select Properties and set the output type to Windows Application. Then right-click on the project again and select Build and run the project, the error will be resolved.