支持非致命故障的 Python 测试框架

发布于 2024-08-02 15:11:40 字数 183 浏览 10 评论 0原文

我正在评估自动化系统测试的“测试框架”;到目前为止我正在寻找一个Python框架。 在 py.test 或鼻子中,我看不到类似我从谷歌测试框架中知道的 EXPECT 宏的东西。 我想在一个测试中做出多个断言,同时不在第一次失败时中止测试。 我是否在这些框架中遗漏了某些内容或者这不起作用? 有人对可用于自动化系统测试的 python 测试框架有建议吗?

I'm evaluating "test frameworks" for automated system tests; so far I'm looking for a python framework.
In py.test or nose I can't see something like the EXPECT macros I know from google testing framework.
I'd like to make several assertions in one test while not aborting the test at the first failure.
Am I missing something in these frameworks or does this not work?
Does anybody have suggestions for python test framworks usable for automated system tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦途 2024-08-09 15:11:40

我想要类似的功能测试,我正在使用鼻子进行。我最终想出了这个:

def raw_print(str, *args):
    out_str = str % args
    sys.stdout.write(out_str)

class DeferredAsserter(object):
    def __init__(self):
        self.broken = False
    def assert_equal(self, expected, actual):
        outstr = '%s == %s...' % (expected, actual)
        raw_print(outstr)
        try:
            assert expected == actual
        except AssertionError:
            raw_print('FAILED\n\n')
            self.broken = True
        except Exception, e:
            raw_print('ERROR\n')
            traceback.print_exc()
            self.broken = True
        else:
            raw_print('PASSED\n\n')

    def invoke(self):
        assert not self.broken

换句话说,它打印出指示测试是否通过或失败的字符串。在测试结束时,您调用实际上执行真实断言的invoke方法。这绝对不是优选的,但我还没有见过可以处理这种测试的Python测试框架。我也没有抽出时间去弄清楚如何编写一个鼻子插件来完成这种事情。 :-/

I was wanting something similar for functional testing that I'm doing using nose. I eventually came up with this:

def raw_print(str, *args):
    out_str = str % args
    sys.stdout.write(out_str)

class DeferredAsserter(object):
    def __init__(self):
        self.broken = False
    def assert_equal(self, expected, actual):
        outstr = '%s == %s...' % (expected, actual)
        raw_print(outstr)
        try:
            assert expected == actual
        except AssertionError:
            raw_print('FAILED\n\n')
            self.broken = True
        except Exception, e:
            raw_print('ERROR\n')
            traceback.print_exc()
            self.broken = True
        else:
            raw_print('PASSED\n\n')

    def invoke(self):
        assert not self.broken

In other words, it's printing out strings indicating if a test passed or failed. At the end of the test, you call the invoke method which actually does the real assertion. It's definitely not preferable, but I haven't seen a Python testing framework that can handle this kind of testing. Nor have I gotten around to figuring out how to write a nose plugin to do this kind of thing. :-/

孤凫 2024-08-09 15:11:40

您询问了建议,所以我会建议机器人框架

You asked for suggestions so I'll suggest robot framework.

往事风中埋 2024-08-09 15:11:40

奇怪的是,听起来你正在寻找类似我的 claft 的东西(命令行和过滤器测试器)。类似的东西,但更成熟。

claft(到目前为止)只是我为帮助学生进行编程练习而编写的一个玩具。其想法是为练习提供简单的配置文件,这些配置文件以人类合理可读的方式表示程序的要求(并且是声明性的而不是编程性的),同时也适合自动化测试。

claft 运行所有定义的测试,为每个测试提供参数和输入,检查返回代码,并匹配输出 (stdout) 和错误消息 (stderr) )针对正则表达式模式。它将所有失败收集在一个列表中,并在每个套件的末尾打印整个列表。

它还没有对输入/输出序列进行任意对话。到目前为止,它只是输入数据然后读出所有数据/错误。它也不实现超时,事实上,甚至不捕获失败的执行尝试。 (到目前为止,我确实说过这只是一个玩具,不是吗?)。我还没有实现对安装、拆卸和外部检查脚本的支持(尽管我有计划这样做)。

布莱恩提出的“机器人框架”可能更适合您的需求;尽管快速浏览一下它会发现它比我想要的要复杂得多。 (我需要让事情足够简单,以便刚接触编程的学生可以专注于他们的练习,而不是花费大量时间来设置他们的测试工具)。

欢迎您查看claft并使用它或从中派生您自己的解决方案(它是 BSD 许可的)。显然,我们欢迎您做出贡献。 (它位于 [bitbucket]:(http://www.bitbucket.org/),因此您可以使用Mercurial 克隆,并分叉您自己的存储库......如果您希望我考虑将您的更改合并回我的存储库,请提交“拉取请求”)。

再说一遍,也许我误读了你的问题。

Oddly enough it sounds like you're looking for something like my claft (command line and filter tester). Something like it but far more mature.

claft is (so far) just a toy I wrote to help students with programming exercises. The idea is to provide the exercises with simple configuration files that represent the program's requirements in terms which are reasonably human readable (and declarative rather than programmatic) while also being suitable for automated testing.

claft runs all the defined tests, supplying arguments and inputs to each, checking return codes, and matching output (stdout) and error messages (stderr) against regular expression patterns. It collects all the failures in a list an prints the whole list at the end of each suite.

It does NOT yet do arbitrary dialogs of input/output sequences. So far it just feeds data in then reads all data/errors out. It also doesn't implement timeouts and, in fact, doesn't even capture failed execute attempts. (I did say it's just a toy, so far, didn't I?). I also haven't yet implemented support for Setup, Teardown, and External Check scripts (though I have plans to do so).

Bryan's suggestion of the "robot framework" might be better for your needs; though a quick glance through it suggests that it's considerably more involved than I want for my purposes. (I need to keep things simple enough that students new to programming can focus on their exercises and not spend lots of time fighting with setting up their test harness).

You're welcome to look at claft and use it or derive your own solution there from (it's BSD licensed). Obviously you'd be welcome to contribute back. (It's on [bitbucket]:(http://www.bitbucket.org/) so you can use Mercurial to clone, and fork your own respository ... and submit a "pull request" if you ever want me to look at merging your changes back into my repo).

Then again perhaps I'm misreading your question.

千秋岁 2024-08-09 15:11:40

为什么不(在 unittest 中,但这应该在任何框架中都有效):

class multiTests(MyTestCase):
    def testMulti(self, tests):
        tests( a == b )
        tests( frobnicate())
        ...

假设您实现了 MyTestCase,以便将函数包装到

testlist = []
x.testMulti(testlist.append)
assert all(testlist)

Why not (in unittest, but this should work in any framework):

class multiTests(MyTestCase):
    def testMulti(self, tests):
        tests( a == b )
        tests( frobnicate())
        ...

assuming your implemented MyTestCase so that a function is wrapped into

testlist = []
x.testMulti(testlist.append)
assert all(testlist)
2024-08-09 15:11:40

如果您在命令行中传递 -x 选项,nose 只会在第一次失败时中止。

test.py:

def test1():
    assert False

def test2():
    assert False

不带 -x 选项:

C:\temp\py>C:\Python26\Scripts\nosetests.exe test.py
FF
======================================================================
FAIL: test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 2, in test1
    assert False
AssertionError

======================================================================
FAIL: test.test2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 5, in test2
    assert False
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.031s

FAILED (failures=2)

带 -x 选项:

C:\temp\py>C:\Python26\Scripts\nosetests.exe test.py -x
F
======================================================================
FAIL: test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 2, in test1
    assert False
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.047s

FAILED (failures=1)

您可能需要考虑查看 鼻子文档

nose will only abort on the first failure if you pass the -x option at the command line.

test.py:

def test1():
    assert False

def test2():
    assert False

without -x option:

C:\temp\py>C:\Python26\Scripts\nosetests.exe test.py
FF
======================================================================
FAIL: test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 2, in test1
    assert False
AssertionError

======================================================================
FAIL: test.test2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 5, in test2
    assert False
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.031s

FAILED (failures=2)

with -x option:

C:\temp\py>C:\Python26\Scripts\nosetests.exe test.py -x
F
======================================================================
FAIL: test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\nose-0.11.1-py2.6.egg\nose\case.py", line
183, in runTest
    self.test(*self.arg)
  File "C:\temp\py\test.py", line 2, in test1
    assert False
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.047s

FAILED (failures=1)

You might want to consider reviewing the nose documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文