0

Python 3.6.7 in Jupyter Notebook. Running in Lubuntu 18.04.2 LTS, which is itself running in Virtual Box.

I am learning to use the module unittest. It was my understanding that each individual method was a test, more or less confirmed by this other question: Python unittest counting the number of tests

Nevertheless, when I run this code:

import unittest

def square(x):
    return x * x


class TestSquareFunction(unittest.TestCase):

    def test_positive(self):
        a = 2.0
        self.assertEqual(square(a), 4.0)

    def test_negative(self):
        a = -3.0
        self.assertEqual(square(a), 9.0)


if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)

I get the result:

.....
----------------------------------------------------------------------
Ran 5 tests in 0.006s

OK

When I was expecting 2 tests.

If I change the values in the methods so that the tests fail, I get this result:

Ran 5 tests in 0.017s

FAILED (failures=2)

The failed tests are the methods test_positive and test_negative. But, which are the other tests?

Added later

In the same Notebook there is another cell, containing another TestCase object with its own 3 methods.

Community
  • 1
  • 1
jorvaor
  • 73
  • 1
  • 2
  • 10
  • When I test your code, I get 2 tests.. – Wimanicesir Jun 07 '19 at 15:23
  • 1
    Paste your code here: https://repl.it/languages/python3 and see how it works perfectly! – Wimanicesir Jun 07 '19 at 15:24
  • The test counting based on count of methods of TestCase base class! It seems that there is a problem in your running environemnt! If you want to get out help, you must explain more about your running environment and the way you run that! and other files(if is available) are in your project! – AbdolHosein Jun 07 '19 at 15:29
  • 1
    turn on verbosity to see which tests it is actually running.. there are clearly only 2 tests defined in your example. – Corey Goldberg Jun 07 '19 at 15:29
  • Thank you for all your comments. I found were the problem was. In the same Notebook is another cell containing another TestCase object with 3 methods. When I ran the cell containing the code I have posted here, the result counts the methods of both TestCase objects. If I restart the kernel and run just this code, I get 2 tests. I will modify the question to reflect this. – jorvaor Jun 07 '19 at 15:44
  • @jorvaor You can just delete the question. – chepner Jun 07 '19 at 15:53
  • @chepner I prefer not to. Even if it seems trivial a posteriori, should I have had found this same question, just reading the comments would have saved me plenty of time of searching and reading. I think it would be useful for others, especially if they are new to unittest AND Jupyter Notebook. – jorvaor Jun 07 '19 at 15:59

1 Answers1

1

As AbdolHosein commented, the problem was in the running environment.

The same Notebook contains two different TestCase objects, with different methods. When one of the tests is ran, it counts all of the methods that have ran previously even if they are contained in another object.

Since one object has 3 methods and the other has 2, the final result is that 5 tests have been ran.

jorvaor
  • 73
  • 1
  • 2
  • 10