Sc37 Rushan Shaikh Assign-7
Sc37 Rushan Shaikh Assign-7
CLASS-SY CSE-C
ROLL NO- SC 37
DESCRIPTION
Unit tests are small bits of code designed to check the behaviour of other code, often specific
functions or methods, known as units. They play a crucial role in software development by verifying
that code functions correctly and identifying issues at an early stage, which can save time and money
by preventing major problems.
Python provides two primary frameworks to simplify the process of creating unit tests: unittest and
PyTest. Unittest has been included in Python's standard library since Python 2.1, making it readily
available for testing Python code.
In Python, the assert statement is a built-in tool used to check whether a specific condition is true or
false. If the condition is true, the program continues as usual. However, if the condition is false, an
error is triggered. It's important to note that the assert statement is distinct from the try and except
clauses, and it should not be used for handling errors in your code. Instead, it's primarily intended
for debugging and testing purposes.
The unittest module in Python is a framework specifically designed to simplify the process of testing
your code. To work effectively with unittest, it's essential to have a basic understanding of object-
oriented concepts, including classes and methods in Python.
In unittest, a test case is considered a single testing unit and is represented by the TestCase class.
This class is one of the key components in the unittest framework and serves as a foundation for
creating your own test cases, which allows you to execute multiple tests simultaneously.
Interestingly, while the assert statement is important for testing, it is not typically used within
unittest. This is because the TestCase class offers a variety of its own assert methods. These assert
methods function similarly to the assert statement but are tailored for specific types of assertions,
making it more convenient and powerful for writing unit tests.
The TestCase class in unittest already includes a setUp method, which is executed before each test.
What we can do when creating a new test case is to customize this setUp method to our specific
needs, effectively overwriting the default method with our own instructions. This allows us to set up
the necessary conditions or objects once, saving time and streamlining the testing process.
IMPLEMENTATION
Q1) Write a Python unit test program to check if a list is sorted in ascending order.
Program
Output
Q2) Write a Python unit test program to check if a string is a palindrome.
Program
Output
Q3) Write a Python unit test program that checks if two lists are equal.
Program
Output
Q4) Write a Python unit test program to check if a given number is prime or not.
Program
Output