Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "ValueError: Empty module name"?

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.

like image 457
Shams Nahid Avatar asked Jan 06 '19 08:01

Shams Nahid


1 Answers

Use python -m unittest test_mymath

like image 165
amanb Avatar answered Sep 21 '22 13:09

amanb