I have these 3 tests:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
namespace FirstTestCase
{
class _04_02_Media
{
class NUnitTest
{
[TestCase(TestName = "04_02_01_Libraries_Add_OnDemand_Video")]
public void Libraries()
{}
[TestCase(TestName = "04_02_02_Replace_OnDemand")]
public void OnDemandReplace()
{}
[TestCase(TestName = "04_02_03_Delete_OnDemand")]
public void OnDemandDelete()
{}
For some reason i cannot understand and is making me go crazy, the "delete" test, the one supposed to be the last, happens second.
This is a big deal as the "replace" test, that happens last, uses the deleted video.
Why does it run in this order? Is there anything else i should use to change the order?
You can use the Order attribute to specify the order:
[Order(1)]
public void Test1() { /* ... */ }
[Order(2)]
public void Test2() { /* ... */ }
[Order(3)]
public void Test3() { /* ... */ }
However, you should really try to make sure your tests are self-contained otherwise they can be quite brittle.
Just use order attribute.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
namespace FirstTestCase
{
class _04_02_Media
{
class NUnitTest
{
[Test, Order(1)]
public void Libraries()
{}
[Test, Order(2)]
public void OnDemandReplace()
{}
[Test, Order(3)]
public void OnDemandDelete()
{}
}
}
}
Related
I am trying to create a web automation that logs in and out just once. As the website is formed by different products I am required to build different sets of tests in different classes/files(Exaple: ReportsTests, AuthenticationTests, etc.) I am perfectly able to create a TestFixture which logs in and out using the OneTimeSetup and OneTimeTearDown but that happens once for every class.
What I am trying to create is a TestFixture within a TestFixure in a way that the Login and Logout happens once on the first TestFixture and the second execute the sets of tests.
So far I've got this:
Setup.cs
using NUnit.Framework;
using System;
namespace TestsSetup
{
[TestFixture]
public class TestSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Login in");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out");
}
}
}
TestSuit.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TestsSetup;
namespace TCISuiteSetup
{
[TestFixture]
public class CWTestSuite : TestSetup
{
[TestFixture(1)]
public class SuiteSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Nothing happens on this step");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Nothing happens on this step");
}
}
}
}
Test1.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TCISuiteSetup;
namespace TCI.Tests
{
[TestFixture(1)]
public class UserManagerTests : CWTestSuite.SuiteSetup
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
test2.cs
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using TCISuiteSetup;
namespace TCI.Tests
{
[TestFixture(1)]
public class ReportTests : CWTestSuite.SuiteSetup
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
If I run the test like this all tests fail. If I remove the (1) from the TestFixture(1) it pass everything but it does not hit any of the [OneTimeSetup]/[OneTimeTearDown] if I change the UserManagerTests : TestSuite.SuiteSetup to UserManager : TestSetup it hits just the first [TestFixture]
Am I missing anything.
dNesting of test fixture classes has no meaning whatsoever to NUnit. For a few years we have talked about making it meaningful, but never did anything about it.
SO, in terms of test fixtures, CWTestSuite and CWTestSuite.SetUpFixture have no relationship at all. It's just as if they were defined separately, without nesting.
If you want one fixture to group fixtures, NUnit provides SetUpFixture, which is a way to define one-time setup and teardown behavior that applies across multiple classes. A SetUpFixture is defined for a particular namespace and essentially wraps all the TestFixtures defined within that namespace and subordinate namespaces.
For example, if you were to put all your TestFixtures in some common namespace, then you could add a SetUpFixture in the same namespace like this...
namespace Some.Common.Namespace
{
[SetUpFixture]
public class TestSetup
{
[OneTimeSetUp]
public void Setup()
{
Console.WriteLine("Login in");
}
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out");
}
}
public class UserManagerTests
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
public class ReportTests
{
[Test]
public void Test1()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test2()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
Note that I have made no use of inheritance in this example. It's not required and would break things if I had used it. If you need some common initialization logic for some but not all of the fixtures in the namespace, then you can give them a common base class, but that must be separate from the SetUpFixture and should ideally be abstract.
As a minor note, I removed all redundant [TestFixture] annotations. That's the style I recommend.
I found a way but maybe it is not the most elegant:
Setup.cs
using NUnit.Framework;
using System;
namespace TestsSetup
{
[TestFixture]
public class CWTestSuite : TestSetup
{
[TestFixture]
public class SuiteSetup
{
static private bool isLoggedOn;
[OneTimeSetUp]
public void TestSuiteSetup()
{
if (isLoggedOn == false)
{
Console.WriteLine("Loggin happens here");
isLoggedOn = true;
}
}
}
}
[SetUpFixture]
public class TestSetup
{
[OneTimeTearDown]
public void Teardown()
{
Console.WriteLine("Login out happens here");
}
[TestFixture]
public class Testing
{
[Test]
public void Test1()
{
Console.WriteLine("All tests for this product has been executed");
}
}
}
}
any of the test.cs matching the following format
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using static TestsSetup.CWTestSuite;
namespace TestSetup
{
[TestFixture]
public class UserManagerTests : SuiteSetup
{
[Test]
public void Test3()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
[Test]
public void Test4()
{
Console.WriteLine("Assertion");
Assert.AreEqual(1, 1);
}
}
}
I could not get rid of the [TestFixture] within the [TestFixture] (Setup.cs/public class CWTestSuite). If I delete it it will hit the [OneTimeTearDown] after running the tests in a class.
I had to add a flag on the login steps as that [OneTimeSetUp] is called at the beginning of every set of tests.
The only way I found to trigger the [OneTimeTearDown] was to add a [TestFixture]/[Test] afterwards. it is called at the very end and it may just print a console.log but it needs to be there and it needs to be called.
BTW. This is a possible solution/work around but not recommended at all. If you want to do something like this I recommend using NUnit + SpecFlow (The [BeforeTestRun] is what you are looking for.
Hey guys im new with the NSubstitute framework. I'm trying to test some of my classes, but when i use NSubstitute to check received calls it says received no matching calls.
I'm trying to test if the method Tick() is receiving update() from track class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Region;
using ATM_System.Track;
namespace ATM_System
{
public class ATM
{
private List<ITrack> _tracks;
private IRegion _region;
private List<IEventDetection> _eventdetects;
private List<IEvent> _events;
public ATM()
{
_tracks = new List<ITrack>();
_region = new Region.Region(100000,100000); //could be changed by user
_events = new List<IEvent>();
_eventdetects = new List<IEventDetection>();
}
public void Tick()
{
// update track positions
foreach (var track1 in _tracks)
{
track1.update();
}
//check for events
foreach (var detector in _eventdetects)
{
_events.AddRange(detector.DetectEvent(_tracks));
}
//handle events and output
foreach (var event1 in _events)
{
event1.HandleEvent();
event1.LogEvent();
}
}
public void IncomingTrack(ITrack track)
{
//add incoming track
_tracks.Add(track);
}
}
}
TEST FILE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Track;
using NUnit.Framework;
using NSubstitute;
namespace ATM_System.Tests.Unit
{
[TestFixture]
class ATMUnitTests
{
private ATM _uut;
private ITrack _track;
private IEvent _event;
private IEventDetection _eventDetection;
[SetUp]
public void Setup()
{
_track = Substitute.For<ITrack>();
_event = Substitute.For<IEvent>();
_eventDetection = Substitute.For<IEventDetection>();
_uut = new ATM();
}
[Test]
public void Tick_UpdateTracks_TracksUpdated()
{
_uut.Tick();
_track.Received().update();
}
}
}
You forgot to include _track in notification receivers. It simply hasn't subscribed to event and as a result is not notified. To fix simply call your IncomingTrack method:
[Test]
public void Tick_UpdateTracks_TracksUpdated()
{
_uut.IncomingTrack(_track);
_uut.Tick();
_track.Received().update();
}
I am trying to assign the property dynamically using post sharp. Such as in the below example I would like to have custom logging messages. But the problem is how could I set xname in the attribute part.In the first log I would like to have the logging message would have the persons name in the beginning but the second logging message would have the persons name at the end.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationLogging
{
internal static class Program
{
private static void Main()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
SayHelloInTurkish("Ayse");
SayGoodByeSayHelloInEnglish("Elizabeth");
}
[Trace("xname Loglanıyor Gunaydın")]
private static void SayHelloInTurkish(string personName)
{
Console.WriteLine("Hello, world.");
}
[Trace("Good Bye Logging for xname")]
private static void SayGoodByeSayHelloInEnglish(string personName)
{
Console.WriteLine("Good bye, world.");
}
}
}
using System;
using System.Diagnostics;
using PostSharp.Aspects;
namespace ConsoleApplicationLogging
{
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
private readonly string category;
public TraceAttribute(string category)
{
this.category = category;
}
public string Category { get { return category; } }
public override void OnEntry(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnExit(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
}
}
If I understand correctly, you need something like this (no sanity checking included):
this.category.Replace("xname", (string)args.Arguments[0])
If I did not understand correctly, feel free to comment below.
I don't have enough rep to comment, so I am posting a question here. I read this question Get list of failing tests from Nunit. I am trying to implement the nunit addin, I used this code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Core.Extensibility;
namespace NunitAddin
{
[NUnitAddinAttribute(Type = ExtensionType.Core,
Name = "addin",
Description = "addin")]
public class NunitAddin : IAddin
{
public bool Install(IExtensionHost host)
{
IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;
listeners.Install(this);
return true;
}
public void TestStarted(NUnit.Core.TestName testName)
{
}
public void TestFinished(NUnit.Core.TestResult result)
{
}
public void RunStarted(NUnit.Core.TestName testName)
{
}
public void RunFinished(NUnit.Core.TestResult result)
{
}
public void UnhandledException(Exception exception)
{
}
public void TestOutput(NUnit.Core.TestOutput testOutput)
{
}
}
}
But when I call it using
var addin = new NunitAddin.NunitAddin();
var a = addin.Install(CoreExtensions.Host);
I get an error
NunitAddin.NunitAddin is not {0} extension point
on
listeners.Install(this);
Does anyone know how to solve this problem?
Never mind, issue solved. Just a stupid mistake, I had NunitAddin : IAddin instead of NunitAddin : IAddin; EventListener
I have two classes in class library
namespace ClassLibrary3
{
public class Class1
{
public string title;
public string author;
public Class1(string title, string author)
{
this.title = title;
this.author = author;
}
}
}
Another class
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace ClassLibrary3
{
class Class2
{
private Hashtable books;
public Class2()
{
books = new Hashtable();
}
public void addBook(Class1 book)
{
books.Add(book.title, book);
}
public Class1 getBook(String title, String author)
{
return (Class1)books[title];
}
public void removeBook(string title)
{
if (books[title] != null)
books.Remove(title);
}
}
}
And my test is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
namespace ClassLibrary3
{
[TestFixture]
class TEST
{
[Test]
public void getbooktest()
{
Class1 c1 = new Class1("story", "James");
Class2 c2 = new Class2();
Assert.AreEqual("story", c2.getBook("story", "James"));
}
}
}
Basicly the problem is Nunit doesnt test it. It finds the dll. Loads the test class. But dont come upto the test method.
Please any idea..........
NUnit can't see your TEST class unless you mark it as public, change it to
[TestFixture]
public class TEST
{
...
Side note, consider giving it a better name than TEST ;-)