简体   繁体   English

如何使用pytest编写集成测试以及如何重复集成测试

[英]how to write integration tests using pytest and how to repeat the integration tests

I am new to this so please do not mind if the question is not specific enough. 我对此并不陌生,所以请不要介意这个问题不够具体。

I want to know how to club unit tests into a single integration test in pytest. 我想知道如何将单元测试合并到pytest中的单个集成测试中。 Furthermore, I would like to repeat the integration test in a single test session a couple of times. 此外,我想在单个测试会话中重复几次集成测试。 Please let me know if there is a way to do this in pytest. 请让我知道pytest中是否有办法做到这一点。

Scenario: I have two unit tests name test_start_call and test_end_call that are invoked by pytest in that order. 方案:我有两个单元测试,名称分别为test_start_call和test_end_call,它们分别由pytest调用。

Now I wanted to repeat the process a couple of times so I did this: 现在,我想重复此过程几次,所以我这样做:

for i in range(0,c): pytest.main(some command) 对于我在范围(0,c)中:pytest.main(一些命令)

which works fine which will start the test session and tear down the test session as many times as I want with one call being made in each test session. 该方法工作正常,可以启动测试会话,并在每次测试会话中进行一次调用后,根据需要将测试会话断开多次。

But I want to make several calls in a single test session and by far I have not found any way to do this since last two days. 但是我想在一个测试会话中打几个电话,到目前为止,自最近两天以来,我还没有找到任何方法可以这样做。 I tried looking into xdist but I don't want to start new processes in parallel. 我尝试研究xdist,但不想并行启动新进程。 The integration tests should serially execute unit tests (start call and end call) as many times as I want in a single test session. 集成测试应该在单个测试会话中按我希望的顺序连续执行单元测试(开始调用和结束调用)。

I am stuck. 我被困住了。 So any help would be great. 因此,任何帮助都会很棒。 Thank you! 谢谢!

review https://docs.pytest.org/en/latest/parametrize.html 查看https://docs.pytest.org/en/latest/parametrize.html

Then add mult parameter to each test and implement --mult in hook pytest_generate_tests to provide the fixture value. 然后将mult参数添加到每个测试中,并在pytest_generate_tests钩子中实现--mult以提供固定值。

# conftest
def pytest_addoptions(parser):
    parser.addoption('--mult', default=1, help="run many tests")

def pytest_generate_tests(metafunc):
    mult = metafunc.config.getoption('--mult')
    if 'mult' in metafunc.fixturenames:
        for x in xrange(1, mult):
            metafunc.parametrize("mult", mult)

# testfoo
def test_start_call(mult):
    ...

From what you're saying, I'm not quite sure that you are using the right toolset. 按照您的意思,我不太确定您使用的是正确的工具集。 It sounds like you are either trying to load test something ( run it multiple times and see if it falls over ), or trying to do something more "data driven" - aka given input values x through y, see how it behaves. 听起来您正在尝试加载某些东西(多次运行,看看它是否掉落了),或者试图做更多的“数据驱动”(又名给定输入值x到y,看看它的行为)。

If you are trying to do something like load testing, I'd suggest looking into something like locust . 如果您尝试进行负载测试之类的事情,建议您研究locust之类的东西。

Here is a reasonable blog with different examples on driving unit tests via different data. 是一个合理的博客,其中包含有关通过不同数据进行驾驶单元测试的不同示例。

Again, not sure if either of these are actually what you're looking for. 同样,不确定其中任何一个是否确实是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM