How to order TestMethods of execution using Visual Studio - visual-studio-2010

I am writing test cases using using VS 2010
I have list of test methods and i am looking to run test cases in a particular order
for an example, here is i have few test methods.
[TestMethod]
public void AddEmployee()
{
//
}
[TestMethod]
public void UpdateEmployee()
{
//
}
[TestMethod]
public void DeleteEmployee()
{
//
}

You can use an 'Ordered Test' :
How to: Create an Ordered Test
The tests end up listed in an XML file that Visual Studio executes.

Related

Xunit Test for ASP.NET Boilerplate

For ASP.NET Boilerplate, I am trying to implement Xunit test for the project. Is there any method to provide the parameters to test methods and configure the test methods process order.
In the normal test project, I could use [InlineData("Test", 3)] to provide the value to test methods public async void AllInOne_Test(string userName, int count), but for ASP.NET Boilerplate, it did not pass the value to test method.
I also want to process TestB after TestB, how could I achieve this?
Update1:
For InlineData issue, I could inherit TheoryAttribute like below:
public sealed class MultiTenantTheoryAttribute : TheoryAttribute
{
public MultiTenantTheoryAttribute()
{
if (!CloudConsts.MultiTenancyEnabled)
{
Skip = "MultiTenancy is disabled.";
}
}
}
And then, use it like below:
[MultiTenantTheory]
[InlineData("Test_CreateCoupon")]
public async Task CreateCoupon_Test(string title)
{
//....
}
But, I still have an issue with test method processing order.
[MultiTenantTheory]
[InlineData("Test_CreateCoupon")]
public async Task CreateCoupon_Test(string title)
{
//...
}
[MultiTenantTheory]
[InlineData("Test_CreateCoupon", "Test_UpdateCoupon")]
public async Task UpdateCoupon_Test(string oldTitle, string updateTitle)
{
//。。。
}
In my tests, I have CreateCoupon_Test and UpdateCoupon_Test, I want to exec CreateCoupon_Test, then UpdateCoupon_Test when I run all tests. But, currently, its order is random, and I got UpdateCoupon_Test run before CreateCoupon_Test, then my tests failed, since I update the record which has not been created.

Mvvmcross Testing different view models fails when running together

I've come across an interesting error. I have two test files for my xamarin mobile application, both testing view models:
public class TestFirstViewModel : MvxIoCSupportingTest
{
public void AdditionalSetup() {
//Register services and dependencies here.
}
[Fact]
public TestMethod1() {
// Successful test code here.
}
}
That's in one file. In another file, I have:
public class TestSecondViewModel : MvxIoCSupportingTest
{
public void AdditionalSetup() {
//Register services and dependencies here, slightly different from first
}
[Fact]
public TestMethod2() {
// Successful test code here.
}
}
When I run these files individually (I'm using xunit), they work just fine. However, when I run them together, I get the following error on one of the test cases:
Result Message: Cirrious.CrossCore.Exceptions.MvxException : You cannot create more than one instance of MvxSingleton
Result StackTrace:
at Cirrious.CrossCore.Core.MvxSingleton`1..ctor()
at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer..ctor(IMvxIocOptions options)
at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Initialize(IMvxIocOptions options)
at Cirrious.MvvmCross.Test.Core.MvxIoCSupportingTest.ClearAll()
at Cirrious.MvvmCross.Test.Core.MvxIoCSupportingTest.Setup()
at Project.Test.TestFirstViewModel.TestMethod1() in ...
Can anyone tell me what's going on here?
The issue stems from the parallelization of XUnit without the option to do proper tear-down. You could diable parallelization in the AssemblyIndo.cs file in you test project by adding:
[assembly: CollectionBehavior(DisableTestParallelization = true)]
I ended up solving this question by changing testing frameworks. I had different ioc singleton initializations, because, well, they're different test cases and needed different inputs/mocks. Instead of using Xunit, I resorted to Nunit where their cache clearing was much more defined: Xunit doesn't exactly believe in setup and tear-down, so it made a test environment like this more difficult.
I fixed the issue by using the collection attribute.
[Collection("ViewModels")]
class ViewModelATest : BaseViewModelTest {
...
}
[Collection("ViewModels")]
class ViewModelBTest : BaseViewModelTest {
...
}
The base view model test class has the mock dispatcher and performs the singleton registrations in the additional setup method.
Each of my tests calls ClearAll() at the beginning.
I hade some success with setup things in a constructor and add this check:
public PaymentRepositoryTests()
{
if (MvxSingletonCache.Instance == null)
{
Setup();
}
//other registerings.
}`
Also I did implement the IDisposable Interface
public void Dispose()
{
ClearAll();
}
But tbh not sure how much impact that had..
It works ok with xunit
Copy MvxIocSupportingTest and Mvxtest in your xunit PCL project.
Modify MvxTest to remove the attributes and use a simple contructor:
public class MvxTest : MvxIoCSupportingTest
{
protected MockMvxViewDispatcher MockDispatcher { get; private set; }
public MvxTest()
{
Setup();
}
...
And in each of you test, derive from IClassFixture
public class TestRadiosApi : IClassFixture<MvxTest>
{
[Fact]
public async Task TestToken()
{
...
xunit will create the MvxTest class only once for all tests.

Why Unit test fails using mstest command line but not in VS2010 Professional?

I have a little problem with UnitTests for asp.net mvc3 application.
If I run some unit-tests in Visual Studio 2010 Professional, they have been passed successfully.
If I use the Visual Studio 2010 Professional command line
mstest /testcontainer:MyDLL.dll /detail:errormessage /resultsfile:"D:\A Folder\res.trx"
Then the error occurred:
[errormessage] = Test method MyDLL.AController.IndexTest threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
and my controller:
public ActionResult Index(){
RedirectToAction("AnotherView");
}
and in the test
AController myController = new AController();
var result = (RedirectToRouteResult)myController.Index();
Assert.AreEqual("AnotherView", result.RouteValues["action"]);
How to solve this to work properly in both situations (VS2010 and mstest.exe) ?
Thank you
PS: I read Test run errors with MSTest in VS2010 but that could solve if I have VS2010 Ultimate/Premium.
I found the problem. The problem was AnotherView action.
The action AnotherView contains
private AModel _aModel;
public ActionResult AnotherView(){
// call here the function which connect to a model and this connect to a DB
_aModel.GetList();
return View("AnotherView", _aModel);
}
What is need to works:
1.Make a controller constructor with a parameter like
public AController(AModel model){
_aModel = model;
}
2.In test or unit-test class, create a mock like
public class MockClass: AModel
{
public bool GetList(){ //overload this method
return true;
}
// put another function(s) which use(s) another connection to DB
}
3.In current test method IndexTest
[TestMethod]
public void IndexTest(){
AController myController = new AController(new MockClass());
var result = (RedirectToRouteResult)myController.Index();
Assert.AreEqual("AnotherView", result.RouteValues["action"]);
}
Now the unit test will works. Not applicable to Integration tests. There you must provide configuration for a connection to DB and do not apply a mock, just use the code from my question.
Hope this help after researching for 5-6 hours :)

Allow own TestContext class to inject different connections Strings inVisual Studio Unit Test Project

In MBUnit I can annotate my Test class with a Factory and multiple getter returning an oracle/sql/mysql connection string which can be obtained by every test method in the test class.
How can I do this with the Unit Test Project for Visual Studio 2010?
e.g. I am not allowed to inherit from TestContext and pass that object to my ClassInit method?
[ClassInitialize()]
public static void MyClassInitialize(MyContextDerivedFromTextContext testContext)
{
}
I do not want to hardcode-annotate my TestMethods with such an attribute:
[DataSource("System.Data.SqlClient", "Data Source=.\\SQLEXPRESS;Initial Catalog=STM;Integrated Security=True;Pooling=False", "CustomerTable", DataAccessMethod.Sequential), TestMethod]
public void TestMethod1()
{
//
// TODO: Add test logic here
//
}
I want this:
public void TestMethod1(String testconnectionStringOracleORMySQLORMSSQLetc...)
{
//
// TODO: Add test logic here
//
}
I think you're out for some disappointment. MSTest supports data driven tests as you've already shown but it sucks compared to other test frameworks.
The only approach I can think of that might do what you want is to store your connection strings in an XML file and that as a data source for your MSTest tests to set up the connection and then do whatever else it is you need to do.

Forcing MSTest to use a single thread

Given this test fixture:
[TestClass]
public class MSTestThreads
{
[TestMethod]
public void Test1()
{
Trace.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
[TestMethod]
public void Test2()
{
Trace.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
}
Running the test with MSTest through Visual Studio or command line prints two different thread numbers (yet they are run sequentially anyway).
Is there a way to force MSTest to run them using a single thread?
I solved this problem with locking:
public static class IntegrationTestsSynchronization
{
public static readonly object LockObject = new object();
}
[TestClass]
public class ATestCaseClass
{
[TestInitialize]
public void TestInitialize()
{
Monitor.Enter(IntegrationTestsSynchronization.LockObject);
}
[TestCleanup]
public void TestCleanup()
{
Monitor.Exit(IntegrationTestsSynchronization.LockObject);
}
//test methods
}
// possibly other test cases
This can of course be extracted to a base test class and reused.
I've fought for endless hours to make MSTest run in a single threaded mode on a large project that made heavy use of nHibernate and it's not-thread-safe (not a problem, it's just not) ISession.
We ended up more time writing code to support the multi-threaded nature of MSTest because - to the best of my and my teams knowledge - it is not possible to run MSTest in a single threaded mode.
You can derive your test class from
public class LinearTest
{
private static readonly object SyncRoot = new object();
[TestInitialize]
public void Initialize()
{
Monitor.Enter(SyncRoot);
}
[TestCleanup]
public void Cleanup()
{
Monitor.Exit(SyncRoot);
}
}
We try hard to make out tests isolated from each other. Many of them achieve this by setting up the state of a database, then restoring it afterwards. Although mostly tests set up different data, with some 10,000 in a run there is a fair chance of a collision unless the code author of a test takes care to ensure its initial data is unique (ie doesn't use the same primary keys as another test doing something similar). This is, frankly, unmanageable, and we do get occasional test failures that pass second time around. I am fairly sure this is caused by collisions that would be avoided running tests strictly sequentially.
The way to make an MSTest method run in single-threaded mode:
Nuget:
install-package MSTest.TestAdapter
install-package MSTest.TestFramework
In your test source on those methods that need to run while no other tests are running:
[TestMethod]
[DoNotParallelize]
public void myTest(){
//
}
Whilst it is a cop out answer, I would actually encourage you to make your code thread-safe. The behaviour of MSTest is to ensure isolation as Richard has pointed out. By encountering problems with your unit tests you are proving that there could be some problems in the future.
You could ignore them, use NUnit, or deal with them and continue to use MSTest.
I tried a bit of a different approach, because the underlying problem is that the names of the pipes are the problem. So I made a fakePipe, derived it from the one I use in the program. And named the pipe with the tests name.
[TestClass]
public class PipeCommunicationContractTests {
private PipeDummy pipe;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set; }
[TestInitialize]
public void TestInitialize() {
pipe = new PipeDummy(TestContext.TestName);
pipe.Start();
}
[TestCleanup]
public void TestCleanup() {
{
pipe.Stop();
pipe = null;
}
...
[TestMethod]
public void CallXxOnPipeExpectResult(){
var result = pipe.Xx();
Assert.AreEqual("Result",result);
}
}
It appears to be a bit faster, since we can run on multiple cores and threads...

Resources