pytest1-Installation and Getting Started

pytest是python的一种单元测试框架(非自带,需要安装),与python自带的unitest测试框架相比,使用起来更加简洁、效率更高。总之,一句话,pytest优于unitest。

1.安装pytest  (其中-U 指升级原来已经安装的包)查看安装版本

查看安装版本  pip show pytest 或者 pytest --version

 

2.使用pytest的一个demo

# test_open_url.py
from selenium import webdriver


def test_open_url():
    try:
        driver = webdriver.Chrome()
        driver.get("http://www.baidu.com")
        title = driver.title
        print(driver.title)

        assert title == '百度一下,你就知道'   # pytest允许使用python的标准assert语句进行断言处理

    except AssertionError:
        raise AssertionError("断言失败!")
    driver.quit()


def testbaidu():
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    title = driver.title
    print(title)

方式一:进入命令行模式(Terminal):输入pytest 后回车

方式二:(方法二需要设置pycharm执行器为pytest(该执行器支持unitest的代码)pycharm中如何为项目设置默认执行器?

原文地址:https://www.cnblogs.com/wang-mengmeng/p/11435610.html