239
社区成员




这个作业属于哪个课程 | FZU_SE_teacherW_4 |
---|---|
这个作业要求在哪里 | 软件工程实践总结&个人技术博客 |
这个作业的目标 | 总结个人技术 |
其他参考文献 | 《构建之法》 |
自动化测试技术用于模拟用户操作进行程序测试,提高测试效率与质量。学习该技术因需应对复杂测试场景,难点在于脚本编写与测试覆盖率优化。
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://www.example.com/login") # 假设这是登录页面的URL
def test_valid_login(self):
try:
username_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
password_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "password"))
)
login_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.ID, "login-button"))
)
username_input.send_keys("testuser")
password_input.send_keys("testpassword")
login_button.click()
# 验证是否成功登录
success_message = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".success-message"))
)
self.assertIn("Login successful", success_message.text)
except Exception as e:
self.fail(f"Login test failed: {e}")
def tearDown(self):
self.driver.quit()
if _name_ == "_main_":
unittest.main()
流程:
代码提交 -> Git触发Jenkins Job -> Jenkins拉取最新代码 -> Selenium执行测试脚本 -> 生成测试报告(Allure)-> 邮件通知测试结果
wait = WebDriverWait(self.driver, 10) # 等待最多10秒
element = wait.until(EC.visibility_of_element_located((By.ID, "dynamic-element")))
element.click()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_dynamic_element(driver):
driver.get('https://www.example.com')
# 使用显式等待
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'dynamic-element'))
)
element.click()
解决步骤:
a. 创建配置文件:在项目根目录下创建一个名为config.ini的文件,内容如下:
[DEFAULT]
base_url = https://www.example.com
[development]
base_url = https://dev.example.com
[testing]
base_url = https://test.example.com
[production]
base_url = https://www.example.com
b. 读取配置文件:在测试用例中,根据当前环境读取相应的基础URL。
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
current_env = 'testing' # 示例:可以通过os.getenv获取
base_url = config[current_env]['base_url']
driver.get(base_url + '/login')
c. 使用环境变量:可以通过设置环境变量来动态选择配置,例如在Jenkins中配置构建参数,或者在本地开发时通过命令行参数传递。
解决步骤:
a. 并行测试:使用 pytest 和 pytest-xdist 插件实现并行测试。
pip install pytest pytest-xdist
然后在执行测试时可以使用 -n
参数指定并行工作线程数:
pytest -n 4
b. 优化测试用例:分析每个测试用例的执行时间,找出性能瓶颈,合并相似的测试用例,减少不必要的页面加载。
c. 使用模拟服务:对于一些依赖后端服务的测试,可以使用 mock 库模拟后端接口,避免每次测试都真实调用外部服务。
from unittest.mock import patch
@patch('requests.get')
def test_api_call(mock_get):
mock_get.return_value.status_code = 200
response = requests.get('https://api.example.com/data')
assert response.status_code == 200
解决步骤:
a. 安装Allure:
pip install allure-pytest
b. 在测试用例中添加注释:
import allure
@allure.feature('User Login Feature')
@allure.story('Successful login with valid credentials')
def test_user_login(driver):
driver.get(base_url + '/login')
with allure.step('Input username and password'):
driver.find_element_by_id('username').send_keys('testuser')
driver.find_element_by_id('password').send_keys('password123')
driver.find_element_by_id('login-button').click()
with allure.step('Verify login success'):
assert "Welcome" in driver.page_source
c. 生成Allure报告:
在测试执行完成后,生成Allure报告,并通过命令行查看:
allure serve <path_to_results>
自动化测试技术极大提升了测试效率与质量,但在实际使用中需注意脚本的稳定性、测试覆盖率和测试环境的一致性。通过合理配置和持续优化,可确保外卖小程序的高质量交付。