BY Mike Arias 09/17/2020 0

If Clauses vs. ASSERT Statements – When to use which one?

If clauses and ASSERT statements serve different, yet similar functions. The goal of this article is to establish once and for all when to use “if” and when to use “asserts” in our test cases. There will be situations when this decision could not be so clear-cut, but with these rules of thumb, we’ll be able to be consistent throughout our tests.


First of all, we need to establish what an “If” clause is, and what an “Assert” statement does. Take into account that we’ll be using Python 3 as our language for this article.

 

if clausses

IF Clause


An “If” clause is a condition that can be either
True or False
. Those are the only values that it can have. They are usually written in this way:

 

if condition:
  statementBlock

Depending on the result of this condition, a statement or action is executed.

 

ASSERT Statement

 

An “Assert” statement is a condition that can be either True or False. Wait… That’s the same thing we said before about “If” clauses. What’s the difference? Let’s dig a bit deeper.

 

Usually, an “Assert” statement will be written in this way:

 

assert condition, "Message"

 

If this condition is True, the code proceeds. If the condition is False, the execution stops and we get an error with the Message we specified in the assertion. This is where, functionally, the differences start. If the condition of an “If” clause is false, the code proceeds with the intended behavior we coded for that scenario. When an “Assert” statement is false, the execution stops because it found something that makes our test fail.

 

But what does this mean at a concept level? That an “Assert” is used only for validations, where an “If” clause is used for the logic within our code. We can use an “If” clause to determine whether our automation should follow one path or another, but an “Assert” statement to validate the elements within those paths.

 

Real-World Examples


The theory is all well and good, but let’s check how this plays out in the real world. First of all, let’s start with an example of a normal situation where I would use an “If” clause in my automation. Let’s say we have a textbox and depending on the scenario, we want to write either a valid or an invalid string in it.

 

def write_string(self, flag):
	if flag == True:
		self.sample_textbox.send_keys("This is a valid string.")
	else:
		self.sample_textbox.send_keys("!%#$^%&@@%$^@ Not a valid string.")

 

Here we can see that depending on what we get from the flag, we either write a valid string or not. In this case, we’re manipulating the flow of our code, playing around with the logic of what we’re doing. We’re not doing any validations whatsoever. Now, let’s take a look at the same fragment of code on how we can properly use assertions.

 

def write_string(self, flag):
	if flag == True:
		self.sample_textbox.send_keys("This is a valid string.")
		assert self.sample_textbox.text = "This is a valid string.", "The textbox doesn't have the correct text."
	else:
		self.sample_textbox.send_keys("!%#$^%&@@%$^@")
		assert self.sample_textbox.text = "", "The texbox doesn't have the correct text."


The assert here has a pretty straight forward goal. To validate whether the
sample_textbox has the correct text in it after a string is written into it. A string, I may add, that is decided base on the result of our “If”. This is the perfect scenario to see both the “If” clause and the “Assert” statement working together and to see clearly the differences between them.

 

Keep in touch with us to learn more about Software Testing tools.

 

This article was written by Mike Arias
Senior Software Testing Engineer of TechAID.
Twitter: @theqaboy
Blog: qaboy.com/

GO BACK

SHARE THIS POST

[addtoany]

Leave a Reply

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

OTHER POSTS YOU MIGHT LIKE

The True Cost of Hiring Someone for your QA Team

BY Maria Tejeda
05/18/2021 1

API Testing Tools: An Example-Based Guide

BY Manuel Marinez
03/29/2021 1