Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 309 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does TestInitialize get fired for every test in my Visual Studio unit tests?

#1
I'm using Visual Studio 2010 Beta 2. I've got a single `[TestClass]`, which has a `[TestInitialize]`, `[TestCleanup]` and a few `[TestMethods]`.

Every time a test method is run, the initialize and cleanup methods are ALSO run!

I was under the impression that the `[TestInitialize]` & `[TestCleanup]` should only be run once, per local test run.

Is that correct? If not, what is the proper way to do this?
Reply

#2
this is rather standard behaviour for test suites: setup and teardown before and after each test. The philosophy is that tests should not depend on each other. If you want another behaviour, you should probably use static objects that persist between each test.
Reply

#3
Methods that are marked with [TestInitialize()] attribute are used to prepare aspects of the environment in which your unit test will run. The purpose of this is to establish a known state for running your unit test. You may use the [TestInitialize()] method to copy, alter, or create certain data files that your test will use.

Create methods that are marked with [TestCleanUp{}] attribute to return the environment to a known state after a test has run. This might mean the deletion of files in folders or the return of a database to a known state. An example of this is to reset an inventory database to an initial state after testing a method that is used in an order-entry application.

For more information please refer :

[To see links please register here]

Reply

#4
Full example taken from the [microsoft documentation][1]:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SampleClassLib;
using System;
using System.Windows.Forms;

namespace TestNamespace
{
[TestClass()]
public sealed class DivideClassTest
{
[AssemblyInitialize()]
public static void AssemblyInit(TestContext context)
{
MessageBox.Show("AssemblyInit " + context.TestName);
}

[ClassInitialize()]
public static void ClassInit(TestContext context)
{
MessageBox.Show("ClassInit " + context.TestName);
}

[TestInitialize()]
public void Initialize()
{
MessageBox.Show("TestMethodInit");
}

[TestCleanup()]
public void Cleanup()
{
MessageBox.Show("TestMethodCleanup");
}

[ClassCleanup()]
public static void ClassCleanup()
{
MessageBox.Show("ClassCleanup");
}

[AssemblyCleanup()]
public static void AssemblyCleanup()
{
MessageBox.Show("AssemblyCleanup");
}

[TestMethod()]
[ExpectedException(typeof(System.DivideByZeroException))]
public void DivideMethodTest()
{
DivideClass.DivideMethod(0);
}
}
}


[1]:

[To see links please register here]

Reply

#5
`TestInitialize` and `TestCleanup` are ran before and after *every* test in the same class they are defined, this to ensure that no test is coupled to any other test in the same one class and to always start each test in a clean state.

---

If you want to run methods before and after *all* tests in a particular class, decorate relevant methods with the `ClassInitialize` and `ClassCleanup` attributes.

If you want to run methods before and after *all* tests in a particular test project (assembly), decorate relevant methods with the `AssemblyInitialize` and `AssemblyCleanup` attributes.

Relevant information from the auto generated test-file in Visual Studio:


You can use the following additional attributes as you write your tests:

<!-- language: c# -->

// Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) { }

// Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public static void MyClassCleanup() { }

// Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize() { }

// Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup() { }

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through