当前位置:网站首页>Selenium3 automatic test practice (5)

Selenium3 automatic test practice (5)

2022-06-13 12:14:00 Sixiaoyou


1.Python A simple example

import pytest


def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5


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

2. Assertion

# function :  Used to calculate a And b Additive sum 
def add(a, b):
    return a + b

# function :  Used to judge prime numbers 
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

# Test equality 
def test_add_1():
    assert add(3, 4) == 7

# The tests are not equal 
def test_add_2():
    assert add(17, 22) != 50

# Test less than or equal to 
def test_add_3():
    assert add(17, 22) <= 50

# Test greater than or equal to 
def test_add_4():
    assert add(17, 22) >= 38

# The test contains 
def test_in():
    a = "hello"
    b = "he"
    assert b in a

# Test does not contain 
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

# Judge whether it is True
def test_true_1():
    assert is_prime(13)

# Judge whether it is True
def test_true_2():
    assert is_prime(7) is True

# Judge whether it is not True
def test_true_3():
    assert not is_prime(4)

# Judge whether it is not True
def test_true_4():
    assert is_prime(6) is not True

# Judge whether it is False
def test_fasle_1():
    assert is_prime(8) is False

3.Fixture

import pytest


def multiply(a,b):
    return a * b

def setup_module(module):
    print("setup_module===================>")

def teardown_module(module):
    print("teardown_module==================>")

def setup_function(function):
    print("setup_function=================>")

def teardown_function(function):
    print("teardown_function===============>")

def setup():
    print("setup==================>")

def teardown():
    print("teardown==================>")

# ============== The test case ==================
def test_multiply_3_4():
    print('test_numbers_3_4')
    assert multiply(3,4) == 12

def test_multiply_a_3():
    print('test_strings_a_3')
    assert multiply('a',3) == 'aaa'
# Function function 
def multiply(a, b):
    return a * b

class TestMultiply:
    # ======Fixture======
    @classmethod
    def setup_class(cls):
        print("setup_class=============>")

    @classmethod
    def teardown_class(cls):
        print("teardown_class===============>")

    def setup_method(self,method):
        print("setup_method-------->>")

    def teardown_method(self,method):
        print("teardown_method-->>")

    def setup(self):
        print("setup----->")

    def teardown(self):
        print("teardown----->")

    # =========== The test case ===========
    def test_numbers_5_6(self):
        print('test_numbers_5_6')
        assert multiply(5,6) == 30

    def test_strings_b_2(self):
        print('test_strings_b_2')
        assert multiply('b',2) == 'bb'

4. A parameterized

import pytest
import math

#pytest  A parameterized 
@pytest.mark.parametrize(
    "base, exponent, expected",
    [(2, 2, 4),
     (2, 3, 8),
     (1, 9, 1),
     (0, 9, 0)],
    ids = ["case1","case2","case3","case4"]
    )
def test_pow(base,exponent,expected):
    assert math.pow(base, exponent) == expected

5.conftest.py

import pytest

@pytest.fixture()
def test_url():
    return "http://www.baidu.com"

def test_baidu(test_url):
    print(test_url)

6.pytest-rerunfailures

pytest -v test_rerunfailures.py --reruns 3

def test_fail_rerun():
    assert 2 + 2 == 5

7.pytest-parallel

pytest -q test_parallel.py --test-per-worker auto

from time import sleep

def test_01():
    sleep(3)

def test_02():
    sleep(5)

def test_03():
    sleep(6)
原网站

版权声明
本文为[Sixiaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131211594407.html