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.
Related
I have a question on running UnitTests sequentially. Unfortunately in scenario it is not an option to run them parallel or mock the database. The project is written in .NET core 3.1 and the UnitTests need to execute database operations before and after a Unittest has run.
After reading https://www.meziantou.net/mstest-v2-execute-tests-in-parallel.htm and a lot of other articles about sequential UnitTesting I came up with this (simplified):
BaseClass:
namespace XY.Test
{
[TestClass]
public class BaseTest: TimerModel
{
private static readonly DbCreator Creator = new DbCreator();
public static readonly DbConnectionManager ConnectionManager = new DbConnectionManager();
[TestInitialize]
public void BaseTestInitialize()
{
CreateTestData();
}
[TestCleanup]
public void BaseTestCleanup()
{
RemoveTestData();
}
public void CreateTestData()
{
RemoveTestData();
Creator.ExecuteSqlFromScript(ConnectionManager, #"Resources\CreateTestData.sql");
}
public void RemoveTestData()
{
Creator.ExecuteSqlFromScript(ConnectionManager, #"Resources\EmptyTestDataTables.sql");
}
}
}
TestClass:
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)] //<-- Also tried out Workers = 1 and Scope = ExecutionScope.MethodLevel
namespace XY.Test.Models
{
[TestClass]
public class TerminalConfigModelTest: BaseTest
{
[TestMethod]
[DoNotParallelize]
public void TestMethod1()
{
...
}
[TestMethod]
[DoNotParallelize]
public void TestMethod2()
{
...
}
}
}
For some reason, no matter what I do, the UnitTests are being executed parallel. What do I have to change in order to have them executed sequentially?
When I execute all tests in the test class, the TestInitialize of the base class is called twice before the TestCleanup is run. This causes the CreateTestData method to fail as indexes prevent a double insert of the test data.
What I would expect:
TestInitialize1 is called
TestMethod1 is executed
TestCleanup1 is called
TestInitialize2 is called
TestMethod2 is executed
TestCleanup2 is called
...
What happens:
TestInitialize1 is called
TestMethod1 is executed
TestInitialize2 is called before TestCleanup1 is called
TestMethod2 execution fails
Am I missunderstanding the [DoNotParallelize] option?
Paralelism isn't the problem here, my tests are definitely sequential and [ClassCleanup] also screwed me over. It's just unintuitive and weird, more info here.
I wanted to use ordered tests but it seems it's a legacy functionality only in MSTest-v1 and new versions of Visual Studio don't even support it.
Best thing i can tell you now is just don't use [ClassCleanup].
Use [TestCleanup] or [AssemblyCleanup].
I write this helloworld unit test and build it. It always have this build error: "The command://...../Nunit.ConsoleRunner3.6/nunit3-console.exe exited with code 1".I search all the stackoverflow but there is no a single thread about Nunit test exist code 1, do you know where can I find the information for this code?
using NUnit.Framework;
namespace SomeName
{
[TestFixture]
class IdentifierTest
{
[SetUp]
public void SetUp()
{
}
[TearDown]
public void TearDown()
{
}
[Test]
public void DetectIDTest()
{
Assert.AreEqual("hi","ho");
}
}
}
A positive exit code indicates the number of tests that has failed (in your case 1) as per this line in the NUnit console runner source code. Additional (negative) failure codes also have meaning per these six lines. This is substantiated by a discussion about exit codes here, I have attached the response as an image for easy reference
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'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.