6月9日
检查内容:
import pytest from app import app, db, User, Product, InventoryLog from flask_login import current_user 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 import time import threading import os # 设置测试数据库路径 TEST_DB_PATH = 'test.db' def init_db(): with app.app_context(): # 确保使用测试数据库 app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{TEST_DB_PATH}' db.create_all() # 创建测试用户 if not User.query.filter_by(username='selenium_user').first(): user = User(username='selenium_user') user.set_password('selenium_pass') db.session.add(user) db.session.commit() def cleanup_db(): # 清理测试数据库 if os.path.exists(TEST_DB_PATH): os.remove(TEST_DB_PATH) def run_flask_app(): init_db() # 初始化数据库 app.run(host='localhost', port=5000, debug=False, use_reloader=False) @pytest.fixture(scope="session", autouse=True) def start_flask(): # 清理旧的测试数据库 cleanup_db() # 在新线程中启动Flask应用 thread = threading.Thread(target=run_flask_app) thread.daemon = True thread.start() # 等待应用启动 time.sleep(2) yield # 测试结束后清理数据库 cleanup_db() @pytest.fixture def client(): app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{TEST_DB_PATH}' with app.test_client() as client: with app.app_context(): db.create_all() yield client db.session.remove() db.drop_all() @pytest.fixture def selenium_driver(): # 使用Firefox浏览器 driver = webdriver.Firefox() driver.implicitly_wait(10) yield driver driver.quit() def test_register(client): print("\n测试用户注册功能...") response = client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }, follow_redirects=True) assert response.status_code == 200 user = User.query.filter_by(username='testuser').first() assert user is not None print("[成功] 用户注册成功") def test_login(client): print("\n测试用户登录功能...") # 先注册用户 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) # 测试登录 response = client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }, follow_redirects=True) assert response.status_code == 200 print("[成功] 用户登录成功") def test_add_product(client): print("\n测试添加商品功能...") # 先注册并登录 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }) # 添加商品 response = client.post('/add_product', data={ 'product_code': 'TEST001', 'name': 'Test Product', 'description': 'This is a test product', 'quantity': 10, 'price': 99.9 }, follow_redirects=True) assert response.status_code == 200 product = Product.query.filter_by(product_code='TEST001').first() assert product is not None assert product.quantity == 10 print("[成功] 商品添加成功") def test_search_product(client): print("\n测试搜索商品功能...") # 先注册并登录 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }) # 添加测试商品 client.post('/add_product', data={ 'product_code': 'TEST001', 'name': 'Test Product', 'description': 'This is a test product', 'quantity': 10, 'price': 99.9 }) # 测试按编号搜索 response = client.get('/search?query=TEST001&type=code') assert response.status_code == 200 assert b'TEST001' in response.data # 测试按名称搜索 response = client.get('/search?query=Test Product&type=name') assert response.status_code == 200 assert b'Test Product' in response.data print("[成功] 搜索功能测试通过") def test_outbound_product(client): print("\n测试商品出库功能...") # 先注册并登录 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }) # 添加测试商品 client.post('/add_product', data={ 'product_code': 'TEST001', 'name': 'Test Product', 'description': 'This is a test product', 'quantity': 10, 'price': 99.9 }) product = Product.query.filter_by(product_code='TEST001').first() # 测试出库 response = client.post(f'/outbound/{product.id}', data={ 'quantity': 5 }, follow_redirects=True) assert response.status_code == 200 # 验证库存更新 product = Product.query.filter_by(product_code='TEST001').first() assert product.quantity == 5 print("[成功] 出库功能测试通过") def test_delete_product(client): print("\n测试删除商品功能...") # 先注册并登录 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }) # 添加测试商品 client.post('/add_product', data={ 'product_code': 'TEST001', 'name': 'Test Product', 'description': 'This is a test product', 'quantity': 10, 'price': 99.9 }) product = Product.query.filter_by(product_code='TEST001').first() # 测试删除 response = client.post(f'/delete_product/{product.id}', follow_redirects=True) assert response.status_code == 200 # 验证商品已删除 product = Product.query.filter_by(product_code='TEST001').first() assert product is None print("[成功] 删除功能测试通过") def test_edit_product(client): print("\n测试编辑商品功能...") # 先注册并登录 client.post('/register', data={ 'username': 'testuser', 'password': 'testpass' }) client.post('/login', data={ 'username': 'testuser', 'password': 'testpass' }) # 添加测试商品 client.post('/add_product', data={ 'product_code': 'TEST001', 'name': 'Test Product', 'description': 'This is a test product', 'quantity': 10, 'price': 99.9 }) product = Product.query.filter_by(product_code='TEST001').first() # 测试编辑 response = client.post(f'/edit_product/{product.id}', data={ 'product_code': 'TEST001', 'name': 'Updated Product', 'description': 'This is an updated product', 'price': 199.9 }, follow_redirects=True) assert response.status_code == 200 # 验证商品已更新 product = Product.query.filter_by(product_code='TEST001').first() assert product.name == 'Updated Product' assert product.description == 'This is an updated product' assert product.price == 199.9 print("[成功] 编辑功能测试通过") # Selenium测试 def test_selenium_workflow(selenium_driver): print("\n开始Selenium端到端测试...") driver = selenium_driver wait = WebDriverWait(driver, 10) # 添加显式等待 try: # 注册新用户 driver.get('http://localhost:5000/register') username_input = wait.until(EC.presence_of_element_located((By.NAME, 'username'))) username_input.send_keys('selenium_user') password_input = driver.find_element(By.NAME, 'password') password_input.send_keys('selenium_pass') driver.find_element(By.TAG_NAME, 'form').submit() # 等待注册完成并跳转到登录页面 time.sleep(1) # 登录 driver.get('http://localhost:5000/login') username_input = wait.until(EC.presence_of_element_located((By.NAME, 'username'))) username_input.send_keys('selenium_user') password_input = driver.find_element(By.NAME, 'password') password_input.send_keys('selenium_pass') driver.find_element(By.TAG_NAME, 'form').submit() # 等待登录完成并跳转到产品列表页面 time.sleep(1) # 添加商品 driver.get('http://localhost:5000/add_product') product_code_input = wait.until(EC.presence_of_element_located((By.NAME, 'product_code'))) product_code_input.send_keys('SEL001') driver.find_element(By.NAME, 'name').send_keys('Selenium Test Product') driver.find_element(By.NAME, 'description').send_keys('This is a Selenium test product') driver.find_element(By.NAME, 'quantity').send_keys('20') driver.find_element(By.NAME, 'price').send_keys('299.9') driver.find_element(By.TAG_NAME, 'form').submit() # 等待添加商品完成 time.sleep(1) # 搜索商品 driver.get('http://localhost:5000/search?query=SEL001&type=code') wait.until(EC.presence_of_element_located((By.XPATH, "//td[contains(text(), 'Selenium Test Product')]"))) # 编辑商品 edit_button = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, '编辑'))) edit_button.click() name_input = wait.until(EC.presence_of_element_located((By.NAME, 'name'))) name_input.clear() name_input.send_keys('Updated Selenium Product') driver.find_element(By.TAG_NAME, 'form').submit() # 等待编辑完成 time.sleep(1) # 出库操作 outbound_button = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, '出库'))) outbound_button.click() quantity_input = wait.until(EC.presence_of_element_located((By.NAME, 'quantity'))) quantity_input.send_keys('5') driver.find_element(By.TAG_NAME, 'form').submit() # 等待出库完成 time.sleep(1) # 删除商品 delete_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), '删除')]"))) delete_button.click() driver.switch_to.alert.accept() print("[成功] Selenium端到端测试完成") except Exception as e: print(f"测试过程中出现错误: {str(e)}") raise
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号