Python notests跳过某些测试

2024-06-12 08:43:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个用python编写的web应用程序进行测试。

假设我的test_login.py模块中有5个测试。

每一次考试都是一门课。

通常有一个扩展TestFlow类的基本测试,它是我们预定义的测试类。

然后这个模块中的其他测试扩展了这个基本测试。

例如:

#The base test 

TestLogin(TestFlow):
    #do login_test_stuff_here

#Another test in the same module

TestAccountDetails(TestLogin)
    #do account_details_test_stuff_here

...

它实际上非常方便,因为为了测试AccountDetails用户必须登录,所以我可以从TestLogin测试继承,并准备好作为登录用户测试其他功能。

所有测试都在Project/Project/tests文件夹中。

我们使用带有选项的nosetest——使用挂架来运行测试。

我的问题是是否有办法将某个TestClass标记为“不要测试这个”。

因为我不想浪费时间直接执行这些“基本测试”,因为它们将由iherit从中提取的其他测试执行。

可能会有这些测试的音调,我想把每一秒都保存在可能的地方。

我已经找到了Skip、Skip test或@nottest之类的东西,但是这些只适用于ceratin test class中的test_方法,所以我认为如果每个测试用例只有一个类,那么在这里就行不通了。


Tags: 模块the用户pytestprojectweb应用程序
3条回答

表单nosetests通过指定属性可以如下完成 http://nose.readthedocs.org/en/latest/plugins/attrib.html

Oftentimes when testing you will want to select tests based on criteria rather than simply by filename. For example, you might want to run all tests except for the slow ones. You can do this with the Attribute selector plugin by setting attributes on your test methods. Here is an example:

def test_big_download():
    import urllib
    # commence slowness...

test_big_download.slow = 1

Once you’ve assigned an attribute slow = 1 you can exclude that test and all other tests having the slow attribute by running

$ nosetests -a '!slow'

http://nose.readthedocs.org/en/latest/writing_tests.html

Writing tests

As with py.test, nose tests need not be subclasses of unittest.TestCase. Any function or class that matches the configured testMatch regular expression ((?:^|[\b_\.-])[Tt]est) by default – that is, has test or Test at a word boundary or following a - or _) and lives in a module that also matches that expression will be run as a test. For the sake of compatibility with legacy unittest test cases, nose will also load tests from unittest.TestCase subclasses just like unittest does. Like py.test, nose runs functional tests in the order in which they appear in the module file. TestCase-derived tests and other test classes are run in alphabetical order.

注意上面的regex和规则。将函数/方法/类命名为与regex不匹配,它们将不会运行。

也就是说,我不建议您将继承链接到测试中。这是沮丧和混乱的秘诀。

您最好创建一个mixin类,或者定义一个没有任何实际测试的基类——但是有很多帮助函数,继承类可以调用并使用它们自己的帮助函数。

如果你看看比较流行的包的测试,它们几乎都使用这种方法。

实际上,您也可以将skiptest用于类。

import unittest

@unittest.skip("Class disabled")
class TestLogin(TestFlow):
    ...

相关问题 更多 >