pytest测试框架实战一

知识点

演练代码

https://github.com/ceshiren/HogwartsLG4

pytest

fixture

  • setup_session
  • setup_class
  • setup
  • teardown
  • teardown_class
  • teardown_session

测试报告allure

课间作业

编写Calc的测试用例,看谁测试用例设计的全面,把测试代码贴到回复里,或者直接贴自己的github文件地址。

课后作业

完善你的Calc的用例,增加更多用例(浮点数相乘bug)

  • 提交你的github测试文件地址
  • 把allure的首页截图到回帖中

参考链接

https://docs.pytest.org/en/stable/index.html

https://docs.qameta.io/allure/

PPT

import pytest

from test_pytest.core.calc import Calc


class TestCalc:
    def setup_class(self):
        self.calc = Calc()

    def setup(self):
        pass

    @pytest.mark.parametrize('a, b, c', [
        [1, 2, 2],
        [-1, -1, 1],
        [1, -1, 1],
        [0, 1, 0],
        [0.2, 1],
        [0.2, 0.2]
    ])
    def test_mul(self, a, b, c):
        assert self.calc.mul(a, b) == c

    @pytest.mark.parametrize('a, b, c', [
        [1, 2, 0.5],
        [2, 1, 2],
        [-1, 1, 1],
        [1, -1],
        [0.5, 1],
        [1, 0.5],
        [0, -1, 0],
    ])
    def test_div(self, a, b, c):
        assert self.calc.div(a, b) == c

    def test_div1(self):
        """除0, 报错除0错误"""
        try:
            self.calc.div(0, 2)
        except ZeroDivisionError as e:
            assert True

1 个赞
import pytest

from test_pytest.src.Calc import Calc


class TestCalc:
    def setup_class(self):
        self.calc = Calc()

    """
    正常值、边界值、错误等
    """
    @pytest.mark.parametrize("a, b", [
        (1, 2),
        (3, -4),
        (0, 0),
        ('a', 'b'),
        ('*', ','),
        (999999999, 9999999999)
    ])
    def test_mul(self, a, b):
        assert self.calc.mul(a, b) == a * b

    @pytest.mark.parametrize("a, b", [
        (1, 2),
        (0, 3),
        (10, 0),
        (10, 1),
        (11, -1),
        ('a', 'b'),
        ('*', ',')
    ])

    def test_div(self, a, b):
        assert self.calc.div(a, b) == a / b

import pytest

from test_pytest.core.calc import Calc


class TestCalc:
    def setup_class(self):
        self.calc = Calc()

    def setup(self):
        pass

    @pytest.mark.parametrize('a, b, c', [
        [0, 1, 0],
        [10, 2, 5],
        [5, 2, 2.5],
        [3, 3, 1],
        [10, 3, 3.33],
        [1, 0.56, 1.79],
        [1, 0, "错误"],
        ['a', 1, "错误"],
        [2, "", "错误"],
        [3, -1, -3],
        [-4, -2, 2]
    ])
    def test_div(self, a, b, c):
        """除法要求四舍五入,保留2位小数"""
        assert self.calc.div(a, b) == c
        # assert calc.mul(-1, -1) == 1
        # assert calc.mul(1, -1) == 1
1 个赞
import pytest

from test_pytest.core.calc import Calc


class TestCalc:
    def setup_class(self):
        self.calc = Calc()

    @pytest.mark.parametrize('a, b, c', [
        [1, 2, 2],
        [-1, -1, 1],
        [1, -1, 1],
        ["a", -1, 1],
        [" ", -1, 1],
        [None, -1, 1],
    ])
    def test_mul(self, a, b, c):
        assert self.calc.mul(a, b) == c

    @pytest.mark.parametrize('a, b, c', [
        [4, 2, 2],
        [-2, -1, 2],
        [1, 0, 1],
        [0, 1, 0],
        [0, "", 0],
        [0, " ", 0],
        [0, "string", 0],
        [0, None, 0],
    ])
    def test_div(self, a, b, c):
        assert self.calc.div(a, b) == c

from test_pytest.core.calc import Calc


class TestCalc:
    def setup_class(self):
        self.calc = Calc()

    def setup(self):
        pass

    @pytest.mark.parametrize('a, b, c', [
        [1, 2, 2],
        [-1, -1, 1],
        [1, -1, 1],
        [0, 1, 0],
        [0.2, 1],
        [0.2, 0.2]
    ])
    def test_mul(self, a, b, c):
        assert self.calc.mul(a, b) == c

    @pytest.mark.parametrize('a, b, c', [
        [1, 2, 0.5],
        [2, 1, 2],
        [-1, 1, 1],
        [1, -1],
        [0.5, 1],
        [1, 0.5],
        [0, -1, 0],
    ])
    def test_div(self, a, b, c):
        assert self.calc.div(a, b) == c

    def test_div1(self):
        """除0, 报错除0错误"""
        try:
            self.calc.div(0, 2)
        except ZeroDivisionError as e:
            assert True

class TestCalc:
def setup_class(self):
self.calc = Calc()

def setup(self):
    pass

@pytest.mark.parametrize('a, b, c', [
    [0, 0, 0],
    [1.1, 1.1, 1.1],
    [-1, -1, 1], [-1, -1, -1]
    ["-1", "-1", "1"], ["a", "b", "c"], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [";", ";", "#"]
])
def test_mul(self, a, b, c):
    assert self.calc.mul(a, b) == c
    # assert calc.mul(-1, -1) == 1
    # assert calc.mul(1, -1) == 1

@pytest.mark.parametrize('d, e, f', [
    [0, 0, 0],
    [-1, -1, 1], [1.1, 1.1, 1.1], ["-1", "-1", "1"], ["a", "b", "c"], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [";", ";", "#"]
    [1, -1, 1]
])
def test_div(self, d, e, f):
    try:
        assert self.calc.mul(d, e) == f
    except:
        print("can't test")
 # 测试除数不能为0,
# 测试数据的数据类型为字符串
# 数据类型为float
# 两个数都为None
# 有一个为None
@pytest.mark.parametrize("a, b", [(1, 2), (4, 0), ("aa", "bb"), (1.2, 4.5), (None, None), (1, None)])
def test_div(self, a, b):
    assert self.calc.div(a, b) == a/b


老师求帮看看

直接打开 HogwartsLG4, 不要从上层目录打开

我的代码运行过程总是报错

Testing started at 9:34 下午 …
/usr/local/bin/python3.8 “/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py” --target test_calc.py::TestCalc.test_procaess
Traceback (most recent call last):
File “/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py”, line 24, in
if not get_plugin_manager().hasplugin(“pytest-teamcity”):
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pytest/config/init.py”, line 289, in get_plugin_manager
return get_config().pluginmanager
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pytest/config/init.py”, line 275, in get_config
pluginmanager.import_plugin(spec)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pytest/config/init.py”, line 689, in import_plugin
import(importspec)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pytest/helpconfig.py”, line 96, in
@pytest.hookimpl(hookwrapper=True)
AttributeError: module ‘pytest’ has no attribute ‘hookimpl’

Process finished with exit code 1

Empty suite

Empty suite

不要把自己的包命名为pytest或者其他的常用的包名,这会导致你的import pytest的时候,导入的是你自己的包,而不是默认的第三方库。命名冲突

https://github.com/cuiqingliang/cql_horg.git

https://github.com/LQZuan/Hogwarts_Pytest_AllureReport.git


1 个赞


https://github.com/dmingzhu/hogwarts_demo.git

https://github.com/Just-zhs/Hogwarts_AAA

https://github.com/zy-87/hogwarts_lg4/tree/master/pytest_live_1

20201027作业:

https://github.com/Bright1116/AllenLG4_Python

Pytest测试框架实战一作业——张涛

GitHub地址:

https://github.com/InsaneLoafer/HogwartsLG4_ZhangTao.git

Allure报告截图: