Running single test from unittest.TestCase via command line

前端 未结 7 1603
甜味超标
甜味超标 2020-11-28 01:14

In our team, we define most test cases like this:

One \"framework\" class ourtcfw.py:

import unittest

class OurTcFw(unittest.TestCase):         


        
相关标签:
7条回答
  • 2020-11-28 01:57

    TL;DR: This would very likely work:

    python mypkg/tests/test_module.py MyCase.testItIsHot
    

    The explanation:

    • The convenient way

      python mypkg/tests/test_module.py MyCase.testItIsHot
      

      would work BUT its unspoken assumption is you already have this conventional code snippet inside (typically at the end of) your test file.

      if __name__ == "__main__":
          unittest.main()
      
    • The inconvenient way

      python -m unittest mypkg.tests.test_module.TestClass.test_method
      

      would always work, without requiring you to have that if __name__ == "__main__": unittest.main() code snippet in your test source file.

    So why does the 2nd method considered inconvenient? Because it would be a pain in the (_ insert one of your body part here _) to type that long, dot-delimited path by hand. While in 1st method, the mypkg/tests/test_module.py part can be auto-completed, either by a modern shell, or by your editor.

    PS: If you thought that body part is somewhere below your waist, you are an authentic person. :-) I mean to say "finger joint". Too much typing would be bad for your joints. ;-)

    0 讨论(0)
提交回复
热议问题