Note: I am using TestDriven.NET 3.0.2749 and NUnit 2.6.0.12051 for this project.
I have installed both TestDriven.NET and NUnit and am trying to get TestDriven.NET to run all tests in a test class via the right-click context menu.
From the TestDriven.NET documentation:
If the code editor window is selected, the test(s) to execute will be determined by the position of the caret; individual tests are executed by right-clicking anywhere inside a test method and selecting 'Run Test(s)' as shown in Figure 2; all tests in a test fixture are executed by right-clicking inside a class (but outside of any method) and selecting 'Run Test(s)'; all tests in a namespace are executed by right-clicking inside a namespace and selecting 'Run Test(s)'.
I can successfully run a specific test method using the right-click context menu and the NUnit GUI runner will successfully run all test for a given class, but I would like to use the quick access TestDriven.NET provides for this tasks while I'm developing.
I receive the follow error when I place the caret outside of test method:
The target type doesn't contain tests from a known test framework or a 'Main' method.
Updated 1: Added example code.
Example code to test:
namespace TDDN.Framework
{
public class ExampleClass
{
public ExampleClass() { }
public Int32 Add(Int32 x, Int32 y)
{
return x + y;
}
public Int32 Subtract(Int32 x, Int32 y)
{
return x - y;
}
}
}
Unit tests:
using NUnit.Framework;
using TDDN.Framework;
namespace TDDN.UnitTests
{
[TestFixture] // Cursor caret placed here results in error above.
public class ExampleClassTests
{
[Test] // Cursor caret placed here works.
public void Add_SumTwoIntegers_SumReturned()
{
ExampleClass exampleClass = new ExampleClass();
Assert.AreEqual(10, exampleClass.Add(5, 5));
}
[Test] // Cursor caret placed here works also.
public void Subtract_SubtractTwoIntegers_DifferenceReturned()
{
ExampleClass exampleClass = new ExampleClass();
Assert.AreEqual(5, exampleClass.Subtract(10, 5));
}
}
}
I just encountered this exact problem when using the same versions of TestDriven.NET and NUnit (3.0.2749 and 2.6.0.12051).
The issue is that TestDriven.NET 3.0 doesn't support NUnit 2.6, so it won't recognize the NUnit [Test] and [TestFixture] attributes. Thus, TestDriven.NET will still run your individual test functions, but as Ad Hoc (as displayed at the end of the Pass/Fail/Skip message when testing).
I was able to solve the issue by installing a newer version of TestDriven.NET (3.3 Beta 2), which fully supports NUnit 2.6 (See: https://groups.google.com/d/msg/nunit-discuss/pTCDx2_L8jU/TlpULzE36wEJ) Now you should be able to run all the tests in the fixture at once and see (NUnit 2.6.0) displayed at the end of the test output.
I had exactly the same error message and similar behaviour on the caret placements.
I already had the newest version of TestDriven.Net.
My problem was that my new TestClass did not have a Category specified and it was filtered out (Tools -> TestDriven.Net -> General -> Categories -> Include tests in categories).
So just specifying the correct Category fixed my problem.
It was the same error message, but a different problem and solution.
Related
I successfully installed Visual Studio 2019 for Mac (community edition, version 8.10.14) and managed to install few other required extensions/packages for my work. These are Specflow, Specflow.NUnit, Selenium WebDriver & Server.
I added one feature file and unable to create step def for that feature file as there is no option i can see to create step def. I try and added a separate file (template is called 'specflow step definition' from right click on folder then add>new file) and it's like added sample step def for calculator sample feature. When i right click on steps in feature file it doesn't show an option to generate step defs.
This issue is only on Mac OS. The same project works perfectly fine on windows machine.
A cursory search for specflow visual studio mac seems to conclude that this is not yet available for MacOS versions of Visual Studio. On Windows, the SpecFlow extension for Visual Studio allows you to auto generate step definitions from a feature file. This does not appear to be supported on MacOs, but there is a feature request to add support.
In the meantime, stubbing out a step definition class isn't too bad. The basic shell is:
using TechTalk.SpecFlow;
[Binding]
public sealed class YourStepDefinitions
{
[Given(#"...")]
public void GivenX(...)
{
ScenarioContext.Current.Pending();
}
[When(#"...")]
public void WhenX(...)
{
ScenarioContext.Current.Pending();
}
[Then(#"...")]
public void ThenX(...)
{
ScenarioContext.Current.Pending();
}
}
The challenge is getting the regular expression right. Unfortunately, that requires knowing the step before creating the step definition.
See Step Definitions for more information.
In Visual Studio, in the Test Explorer window, when I group the tests on 'traits', the tests are grouped on a specific category (the public sealed class TestCategoryAttribute class I've put on the unit test)
Is there a way to do the same thing in the Resharper 'unit test sessions' window?
I would like to create a new session with all the tests which have a specific category.
It is possible but is a multi-step process:
Create a session that includes all of your tests (or at least all tests in the target category)
In that session, go to Options -> Group by -> Categories
Right-click the category in the results pane and select "Create New Session"
You could add the annotation Category() to the test method.
[Test]
[Category("MyCategory")]
public void MyTestMethod_Example()
{
// ...
}
I have the following class:-
public Class test{
public void testmethod(int i)
{
i = 56789121;
//code ges here
Console.WriteLine(i);
} }
but i need to run this class an see the result of the Console.writeline,, but i am not sure how i can do this. i usually build a web application using MS visual studio and run the application by clicking on "start" button,, but i have never try to output the result using Console.writeline.
BR
Build a Console application instead.
File -> Add -> New Project... and select Console Application
Change the .cs file that VS produces to be something like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Test
{
public void MyMethod()
{
Console.WriteLine("Hello World!");
}
}
class Program
{
static void Main(string[] args)
{
new Test().MyMethod();
}
}
}
The usual way to do this, as Nigel suggested, is to make a Console App for testing stuff.
However, as you stated, you can't do this from VS Web. I personally have started using LINQPad for one-off testing of my objects:
http://www.linqpad.net/
Once you open it, switch the Language dropdown to "C# Statement(s)" or "C# Program". Hit F4, browse to and add a reference to your DLL and an Import for your namespace. Now you can dim your object and call its methods right from LINQPad. LINQPad will not lock any files, so if you rebuild from VS, you can Alt-Tab back to LINQPad and re-run, and it will use the copy of the library you just build.
I've switched almost exclusively to this, as LINQPad has a really nice interface for exploring objects and exceptions you've .Dump()'ed to the output window.
(No, I'm not affiliated with LINQPad, I'm just a really satisfied customer.)
Alternatively....
Since the C# compiler is part of the .Net framework, and not Visual Studio, you can compile programs on the command line.
For example take the standard HelloWorld program in C#
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}
Create this in notepad, and save as HelloWorld.cs,
Open a command prompt and add the .net framework folder to your path (if not already there):
C:\> PATH=%PATH%;C:\Windows\Microsoft.NET\Framework\v4.0.30319
(note your .net version above may vary)
Then compile using the following command:
C:\> csc HelloWorld.cs
to create HelloWorld.exe.
In an existing solution I added a new Test Project. In my Test Project .cs file I have a class decorated with the [TestClass] attribute and a method decorated with the [TestMethod] attribute. I have verified that in Configuration Manager the build check box is checked for the Test Project (as my google search has revealed was the problem for others with this issue). I have set Test Project as my start up project for the solution. When I try to start the test I get "Can not start test project because the project does not contain any tests". I am really new to unit testing. What am I missing?
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Whole bunch of stuff
Assert.Inconclusive("Done");
}
}
Update: So I opened a new instance of VS, went to File => New => Project => Test Project. Did not touch or edit anything. Went straight to the cs file and here are its contents in its entirety:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject2
{
public class Inspection
{
public bool SubmitInsp()
{
return true;
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Inspection insp = new Inspection();
bool result = insp.SubmitInsp();
Assert.IsTrue(result);
}
}
}
Same error about the project not containing any test when I try to start it. Also found this in the build output "Could not load file or assembly '~\my documents\visual studio 2010\Projects\TestProject2\bin\Debug\TestProject2.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
I don't know that units tests get much simpler than this. What the heck???
I've had the same problem, when tests in an working test project suddenly weren't recognized anymore.
Comparing the project file with one from another working test project showed me that the <ProjectTypeGuids> node was missing from the main <PropertyGroup> node.
Adding this line inside the <PropertyGroup> node solved my problem:
C#:
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
VB:
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>
I was able to get this to work by modifying the devenv.exe configuration file found here:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.config.
Add the following line to the <runtime> section, then restart Visual Studio:
<loadFromRemoteSources enabled = "true" />
(Here is the link that helped me)
The FrstCBC's anwser did not worked for me.
I am on a VirtualBox machine with Windows 7 64 bits and Visual Studio 2012.
I had to move the output to a local folder : open the unit tests project properties and in the Build tab, browse the Output path to a local folder. The tests are now detected and can be run.
Test Projects saved to a network folder or anywhere locally on my computer have this issue. Created another Test Project and saved it to my flash drive, works just fine. I don't know if it is because my machine is 64 bit or because its a virtual machine or what, but I guess I just need to test everything on external storage devices.
For me it was just that my class and method weren't public (I realize the poster did have public, but I found this post by Googling "testclass testmethod margin icons missing"). Dumb mistake on my part, but maybe it will help others.
Verify that all the .cs files are marked as Compile in the Properties window. If it is marked as Content then you will hit this issue.
I'm trying to debug some of my unit tests in Visual Studio 2008 and have noticed that breakpoints don't seem to be halting execution.
I kind of assumed that it was as simple as setting a breakpoint and then doing "Test | Debug | Tests in current context" ... but this never actually hits the breakpoints that I've set.
Am I doing something wrong or is this just broken?
Thanks,
Brandon
I had this same problem until I manually attached to the aspnet_wp.exe process first and then clicked on the Debug Tests buttons. Then my breakpoints were finally hit.
In my case System.Diagnostics.Debugger.Break() doesn't stop at testing method.
[TestClass]
public class ContactListTest
{
#region "Constants"
public const string COVERAGE = "CoverageService";
public const string CompanyList = "CompanyList";
public const string ContactList = "ContactList";
#endregion
[TestMethod]
public void GetContactListTest()
{
System.Diagnostics.Debugger.Break();
var ex = new ServiceFilterExpression(COVERAGE);
ex.Expression = new OpBeginsWith("Type", ContactList);
var result = ex.FetchData();
}
}
if you use nUnit you have to do following
start Nunit with the DLL you want to test.
then in Visual Studio go to
Tools -> Attach to Process
choose your nunit process and click "Attach" then it will halt in all your breakpoints
have fun :-)
The official Microsoft workaround/kludge/zomg-I-can't-believe-they-can't-be-arsed-to-provide-this-after-4-years for MSTEST in VS2010, VS2008, and VS2005 is to add System.Diagnostics.Debugger.Break() to the unit test you want to begin debugging from. This works for all projects with debug symbols referenced by the unit test project.
The .NET runtime will prompt you to dump into debug mode (or close the executing unit test program, or ignore the debug line), and (sometimes) allow you to use instance of visual studio that launched the unit test to do so. You can always debug from a new VS instance. Once you hit that System.Diagnostics.Debugger.Break() line, all other breakpoints will be active and hit (assuming they're in the execution stack).
Check the following:
Are the tests marked with [TestClass] and [TestMethod]?
Are you running Debug or Release mode builds? (Doesn't make a huge difference except when it does) Debug is better.
Are you compiling with or without optimizations? Without is better
Try to run All Tests in Solution in check if you hit the breakpoints
and lastly, maybe you have bug and that's why you are not hitting the code?