In our team, we define most test cases like this:
在我们的团队中,我们定义了大多数这样的测试用例:
One "framework" class ourtcfw.py:
一个“框架”类ourtcfw.py:
import unittest
class OurTcFw(unittest.TestCase):
def setUp:
# something
# other stuff that we want to use everywhere
and a lot of test cases like testMyCase.py:
还有很多测试用例,比如我的测试用例。
import localweather
class MyCase(OurTcFw):
def testItIsSunny(self):
self.assertTrue(localweather.sunny)
def testItIsHot(self):
self.assertTrue(localweather.temperature > 20)
if __name__ == "__main__":
unittest.main()
When I'm writing new test code and want to run it often, and save time, what I do is that I put "__" in front of all other tests. But it's cumbersome, distracts me from the code I 'm writing and the commit noise this creates is plain annoying.
当我编写新的测试代码并想要经常运行它并节省时间时,我所做的就是将“__”放在所有其他测试之前。但是它很麻烦,让我从我写的代码中分心,而这造成的提交噪音是很烦人的。
So e.g. when making changes to testItIsHot()
, I want to be able to do this:
因此,例如,当对evidence tishot()进行更改时,我希望能够做到以下几点:
$ python testMyCase.py testItIsHot
and have unittest
run only testItIsHot()
并且只有unittest运行睾丸()
How can I achieve that?
我怎么才能做到呢?
I tried to rewrite the if __name__ == "__main__":
part, but since I'm new to Python, I'm feeling lost and keep bashing into everything else than the methods.
我试图重写if __name__ =“__main__”:part,但是由于我是Python的新手,所以我感到迷失了方向,继续钻研方法之外的一切。
6 个解决方案
#1
174
This works as you suggest - you just have to specify the class name as well:
正如您所建议的那样,您只需要指定类名:
python testMyCase.py MyCase.testItIsHot
#2
69
If you organize your test cases, that is, follow the same organization like the actual code and also use relative imports for modules in the same package
如果您组织您的测试用例,也就是说,遵循与实际代码相同的组织,并对同一包中的模块使用相对导入
You can also use the following command format:
你也可以使用以下命令格式:
python -m unittest mypkg.tests.test_module.TestClass.test_method
# In your case, this would be:
python -m unittest testMyCase.MyCase.testItIsHot
#3
44
It can work well as you guess
它可以像你想的那样工作
python testMyCase.py MyCase.testItIsHot
And there is another way to just test testItIsHot
:
还有另外一种测试证明的方法:
suite = unittest.TestSuite()
suite.addTest(MyCase("testItIsHot"))
runner = unittest.TextTestRunner()
runner.run(suite)
#4
12
If you check out the help of the unittest module it tells you about several combinations that allow you to run test case classes from a module and test methods from a test case class.
如果您查看unittest模块的帮助,它会告诉您几个组合,这些组合允许您从模块运行测试用例类,从测试用例类运行测试方法。
python3 -m unittest -h
[...]
Examples:
python3 -m unittest test_module - run tests from test_module
python3 -m unittest module.TestClass - run tests from module.TestClass
python3 -m unittest module.Class.test_method - run specified test method
It does not require you to define a unittest.main()
as the default behaviour of your module.
它不需要将unittest.main()定义为模块的默认行为。
#5
1
Inspired by @yarkee I combined it with some of the code I already got. You can also call this from another script, just by calling the function run_unit_tests()
without requiring to use the command line, or just call it from the command line with python3 my_test_file.py
.
受到@yarkee的启发,我将它与我已经获得的一些代码结合起来。您还可以从另一个脚本调用它,只需调用函数run_unit_tests(),而不需要使用命令行,或者只需从命令行使用python3 my_test_file.py调用它。
import my_test_file
my_test_file.run_unit_tests()
Sadly this only works for Python 3.3
or superior:
遗憾的是,这只适用于Python 3.3或更高版本:
import unittest
class LineBalancingUnitTests(unittest.TestCase):
@classmethod
def setUp(self):
self.maxDiff = None
def test_it_is_sunny(self):
self.assertTrue("a" == "a")
def test_it_is_hot(self):
self.assertTrue("a" != "b")
def run_unit_tests():
runner = unittest.TextTestRunner()
classes = \
[
LineBalancingUnitTests,
]
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
"test_it_is_sunny",
# "test_it_is_hot",
]
runner.run( suite( classes, unit_tests_to_run ) )
def suite(classes, unit_tests_to_run):
"""
Problem with sys.argv[1] when unittest module is in a script
https://*.com/questions/2812218/problem-with-sys-argv1-when-unittest-module-is-in-a-script
Is there a way to loop through and execute all of the functions in a Python class?
https://*.com/questions/2597827/is-there-a-way-to-loop-through-and-execute-all-of-the-functions
looping over all member variables of a class in python
https://*.com/questions/1398022/looping-over-all-member-variables-of-a-class-in-python
"""
suite = unittest.TestSuite()
unit_tests_to_run_count = len( unit_tests_to_run )
for _class in classes:
_object = _class()
for function_name in dir( _object ):
if function_name.lower().startswith( "test" ):
if unit_tests_to_run_count > 0 \
and function_name not in unit_tests_to_run:
continue
suite.addTest( _class( function_name ) )
return suite
if __name__ == "__main__":
print( "\n\n" )
run_unit_tests()
Editing the code a little, you can pass an array with all unit tests you would like to call:
稍微编辑一下代码,您可以传递一个数组,其中包含您想要调用的所有单元测试:
...
def run_unit_tests(unit_tests_to_run):
runner = unittest.TextTestRunner()
classes = \
[
LineBalancingUnitTests,
]
runner.run( suite( classes, unit_tests_to_run ) )
...
And another file:
和另一个文件:
import my_test_file
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
"test_it_is_sunny",
# "test_it_is_hot",
]
my_test_file.run_unit_tests( unit_tests_to_run )
#6
-4
Here is another way
这是另一种方式
testname = "testItIsHot"
testcase = MyCase(testname)
testcase.testItIsHot()
#1
174
This works as you suggest - you just have to specify the class name as well:
正如您所建议的那样,您只需要指定类名:
python testMyCase.py MyCase.testItIsHot
#2
69
If you organize your test cases, that is, follow the same organization like the actual code and also use relative imports for modules in the same package
如果您组织您的测试用例,也就是说,遵循与实际代码相同的组织,并对同一包中的模块使用相对导入
You can also use the following command format:
你也可以使用以下命令格式:
python -m unittest mypkg.tests.test_module.TestClass.test_method
# In your case, this would be:
python -m unittest testMyCase.MyCase.testItIsHot
#3
44
It can work well as you guess
它可以像你想的那样工作
python testMyCase.py MyCase.testItIsHot
And there is another way to just test testItIsHot
:
还有另外一种测试证明的方法:
suite = unittest.TestSuite()
suite.addTest(MyCase("testItIsHot"))
runner = unittest.TextTestRunner()
runner.run(suite)
#4
12
If you check out the help of the unittest module it tells you about several combinations that allow you to run test case classes from a module and test methods from a test case class.
如果您查看unittest模块的帮助,它会告诉您几个组合,这些组合允许您从模块运行测试用例类,从测试用例类运行测试方法。
python3 -m unittest -h
[...]
Examples:
python3 -m unittest test_module - run tests from test_module
python3 -m unittest module.TestClass - run tests from module.TestClass
python3 -m unittest module.Class.test_method - run specified test method
It does not require you to define a unittest.main()
as the default behaviour of your module.
它不需要将unittest.main()定义为模块的默认行为。
#5
1
Inspired by @yarkee I combined it with some of the code I already got. You can also call this from another script, just by calling the function run_unit_tests()
without requiring to use the command line, or just call it from the command line with python3 my_test_file.py
.
受到@yarkee的启发,我将它与我已经获得的一些代码结合起来。您还可以从另一个脚本调用它,只需调用函数run_unit_tests(),而不需要使用命令行,或者只需从命令行使用python3 my_test_file.py调用它。
import my_test_file
my_test_file.run_unit_tests()
Sadly this only works for Python 3.3
or superior:
遗憾的是,这只适用于Python 3.3或更高版本:
import unittest
class LineBalancingUnitTests(unittest.TestCase):
@classmethod
def setUp(self):
self.maxDiff = None
def test_it_is_sunny(self):
self.assertTrue("a" == "a")
def test_it_is_hot(self):
self.assertTrue("a" != "b")
def run_unit_tests():
runner = unittest.TextTestRunner()
classes = \
[
LineBalancingUnitTests,
]
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
"test_it_is_sunny",
# "test_it_is_hot",
]
runner.run( suite( classes, unit_tests_to_run ) )
def suite(classes, unit_tests_to_run):
"""
Problem with sys.argv[1] when unittest module is in a script
https://*.com/questions/2812218/problem-with-sys-argv1-when-unittest-module-is-in-a-script
Is there a way to loop through and execute all of the functions in a Python class?
https://*.com/questions/2597827/is-there-a-way-to-loop-through-and-execute-all-of-the-functions
looping over all member variables of a class in python
https://*.com/questions/1398022/looping-over-all-member-variables-of-a-class-in-python
"""
suite = unittest.TestSuite()
unit_tests_to_run_count = len( unit_tests_to_run )
for _class in classes:
_object = _class()
for function_name in dir( _object ):
if function_name.lower().startswith( "test" ):
if unit_tests_to_run_count > 0 \
and function_name not in unit_tests_to_run:
continue
suite.addTest( _class( function_name ) )
return suite
if __name__ == "__main__":
print( "\n\n" )
run_unit_tests()
Editing the code a little, you can pass an array with all unit tests you would like to call:
稍微编辑一下代码,您可以传递一个数组,其中包含您想要调用的所有单元测试:
...
def run_unit_tests(unit_tests_to_run):
runner = unittest.TextTestRunner()
classes = \
[
LineBalancingUnitTests,
]
runner.run( suite( classes, unit_tests_to_run ) )
...
And another file:
和另一个文件:
import my_test_file
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
"test_it_is_sunny",
# "test_it_is_hot",
]
my_test_file.run_unit_tests( unit_tests_to_run )
#6
-4
Here is another way
这是另一种方式
testname = "testItIsHot"
testcase = MyCase(testname)
testcase.testItIsHot()