23

In my UnitTest directory, I have two files, mymath.py and test_mymath.py.

mymath.py file:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(numerator, denominator):
    return float(numerator) / denominator

And the test_mymath.py file is:

import mymath
import unittest

class TestAdd(unittest.TestCase):
    """
    Test the add function from the mymath library
    """

    def test_add_integer(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = mymath.add(1, 2)
        self.assertEqual(result, 3)

    def test_add_floats(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = mymath.add(10.5, 2)
        self.assertEqual(result, 12.5)

    def test_add_strings(self):
        """
        Test that the addition of two strings returns the two strings as one
        concatenated string
        """
        result = mymath.add('abc', 'def')
        self.assertEqual(result, 'abcdef')

if __name__ == '__main__':
    unittest.main()

When I run the command

python .\test_mymath.py

I got the results

Ran 3 tests in 0.000s

OK

But when I tried to run the test using

python -m unittest .\test_mymath.py

I got the error

ValueError: Empty module name

Traceback: Full Traceback

Folder Structure: enter image description here

I am following this article

My python version is Python 3.6.6 and I am using windows 10 in the local machine.

Shams Nahid
  • 6,239
  • 8
  • 28
  • 39
  • 3
    Please run like this: `python -m unittest test_mymath` – amanb Jan 06 '19 at 08:34
  • 1
    Possible duplicate of [Python unittest and test discovery](https://stackoverflow.com/questions/7728371/python-unittest-and-test-discovery) – iBug Jan 06 '19 at 08:34

2 Answers2

19

Use python -m unittest test_mymath

amanb
  • 5,276
  • 3
  • 19
  • 38
  • 3
    According to the [Python 3.6 documentation](https://docs.python.org/3.6/library/unittest.html#command-line-interface), what @shams-nahid tried to do is valid: "Test modules can be specified by file path as well: `python -m unittest tests/test_something.py`". So is this a bug in Python, or is the documentation incorrect? – phantom-99w Nov 11 '19 at 14:50
  • Try to add (empty) __init__.py to folder with the test. Even though it shouldn't be needed in python3.3+, it still is needed in python3.7+ for me... – Filip Happy Feb 18 '20 at 14:05
  • 2
    NOTE: removing **only** the `./` (and not the full path) also works when the test is in a subfolder. e.g. `python -m unittest ./tests/test_something.py` does **not** work while `python -m unittest tests/test_something.py` **does** work. – Morten Grum Jan 04 '22 at 08:17
16

You almost got it. Instead of:

python -m unittest ./test_mymath.py

don't add the ./ so you now have:

python -m unittest test_mymath.py

Your unit tests should now run.

Joshua T
  • 656
  • 8
  • 15
  • I'm running into this error on github actions, where I need to be able to include the `./` as part of the path. Is there another workaround? – szeitlin Apr 15 '21 at 22:28
  • @szeitlin Can you quote the exact error you are getting? – Joshua T Apr 16 '21 at 10:44
  • ImportError: Failed to import test module: test_gopher_changes Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName module = __import__(module_name) File "/home/runner/work/data-lake/data-lake/automated/gopher_changes/test_gopher_changes.py", line 1, in from gopher_changes import Updater ModuleNotFoundError: No module named 'gopher_changes' – szeitlin Apr 16 '21 at 16:36
  • FWIW I worked around this by changing into the directory where the tests live and running with `python -m unittest discover` – szeitlin Apr 16 '21 at 16:38
  • 1
    Windows powershell (for some reason) adds leading ./ after pressing `Tab` – Radzor Sep 11 '21 at 22:21
  • Oh *god* - worked for me. Why does this solve the problem? – alex Sep 29 '22 at 15:26