I found this Java example, that makes it possible to run test methods in a sequential order.
#Test(priority = 10)
public void login(){...}
#Test(priority = 20, dependsOnMethods = "login")
public void verifyUserLogin() {...}
How would the same thing be achieved with a Visual Studio MSTest project and C#?
As per the MSDN documentation:
There's no quick attribute that can be applied to a suite of tests, but there's a concept of an "Ordered Test". In order to create these, you'll first need a compiled suite of tests, contained in a Visual Studio Test Project.
So let's assume we have these three tests:
[TestClass]
public class SampleTests
{
[TestMethod]
public void TestMethod1()
{
}
[TestMethod]
public void TestMethod2()
{
}
[TestMethod]
public void TestMethod3()
{
}
}
Now right click anywhere within the project in Solution Explorer and choose Add > Ordered Test:
This will generate an ordered test, with a wizard type UI. You can now pick and choose your tests that you want to run as part of the ordered test and add them to the right hand window. You can reorder the tests using the arrows on the right hand side:
The way you run an ordered test is the same as you would run a normal test, and they will appear with the name you gave it in your Test Explorer window:
Related
I've added an integration test project to my solution and I'd like to not run them by default when using the test explorer.
The only solution I've come across to isolate these tests is to manually categorize them and choose not to run tests with a particular trait from test explorer. Ideally, I could exclude them so that people on my project don't have to make this choice explicitly.
Thanks!
There is a special attribute in NUnit to mark your tests that should not be run automatically.
[Explicit]
The Explicit attribute causes a test or test fixture to be skipped unless it is explicitly selected for running.
https://github.com/nunit/docs/wiki/Explicit-Attribute
You just put it on the class or method:
[TestFixture]
[Explicit]
public class IntegrationTests
{
// ...
}
[TestFixture]
public class UnitTests
{
[Test]
public void ShouldNotFail()
{
// This will run
}
[Test]
[Explicit]
public void ManualTest()
{
// This will be ignored
}
}
This is the result:
This code should takes a screenshot when test fail:
[TestClass]
public class UnitTest1
{
[OneTimeTearDown]
public void TestFail()
{
IWebDriver driver = new ChromeDriver();
if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != ResultState.Success)
{
string screensLocation = #"D:\";
string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(screensLocation + testName + ".png");
}
}
[TestMethod]
public void TestMethod1()
{
// my code, here test is failed
}
}
But it is not working. I don't have any screen in location D:\
Otherwise is there a way to debug code under OneTimeTearDown Attribute? Because when the test is fail, debugging ends and I don't know what's going on in the method TestFail().
Thanks for your help.
OneTimeTearDownAttribute is a feature of NUnit.
Although your tag says "nunit", your code is not actually using it. TestClassAttribute and TestMethodAttribute are features of MS Test. If you tried to run this test with NUnit, it would not recognize the tests at all.
Obviously, your test assembly does reference the NUnit framework, since it would not otherwise compile.
So... bottom line, your test code references two different frameworks in such a way that it cannot be run successfully by either runner!!! You have to choose which of the two you want to use, remove the other reference and use a runner for the framework you choose to keep.
I am trying to make some coded UI tests to help automate some of the manual testing at the company I work at. I am pretty new to the CUIT part of visual studio, but I feel like I am figuring it out. However I am having an issue with the testing thread being closed before the other tests run.
So I want to make this testing fully automated, as in, all the developer will need to do is to click "Run all" and they will all run automatically. The problem that I am having is that the very first test needs to launch Internet Explorer, go to a website, and log into the website. The rest of the tests are based off of being logged into the system. However, after the first test, the browser closes and gets killed along with the first test method.
Any advice on this would be great, I have searched online for some answers but a lot are for very old versions of visual studio, and the ones I have tried don't work.
Thank you.
Edit: So inside each class lets say "CodedUITest1.cs", I can use the same browser in each of the [Test Method]s that I have in that class (as someone suggested below). The issue I have is that if I want a different test class to test different functionality, "CodedUITest2.cs", the browser will close when the first class finishes its tests.
If I'm understanding your question correctly, then This code segment should work for you:
BrowserWindow window;
[TestMethod]
public void Method1()
{
window = BrowserWindow.Launch(new Uri("http://www.bing.com"));
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method2()
{
window = BrowserWindow.Locate("Bing");
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method3()
{
window = BrowserWindow.Locate("Bing");
}
After reading the new info of this question, I have tested the code a bit. If you want to keep the browser open between CodeUITes1.cs and CodedUITest2.cs, then the following code segment may help you. It is adopted from the following link: https://blogs.msdn.microsoft.com/devops/2012/11/08/coded-ui-test-why-does-application-close-after-each-test-in-visual-studio-2012/
File: CodedUITest1.cs
public class CodedUITest1
{
static BrowserWindow browserWindowInstance = null;
public void LoadLocalHost()
{
if (browserWindowInstance == null)
{
browserWindowInstance = BrowserWindow.Launch(new System.Uri("YourWebSiteAddress"));
browserWindowInstance.CloseOnPlaybackCleanup = false;
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
else
{
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
}
[TestMethod]
public void CodedUITestMethod1()
{
LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsAndEvents();
}
CodedUITest2.cs file:
[TestMethod]
public void CodedUITestMethod2()
{
CodedUITest1 obj1 = new CodedUITest1();
obj1.LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsPage();
}
You can add more CodedUITest classes. Just create a new object like obj1 in the code sample of CodedUITest2 class, and use LoadLocalHost() method that resides in CodedUITest1.class from any subsequent classes. Hoping this will resolve your problem.
To use the debug mode in NUnit I added an online template "NUnit Test application". So when I add a new project I choose NUnit test application instead of a class library. When the project gets created two .cs files gets added automatically. I added a simple program to check the debug mode and it shows an error. How to rectify this error? Thanks.
TypeInitializationException was unhandled.
Error occurs at
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);
The automatically added files are
Program.cs
namespace NUnitTest1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string[] my_args = { Assembly.GetExecutingAssembly().Location };
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);
if (returnCode != 0)
Console.Beep();
}
}
}
TestFixture.cs
namespace NUnitTest1
{
[TestFixture]
public class TestFixture1
{
[Test]
public void TestTrue()
{
Assert.IsTrue(true);
}
// This test fail for example, replace result or delete this test to see all tests pass
[Test]
public void TestFault()
{
Assert.IsTrue(false);
}
}
}
I added a new item class to it and tried to debug
namespace NUnitTest1
{
[TestFixture]
public class Class1
{
IWebDriver driver = null;
[SetUp]
public void setup()
{
//set the breakpoint here
driver = new FirefoxDriver();
}
[Test]
public void test1()
{
driver.Navigate().GoToUrl("http://www.google.com/");
}
[TearDown]
public void quit()
{
driver.Quit();
}
}
}
As already mentioned by #Arran, you really don't need to do all this. But you can make it even easier to debug NUnit tests.
Using F5 in Visual Studio to debug unit tests
Instead of executing NUnit runner and attaching to the process using Visual Studio, it's better to configure yout test project to start the NUnit test runner and debug your tests. All you have to do is to follow these steps:
Open test project's properties
Select Debug tab
Set Start action to Start external program and point to NUnit runner
Set Command line arguments
Save project properties
And you're done. Hit F5 and your test project will start in debug mode executed by NUnit runner.
You can read about this in my blog post.
You don't need to do all this at all.
Open the NUnit GUI, open up your compiled tests. In Visual Studio, use the Attach to Process feature to attach the nunit-agent.exe.
Run the tests in the NUnit GUI. The VS debugger will take it from there.
You're going through way too much effort to get this done.
What I usually do is to go and create a new "Class Library" project. I then add a reference to the nunin-framework.dll onto my project.
You can define your class as follows:
[TestFixture]
public class ThreadedQuery
{
[Test]
public void Query1()
{
}
}
The TestFixture attribute is described here
You can then go ahead and create multiple Tests with public methods as above.
There are 3 things that are quite important to get this to work then.
You need to set your debugger on your project file to an external executable, ie nunint.exe
The arguments that are passed need to be the name of your assembly.
If you're making use of .net 4.0, you need to specify that in your nunint.exe.config
If you do not do this, you will not be able to debug using VS. See snippet of config below:
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- Comment out the next line to force use of .NET 4.0 -->
<!--<supportedRuntime version="v2.0.50727" />-->
<supportedRuntime version="v4.0.30319" />
<supportedRuntime version="4.0" />
</startup>
Hope this is helpful
When i try to run my tests from Visual Studio 2012 Ultimate i get this output
------ Discover test started ------
========== Discover test finished: 0 found (0:00:05.8242806) ==========
Here is the code:
[Then(#"the submitter company list is in alphabetical order")]
public void ThenTheSubmitterCompanyListIsInAlphabeticalOrder()
{
List<string> submitterCompanyList = _currentFilingPage.SubmitterCompanyList;
submitterCompanyList.Should().BeInAscendingOrder();
}
I have created a .bat file and from there i can run my tests. Please assits me with a way to run my tests from visual studio. (Extra Information: I can't see my tests on Test Explorer)
All the Tests must have the TestMethodAttribute so MSTest/Visual Studio can find them.
see: MSDN Anatomy of a Unit Test
So edit your code and add the TestMethod to your method so it can be found as a Test. Also your method must be added inside a TestClass:
[TestClass]
public class TestClass
{
[TestMethod]
public void ThenTheSubmitterCompanyListIsInAlphabeticalOrder()
{
}
}