python pytest - 从外部

时间:2017-05-18 06:30:59

标签: python pytest

我是pytest框架的新手,我想知道是否有一个标准方法我的pytests从其他脚本接收参数。我想最常见的做法是写@pytest.fixture看起来像:

@pytest.fixture
def param(request):
device = request.config.getoption("--parameter")
if parameter == 'A':
    return value_a
else:
    return default_value

使用命令py.test --parameter=value_a运行我的测试(此命令由其他一些脚本执行)

但是如果我的测试需要很多参数,比方说10,那就是一个长命令。所以我在问这样的情况下标准方法是什么 - 我是否提供某种带参数的xml文件或序列化字典以及我的夹具来从中获取参数。

另外我的其他脚本将如何知道我的测试提供什么样的参数 - 我的conftest.py中是否有test_parameters配置文件或一些硬编码数据,其中包含参数的信息,或者我应该使用它们通过from inspect import signature读取测试的签名。

EDITED

以下是我的测试样本

class TestH5Client:
    # runs before everything else in the class
    def setup_class(cls, ip="11.111.111.111",
                    browserType="Chrome",
                    port="4444",
                    client_url="https://somelink.com/",
                    username="some_username",
                    hpassword="some_pass"):

        cls.driver = get_remote_webdriver(ip, port, browserType)
        cls.driver.implicitly_wait(60)
        cls.client_url = client_url
        cls.username = username
        cls.password = password

    def teardown_class(cls):
        cls.driver.quit()

    def test_login_logout(self):
        # opening web_client
        self.driver.set_page_load_timeout(60)
        self.driver.get(self.h5_client_url)

        # opening web_client log-in window
        self.driver.set_page_load_timeout(60)
        self.driver.find_element_by_css_selector("div.gettingStarted p:nth-child(4) a:nth-child(1)").click()

        # log in into the client
        self.driver.find_element_by_id("username").send_keys(self.h5_username)
        self.driver.find_element_by_id("password").send_keys(self.h5_password)
        self.driver.set_page_load_timeout(60)
        self.driver.find_element_by_id("submit").click()

        # clicking on the app_menu so the logout link appears
        self.driver.implicitly_wait(60)
        self.driver.find_element_by_id("action-userMenu").click()

        # clicking on the logout link
        self.driver.implicitly_wait(60)
        self.driver.find_element_by_css_selector("#vui-actions-menu li:nth-child(3)").click()

        assert "Login" in self.driver.title

    def test_open_welcome_page(self):
        """fast selenium test for local testing"""
        self.driver.set_page_load_timeout(20)
        self.driver.get(self.h5_client_url)
        assert "Welcome" in self.driver.title

    def test_selenium_fail(self):
        """quick test of selenium failure for local testing"""
        self.driver.set_page_load_timeout(20)
        self.driver.get(self.h5_client_url)
        assert "NotInHere" in self.driver.title

我需要所有这些参数由提供测试参数框架的外部python提供。我需要知道这个框架应该如何获取参数的名称以及如何使用这些参数运行这些测试。

1 个答案:

答案 0 :(得分:1)

根据评论中的答案,数据每周都会更改。

所以我建议传递一个参数,即指向其余信息的文件路径。

使用您已经在其他地方使用的任何解析机制 - XML,Json,无论如何 - 或者使用像配置文件读取器这样简单的东西。

创建一个具有会话范围的工具(如this),并为其提供一些合理的默认值,或者如果它没有获得有效的参数文件则会严重失败。

相关问题