TechAIDBlogIf Clauses vs. ASSERT Statements – When to use which one?
By Mike Arias 09/17/2020 6

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

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

If clauses and ASSERT statements serve different, yet similar functions. This article aims 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 straightforward 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.

 

Mike Arias wrote this article.
Senior Software Testing Engineer of TechAID.
Twitter: @theqaboy
Blog: qaboy.com/

GO BACK TO Tools

Leave a Reply

OTHER POSTS YOU MIGHT LIKE

API Testing Tools: An Example-Based Guide

By Manuel Marinez 02/02/2023 2

API Testing Tools For API testing, there are many tools out there that let you perform the test and collect the results. In this article, We will focus on three tools by showing how to make a request using the Trello API.   But before…

What are APIs? And How can they enhance your testing?

By Manuel Marinez 07/16/2020 6

What are APIs? And How can they enhance your testing? An API (Application programming interface) is a set of tools that are used to build software and also make them communicate with each other. The way this is done is by using REST for formatting…

if-vs-assert

Thank you for downloading our free resources.

We hope you can find it useful for your daily work. Learn more about what we do.

Your resume was uploaded!

Thank you for being interested in making part of our family.

Redirecting…

¡Tu currículum fue enviado!

Gracias por interesarte en formar parte de nuestra familia.

Redirigiendo…