Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Python's unittest and unittest2 modules?

Tags:

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?

like image 286
Anton Daneyko Avatar asked Jan 18 '16 16:01

Anton Daneyko


People also ask

What is unittest2 in Python?

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.

How do I run a unittest in Python?

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 .

How do you use assertRaises in Python?

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.

Which item in Python will stop a unit test abruptly?

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.


1 Answers

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

like image 117
rbp Avatar answered Oct 16 '22 03:10

rbp