I currently work on some code that uses unittest2 module. I suspect this code was meant for the python2. Can one use python3 unittest as a drop in replacement for unittest2? What is the difference between the two?
unittest2 is a backport of the new features added to the unittest testing framework in Python 2.7 and onwards. It is tested to run on Python 2.6, 2.7, 3.2, 3.3, 3.4 and pypy. To use unittest2 instead of unittest simply replace import unittest with import unittest2. unittest2 is maintained in a mercurial repository.
The command to run the tests is python -m unittest filename.py . In our case, the command to run the tests is python -m unittest test_utils.py .
There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.
An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.
According to the Python 2.7 unittest docs:
unittest2: A backport of new unittest features for Python 2.4-2.6 Many new features were added to unittest in Python 2.7, including test discovery. unittest2 allows you to use these features with earlier versions of Python.
So moving from unittest2
under Python 2 to unittest
under Python 2.7 or Python 3 should do exactly what you want
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With