Web自动化测试项目(三)用例的组织与运行
一、Unittest用例组织
在test_case目录下创建test*.py,组织测试用例
├── test_case
│ ├── __init__.py
│ └── test_login.py
test_login.py
import unittest
from pages.login_page import LoginPageAction
from utils.constants import LOGIN_URL
class TestLoginCase(unittest.TestCase):
def test_login_success(self):
username = 'XXXXX'
password = 'XXXXX'
login_page = LoginPageAction(path=LOGIN_URL)
home_page = login_page.login(username=username, password=password)
user_title = home_page.get_user_title
expect_ret = 'XXXXXX'
self.assertEqual(expect_ret, user_title)
def test_login_success_(self):
username = 'XXXXX'
password = 'XXXXX'
login_page = LoginPageAction(path=LOGIN_URL)
home_page = login_page.login(username=username, password=password)
user_title = home_page.get_user_title
expect_ret = 'XXXXXX'
self.assertEqual(expect_ret, user_title)
if __name__ == '__main__':
unittest.main()
二、组织测试用例
在run_case目录下创建run_login_case.py,用于运行登陆用例集
├── run_case
│ ├── __init__.py
│ └── run_login_case.py
run_login_case.py
import unittest
from test_case.test_login import TestLoginCase
from utils.se_utils import Driver
if __name__ == '__main__':
cases = unittest.TestLoader().loadTestsFromTestCase(TestLoginCase)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(cases)
# 所有用例运行完后关闭浏览器
Driver.quit_driver()
三、添加Python引包路径
创建module_path.py,存放所有引包的绝对路径 放在run_case下,
所有的执行策略均先调用module_path.py,防止找到模块的异常: ModuleNotFoundError: No module named 'XXXXXX'
├── run_case
│ ├── __init__.py
│ └── run_login_case.py
│ └── moudle_path.py
module_path.py
# 添加Python存放模块的路径
import os
import sys
web_auto_test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
pages_dir_path = web_auto_test_path + '/pages'
config_dir_path = web_auto_test_path + '/config'
run_case_dir_path = web_auto_test_path + '/run_case'
test_case_dir_path = web_auto_test_path + '/test_case'
utils_dir_path = web_auto_test_path + '/utils'
sys.path.extend([
web_auto_test_path,
pages_dir_path,
config_dir_path,
run_case_dir_path,
test_case_dir_path,
utils_dir_path,
])
from pprint import pprint
print('python引包路径:')
pprint(sys.path)
run_login_case.py
# 在执行入口添加引包路径
from module_path import *
·
·
·