Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest does not run tests

I made a small project called demo, with a single test in it

import unittest


class Test(unittest.TestCase):


    def testName1(self):
        self.assertEqual(5+9, 14)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

However, from command line

ThinkPad-T520:~/workspacep/demo$ python -m unittest

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Why doesn't this work? In general, how can I run all unit tests from command line with a single line?

The structure of the directory is

demo
    tests
          demo_test1.py  __init__.py
like image 595
Baron Yugovich Avatar asked Aug 25 '18 02:08

Baron Yugovich


People also ask

How do you run all tests in Unittest 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 .

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.

How do I run a Python test?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

What does Unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.

What is unittest testing in Python?

In Python, we have a few popular testing tools. unittest is unit testing standard library being used in most popular Python tools and frameworks. In this article, we are not going to learn the actual unit testing.

How do I run a test from a module in Python?

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods: python -m unittest test_module1 test_module2 python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method

What is xUnit testing in Python?

Unit Testing in Python – Unittest. What is Unit Testing? Unit Testing is the first level of software testing where the smallest testable parts of a software are tested. This is used to validate that each unit of the software performs as designed. The unittest test framework is python’s xUnit style framework.

How do I run a single test module in unittest?

2) Running a single test module To run a single test module, you use the following command: python -m unittest test_package.test_module -v Code language:Python(python) For example, the following execute all tests in the test_circlemodule of the testpackage:


2 Answers

There are three gotcha's that I know of:

  1. Your tests in your TestCases need to be named test_*
  2. Your test files need to be named: test*.py (by default, you can change it with the -p flag when running the tests). e.g. test_demo1.py
  3. Your tests folder needs to have an __init__.py file in it, or else it won't be considered a valid location to import from.

So, for #1, you need to rename the test to test_name_1. And for #2, there's two options:

A - Restructure your files like this:

demo
    tests
        __init__.py
        test_demo1.py

Then run python -m unittest and it should find the test cases.

B - Just run it like: python -m unittest discover -p *test.py

like image 158
Multihunter Avatar answered Sep 21 '22 02:09

Multihunter


I fought with the same exact problem a while ago and I solved it by using test discovery command.

python -m unittest discover -s .

You can pass in your test file pattern as well and a whole other options https://docs.python.org/2/library/unittest.html#test-discovery

like image 23
k.wahome Avatar answered Sep 19 '22 02:09

k.wahome