0% found this document useful (0 votes)
38 views

Python Unit

The document discusses different test runners for Python including unittest, nose, and pytest. unittest is built into the Python standard library and provides both a framework and runner. It requires tests to be written using classes and functions and uses distinct assertion methods. Nose and pytest are also popular options that may have additional features compared to unittest. Unit testing involves testing individual code units independently with all dependencies to check functionality. The Python unittest module is used for this with functions to check for expected values and types.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Python Unit

The document discusses different test runners for Python including unittest, nose, and pytest. unittest is built into the Python standard library and provides both a framework and runner. It requires tests to be written using classes and functions and uses distinct assertion methods. Nose and pytest are also popular options that may have additional features compared to unittest. Unit testing involves testing individual code units independently with all dependencies to check functionality. The Python unittest module is used for this with functions to check for expected values and types.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Choosing a Test Runner

Python contains many test runners. The most popular build-in Python library is called unittest.
The unittest is portable to the other frameworks. Consider the following three top most test
runners.

 unittest
 nose Or nose2
 pytest

We can choose any of them according to our requirements. Let's have a brief introduction.

unittest

The unittest is built into the Python standard library since 2.1. The best thing about the unittest,
it comes with both a test framework and a test runner. There are few requirements of the
unittest to write and execute the code.

 The code must be written using the classes and functions.


 The sequence of distinct assertion methods in the TestCase class apart from the built-in
asserts statements.

Python Unit-testing:

Unit testing is a software testing method in which individual components of the program, called units,
are tested independently with all the required dependencies. Unit testing is mostly done by the actual
programmers, who write the programs for the units. In smaller projects, it is done informally. In most of
the very large-scale projects, unit testing is part of a formal process of development with proper
documentation and proper schedule/ efforts allocated to it.

Python unittest module is used to test a unit of source code. Suppose, you need to test your project. You
know what kind of data the function will return. After writing huge code, you need to check it whether
the output is correct or not.

Normally, what we do is printing the output and match it with the reference output file or check the
output manually.

Python Unit Test Outcome & Basic Functions:

This unittest has 3 possible outcomes. They are mentioned below:

OK: If all test cases are passed, the output shows OK.

Failure: If any of test cases failed and raised an Assertion Error exception

Error: If any exception other than Assertion Error exception is raised. There are several function under
unittest module. They are listed below
METHOD CHECKS THAT

assertEqual(a,b) a==b

assertNotEqual(a,b) a != b

assertTrue(x) bool(x) is True

assertFalse(x) bool(x) is False

assertIs(a,b) a is b

assertIsNot(a, b) a is not b

assertIsNone(x) x is None

assertIsNotNone(x) x is not None

assertIn(a, b) a in b

assertNotIn(a, b) a not in b

assertIsInstance(a, b) isinstance(a, b)

assertNotIsInstance(a, b) not isinstance(a, b)

import unittest

def fun(x):

return x + 1

class MyTest(unittest.TestCase):

def test(self):

self.assertEqual(fun(3), 4)

# test execution starts from here.

if __name__ == "__main__":

unittest.main()

At the bottom of the test file, we have this code

if __name__ == "__main__":

unittest.main()
This allows us to run all of the test code just by running the file.

import unittest

class TestClass01(unittest.TestCase):

def test_case01(self):

my_str = "Digi Brains"

my_int = 999

self.assertTrue(isinstance(my_str, str))

self.assertTrue(isinstance(my_int, int))

def test_case02(self):

my_pi = 3.14

self.assertFalse(isinstance(my_pi, int))

if __name__ == '__main__':

unittest.main()

Running Without unittest.main():

Up until now, you have run the test modules with unittest.main(). Now you will see how to run the test
module without unittest.main()

import unittest

class TestClass07(unittest.TestCase):

def test_case01(self):

self.assertTrue("PYTHON".isupper())

print("\nIn test_case01()")

# Running a testcase without unittest.main()

python -m unittest ut.py –v #Running in verbose mode

import unittest

class TestingSum(unittest.TestCase):
def test_sum(self):

self.assertEqual(sum([2, 3, 5]), 10, "It should be 10")

def test_sum_tuple(self):

self.assertEqual(sum((1, 3, 5)), 10, "It should be 10")

if __name__ == '__main__':

unittest.main()

Writing the First Test

Here we will apply all the concepts that we have learned in earlier section. First, we need to
create a file name test.py or anything. Then make inputs and execute the code being tested,
capturing the output. After successfully run the code, match the output with an expected result.

First, we create the file my_sum file and write code in it.

def sum(arg):

total = 0

for val in arg:

total += val

return total

Now, we create a file name test.py with the following code.

import unittest

from mysum import sum

class CheckSum(unittest.TestCase):

def test_list_int(self):

data = [1, 2, 3]

result = sum(data)

self.assertEqual(result, 6)

if __name__ == '__main__':

unittest.main()

After running the code, it returns dot(.) which means there is no error in the code.

You might also like