前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ChatGPT帮我写Python + pytest + selenium框架

ChatGPT帮我写Python + pytest + selenium框架

原创
作者头像
用户10443079
发布2023-04-24 14:24:52
6.5K0
发布2023-04-24 14:24:52
举报
文章被收录于专栏:测试技术干活

智造喵地址:https://chat.plexpt.com/i/511440

以下是一个简单的Python + pytest + selenium单元测试框架的示例:

代码语言:javascript
复制
import pytest
from selenium import webdriver

class TestWebPage:
    @pytest.fixture(scope="class")
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://www.example.com")
        yield
        self.driver.quit()

    def test_page_title(self, setup_class):
        assert self.driver.title == "Example Domain"

    def test_page_heading(self, setup_class):
        assert self.driver.find_element_by_tag_name("h1").text == "Example Domain"

    def test_page_link(self, setup_class):
        link = self.driver.find_element_by_link_text("More information...")
        assert link.text == "More information..."
        assert link.get_attribute("href") == "https://www.iana.org/domains/example"

if __name__ == '__main__':
    pytest.main()
另外,pytest还提供了很多有用的特性,例如参数化、标记、夹具等,可以更加灵活地组织和运行测试用例。以下是一个使用参数化的示例
import pytest
from selenium import webdriver

class TestWebPage:
    @pytest.fixture(scope="class")
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://www.example.com")

    @pytest.mark.parametrize("link_text,link_url", [("More information...", "https://www.iana.org/domains/example"), ("IANA", "https://www.iana.org/")])
    def test_page_link(self, setup_class, link_text, link_url):
        link = self.driver.find_element_by_link_text(link_text)
        assert link.text == link_text
        assert link.get_attribute("href") == link_url

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

if __name__ == '__main__':
    pytest.main()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档