Python unit tests exception

Easy Python Unit Tests: Setup, Teardown, Fixtures, And More

Python is a versatile and highly popular programming language used broadly across various applications, such as web development, data analysis, machine learning, and more. Yet, experienced developers understand that code creation is merely one facet of the process. Ensuring the code operates correctly under diverse conditions is equally, if not more, crucial. This is where Python’s unit testing framework gains significance.

Unit tests form a vital component of the Python software development process. These tests are designed to ascertain the correctness of discrete units of source code, like functions or methods. By verifying each segment of your software individually, you can safeguard against inadvertently introducing errors when making changes or additions to the code. For instance, if you are developing an application to automate infrastructure deployment using Python scripts and the Boto3 library to interact with AWS without unit tests, verifying that your code functions as intended would involve deploying your infrastructure each time. This process could be time-consuming, costly, and potentially risky.

However, Python’s unit tests allow for mocking the behavior of Boto3, testing scripts without actual AWS interaction. This helps catch and rectify any bugs or logic errors in scripts before they impact your infrastructure. Python’s built-in module unittest provides a systematic method to create and organize test cases, offering a wide range of functionalities to test your Python code efficiently and effectively. The ensuing sections will provide a deeper dive into the specifics of setting up and tearing down tests, using fixtures, asserting conditions, and more. With the knowledge gained from this guide, you will be well on your way to mastering Python unit tests and writing robust, error-free Python code.

Читайте также:  Как распаковать set python

So, stay tuned as we embark on this journey to becoming a Python unit testing expert!

Table of Contents

The Basics of Python Unit Testing

Before we dive into more complex aspects of Python unit testing, it’s crucial to grasp the basics. This section focuses on three fundamental elements of Python unit tests: setup, teardown, and fixtures.

Python Unit Test Setup

In unit testing, ‘setup’ refers to preparation before a test or a group of tests can run. Python’s unittest module provides a setUp() method, where you can write the preparation code. This method is automatically called before each test.

Consider the following example:

Here, we’re testing a simple function that returns the length of a list. The setUp() method initializes the list before each test runs. So, even if a test modifies the list, the setUp() method ensures that each test starts with the original list.

Источник

2 Ways to use Python Unittest assertRaises() in Python

Python Unittest assertRaises

Python Clear

Python unittest assertraises allows you to verify that a certain exception is raised when your code is run. Python’s unittest module provides a set of tools for testing your code. Note that the python unittest assertRaises function does not call the exceptions __init__ method. It can be used to test both your own code and code from third-party libraries.

How to Use Python Unittest Assertraises

To use python unittest assertRaises , you pass in the exception, you expect to be raised as well as a callable that will execute the code you want to test. assertRaises will then automatically verify that the correct exception is raised.

In this article, we’ll take a closer look at how to use the assertRaises method, with some practical examples.

Parameters

assertRaises(exception, callable, *args, **kwds)
  • exception – Exception class expected to be raised by callable .
  • callable – Function to be called. A string identifying a callable object may also be supplied. The callable will be invoked with args and kwds as appropriate.
  • *args – Positional arguments passed to callable when invoked.
  • **kwds – Keyword arguments passed to callable when invoked.

Returns:

Returns : The return value from calling callable (or None if an exception is not raised).

Basic usage

Example – 1

Let’s start with a very simple example for python unittest assertraises. We have a function that raises a ValueError if the input value is negative:

def raise_if_negative(value): if value < 0: raise ValueError('Value cannot be negative')

We can write a test for this using assertRaises :

import unittest class TestRaiseIfNegative(unittest.TestCase): def test_raise_if_negative(self): with self.assertRaises(ValueError): raise_if_negative(-1)

The with the statement is used here to create a context manager. This allows us to write the test in a more “Pythonic” way, without having to explicitly call the assertRaises method. The advantage of this approach is that it makes the text more readable and easier to maintain.

Example – 2

We have a function that converts a string to uppercase, but it fails if the input string is empty:

def to_upper(value): if not value: raise ValueError('Value cannot be empty') return value.upper()

We can write a test for this using assertRaises :

import unittest class TestToUpper(unittest.TestCase): def test_to_upper(self): with self.assertRaises(ValueError): to_upper('')

If you run this test, you’ll see that it fails with the following error message:

AssertionError: ValueError not raised by to_upper

How can this be? We clearly defined a ValueError in our function, so why is the test failing?

The reason is that the assertRaises method only catches exceptions that are raised in the with block. In our case, the to_upper function is never called, so the exception is never raised.

To fix this, we can explicitly call the function inside the with block:

import unittest class TestToUpper(unittest.TestCase): def test_to_upper(self): with self.assertRaises(ValueError): to_upper('')

Now if you run the test, it passes:

If you want to be able to call the function outside of the with block, you can store it in a variable:

import unittest class TestToUpper(unittest.TestCase): def test_to_upper(self): to_upper_fn = to_upper('') with self.assertRaises(ValueError): to_upper_fn() 

Verifying the Exception Message

In the previous section, we saw how to verify that a certain code path is reached when an exception is raised. But sometimes it’s also important to verify the exception message.

The assertRaises method allows us to do this by passing it an additional argument: a function that receives the raised exception as an argument and verifies its contents.

Let’s see how this works with our example from the previous section:

import unittest def test_to_upper(self): with self.assertRaises(ValueError): to_upper('')

If we run this test, it fails:

AssertionError: ValueError not raised by to_upper

To fix this, we can pass a function that verifies the exception message:

import unittest def test_to_upper(self): with self.assertRaises(ValueError) as cm: to_upper('') self.assertEqual(str(cm.exception), 'Value cannot be empty')

The assertEqual method is used here to verify that the exception message is equal to the expected message.

If you run the test now, it passes:

Python Unittest assertRaises Output

Verifying the Exception Type

In addition to verifying the exception message, there are also cases where you need to verify the exception type. For example, you might have a function that can raise either a ValueError or an AttributeError , and you want to verify that only a ValueError is raised:

def do_something(value): if value == 'a': raise ValueError('Invalid value') elif value == 'b': raise AttributeError('Invalid value')

In this case, we can use the assertRaises the method with multiple arguments:

import unittest def test_do_something(self): with self.assertRaises(ValueError): do_something('a') with self.assertRaises(AttributeError): do_something('b')

If we try to call do_something with a value that doesn’t raise any exception, the test fails:

AssertionError: AttributeError not raised by do_something

To fix this, we can add a third with block that verifies that no exception is raised:

import unittest def test_do_something(self): with self.assertRaises(ValueError): do_something('a') with self.assertRaises(AttributeError): do_something('b') with self.assertRaises(Exception): # any exception do_something('c')

If you run the test now, it passes:

Conclusion

In this article, we’ve seen how to use python unittest assertRaises method to write tests that verify that a certain code path is reached when an exception is raised. We’ve also seen how to use this method to verify the exception message and type.

Keep in mind that the assertRaises method can only be used with exceptions that are raised in the with block. This means that you might need to explicitly call the function that raises the exception, as we saw in the examples above.

References

That’s all for this post. To be up to date with our latest posts follow Python Clear. Let’s learn together!

Источник

Оцените статью