Skip to main content

SELENIUM: Test Structure - Use of Different Attributes


As I mentioned in my previous post that in this post we will be discussing about test structure that we will are writing.

Here I will be explaining in brief different types of attributes that can be used while writing our tests. Till now we have just used one attribute i.e. [TestMethod] which contains the test to be executed.

There are other important attributes that can be used during tests to make tests well organized.

Few of the attributes are as given below:
  • [ClassInitialize]
  • [ClassCleanup]
  • [TestInitialize]
  • [TestCleanup]
  • [AssemblyInitialize]
  • [TestMethod]
  • [TestClass]

[ClassInitialize] attribute can be used with a method that you want to execute before intialization of a class. So, let's suppose there are five tests in a test class then the method having [ClassInitialize] attribute will run only once for this class before running all the tests.


[ClassCleanup] attribute can be used with a method that you want to execute after all the tests in the class are run.


If you want a code to execute before execution of each test than [TestInitialize] is the right attribute for you. Methods with this attribute will run before execution of a test.


[TestCleanup] attribute can be used with a method that you want to execute after execution of each test. So, if you have something that you want to run after each test than just put that code in a method with this attribute.


[AssemblyInitialize] attribute can be used with a method that you want to execute before all tests and classes i.e. it runs before running any test in a test assembly.


[TestMethod] is the attribute which we were using till now. Methods with this attribute is the unit test that we want to execute for a particular scenario. So, we put all the neccessary steps that we want to execute in this method.

All of above attributes have there existence if there is [TestClass] attribute used with the class. If this attribute is not used than nothing will be executed. It can be seen that many times people just write everything but forget to include this attribute with the class. While executing tests nothing gets executed and spends lots of time in finding what actually went wrong. So, don't forget to include this attribute.


Summary:
 In short, if there is on test class TESTCLASS_A with five test methods and TESTCLASS_B with five test methods. Also, there is a TESTCLASS_C with [AssemblyInitialize] method and this class is inherited by TESTCLASS_A and TESTCLASS_B.

Now, if you execute all tests, AssemblyInitialize method will be called before running any other method and will called just once. After that within each class methods will be called in below order:
  1. ClassInitialize
  2. TestInitialize
  3. TestMethod..1
  4. TestCleanup
  5. TestMethod..2.
  6. TestCleanup and so on till TestMethod..n
  7. ClassCleanup

In my later posts we will be using these attributes in our examples. In my next post, we will be discussing about logging and I will explain you how we can configure log4net so that we can log to console or files while our tests executes.

Thanks for reading this post and see you in the next post. cya!





Comments

Popular posts from this blog

"Hello Selenium"

Now, as we have already setup the required environment, let's just start with a hello world test with selenium. In this let's just write a basic test to make sure that our environment is working properly. For this hello selenium test we will be automating below test: Navigate to " https:// www.google.com  " Write "Hello selenium" in search box Press search button Below are the code snippets: Test class UnitTest1 containing required test. Here in this class, at the top I have used two statements i.e.     using Microsoft.VisualStudio.TestTools.UnitTesting;     using TestFramework; First using statement is required in for writing unit tests and second using statement is required so that we can access methods from class under TestFramework namespace. There is [TestClass] attribute used with this class. This is required so that tests written in this class can be recognized. If you miss this attribute then your tests are not going ...

SELENIUM: Using Nested Classes

In this post I will be using nested class pattern to make few things easier. e.g. If you see in our test application there is a navigation bar on left side containing various menus i.e. Posts, Pages etc. Further each main menu contains sub-menu e.g. Posts menu contains sub-menues as "All Posts", "Add New", "Categories" and "Tags". In this type of situations, it is good to use nested classes to make things more readable and easy to use. E.g. Suppose I want to access "All Posts" navigation then I would want this to be done like this. " LeftNavigation.Posts.AllPosts.Select(); " i.e. I would want that there would be a class called "LeftNavigation" and when I press '.' (dot) it shows all the main menues in this navigation e.g. "All Posts", "Pages" etc. then I choose " LeftNavigation.Posts " and again when press '.' it shows all the sub-menues inside this Posts menu...

SELENIUM: Adding Next Test With Proper Test Structure-1

As I mentioned in my previous post that now its time to refactor our tests a little bit so that each test is written with proper test structure as required. After refactoring tests we will add one more test to our framework with proper test structure. So, lets do it now. Refactoring tests Till now we have added two tests to our framework i.e.         And Let's refactor these tests first before adding more tests to this framework.  In each method let's log some message so that we come to know how the execution was done. # AssemblyInitialize method:     In this method let's put code to initialize the log so that it can be used throughout the assembly. Before moving to next method let's talk about other refactoring made in this test project. #1: All static classes are made non-static so that we can created instances of page objects instead of directly using them. So, now on we cannot use class directly. Instead we will...