0% found this document useful (0 votes)
18 views3 pages

20C23027 Prac7

The document discusses unit testing in Python using the assertIn() function. It explains that assertIn() checks if a key string is contained within a container string. The document provides the syntax and parameters of assertIn(), and includes an example Python code to demonstrate its use in testing.

Uploaded by

purvivaghela2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

20C23027 Prac7

The document discusses unit testing in Python using the assertIn() function. It explains that assertIn() checks if a key string is contained within a container string. The document provides the syntax and parameters of assertIn(), and includes an example Python code to demonstrate its use in testing.

Uploaded by

purvivaghela2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ITM SLS Baroda University

School of Computer Science Engineering & Technology


BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal
Practical 07
Aim: Python implementation to perform unit testing using assertIn() function as well as to
check loops and conditional statements.
Theory:
Assertions are statements that assert or state a fact confidently in our program. For example,
while writing a division function, we are confident that the divisor should not be zero, you
assert divisor is not equal to zero.
Assertions are simply Boolean expressions that checks if the condition return true or not. If it
is true, the program does nothing and moves to the next line of code. If it is false, the program
stops and throws an error.
It is also a debugging tool as it halts the program as soon as an error occurs and displays it.

1|Page 20C23027 – Purvi Vaghela


ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

assertIn() in Python is a unittest library function that is used in unit testing to check whether a
string is contained or not. This function will take three string parameters as input and return a
Boolean value depending upon the assert condition. If the key is contained in container string
it will return true else it returns false.
Syntax: assertIn(key, container, message)

Parameters: assertIn() accept three parameters which are listed below with explanation:

key: a string whose presence is checked in the given container


container: a string in which key string is searched
message: a string sentence as a message which got displayed when the test case got failed.

Python code :
# test suite
import unittest

class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_negative(self):
key = "gfg"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is not in container."
# assertIn() to check if key is in container
self.assertIn(key, container, message)

if __name__ == '__main__':
unittest.main()

2|Page 20C23027 – Purvi Vaghela


ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal
Output :

# test suite
import unittest

class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_positive(self):
key = "geeks"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is not in container."
# assertIn() to check if key is in container
self.assertIn(key, container, message)

if __name__ == '__main__':
unittest.main()

3|Page 20C23027 – Purvi Vaghela

You might also like