Showing posts with label MSTest. Show all posts
Showing posts with label MSTest. Show all posts

Monday, 19 September 2016

Why Sometimes Visual Studio IDE compiles Code But MSBuild Throws Compilation Error Because Of Assembly Reference Not Found


1. I had a CodedUI Test which used CodedUI library such as -
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;

I was successfully able to compile the entire solution using Visual Stduio IDE 2013 Ultimate.
But, while I was trying to compile the same using MSBuild tool, I was getting errors because of not able to find assemblies
Following errors were thrown:
05:44:41 FunctionalTests\CodedUITest1.cs(7,40): error CS0234: The type or namespace name 'UITesting' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?) [C:\Users\Administrator\.jenkins\workspace\Bodycote\AutomatedTest\AutomatedTest.csproj]
05:44:41 FunctionalTests\CodedUITest1.cs(9,40): error CS0234: The type or namespace name 'UITest' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?)

Solution - 
This error appears, when MSBuild does not find references added to project of specific version. 
Visual studio facilitates to go with any version of references. 
To go with any available reference, not to be very much strict about particular version, we have to set the property - "Specific Version" to false for used references. By default its true.

By setting Specific Version for all references to false, I solved the issue. MSBuild tool started compiling.








Friday, 16 September 2016

How to: Group and Run Automated Tests Using Test Categories


[TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()]
public void DebitTest()
{
}

To run tests that are assigned to the "Nightly" category, run the VSTest.Console.exe using the /TestCaseFilter option, or from MSTest.exe by using the /testcontainer and the /category options:
VSTest.Console.exe
Vstest.console.exe myTestProject.dll /TestCaseFilter:TestCategory=Nightly
MSTest.exe
mstest /testcontainer:MyTestprojectName.dll /category:"Nightly"
You can only use the /category option one time per command line, but you can specify multiple test categories with the test category filter. The test category filter consists of one or more test category names separated by the logical operators '&', '|', '!', '&!'. The logical operators '&' and '|' cannot be used together to create a test category filter.
For Example:
  • /category:group1 runs tests in the test category "group1".
  • /category:"group1&group2" runs tests that are in both test categories "group1" and "group2." Tests that are only in one of the specified test categories will not be run.
  • /category:"group1|group2" runs tests that are in test category "group1" or "group2". Tests that are in both test categories will also be run.
  • /category:"group1&!group2" runs tests from the test category "group1" that are not in the test category "group2." A test that is in both test category "group1" and "group2" will not be run.

MSTest Attributes Comparison With TestNG Test Attributes

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

namespace TestNamespace
{
    [TestClass()]
    public sealed class DivideClassTest
    {
        [AssemblyInitialize()] //Before All Tests Across All Test Classes are run, mimics BeforeSuite in TestNG
        public static void AssemblyInit(TestContext context)
        {
            MessageBox.Show("AssemblyInit " + context.TestName);
        }

        [ClassInitialize()]//Before All Tests in Test Class is run, mimics BeforeClass in TestNG
        public static void ClassInit(TestContext context)
        {
            MessageBox.Show("ClassInit " + context.TestName);
        }

        [TestInitialize()] //Before Each Test Is Run, mimics BeforeTest in TestNG
        public void Initialize()
        {
            MessageBox.Show("TestMethodInit");
        }

        [TestCleanup()] //After Each Test Is Run, mimics AfterTest in TestNG
        public void Cleanup()
        {
            MessageBox.Show("TestMethodCleanup");
        }

        [ClassCleanup()] //After All Tests In Test Class are executed, mimics AfterClass in TestNG
        public static void ClassCleanup()
        {
            MessageBox.Show("ClassCleanup");
        }

        [AssemblyCleanup()] //After All Tests Across All Test Classes are executed, mimics AterSuite in TestNG
        public static void AssemblyCleanup()
        {
            MessageBox.Show("AssemblyCleanup");
        }

        [TestMethod()] //Test Method To Be Executed, mimics Test in TestNG
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideMethodTest()
        {
            DivideClass.DivideMethod(0);
        }
    }
}

Thursday, 15 September 2016

MSTest Options

running MSTest from commandline:
https://msdn.microsoft.com/en-us/library/ms182486.aspx

General Command Line Options
/testcontainer:[file name]
Load a file that contains tests.
Example: /testcontainer:tests.dll 
For more information, see /testcontainer.
/testmetadata:[file name]
Load a file that contains test metadata. For more information, see /testmetadata.
/testlist:[test list path]
Specify the test list, as specified in the metadata file, to be run. For more information, see /testlist.
/category:[test category filter]
Specify and filter which test categories to run. For more information, see /category.
/test:[test name]
Specify the name of a test to be run. For more information, see /test.
/noisolation
Run tests within the MSTest.exe process. This choice improves test run speed but increases risk to the MSTest.exe process.
/testsettings: [file name]
Use the specified test settings file.
Example: /testsettings:Local.Testsettings
For more information, see /testsettings.
/runconfig:[file name]
Use the specified run configuration file.
Example: /runconfig:localtestrun.Testrunconfig
For more information, see /runconfig.
System_CAPS_noteNote
This command-line option is maintained for compatibility with previous versions of Visual Studio. Test run configurations have been replaced by test settings in Visual Studio Enterprise.
/resultsfile:[file name]
Save the test run results to the specified file.
Example: /resultsfile:testResults.trx
For more information, see /resultsfile.
/detail:[property id]
Specify the name of a property that you want to show values for, if any, in addition to the test outcome. For more information, see /detail.
/help
Display the MSTest.exe usage message (short form: /? or /h).
/nologo
Display no startup banner and copyright message.
/usestderr
Use standard error to output error information.