Can I perform action before/after all tests - c#

I know, that in Coded UI there are two methods (MyTestInitialize and MyTestCleanup) which can perform action before and after each tests. I need add some action which launch before and after all tests. For example, if you familiar with rspec there are two methods before() and after(), which take parameter :each (will call before/after each tests) or :all (will call before/after all test).

Create your methods with the [ClassInitialize] and [ClassCleanup] attributes as necessary. This should be within your Test Class. Example:
[CodedUITest]
public class MyTestClass
{
[ClassInitialize]
public void DoSomethingFirst()
{
// your code here that will run at the beginning of each test run.
}
[TestInitialize]
public void RunBeforeEachTest()
{
// your test initialization here
}
[TestMethod]
public void MyTestMethod()
{
}
}
And you would do the same for your [TestCleanup] and [ClassCleanup].
More on this attribute can be found here.

Related

Is it possible to skip the AssemblyInitialize attribute for special tests?

I have two tests: BooUnitTest and BooIntegrationTest.
Within the same testing project I'm holding a method with the AssemblyInitialize attribute decorator:
AssemblyTestsHandler.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AssemblyTestsHandler
{
[AssemblyInitialize]
public static async Task Bootstrap()
{
//Do complex stuff...
}
}
Is it possible to make the Bootstrap method that works only for BooIntegrationTest and not for FooUnitTest?
For example
FooTests.cs:
[TestClass]
public class FooTests
{
[TestMethod]
public async Task FooUnitTest()
{
//Skip Bootstrap()!!
}
}
BooTests.cs
[TestClass]
public class BooTests
{
[TestMethod]
public async Task BooIntegrationTest()
{
//Do Not Skip Bootstrap()!!
}
}
Here is the project structure:
TestingProject
-AssemblyTestsHandler.cs
-BooTests.cs
-FooTests.cs
No, you can't. AssemblyInitialize will be called once per assembly and it will be called before all other methods (AssemblyInitializeAttribute Class):
The method marked with this attribute will be run before methods
marked with the ClassInitializeAttribute, TestInitializeAttribute, and
TestMethodAttribute attributes. Only one method in an assembly may be
decorated with this attribute.
Actually unit tests should be executed in random order:
FooUnitTest1
BooIntegrationTest2
FooUnitTest2
BooIntegrationTest1
in this case any static initialization will affect all other unit-tests.
I think, there are two possibilities:
You can use TestInitialize and TestCleanup attributes for FooTests. But this will affect performance
You can use ClassInitialize for BooTests, but in this case, you have to trigger integration tests separately from unit tests. Integration- and unit tests can be distinguished by TestCathegory attribute.

Test cases to run in sequence instead of parallel

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.

Visual Studio C# unit testing - Run Unit test with varied/multiple test initializations, Run same unit test multiple times?

What i want to do is this :
Create a bunch of Unit Tests.
Create a variety of different permutations/combinations of initialization of mocks, input variables etc.
Run each given unit test with a against a set of such initializations based on some parameters.
How would i go about doing something like this?
Is there already any framework to handle this (I.e. run a given test multiple times while changing initialization)? Can you suggest any design or ideas with which i could make something to do this?
I am aware of unit testing frame works. i use NUnit and Rhino mocks myself.
Shown below is an example of what i need.
[Test Initialize]
Setup( <-possible parameter-> )
[Test Method]
TestA()
now i want TestA() to be run multiple times. Each time the Test initialize would pick another initialization combination.
More clarification
Lets suppose a test would require variables A, B, C. Each of them are very complex objects with the end result that the a large number of combinations can be formed. So i'm hoping that somehow i could create a test initialize that could possible iterate through a list of such combinations, so it would initialize them, run the TESTA, go back to next initialization in the list, run TESTA again and so on until the list runs out. Next it picks another list for TESTB and once again follows this process.
At the least im hoping for some ability to be able to run a given TEST function n times. The rest i know i can build once this is possible
In nUnit you can use the [TestCase] attribute for simple types:
[Test]
[TestCase("a", "b")]
[TestCase("c", "b")]
[TestCase("a", "d")]
public void TestMethod(string param1, string param2){
// run your test with those parameters
}
Or you can use a TestCaseSource method for complex types:
[Test]
[TestCaseSource("GetTestCases")]
public void TestMethod(MyObject1 param1, MyObject2 param2){
// run your test with those parameters
}
private IEnumerable GetTestCases(){
yield return new TestCaseData( new MyObject1("first test args"),
new MyObject2("first test args"))
.SetName("SomeMeaningfulNameForThisTestCase" );
yield return new TestCaseData( new MyObject1("2nd test args"),
new MyObject2("2nd test args"))
.SetName("SomeMeaningfulNameForThisTestCase2" );
}
You can do something similar in MS-Test using a DataSource:
http://codeclimber.net.nz/archive/2008/01/18/How-to-simulate-RowTest-with-MS-Test.aspx
You might be able to do this without needing any framework-specific addons by creating an abstract base class that contains all your test functions, then inheriting that base class with multiple classes, each with their own setup function.
public abstract class MyTests
{
[Test]
public void TestOne()
{
...
}
[Test]
public void TestTwo()
{
...
}
}
[TestFixture]
public class FirstSetup : MyTests
{
[Setup]
public void Setup()
{
...
}
}
[TestFixture]
public class SecondSetup : MyTests
{
[Setup]
public void Setup()
{
...
}
}
I have done this in other languages, but not sure how the various C# frameworks will handle it.

Handle any error in NUnit

I have got a standard NUnit unit test.
[TestFixture]
public class MyTest
{
[SetUp]
public virtual void Setup()
{
}
[TearDown]
public void CleanUp()
{
}
[Test]
public void Test01()
{
Assert.AreEqual(10, 10);
}
[Test]
public void Test02()
{
Assert.AreEqual(4, 1);
}
[Test]
public void Test03()
{
Assert.AreEqual(1, 51);
}
//......a huge amount of Tests
[Test]
public void TestN()
{
Assert.AreEqual(1, 1);
}
}
As you see, there are a lot of tests here. What I need to do is to handle any error in each test and to get the information about the error without modifying the body of a test method.
So in JUnit there is an interface called TestListener which allows to handle any test error, see the error message, see the name of the test, etc.
I didn't find something similar in NUnit. Does it exist? Or is there is a way to do it?
It sounds like you probably want EventListeners:
EventListeners are able to respond to events that occur in the course of a test run, usually by recording information of some kind. Note that EventListeners called asynchronously with respect to test execution and are not able to affect the actual execution of the test.
It sounds like you'll be interested in this method:
void TestFinished(TestResult result);
(As an aside, it seems odd to me that this doesn't use normal .NET events or provide a no-op interface implementation in an abstract class, but there we go...)

ClassInitialize attribute in unit test based class not called

I added these method in a TestBase class :
[ClassInitialize]
public static void InitializBeforeAllTests()
{
}
But when I run in Debug an unit test Test1() :
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
}
The TestBase.InitializBeforeAllTests() method is never called.
Why?
When declaring ClassInitialize attribute on a method, the method has to be static, public, void and should take a single parameter of type TestContext.
If you're having also other method with the AssemblyInitialize attribute on the same unit test, the test will run but will skip on all test methods and will go directly to AssemblyCleanup or just quit.
Try the example on ClassInitialize attribute in MSDN.
You can setup an assembly initialize method in your base class. Not quite the same as ClassInitialize, but it's a viable option. Source:The Workaround mentioned here.
[TestClass]
public abstract class TestBase
{
[AssemblyInitializeAttribute]
public static void Initialize(TestContext context)
{
// put your initialize code here
}
}
You can also add a Cleanup method:
[AssemblyCleanup]
public static void Cleanup()
{
//clean up stuff here
}
for whatever reason, the unit test framework's UnitTestExecuter only allows one ClassInitialize and one ClassCleanup method to be defined per test class... unlike TestInitialize and TestCleanup methods, which get called in both the derived and base test class...
i know this is a very old question, but its the first to popup in google search when looking for a similar problem, anyhow, here is an update for the answer:
[ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
public static void YOUR_INIT_METHOD_NAME(TestContext context)
Note: you need MSTest.TestFramework-Version 2.0.0 package or newer for this to work.
The MS link is not working anymore.
Anyway, one way to work around this issue is to simply move your initialization code into the constructor of the base class. This will ensure that it gets called from any descendant classes whenever they are instantiated.
[TestClass]
public class TestBase
{
public TestBase()
{
// Initialization Code
}
}
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
// At this point the base constructor should have been called
}
}
In my case, I had the[Ignore] attribute applied (test is ran manually)
This caused [AssemblyInitialize] to never be called
If I removed the [Ignore] attribute [AssemblyInitialize] was called as expected.
Oddly, [AssemblyCleanup] is still called with or without the [Ignore] applied to my test

Categories

Resources