I'm using NUnit with AutoFixture, AutoMoq and the Theory attribute.
Here is my test method,
[TestFixture]
public class TestClass
{
[Theory, AutoMoqData]
public void TestI(I i)
{ }
}
the interface
public interface I
{ }
and the attribute
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{ }
}
When I build my solution the test is discovered. When I run the test the following is written to the Output window:
NUnit 1.0.0.0 executing tests is started
Run started: [...].Test.dll
NUnit 1.0.0.0 executing tests is finished
Test adapter sent back a result for an unknown test case. Ignoring result for 'TestI(Mock<TestClass+I:1393>.Object)'.
When using xUnit.net the above test is run correctly. Why is not it working with NUnit?
I have the following Nuget packages installed in the test project:
AutoFixture 3.18.1
AutoFixture.AutoMoq 3.18.1
Moq 4.2.1402.2112
NUnit 2.6.3
NUnitTestAdapter 1.0
I'm running the test from within Visual Studio 2013 Professional. I also tried running the test in the separate GUI runner, with the same result.
The following NUnit test passes in Visual Studio 2013 with the TestDriven.Net add-in:
internal class AutoMoqDataAttribute : AutoDataAttribute
{
internal AutoMoqDataAttribute()
: base(
new Fixture().Customize(
new AutoMoqCustomization()))
{
}
}
public interface IInterface
{
}
public class Tests
{
[Test, AutoMoqData]
public void IntroductoryTest(IInterface i)
{
Assert.NotNull(i);
}
}
The built-in test runner doesn't discover the above test. This looks like a bug in the test runner.
Related
Steps to reproduce:
Create project in rootDir\src\ProjectToTest with class
public class Entity
{
public string Property {get;init;}
}
Create unit test project in rootDir\test\TestProject with test
public class UnitTest1
{
[Fact]
public void Test1()
{
var fixture = new Fixture();
var instance = fixture.Build<Entity>().With(x => x.Property, "1").Create();
}
}
Run in cmd:
dotnet clean
dotnet restore
dotnet build
dotnet minicover instrument
dotnet test --no-build
on last command occurs exception
System.InvalidProgramException : Common Language Runtime detected an invalid program
I run selenium tests, and I have created a custom Test Fixture attribute that I apply to each fixture 3 times, each time with a new driver so my tests can run 3 separate times in 3 separate browsers. It looks like this:
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
class Edit<TWebDriver> : BaseTest<TWebDriver> where TWebDriver : IWebDriver, new()
{
[Test]
public void Test()
{
//test code
}
}
For my test fixtures, I mirror the web applications views in a 1:1 ratio - so the Dashboard\Index view in the web app code would be the Dashboard\Index folder for my tests that test the same view, therefore the test organization is very strict.
I am running into an issue that there are certain tests that should not run in certain browsers, such as IE. The majority of the tests need to however. What I am trying to do is for each test fixture
Is there any way to get the Test Fixture typeof value at test run time so I can do the following (pseudo code)...:
[Test]
public void Test()
{
if(testFixture typeof is InternetExplorerDriver)
{
Assert.Ignore("test not to be run in IE");
}
// all the test code
}
Please try the code modified below.
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
class Edit<TWebDriver> : BaseTest<TWebDriver> where TWebDriver : IWebDriver, new()
{
private TWebDriver webDriver;
public Edit(TWebDriver webDriver)
{
this.webDriver = webDriver;
}
[Test]
public void Test()
{
//test code
if (this.webDriver.GetType() == typeof(InternetExplorerDriver))
{
Assert.Ignore("test not to be run in IE");
}
}
}
I develop the following unit test using Nunit 3.6:
using System;
using NUnit.Framework;
namespace UnitTest.Test
{
[TestFixture]
public class UnitTest1
{
[Test]
public void Test_MarkedAsTest()
{
Assert.IsTrue(true);
}
[Test]
[Explicit]
public void Test_MarkedAsTestExplicit()
{
Assert.IsTrue(true);
}
[Test]
[Category("Manual")]
public void Test_MarkedAsTestManual()
{
Assert.IsTrue(true);
}
[Test]
[Category("ShortRunning")]
public void Test_MarkedAsTestShortRunning()
{
Assert.IsTrue(true);
}
}
}
I want to be able to filter the test upon its category. I am using VSTest to execute the test and following the documentation
When the command is executed without exclusions, 3 test ran (Explicit does not ran):
If test filter parameter is introduced, then "Explicit" test are executed:
If I try to filter, in order to execute the test NOT in the Manual or Explicit category, VSTest runs all the test
How to filter a category and the Explicit tests?
Good morning,
As context, I am working in an automated test suite using nUnit 3.0.1 (with Selenium). The tests are being runned directly from Visual Studio 2013' Test Explorer.
My problem is this: There is some Setting up functionality that I am trying to make automatic when starting to run the test suite. These are things that need to be done only ONCE for all the suite of tests, so using the [SetUp] Attribute for each test class is not what I intend.
When I run the Tests however, my setting up is not even touched :(. Debugging confirms that the setting up is not being done.
So far I have tried using: [SetUpFixture] along with [OneTimeSetUp] or [SetUp] (in many combinations), also tried removing the namespace. I am still not getting what I need.
I am fairly new with nUnit, so I would like some guidance about what could be wrong, or some alternatives to making sure the scripted setup is run.
I hope you followed this pattern:
In the below MySetupClass which defines the Test Suite setup and teardown methods which are executed only once at the start and end of the test runs.
Refer SetupFixture documentation.
MySetupClass.cs
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
Console.WriteLine("SetupFixture - OneTimeSetup");
}
[OneTimeTearDown]
public void RunAfterAllTests()
{
Console.WriteLine("Suite TearDown - OneTimeTearDown");
}
}
}
In the following SuccessTests.cs, Setup and TearDown are executed before and after each test, since these methods are defined in a class marked with atttribute TestFixture. OneTimeSetup & OneTimeTearDown attributes defines methods in the test case to be executed once before and after all the test methods in the class.
Refer to the documentation of Setup attribute [here][2].
SuccessTests.cs
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[SetUp]
public void SetUp()
{ Console.WriteLine("Test Setup"); }
[TearDown]
public void TearDown()
{ Console.WriteLine("Test Teardown"); }
[OneTimeSetup]
public void OneTimeSetup()
{ Console.WriteLine("Test Fixture - OneTimeSetup"); }
[OneTimeTearDown]
public void OneTimeTearDown()
{ Console.WriteLine("Test Fixture - OneTimeTearDown"); }
[Test]
public void Test1()
{ Console.WriteLine("Actual Test1"); }
[Test]
public void Test2()
{ Console.WriteLine("Actual Test2"); }
}
}
Running the above tests will give this result.
Suite Setup - OneTimeSetup
Test Fixture - OneTimeSetup
Test Setup
Actual Test1
Test Teardown
Test Setup
Actual Test2
Test Teardown
Test Fixture TearDown - OneTimeTearDown
Suite TearDown - OneTimeTearDown
I have a simple NUnit project in C#. I have a file TestFixture.cs that successfully runs its four tests. If I want to add additional tests in a new CS file, how do I do this? If I simply copy the code from TestFixture.cs into a new TestFixture2.cs and rename the TestFixture classes to TestFixture3 and 4 respectively, they do not execute. I would think they would automatically run. Do I need to tell the runner specifically if it is a file other than TestFixture.cs? Here is my code:
namespace NUnitTest1
{
[TestFixture]
public class TestFixture3
{
[Test]
public void TestTrue()
{
Assert.IsTrue(true);
}
// This test fail for example, replace result or delete this test to see all tests pass
[Test]
public void TestFault()
{
Assert.IsTrue(true);
}
}
[TestFixture]
public class TestFixture4
{
[Test]
public void TestTrue()
{
Assert.IsTrue(true);
}
// This test fail for example, replace result or delete this test to see all tests pass
[Test]
public void TestFault()
{
Assert.IsTrue(true);
}
}
}
My unit test runs by executing a command line program using the following code:
namespace NUnitTest1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string[] my_args = { Assembly.GetExecutingAssembly().Location };
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);
if (returnCode != 0)
Console.Beep();
}
}
}
NUnit will automatically get all types marked with TestFixture attribute from test assembly when you load it (it does not matter whether fixtures in one .cs file or in separate). Then NUnit will search for methods marked with Test or TestCase attributes and load them. If NUnit don't see some test, then make sure you loaded latest version of your test assembly.
NOTE: If you are using NUnit test runner, then there is nice setting Reload when test assembly changes. With this option turned on NUnit will automatically reload test assembly when you rebuild it in Visual Studio.