How to resolve "ValueError: Empty module name"?

Viewed 21056

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 Answers

Use python -m unittest test_mymath

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.

Related