Wednesday, 27 April 2016

Different Test Attribute in .Net Based Application and Data Driven Testing

We use different test attributes, to run our test , based on priority, categorization , Description , running test multiple times with different set of data and so on...

For Assemblies:


AssemblyInitialize and AssemblyCleanup are called right after your assembly is loaded and right before your assembly is unloaded.
For classes
ClassInitialize and ClassCleanup are called right after your class is loaded and right before your class is unloaded.
For test methods

Attributes Used to Identify Test Classes and Methods

Every test class must have the TestClass attribute, and every test method must have the TestMethod attribute. For more information, see Structure of Unit Tests.

Assert Classes and Related Exceptions

Unit tests can verify specific application behavior by their use of various kinds of Assert statements, exceptions, and attributes. For more information, see Using the Assert Classes.
   This attribute is used to test whether an expected exception is thrown. The test method will pass if the expected exception is thrown. The test will fail if the thrown exception inherits from the expected exception.

        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 4;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }

The TestContext Class

The properties of the test context class store information about the current test run. For example, the TestContext.DataRow and TestContext.DataConnection properties contain information that is used by the test for data-driven unit testing.
You use the TestContext class in unit tests for any of several purposes. These are its most frequent uses:
  • In any unit test, because the TestContext class stores information that is provided to unit tests, such as the path to the deployment directory.
  • In unit tests to test Web services that run on ASP.NET Development Server. In this case, TestContext stores the URL of the Web service. 
  • In ASP.NET unit tests, to obtain access to the Page object. 
  • In data-driven unit tests, the TestContext class is required because it provides access to the data row.

Attributes for Identifying and Sorting Tests

The following attributes and the values assigned to them appear in the Visual Studio Properties window for a particular test method.
These attributes are not meant to be accessed through the code of the unit test. Instead, they affect the ways the unit test is used or run, either by you through the IDE of Visual Studio, or by the Team System test engine.
For example, some of these attributes appear as columns in the Test Manager window and Test Results window, which means you can use them to group and sort tests and test results.
One such attribute is TestPropertyAttribute, which you use to add arbitrary metadata to unit tests. For example, you could use it to store the name of a test pass that this test covers, by marking the unit test with [TestProperty("TestPass", "Accessibility")]. Or to store an indicator of the kind of test it is: [TestProperty("TestKind", "Localization")]. The property you create by using this attribute, and the property value you assign, are both displayed in the Visual Studio Properties window under the heading Test specific.
Used to specify deployment items such as files or directories for per-test deployment.
This attribute can be specified on a test method. There can be multiple instances of this attribute to specify more than one item. The item path can be absolute or relative. Relative paths are relative to the RelativePathRoot setting found in the .testrunconfig file.
The following examples demonstrate different usage of the DeploymentItemAttribute:
  • [DeploymentItem("file1.xml")]    Deploys an item named file1.xml located at the RelativeRootPath. The file is deployed to the deployment root directory.
  • [DeploymentItem("file2.xml", "DataFiles")]    Deploys an item named file2.xml located at the RelativeRootPath. The file is deployed to the DataFiles subdirectory of the deployment root directory.
  • [DeploymentItem("C:\\MyDataFiles\\")]    Deploys all items and directories found within the MyDataFiles directory. This does not create the MyDataFiles directory underneath the deployment directory. All files and directories within MyDataFiles will be deployed to the deployment root directory. To copy the entire MyDataFiles directory structure, you must specify MyDataFiles as an output directory.
  • [DeploymentItem("%myDir%\myFile.txt")]    Deploys the file myFile.txt if that file exists in the directory to which %myDir% resolves.
        [TestMethod()]
        [DeploymentItem("testFile1.txt")]
        public void ConstructorTest()
        {
            // Create the file to deploy
            Car.CarInfo();
            string file = "testFile1.txt";
            // Check if the created file exists in the deployment directory
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }

Conclusion: If we want to access any file like excel, csv , xml ... we can define in
"DeploymentItem" attribute , after running the test it will copy into deployment directory
, we can use those file through code or we can define relative path in testrunconfig  file. 
   [TestMethod()]
   [HostType("ASP.NET")]
   [UrlToTest("http://localhost:1371/webSite12")]
   [AspNetDevelopmentServerHost("d:\\MyWebSite", "/MyWebSiteRoot")]
   public void ConstructorTest()
    { 
      object target = TestProject1.Class1Accessor.CreatePrivate();
      Assert.Inconclusive("TODO: Implement code to verify target");
    }

Test Configuration Classes

Provides access to a TestConfigurationSection that represents the microsoft.visualstudio.testtools section in an app.config file.

Attributes Used for Generating Reports

The attributes in this section relate the test method that they decorate to entities in the project hierarchy of a Team Foundation Server team project. For more information, see How to: Enable Reporting of Test Results.

Classes Used with Private Accessors

As described in How to: Test a Private Method and How to: Regenerate Private Accessors, you can generate a unit test for a private method. This generation creates a private accessor class, which instantiates an object of the PrivateObject class. The PrivateObject class is a wrapper class that uses reflection as part of the private accessor process. The PrivateType class is similar but is used for calling private static methods instead of calling private instance methods.
How to: Group and Run Automated Tests Using Test Categories
Test categories let you run groups of tests based on their assigned categories without the requirement to maintain test lists. A test category is a test method attribute that you can assign to one or more tests.
You can use logical operators with test categories to run tests from multiple categories together or to limit the tests that you run to tests that belong to multiple categories. Also, test categories are easy to add as you create your test methods and you do not have to maintain test lists after you have created your test methods.

Requirements

  • Visual Studio Enterprise, Visual Studio Test Professional

Creating and Assigning Test Categories

To manually add test categories to a test

  1. In your unit test project, or coded UI test project in Solution Explorer, open the file that contains the unit test, and then locate the unit test method that you want to change.
  2. Directly above the test method declaration, add a [TestCategory()] attribute for each test category that you want to assign to the test. Separate each attribute by using a comma.
  3. Add the category name enclosed in parentheses to each [TestCategory()] attribute. The following example is a method with three Test Categories assigned to it named "Nightly", "Weekly", and "ShoppingCart":
[TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"),
 TestMethod()]
public void DebitTest()
{
}
Running Tests by Categories
When you run tests from the command line, you can also use logical operators & (AND), | (OR) and !(NOT) to select the tests to run based on the categories assigned to the tests.

To run test using categories from the command-line

  1. Open a Visual Studio command prompt. (Go to Start, All Programs, Microsoft Visual Studio, Visual Studio Tools, Developer Command Prompt.)
  2. By default, the Visual Studio command prompt opens to the following folder:
    <drive letter>:\Program Files\Microsoft Visual Studio 12.0\VC
  3. Either change the directory to the location in your solution folder where the test container is located, typically the test project's .dll file, or, when you run the MSTest.exe program in step 3, specify a full or relative path for the test container.
    To identify your solution folder, first identify the Visual Studio Projects folder. To do this, choose Options on the Tools menu in Visual Studio, and then choose Projects and Solutions. Under Visual Studio projects location, you see a path such as the following:
    <drive letter>:\Documents and Settings\<user name>\My Documents\Visual Studio\Projects
    Your solution folder is typically a child of this Projects folder, such as the Bank folder in the following example:
    <drive letter>:\Documents and Settings\<user name>\My Documents\Visual Studio\Projects\Bank
  4. 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"
    The results and summary are displayed in the command prompt window.
Note:  You can use either AND or OR in your expression to select categories of tests, but not both in the same expression. Use in above command only or while creating build definition in TFS for CI setup.
RollBack attribute is used in MBUnit and NUnit framework for database testing

Few Important Assertion

List<object> itemList2 = new List<object>() { new AnObject() { SomeValue = 2 }, new AnObject() { SomeValue = 1 } };

CollectionAssert.AreEquivalent(itemList1, itemList2);

IsSubsetOf/IsNotSubsetOf
This assertion verifies that the first parameter (the subset) is contained in the second parameter’s collection (the superset).
Note that the subset test does not test for order or sequence—it simply tests whether the items in the subset list are contained in the superset. 

String Assertions
These methods are implemented in the Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert class:
? Contains
? Matches/DoesNotMatch
? StartsWith/EndsWith

The Contains method asserts that the subset (note that this is the second parameter) is contained in the string (the first parameter). 
For example, this test passes

 [TestMethod] 
public void SubsetTest() 
{
string subset = "abc"; string superset = "1d2c3b4a"; 
CollectionAssert.IsSubsetOf(subset.ToCharArray(), superset.ToCharArray());
 }

[TestClass] 
public class StringTests
 {
 [TestMethod] public void ContainsTest() 

string subset = "abc"; string superset = "123abc456";
 StringAssert.Contains(superset, subset);
}

}

Matches/DoesNotMatch
This method asserts that the string (the first parameter) matches the regex pattern provided in the second parameter.

StartsWith/EndsWith

This method asserts that the string (the first parameter) either starts with or ends with another string (the second parameter).

Accessign CSV file in Test Code:


[TestClass] 
public class DataSourceExamples
 { 
public TestContext TestContext { get; set; }

[TestMethod] 
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "C:\\temp\\csvData.txt", "csvData#txt", DataAccessMethod.Sequential)] 
public void CsvDataSourceTest()
 { 
int n = Convert.ToInt32(TestContext.DataRow["Numerator"]); 
int d = Convert.ToInt32(TestContext.DataRow["Denominator"]); int r = Convert.ToInt32(TestContext.DataRow["ExpectedResult"]); Debug.WriteLine("n = " + n + " , d = " + d + " , r = " + r); 
}
}

XML Data Source
Given an XML file such as:
an example of using an XML data source for a unit test is:
Note that other than the data source attribute parameters, the test code is the same.
Database Data Source
A database table can also be used as the data source. Given a table such as:
Figure 15: Database Table as a Data Source

and data:
 <Data>
 <Row Numerator = "10" Denominator = "5" ExpectedResult = "2"/>
 <Row Numerator = "20" Denominator = "5" ExpectedResult = "4"/> 
<Row Numerator = "33" Denominator = "3" ExpectedResult = "11"/>
 </Data>

 [TestMethod] [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "C:\\temp\\xmlData.xml", "Row", DataAccessMethod.Sequential)] public void XmlDataSourceTest() { int n = Convert.ToInt32(TestContext.DataRow["Numerator"]); int d = Convert.ToInt32(TestContext.DataRow["Denominator"]); int r = Convert.ToInt32(TestContext.DataRow["ExpectedResult"]); Debug.WriteLine("n = " + n + " , d = " + d + " , r = " + r); }

Accesign Sql Server database:


[TestMethod] [DataSource("System.Data.SqlClient", "Data Source=INTERACX-HP;Initial Catalog=UnitTesting;Integrated Security=True", "DivideTestData", DataAccessMethod.Sequential)] public void XmlDataSourceTest() { int n = Convert.ToInt32(TestContext.DataRow["Numerator"]); int d = Convert.ToInt32(TestContext.DataRow["Denominator"]); int r = Convert.ToInt32(TestContext.DataRow["ExpectedResult"]); Debug.WriteLine("n = " + n + " , d = " + d + " , r = " + r); 
}


See Data Driven Testing in next post... 
Happy Reading   :)
Help Link: https://msdn.microsoft.com/en-us/library/ms243147(vs.80).aspx

2 comments:

  1. Really nice topics you had discussed above. I am much impressed. Thank you for providing this nice information here

    Software Testing Company

    QA Services

    XBOX Game Tester

    Game Testing Companies

    Console Game Testing

    ReplyDelete
  2. There are certainly a couple more details to take into consideration, but thank you for sharing this information. mega888 jackpot

    ReplyDelete