pytest-dev / pytest-rerunfailures

a pytest plugin that re-runs failed tests up to -n times to eliminate flakey failures
Other
369 stars 82 forks source link

Will certain data be sent with changes during parameterization and rerun? #271

Open TestNovice opened 1 week ago

TestNovice commented 1 week ago
import hashlib

import pytest

def encrypt_password(password):
    """
    使用MD5算法对密码进行加密

    :param password: 明文密码字符串
    :return: 加密后的MD5值,以32位小写十六进制字符串表示
    """
    md5_hash = hashlib.md5()
    # 注意:hashlib.md5()接收的是字节类型,因此如果password是字符串,需要先转换为字节
    md5_hash.update(password.encode('utf-8'))
    return md5_hash.hexdigest()

param_data = {
    "username": "admin",
    "password": "123456"
}

@pytest.mark.flaky(reruns=2, reruns_delay=2)
@pytest.mark.parametrize("source_data", [param_data])
def test_sum_data(source_data):
    source_data["password"] = encrypt_password(source_data["password"])
    print("encrypt_data:", source_data)

    assert False

this result: Snipaste_2024-07-05_16-24-49

So, does re running not reinitialize some data?

icemac commented 2 days ago

Hi @TestNovice, inside the test function you are changing the values of your param_data dict. – This is the way Python works. There is no way to reinitialize the parameters of the function.