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

SELENIUM: Some Useful Facts

Before we start designing a framework, there are few facts that we should always remember. It is not important what you have designed, it is more important that how good your product is. Also, this kind of analysis should always be done in the beginning so that you don't have to devote extra time and efforts for it later. It is always good to fix something in beginning than worrying about it later when it has become a big issue and become difficult to locate the issue. It's not like that we won't be able to fix it, we will definitely fix it but with more cost and efforts. Let's go through few of these facts. Layers in Framework You should always keep in mind the layers that should be there in the framework you are designing. Layers to be created generally varies application to application that you are creating but most of the frameworks contains below layers often. In above picture, there is workflow layer at the top. Workflows can be think of as somet...

Logging in Selenium - To File

In my previous post I explained you how you can configure log4net in your project to do logging to console with example. Now, in this post I will be explaining how we can configure log4net to log to file instead of console. As we already have working logs to console from our TestFramework so logging to file doesn't take much efforts. You just need to follow below steps and you are done. Configuring log4net to log to file Step 1 : Add an " app.config " file in your UITests project with content as shown below: Step 2:   Change configurator in Configure method of Log class That's it. Now, when you will run the test it will log to console as well as file named " Test.log ". This file can be found at \TestFramework\UITests\bin\Debug location in you project folder. As you saw, we just added app.config file and then changed the configurator in order to log to file. Earlier we used "BasicConfigurator.Configure()" which writes logs to cons...

Logging in Selenium

Any application is incomplete until it's activities are traceable. Here comes the use of logging. By logging important actions you can later on track anything you want especially when there are some error situations. It becomes easy to debug the issues if logs are generated properly. Similarly, it is very important that our automation framework generates logs so that whenever any test fails, we can track the issue by tracing it's logs. In this post, I will be explaining you how we can log using log4net in selenium. Before using methods of this library we need to configure this to our application. Let's do this first then. Configuring log4net in selenium: First of all you need to download this library so that this can be used. You can read about log4net and then download it from this link and save the dll file after extracting it to folder that is under your project folder. After downloading this library you need to add references of this in you project. To do ...