python-m unittest执行什么函数?
在Python2.6中,我一直在阅读。但我仍然没有找到这个答案 pyton-m单元测试执行什么函数 例如,我如何修改这段代码,以便只执行python-m unittest执行什么函数?,python,python-unittest,Python,Python Unittest,在Python2.6中,我一直在阅读。但我仍然没有找到这个答案 pyton-m单元测试执行什么函数 例如,我如何修改这段代码,以便只执行python-m unittest就能检测到它并运行测试 import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(s
python-m unittest
就能检测到它并运行测试
import random
import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = range(10)
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
self.assertRaises(ValueError, random.sample, self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__ == '__main__':
unittest.main()
编辑:
注意,这只是一个示例,实际上我正试图让它作为一个套件检测和运行多个测试,这是我的出发点-但是python-m unittest
没有检测到它,也没有python-m unittest discovery
使用它。我必须调用python discovery.py
来执行它
discovery.py:
import os
import unittest
def makeSuite():
"""Function stores all the modules to be tested"""
modules_to_test = []
test_dir = os.listdir('.')
for test in test_dir:
if test.startswith('test') and test.endswith('.py'):
modules_to_test.append(test.rstrip('.py'))
all_tests = unittest.TestSuite()
for module in map(__import__, modules_to_test):
module.testvars = ["variables you want to pass through"]
all_tests.addTest(unittest.findTestCases(module))
return all_tests
if __name__ == '__main__':
unittest.main(defaultTest='makeSuite')
python-msothing
将模块something
作为脚本执行。i、 e.来自python的帮助:
-m mod:以脚本形式运行库模块(终止选项列表)
unittest模块——以及传递给它的参数确定它测试哪些文件。命令行接口也记录在。
python-m unittest您的_test_module_name
@falsetru-yes中,这会起作用,但我不想实际指定每个测试python-m unittest-h
似乎暗示存在一个“默认值”。。。那么默认值是什么呢?如果您使用Python2.7+,您可以使用Python-m unittest discover
。但这是在Python2.7中引入的。使用py.test
/nose
怎么样?我必须通过安全性推送py.test
或nose
。如果我不必那么做。。。我会更高兴:-)也许我可以写我自己的“发现”,我想我已经完成了一半,但是调用python-m unittest Suite
或Suite.makeSuite
由于某种原因失败了……啊,我看到-m unittest
在2.7中有记录(我一直在使用2.6)。似乎我一直在写我自己的探索小部件。。。现在我只需要让我的当前代码为-m unittest
。。。