25

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.

2
  • 3
    Please run like this: python -m unittest test_mymath Commented Jan 6, 2019 at 8:34
  • 1
    Possible duplicate of Python unittest and test discovery Commented Jan 6, 2019 at 8:34

2 Answers 2

23

Use python -m unittest test_mymath

Sign up to request clarification or add additional context in comments.

4 Comments

According to the Python 3.6 documentation, 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?
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...
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.
@MortenGrum neither works for me
17

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.

6 Comments

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 Can you quote the exact error you are getting?
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 <module> from gopher_changes import Updater ModuleNotFoundError: No module named 'gopher_changes'
FWIW I worked around this by changing into the directory where the tests live and running with python -m unittest discover
Windows powershell (for some reason) adds leading ./ after pressing Tab
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.