I haved created a class with the SetupFixture attribute to have a one time setup as needed for my integration test assembly.
[SetUpFixture]
public static class IntegrationTestsBase
{
public static IKernel Kernel;
[SetUp]
public static void RunBeforeAnyTests()
{
Kernel = new StandardKernel();
if (Kernel == null)
throw new Exception("Ninject failure on test base startup!");
Kernel.Load(new ConfigModule());
Kernel.Load(new RepositoryModule());
}
[TearDown]
public static void RunAfterAnyTests()
{
Kernel.Dispose();
}
}
Resharpers Unit Test Session window has a grouping set to: Projects and Namespaces. However if I use this instance class, Resharpers Unit Test Session says that:
Ignored: Test should be run explicitly
Even tried running these tests with MsTest runner:
Result Message: IntegrationTestsBase is an abstract class.
I have tried to wrap this class to a namespace but nothing changed. If I run individual tests one-by-one it gets runned, however I cannot run them all from the GUI.
How can I fix this issue to be able to run all the tests included in this assembly?
Using NUnit 2.6.4, Resharper 2015.2 and VS2015 update 1.
Your Testclass doesn't need to be static as it gets instantiated by the Testframework and static classes typically can't be instantiated.
The quickest fix is to remove the static keyword except from your Kernel property.
[SetUpFixture]
public class IntegrationTestsBase
{
public static IKernel Kernel;
[SetUp]
public void RunBeforeAnyTests()
{
Kernel = new StandardKernel();
if (Kernel == null)
throw new Exception("Ninject failure on test base startup!");
Kernel.Load(new ConfigModule());
Kernel.Load(new RepositoryModule());
}
[TearDown]
public void RunAfterAnyTests()
{
Kernel.Dispose();
}
}
Keep in mind that whatever you put in Kernel is now shared so if this test is run with multiple threads, the class in Kernel is not isolated to a single test. Which is something you should either be aware of or compensate for.
Related
i've got an issue with ExtentReport, i have few classes with tests and i want to generate a report with all the tests included in it. I have created a BaseTest class with extent report initialization the the test classes has inhertied it and using the static variables to create test, my issue is the BaseTest class test has an [OneTimeTearDown] method in it with extent.Flush() and it called after each of the classes is finished the tests in it and then the result is the last class has overrides the classes before it. Thank you in advance !
Base Class:
[SetUpFixture]
public class BaseClass
{
public static ExtentReports extent;
public static ExtentHtmlReporter htmlReporter;
public static ExtentTest extentTest;
private string path = ""
[OneTimeSetUp]
public void SetUp()
{
htmlReporter = new ExtentHtmlReporter(path);
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
htmlReporter.Config.DocumentTitle = "Tests Report";
htmlReporter.Config.ReportName = "Issta Tests Report";
extent.AddSystemInfo("OS", "Windows 10");
extent.AddSystemInfo("Environment", "QA");
extent.AddSystemInfo("User Name", "Valeri Rozenberg");
}
[OneTimeTearDown]
public void TearDown()
{
extent.Flush();
// Email.sendEmail(path);
}
}
Test class:
namespace AutomationTests
{
[TestFixture]
public class AutomaticDeals: BaseClass
{
DriverManager driverManager;
public IWebDriver driver;
public string url = ""
[SetUp]
public void SetUpTests()
{
driverManager =
DriverManagerFactory.GetDriverManager(DriverType.Chrome);
driver = driverManager.GetWebDriver();
driver.Url = url;
}
[Test]
public void TestLinks()
{
extentTest = extent.CreateTest("TestLinks");
AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
Assert.IsTrue(aDeals.CheckEqualUrls(1));
extentTest.Log(Status.Pass, "Url's in the automatic deals
page are equal.");
}
[Test]
public void TestPrices()
{
extentTest = extent.CreateTest("TestPrices");
AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
Assert.IsTrue(aDeals.allPricesEqual());
extentTest.Log(Status.Pass, "Prices in the automatic deals
page are equal.");
}
}
}
Simplifying the problem statement:
You have an action (initializing the extent report), which you want to perform before any tests run.
You have another action (flushing the extent report), which you want to perform after all the tests have run.
If these actions are made part of a base class, the code is run repeatedly, either once for each test method if you use '[SetUp]and[TearDown]or once for each test fixture class using[OneTimeSetUp]` and '[OneTimeTearDown]'. So what you want to do can't be accomplished in a base class.
Actually, the first part (initialization) can be done in the base class, using a static flag so that you only initialize the first time. However, there's no way for your code to know that it is being called for the last time, so the second part is impossible.
This kind of situation is what SetUpFixtureAttribute is intended to deal with.
Create a new class marked as a [SetUpFixture]. Place the class either in a top-level namespace, which contains all your tests, or (simpler) outside of any namespace.
Give that class [OneTimeSetUp] and [OneTimeTearDown] methods. Move the actions you want to perform before and after running tests into those methods, respectively.
Defined in a SetUpFixture outside of any namespace, the initialization actions will happen before any tests in the assembly are run and the teardown after all of them have completed.
If the one-time initialization leaves behind any info for your tests to use, save that info in static properties of the class.
I googled for this issue but could not find the answer.
My test runs appear to run in parallel and cause each other to fail. They do all pass when run individually. I tried to add thread in the test and put them to sleep but no luck.
Is there a way to run these tests in sequence one after another?
My environment:
Visual Studio 2010
Resharper Jet brains 6.1
I would suggest you have unit tests that are deterministic. That is they don't depend on the order they are run or that other tests be run before or after. Not doing this is a recipe for failure. Most test runners are based on the fact that test methods are completely independent.
This fact is inherently obvious in the way the methods of a test class are invoked. e.g. with MS Test you can have Assembly, Class and Test initialize methods. All of these are invoked for each TestMethod being invoked. For example, with the following class:
[TestClass()]
public class DivideClassTest
{
[AssemblyInitialize()]
public static void AssemblyInit(TestContext context)
{
Console.WriteLine("Assembly Init");
}
[ClassInitialize()]
public static void ClassInit(TestContext context)
{
Console.WriteLine("ClassInit");
}
[TestInitialize()]
public void Initialize()
{
Console.WriteLine("TestMethodInit");
}
[TestCleanup()]
public void Cleanup()
{
Console.WriteLine("TestMethodCleanup");
}
[ClassCleanup()]
public static void ClassCleanup()
{
Console.WriteLine("ClassCleanup");
}
[AssemblyCleanup()]
public static void AssemblyCleanup()
{
Console.WriteLine("AssemblyCleanup");
}
[TestMethod()]
public void Test1()
{
Console.WriteLine("Test1");
}
[TestMethod()]
public void Test2()
{
Console.WriteLine("Test2");
}
}
You'll see output like
Assembly Init
ClassInit
TestMethodInit
Test1
TestMethodCleanup
TestMethodInit
Test2
TestMethodCleanup
ClassCleanup
AssemblyCleanup
Although there is a "Test" class, the TestMethod itself is considered the test. A "test" class can effectively have many tests.
I want to do some integration tests on my project. Now I am looking for a mechanism that allow me execute some code before all tests begin and execute some code after all tests ends.
Note that I can have set up methods and tear down method for each tests but I need the same functionality for all tests as a whole.
Note that I using Visual Studio, C# and NUnit.
Annotate your test class with the NUnit.Framework.TestFixture attribute, and annotate you all tests setup and all tests teardown methods with NUnit.Framework.TestFixtureSetUp and NUnit.Framework.TestFixtureTearDown.
These attributes function like SetUp and TearDown but only run their methods once per fixture rather than before and after every test.
EDIT in response to comments:
In order to have a method run after ALL tests have finished, consider the following (not the cleanest but I'm not sure of a better way to do it):
internal static class ListOfIntegrationTests {
// finds all integration tests
public static readonly IList<Type> IntegrationTestTypes = typeof(MyBaseIntegrationTest).Assembly
.GetTypes()
.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(MyBaseIntegrationTest)))
.ToList()
.AsReadOnly();
// keeps all tests that haven't run yet
public static readonly IList<Type> TestsThatHaventRunYet = IntegrationTestTypes.ToList();
}
// all relevant tests extend this class
[TestFixture]
public abstract class MyBaseIntegrationTest {
[TestFixtureSetUp]
public void TestFixtureSetUp() { }
[TestFixtureTearDown]
public void TestFixtureTearDown() {
ListOfIntegrationTests.TestsThatHaventRunYet.Remove(this.GetType());
if (ListOfIntegrationTests.TestsThatHaventRunYet.Count == 0) {
// do your cleanup logic here
}
}
}
I know I'm missing one important rule about static methods but would would be the point of initializing something if you couldn't' use it later on for different purposes?
I have a method called LoadValidConfig and a private member called configSetup
[TestClass]
public class ConfigControllerTest
{
private ConfigSetup configSetup;
private TestContext testContextInstance;
[ClassInitialize]
public static void LoadValidConfig(TestContext context)
{
ConfigSetup setup;
ConfigController.LoadConfig(out setup);
}
[TestMethod]
public void ConfigTest1()
{
//example test
}
}
I can't get the static method to access and initialize the configSetup class with the ConfigSetup reference populated by the ConfigController.LoadConfig() method.
I could really use some direction here. I've used N Unit in the past to do [setup] and [teardown] initialization and cleanup respectively before but this isn't working like those.
[ClassInitialize] runs in a static context and is run before any of the test methods run. You are probably looking for [TestInitialize] which is for an instance initialization method, and is similar to NUnit's [Setup].
Then try assigning your field after the LoadConfig method:
[TestInitialize]
public void LoadValidConfig()
{
ConfigSetup setup;
ConfigController.LoadConfig(out setup);
configSetup = value;
}
(Or, you could keep ClassInitialize if that suits you better, and make configSetup static).
I'm in the process of setting up tests in NUnit and have a newbie question.
Is it possible to have a Test/s that could be used in multiple [TestFixture]s?
So
[Test]ValidateString(string bob)
Could be called in a series of different [TestFixture]?
That doesn't sound like a test to me. Tests are typically parameterless (unless you're using [TestCase]s) and running it within a context of a single fixture would typically be enough -- it either passes once and that's good or it doesn't and it's a broken test.
If you just have a method that does some validation on a string, you could set it up as a static method on some class (e.g. TestHelpers) and call it from whatever tests (in multiple test fixtures) need it.
Here's another idea: inheritance. You can have a base fixture that has all your tests, and then fixtures that inherit from it that set up whatever variables you need. The tests will run for each fixture. I'm not familiar with Selenium RC, but you should be able to adapt the code below to set up whatever variables you need in various fixtures.
[TestFixture]
public class BaseFixtureTests
{
protected IMyClass _myClass;
[TestFixtureSetUp]
public void FixtureSetup()
{
_myClass = ConfigureMyClass();
}
protected virtual IMyClass ConfigureMyClass()
{
// fixtures that inherit from this will set up _myClass here as they see fit.
}
[Test]
public void MyClassTest1()
{
// test something about _myClass;
}
}
[TestFixture]
public class MySpecificFixture1 : BaseFixtureTests
{
protected override IMyClass ConfigureMyClass()
{
return new MySpecificMyClassImplementation();
}
}
public class MySpecificMyClassImplementation : IMyClass
{
//some implementation
}
You can also add extra tests in each fixture as well that don't test common functionality and don't need to be reused across fixtures.
The newer version of NUnit supports generics. This is a great fit if what you are testing doesn’t need to be configured (only created) from your test code. Here is an example copied from http://nunit.net/blogs/:
[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
private IList list;
[SetUp]
public void CreateList()
{
this.list = new TList();
}
[Test]
public void CanAddToList()
{
list.Add(1); list.Add(2); list.Add(3);
Assert.AreEqual(3, list.Count);
}
}
I’ve also used Anna’s approach of inheritance. One possible refinement to her example (depending on personal preference): Don’t mark the base class as a TestFixture, only the child classes. Each class that you mark as a TestFixture will be displayed as a set of tests in the NUnit client. You will probably never want to run the base class methods directly because the child is providing all of the setup code. If you remove TestFixture from the base class, running invalid tests won’t be an option in the UI. This allows you to run all the tests and see all green… always a nice feeling.
You might be able to achieve what you want with inheritance.
using NUnit.Framework;
namespace ClassLibrary1
{
[TestFixture]
public class TestFixtureBase
{
[SetUp]
public virtual void Setup()
{
// setup code here
}
[Test]
public void CommonTest1()
{
Assert.True(true);
}
[Test]
public void CommonTest2()
{
Assert.False(false);
}
}
public class MyClassTests : TestFixtureBase
{
[SetUp]
public override void Setup()
{
base.Setup();
// additional setup code
}
[Test]
public void MyClassTest1()
{
Assert.True(true);
}
}
}
You can write a method to be called from multiple [Test] methods. But I don't think there is a way to have the same [Test] included in multiple [TestFixture]s.
[TestFixture]
public class TestsOne{
[Test] public void TryOne(){
Helpers.ValidateString("Work?");
}
}
[TestFixture]
public class TestsTwo{
[Test] public void TryTwo(){
Helpers.ValidateString("Work?");
}
}
public static class Helpers{
public static void ValidateString(string s){
Assert.IsNotNull(s);
}
}