在p语言中如何在测试套件中执行测试

2024-04-26 20:25:03 发布

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

我已经通过使用webdriver对selenium集线器和节点并行运行测试。此代码在测试运行之前调用。在

cls.driver = webdriver.Remote(
   command_executor="http://localhost:4444/wd/hub",
   desired_capabilities={
        "browserName": "chrome",
        })

    cls.driver.maximize_window()
    cls.driver.get(cls.serverUrl)
    p = multiprocessing.Process(target=cls.driver.get(cls.serverUrl), args=())
    p.start()
    p.join()

通过这种方式,我可以从Eclipse手动执行多个浏览器。不过,我想在一个测试套件中自动完成。但在测试套件中,所有测试都是按顺序启动的。如果有人知道如何进行,那就太好了。在


Tags: 代码localhosthttpget节点remote套件driver
1条回答
网友
1楼 · 发布于 2024-04-26 20:25:03

准备工作

我准备了一些样品测试。这些是一些简单的页面标题检查。我们有一个模块test_google.py,其中两个单元测试检查www.google.commail.google.com的标题:

# test_google.py

import unittest
from selenium import webdriver


class GoogleTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()

    def tearDown(self):
        self.driver.close()


    def test_google_page_title(self):
        self.driver.get('https://www.google.com')
        assert self.driver.title == 'Google'

    def test_gmail_page_title(self):
        self.driver.get('https://mail.google.com')
        assert self.driver.title == 'Gmail'

第二个模块是test_stackoverflow.py,它包含一个检查stackoverflow.com标题的测试:

^{pr2}$

使用空的unittest运行测试可以得到:

$ python setup.py test
running test
running egg_info
...
running build_ext
test_gmail_page_title (test_google.GoogleTests) ... ok
test_google_page_title (test_google.GoogleTests) ... ok
test_so_page_title (test_stackoverflow.StackoverflowTests) ... ok

                                   
Ran 3 tests in 11.657s

OK

迁移到pytest

通过pip安装pytest

$ pip install pytest

pytest支持开箱即用的单元测试,因此我们不需要接触测试,我们可以立即运行它们。试用pytest跑步者:

$ pytest -v
================ test session starts ================
platform darwin   Python 3.6.3, pytest-3.2.5, py-1.5.2, pluggy-0.4.0   /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-47439103, inifile:
collected 3 items                                                                                                                                                                                   

test_google.py::GoogleTests::test_gmail_page_title PASSED
test_google.py::GoogleTests::test_google_page_title PASSED
test_stackoverflow.py::StackoverflowTests::test_so_page_title PASSED

================ 3 passed in 13.81 seconds ================

并行运行测试

这需要pytest-xdist插件用于pytest。通过pip安装:

$ pip install pytest-xdist

插件现在已经安装,但默认情况下不会处于活动状态,因此如果您再次运行pytest,您不会注意到任何差异。使用numprocesses键并行化测试执行。这表示为运行测试而保留的进程数,这里我使用auto值生成与我的计算机具有的cpu相同多的进程:

$ pytest -v  numprocesses=auto
================ test session starts ================
platform darwin   Python 3.6.3, pytest-3.2.5, py-1.5.2, pluggy-0.4.0   /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-47439103, inifile:
plugins: xdist-1.20.1, forked-0.2
[gw0] darwin Python 3.6.3 cwd: /Users/hoefling/projects/private/stackoverflow/so-47439103
[gw1] darwin Python 3.6.3 cwd: /Users/hoefling/projects/private/stackoverflow/so-47439103
[gw2] darwin Python 3.6.3 cwd: /Users/hoefling/projects/private/stackoverflow/so-47439103
[gw3] darwin Python 3.6.3 cwd: /Users/hoefling/projects/private/stackoverflow/so-47439103
[gw0] Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08)    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
[gw1] Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08)    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
[gw2] Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08)    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
[gw3] Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08)    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
gw0 [3] / gw1 [3] / gw2 [3] / gw3 [3]
scheduling tests via LoadScheduling

test_google.py::GoogleTests::test_google_page_title 
test_stackoverflow.py::StackoverflowTests::test_so_page_title 
test_google.py::GoogleTests::test_gmail_page_title 
[gw0] PASSED test_google.py::GoogleTests::test_gmail_page_title 
[gw1] PASSED test_google.py::GoogleTests::test_google_page_title 
[gw2] PASSED test_stackoverflow.py::StackoverflowTests::test_so_page_title 

================ 3 passed in 7.81 seconds ================

您将看到所有三个测试都由同时打开的三个chrome实例并行运行。每个测试在自己的进程中运行,因此它们不会相互干扰。另外,请注意,GoogleTests类的两个测试方法也并行运行,因此这并不局限于不同模块或类中的测试。在

setup.py集成

当我第一次开始迁移到pytest时,我遇到的一个条件是命令python setup.py test应该仍然可以工作,这样我们就不需要记住额外的pytest命令来运行测试,因此我们也不必调整所有的实用程序脚本或在集成服务器上构建作业,下面是更新setup.py脚本的步骤:

  1. 将以下包添加到测试需求:

    from setuptools import setup
    
    setup(
        ...
        tests_require=[
            'pytest',
            'pytest-runner',  # this one is needed to install distutils command for pytest
            'pytest-xdist'
        ],
    )
    
  2. setup.cfg添加别名:

    [aliases]
    test=pytest
    
  3. {cd7>

    [tool:pytest]
    addopts= verbose  numprocesses=auto
    

现在,如果您运行python setup.py test,那么将调用正确的运行程序,xdist插件将在默认情况下处于活动状态。在

附加说明

我个人非常喜欢pytest,因为它提供的不仅仅是简单的测试执行—您可以将测试作为纯函数编写(不需要包装到TestCase类中)、收集测试而不执行它们、轻松地只重新运行最近失败的测试,用不同格式的多个报表以及更多的报表来钩住代码覆盖率度量。请参阅official docs了解更多详细信息,它确实值得花时间阅读!在

相关问题 更多 >