简体   繁体   中英

python unittest where does the “X tests run” number come from?

import unittest

class Tests(unittest.TestCase):

    def test_one(self):
      a = 1
      self.assertEqual(a,1)

    def test_two(self):
      b = 2
      c = 3
      d = 4
      self.assertEqual(b,2)
      assert c == 3
      self.assertEqual(d,4)

    def test_three(self):
      e = 5
      f = 6
      self.assertEqual(e,5)
      assert f ==6

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

I am getting 3 tests run whereas clearly I have 6 asserts. Are all my asserts not getting tested?

python test.py

...

Ran 3 tests in 0.000s

OK

unittest reports the number of executed test methods , not the number of the assertions made.


You can increase the verbosity level to see what methods have been executed:

unittest.main(verbosity=3)

Which would produce:

test_one (__main__.Tests) ... ok
test_three (__main__.Tests) ... ok
test_two (__main__.Tests) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

Just a side note: having a single assert statement/call per test method is considered a good practice, see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM