Global.asax for Unit Tests? - c#

In my MSTest UnitTest project, before running any tests, I need to execute some commands. Is there a feature, kind of like Global.asax is for web based projects, that will let me kick off something before any tests run?
I should make it clear that when I say "execute some commands", I don't mean DOS commands, but execute some code.

If I understand correctly, you need to have some initialization code run before you start your tests. If that is indeed the case you should declare a method inside your unit-test class with the ClassInitializeAttribute like this:
[ClassInitialize]
public void ClassSetUp()
{
//initialization code goes here...
}
Edit: there is also the AssemblyInitializeAttribute that will run before any other tests in assembly

Unit test frameworks usually support set up and "tear down" methods for both the entire test fixture and individual tests. MSTest lets you specify which methods to run when with these attributes:
[ClassIntialize()]
public void ClassInitialize() {
// MSTest runs this code once before any of your tests
}
[ClassCleanup()]
public void ClassCleanUp() {
// Runs this code once after all your tests are finished.
}
[TestIntialize()]
public void TestInitialize() {
// Runs this code before every test
}
[TestCleanup()]
public void TestCleanUp() {
// Runs this code after every test
}
Having said that, be careful with the class initialize and cleanup methods if you're running ASP.NET unit tests. As it says in the ClassInitializeAttribute documentation:
This attribute should not be used on
ASP.NET unit tests, that is, any test
with [HostType("ASP.NET")] attribute.
Because of the stateless nature of IIS
and ASP.NET, a method decorated with
this attribute may be called more than
once per test run.

properties of you project and then debug field there you can specify arguments
EDIT
When you see the debug menu in the properties you can start an external program to do certain things for you when you start debugging. This will trigger when you launch an instance of your test project. You can also specify command line arguments in the command line arguments box.
For example I use NUnit I specify NUnit as the external program and specify the location of the .dll in the command line arguments

Related

Run xUnit tests only on certain conditions

I'm using xUnit and I have same load tests which I'd like to execute only under certain conditions:
Execute them always on our build server.
Do not execute them locally within the IDE when clicking Run all tests in the test runner.
Execute all load tests locally within the IDE if explicitly triggered.
How can I achieve this? Is there any chance to accomplish this with xUnit, preferably without conditional compiles?
I think the easiest way to get around this if not the prettiest, is enclosing the test you want to "skip" locally is to enclose your test like this:
public class TestClass1
{
#if !DEBUG
[Fact]
#endif
public void Test1()
{
Assert.True(true);
}
}
This will skip the [Fact] attribute which triggers the test at run time, as long as you IDE or editor, or however you run your code, is set to build for "Debug", which is default for most.
A better way to handle this is probably using the xUnit [Fact(Skip = "Reason")] but I have had issues with making this work on runners on Gitlab sometimes.
And for your last case where you want it to run, you can just "run your code or tests" in "Release mode".
EDIT: In regards to your answer and my own suggestion of using the build in "skip" functionality for xUnit perhaps this post could be of help.
You could then create your own Attribute method to check the environment you run on:
https://josephwoodward.co.uk/2019/01/skipping-xunit-tests-based-on-runtime-conditions

How do I order execution of NUnit test fixtures?

I have a NUnit test project which has two [TextFixture]s.
I want one of them to run before the other as it deals with file creation. They're currently running in the wrong order.
Given that I can't change the [Test]s or group them into a single unit test, is there a way in which I can have control over test fixture running order?
I have tried [Order(int num)] attribute and have also tried to create a new playlist.
Both of them aren't working.
C#, .NET Framework, NUnit Testing Framework, Windows.
The documentation for [OrderAttribute] states that ordering for fixtures applies within the containing namespace.
Make sure that your fixtures are within the same namespace & that you've applied [OrderAttribute] at the test fixture level:
namespace SameNamespace {
[TestFixture, Order(1)]
public class MyFirstFixture
{
/* ... */ }
}
[TestFixture, Order(2)]
public class MySecondFixture
{
/* ... */ }
}
}
Also, it's important to remember that while MyFirstFixture will run before MySecondFixture, the ordering of the tests inside is local to the test fixture.
A test with [Order(1)] in MySecondFixture will run after all the tests in MyFirstFixture have completed.
Important note: the documentation also does not guarantee ordering.
Tests do not wait for prior tests to finish. If multiple threads are in use, a test may be started while some earlier tests are still being run.
Regardless, tests should be following the F.I.R.S.T principles of testing, introduced by Robert C. Martin in his book "Clean Code".
The I in F.I.R.ST. stands for isolated, meaning that tests should not be dependable on one another & each test should be responsible for the setup it requires to be executed correctly.
Try your best to eventually combine the tests into one if they are testing one thing, or rewrite your logic in a way where the piece of code being tested by test 1, can be tested isolated from the piece of code being tested by test 2.
This will also have the side effect of cleaner code adhering to SRP.
Win-win situation.

Ranorex-like test case structure

Figure above showed a TestSuite/Plan in Ranorex.
[SETUP] represents launching .exe recording while [TEARDOWN] represents exiting .exe.
How can I imitate the test case plan structure using only Visual Studio coded ui.
Since it will be repetitive to launch and close my .exe in every test case. If possible I would like to set it only once.
Does a [TestMethod] in coded ui represents a test case?
We have faced the same problem and resolved it by first making an assumption.
A Microsoft TestMethod is not corresponding to a Ranorex Test Case, it is a Ranorex Run Configuration (as defined in the test suite).
A Run Configuration comes with configuration. As you may already know, on the command line, it is possible to execute a Ranorex Test Case or a Ranorex Run Configuration, but it is better/easier to execute a Run Configuration since it comes with context (and also most development can be done by non-programmer from within Ranorex!).
In the end, what we did is use TestMethod to call Run Configuration(s).
The following Ranorex How To article describes how to do this:
http://www.ranorex.com/news/article/howto-test-automation-with-tfs-and-ranorex.html
If this method does not suit your setup, you can probably invoke Ranorex Test Cases directly in test method (and do whatever sequence you wish to replicate as shown in your test suite), but that would be more complicated and involve more maintenance IMHO (which must be done by programmers).
Hope this helps!
Hugo
You're right about [TestMethod] representing a test case.
To Imitate the [Setup] and [TearDown] behavior of Ranorex, instead of using the [TestInitialize] and [TestCleanup] attributes, you should use the [ClassInitialize] and [ClassCleanup] attributes (or [AssemblyInitialize] and [AssemblyCleanp] if you want them to run once for all classes in the project).
Note that these methods must be static, and the initialize ones should accept a TestContext parameter.

How to execute code with nunit at start only

I'm using the latest NUnit to run Selenium tests. The tests are compiled into a class library DLL file which is then run by NUnit.
My problem is that before the automation begins, I need to run some initialization such as creating a log file, setting up specific parameters, etc. I don't see a way to do this in NUnit - setup() does this but for every Test or Fixture - I just need to run this code once at the start of the application.
Any idea how I can do what I want?
Your help is very appreciated.
J.
Take a look at SetUpFixtureAttribute (more information here). It says:
This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. The class may contain at most one method marked with the SetUpAttribute and one method marked with the TearDownAttribute.

tests failing when "Run All Test in Solution" is used

I am trying to get unit tests to run successfully for my MVC 4 Web Application project.
When I run the tests classes individually all of the tests pass, when I come to run all test in solution only 2/9 pass, I have clicked Debug Checked Tests and they all pass when I the hit run again they also all pass.
This problem is also being replicated when I check the project into TFS, I have setup continuous integration they project builds, runs the tests and fails on exactly the same tests.
The error I'm getting back is *"A route named '' is already in the route collection"
Does anybody have any ideas why this might be happening?
In each class I have a [TestInitialize] block which is shown below:
[TestInitialize]
public void Setup()
{
var builder = new TestControllerBuilder();
controller = new MyController();
builder.InitializeController(controller);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
I had a similar error in the classes when I hadn't included the TestControllerBuilder, could it be that this code is not running correctly?
RouteTable.Routes is static and therefore will only be initialized once per AppDomain.
So every time you run a test, you are in effect trying to re-register the same routes over again.
You would probably be better off moving your route registration into an AssemblyInitialize attribute so it will only run once at the beginning of the entire test run.

Categories