Code & QA

Simple Example of a Test

Since the tools selected for tests execution are NUnit and MSTest, the tests should meet certain rules determined by the tools. Obviously, all the tests must be created in the Tests project.

Below is an example of a simple test with Selenium and NUnit/MSTest tools.

First of all the references to necessary libraries which classes will be used in test development must be added to the project. Some of the references have been added automatically right after the project had been created.

clip_image001_thumb

Some should be added explicitly in order to be able to use the required classes.

clip_image002_thumb

clip_image004_thumb

clip_image005_thumb

Selenium WebDriver library is located in previously downloaded archive selenium-dotnet-version_number. Unpack it and select all *.dll files. Selected libraries will appear inside the References folder.

Since I’m going to develop tests for running with NUnit, I have to add a reference to the NUnit framework as well. Usually, it is located inside bin\framework folder of installed NUnit application.

clip_image006_thumb

To be able to execute tests with MSTest tool I added the reference to its library too. It is named Microsoft.VisualStudio.QualityTool.UnitTestFramework.

clip_image007_thumb

clip_image009_thumb

No additional actions are required if execute tests with Mozilla Firefox browser only. But Internet Explorer and Chrome requires the presence of their own server files in project working directory.

clip_image010_thumb

For now all preparations are complete and the first test may be created.

The code above shows the structure of the test. The sequence of actions that are carrying on during test run is following:

[TestFixtureSetUp] or [ClassInitialize]

[SetUp] / [TestInitialize]

[Test] / [TestMethod] //Test 1

[TearDown] /[TestCleanup]

[SetUp] / [TestInitialize]

[Test] / [TestMethod] //Test 2

[TearDown] /[TestCleanup]

[SetUp] / [TestInitialize]

[Test] / [TestMethod] //Test n

[TearDown] /[TestCleanup]

[TestFixtureTearDown] or [ClassCleanup]

One more test.

If you compile the code (Build -> Build Solution) and run all the tests with MSTest or with NUnit tool you can notice that new instance of IWebDriver interface is created for every single test (new browser instance is launched). This means that [SetUp] / [TestInitialize] method is invoked every time new test has started running and [TearDown] / [TestCleanup] method is invoked after a test finishes its work.

clip_image013_thumb

There are also additional types of attributes in MSTest that are very useful. They are [AssemblyInitialize()] / [AssemblyCleanup()] marked methods. The methods are invoked only once per the entire session. [AssemblyInitialize()] method is invoked in the very beginning of tests run session before any tests are started and [AssemblyCleanup()] method is performed when all work is completed. You can put a logic which affects the entire assembly inside these methods.

NUnit tool has methods with similar behavior as well but there is an important constraint for them. The methods must be located in a separate class marked with [SetUpFixture] attribute, apart the methods are not visible for the tool and cannot be performed. The class should be in the same namespace as the tests are.

One more thing I want to mention is that a good style of programming assumes having reusable code in a base class whose children inherit it. In my case such kind of code is all methods but ones marked with [Test] / [TestMethod].

clip_image015_thumb

Add->Class… (or hit Shift+Alt+C)

clip_image016_thumb

Base class may be public or internal. All auxiliary methods and variables from Example class may be moved into it; all but those marked as [Test] / [TestMethod].

Plus class with [SetUpFixture] attribute. It may be located TestBase.cs file or in separate file. The class contains method for assembly set up compatible both with NUnit and MSTest as well.

The final thing to do is to inherit the test class from the BaseTest.

namespace Tests

{

[TestFixture]

[TestClass]

public class Example : BaseTest

{

[Test]

[TestMethod]

public void GoogleSearch()

{

//…//

}

[Test]

[TestMethod]

public void GoogleAccountTitleVerification()

{

//…//

}

}

}

The methods of GlobalSetup class will be invoked once in the very beginning and in the end of entire test session. All the methods from BaseClass are inherited in Example class and will be invoked in accordance of tests run flow described above.

[row]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
NUnit Console Runner [/column]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Table Of Content
[/column]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Test Run Summary Report
[/column]
[/row]

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined offset: 0 in /home/chyqruxavcln/public_html/learnseleniumtesting.com/wp-content/plugins/cardoza-facebook-like-box/cardoza_facebook_like_box.php on line 924