1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import pytest
class TestUserValidation:
@pytest.mark.parametrize("email,password,expected_result,test_description", [ ("valid@example.com", "ValidPass123!", True, "有效邮箱和密码"), ("invalid-email", "ValidPass123!", False, "无效邮箱格式"), ("valid@example.com", "weak", False, "弱密码"), ("", "ValidPass123!", False, "空邮箱"), ("valid@example.com", "", False, "空密码"), ("test@test.com", "Test123!", True, "边界有效数据"), ("a" * 100 + "@example.com", "ValidPass123!", False, "超长邮箱"), ("valid@example.com", "a" * 200, False, "超长密码") ]) def test_user_registration_validation(self, email, password, expected_result, test_description): """参数化测试用户注册验证""" result = validate_user_registration(email, password) assert result == expected_result, f"测试失败: {test_description}"
@pytest.mark.parametrize("age,expected_category", [ (5, "child"), (17, "teenager"), (18, "adult"), (65, "adult"), (66, "senior"), (0, "infant"), (-1, "invalid"), (150, "invalid") ]) def test_age_categorization(self, age, expected_category): """年龄分类测试""" result = categorize_by_age(age) assert result == expected_category
|