Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest and discovery

I have directories, which contain files named like: test_foo.py

Each file is a test case.

I would like to

  1. Run all the tests in a directory from the command line. I am using unittest2, since we are running Python 2.5.1. From one of these directories I tried typing this at the command line:

     python -m unittest2 discover -p 'test_*.py' 

    and several different variants. I get no error, but nothing happens. I was expecting all the tests within all the test cases in that directory to run and get results.

  2. I also tried having a script in the directory where I did this:

     loader = unittest2.TestLoader()  t = loader.discover('.') 

    If I print the t variable, I can see my test cases, but from the documentation I can't figure out what to do with the loader object once I have it.

like image 802
Aaron Avatar asked Jul 21 '10 00:07

Aaron


People also ask

What is Python Unittest discover?

The TestLoader class has a discover() function. Python testing framework uses this for simple test discovery. In order to be compatible, modules and packages containing tests must be importable from top level directory.

Is Python capable to discover unit tests?

Unit testing is a technique in which particular module is tested to check by developer himself whether there are any errors. The primary focus of unit testing is test an individual unit of system to analyze, detect, and fix the errors. Python provides the unittest module to test the unit of source code.

Which is better Pytest or Unittest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.

Can we use Pytest and Unittest together?

pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.


1 Answers

I ran into the same issue when running python -m unittest discover. Here is a good checklist to verify your setup. Nose is more flexible with the allowed configurations, but not necessarily better.

  1. Make sure all files/directories start with test. Do not use test-something.py, since that is not a valid Python module name. Use test_something.py.

  2. If you are putting your tests in a sub-directory (e.g. test/), make sure you create a test/__init__.py file so python will treat the directory as a package.

  3. All class test cases definitions must be extend unittest.TestCase. For example,

    class DataFormatTests(unittest.TestCase) 
  4. All test cases methods definitions must start with test_

     def test_data_format(self): 
like image 178
cmcginty Avatar answered Sep 21 '22 08:09

cmcginty