是否可以在 python 中过滤具有不同模式的测试文件和单个测试?
Is it possible to filter test files and individual tests with different patterns in python?
根据 nose documentation,--match
选项设置 all 测试发现的模式:
Files, directories, function names, and class names that match this
regular expression are considered tests. Default: (?:\b|_)[Tt]est
[NOSE_TESTMATCH]
我经常发现我更喜欢对文件、目录、函数名称和 class 名称使用单独的匹配,而不是必须匹配所有这些的模式。例如,使用这个测试套件:
import unittest
class CoolBeansTest(unittest.TestCase):
def testCool(self):
self.assertEqual(cool_function_takes_forever(), 7)
def testBeans(self):
inst = MyCoolBeansClass()
self.assertEqual(inst.getBeanz(), "CoolBeans")
class WarmBeansTest(unittest.TestCase):
def testWarm(self):
self.assertEqual(warm_function_takes_forever(), 21)
def testBeans(self):
inst = MyWarmBeansClass()
self.assertEqual(inst.getBeanz(), "WarmBeans")
我可能想要 运行 所有测试 testBeans
,但对除函数之外的所有内容都使用标准测试发现模式。除了枚举所有测试,或将某些 grep
表达式中的管道输送到 nosetests
之外,有没有办法 运行 所有 测试 匹配一个模式?
Nose 提供了一种方法来通过自定义 Selector
:
来更改内置测试选择逻辑
在您的情况下,您只需要根据 nose.selector.Selector
创建自己的 Selector
class 并覆盖 wantMethod
方法:
from nose.selector import Selector
class MySelector(Selector):
def wantMethod(self, method):
# custom logic here - returns True/False
仅供参考,built-in Selector
的所有 want*
方法都使用 matches
函数,该函数使用配置的 --match
值,默认情况下为 (?:\b|_)[Tt]est
.
另请参阅wantMethod
is implemented in nose.selector.Selector
。
准备好选择器后,您应该通过制作插件将其注入测试加载器:
>>> from nose.plugins import Plugin
>>> class UseMySelector(Plugin):
... enabled = True
... def configure(self, options, conf):
... pass # always on
... def prepareTestLoader(self, loader):
... loader.selector = MySelector(loader.config)
根据 nose documentation,--match
选项设置 all 测试发现的模式:
Files, directories, function names, and class names that match this regular expression are considered tests. Default: (?:\b|_)[Tt]est [NOSE_TESTMATCH]
我经常发现我更喜欢对文件、目录、函数名称和 class 名称使用单独的匹配,而不是必须匹配所有这些的模式。例如,使用这个测试套件:
import unittest
class CoolBeansTest(unittest.TestCase):
def testCool(self):
self.assertEqual(cool_function_takes_forever(), 7)
def testBeans(self):
inst = MyCoolBeansClass()
self.assertEqual(inst.getBeanz(), "CoolBeans")
class WarmBeansTest(unittest.TestCase):
def testWarm(self):
self.assertEqual(warm_function_takes_forever(), 21)
def testBeans(self):
inst = MyWarmBeansClass()
self.assertEqual(inst.getBeanz(), "WarmBeans")
我可能想要 运行 所有测试 testBeans
,但对除函数之外的所有内容都使用标准测试发现模式。除了枚举所有测试,或将某些 grep
表达式中的管道输送到 nosetests
之外,有没有办法 运行 所有 测试 匹配一个模式?
Nose 提供了一种方法来通过自定义 Selector
:
在您的情况下,您只需要根据 nose.selector.Selector
创建自己的 Selector
class 并覆盖 wantMethod
方法:
from nose.selector import Selector
class MySelector(Selector):
def wantMethod(self, method):
# custom logic here - returns True/False
仅供参考,built-in Selector
的所有 want*
方法都使用 matches
函数,该函数使用配置的 --match
值,默认情况下为 (?:\b|_)[Tt]est
.
另请参阅wantMethod
is implemented in nose.selector.Selector
。
准备好选择器后,您应该通过制作插件将其注入测试加载器:
>>> from nose.plugins import Plugin
>>> class UseMySelector(Plugin):
... enabled = True
... def configure(self, options, conf):
... pass # always on
... def prepareTestLoader(self, loader):
... loader.selector = MySelector(loader.config)