Web端自动化,两种方法执行多条case

本文介绍了在Web自动化测试中执行多条测试用例的两种方法。第一种方法通过Test_suit执行测试套,而第二种方法是在生成测试报告并发送邮件的过程中执行case,前提需导入HTMLTestRunner。关键点在于,每条case结束后,在tearDown方法中不应直接结束清理操作,应使用pass确保后续用例能正常执行,最终在最后一个case结束后调用self.driver.quit()退出驱动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说,直接上源码

目录结构

test_01

 

# -*- coding:utf-8 -*-
# Author:st
# time:2018-6-28
# function: 首页自动化
import time
import unittest
from utils.GetseleniumDriver import GetseleniumDriver
from TestShareLib.CommonIndex import CommonIndex


# 定义一个类,并继承这个类
class test01(unittest.TestCase):

    # 初始化操作
    def setUp(self):
        self.driver = GetseleniumDriver().driver

    # 结束清理操作
    def tearDown(self):
        pass
        # self.driver.quit()

    def test001(self):
        CommonIndex().indent().click()  # 缩进
        time.sleep(2)
        CommonIndex().outpage().click()  # 缩回
        time.sleep(2)
        CommonIndex().peoplepage().click()  # 点人员管理
        time.sleep(2)
        CommonIndex().kaoqinpage().click()  # 点考勤管理
        time.sleep(2)
        CommonIndex().equipmentpage().click()  # 点设备管理
        time.sleep(2)
        CommonIndex().zuzhipage().click()  # 点时段管理
        time.sleep(2)
        CommonIndex().timepage().click()  # 点规则管理
        time.sleep(2)
        CommonIndex().rulepage().click()  # 点击访客管理
        time.sleep(2)
        CommonIndex().powerpage().click()  # 点击刷卡管理
        time.sleep(2)
        CommonIndex().passenger().click()  # 点击配置管理
        time.sleep(2)
        CommonIndex().namepage().click()  # 点击公司名称
        time.sleep(2)
        CommonIndex().homePage().click()  # 点击首页按钮
 
<think>好的,用户现在想了解Spring Boot与MyBatis结合实现数据持久化的方式。我需要综合之前的引用内容来回答这个问题。首先,用户之前的问题是关于SpringBoot和Uniapp的联系,现在转向了SpringBoot与MyBatis的数据持久化,所以需要调整思路,专注于后技术栈。首先,我应该回顾用户提供的引用内容。引用[1]到[5]都提到了Spring Boot和MyBatis的整合,包括依赖配置、MyBatis的特性、以及如何编写测试用。用户可能希望了解具体的整合步骤、配置方法以及使用方式。接下来,我需要组织回答的结构。通常,实现数据持久化包括环境准备、依赖添加、配置、编写实体和Mapper、以及测试等步骤。引用中提到的MyBatis的两种开发模式(注解和XML)也需要详细说明,因为这是MyBatis的核心特性。另外,用户可能对分页插件、多数据源配置感兴趣,这些在引用[5]中有提到。需要确保覆盖这些高级主题,但也要保持基础步骤的清晰。同时,安全性和性能优化可能也是用户关心的点,但根据引用内容,可能暂时需要深入,除非用户有后续问题。需要注意引用标记,比如在提到MyBatis支持定制化SQL时引用[1],在配置application.yml时引用[3]。同时,代码示需要准确,比如添加依赖的部分,确保版本号与引用一致。如,引用[3]中的mybatis-spring-boot-starter版本是3.0.3,而引用[5]提到的是SpringBoot3.4.x和MyBatis3.5.x,可能需要说明版本差异或建议使用较新版本。用户可能存在的疑问包括如何选择注解还是XML方式,如何配置多数据源,或者如何处理事务管理。这些问题应该在回答中提及,或者在相关问题中引导用户进一步思考。 最后,生成的相关问题需要与主题相关,比如分页实现、多数据源配置、动态SQL使用等,确保用户能够深入探索相关主题。同时,确保所有技术术语准确,步骤清晰,便于用户跟随操作。</think>### Spring Boot与MyBatis实现数据持久化的核心方式 Spring Boot与MyBatis结合实现数据持久化,主要通过**简化配置**和**灵活SQL管理**完成数据库操作。以下是具体实现方式及关键步骤: --- #### 一、基础环境搭建 1. **项目初始化** 使用Spring Initializr创建项目时,需添加以下依赖: - `Spring Web`:支持Web应用开发 - `MySQL Driver`:数据库连接驱动 - `MyBatis Framework`:集成MyBatis核心功能[^5] 2. **依赖配置** 在`pom.xml`中明确添加MyBatis及数据库依赖: ```xml <!-- MySQL连接器 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <!-- MyBatis-Spring Boot启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 引用[3]中明确说明了依赖版本要求[^3]。 --- #### 二、数据库配置 1. **application.yml配置** 配置数据源及MyBatis参数: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/db_name?useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml # XML映射文件路径 configuration: map-underscore-to-camel-case: true # 自动驼峰命名转换 ``` 此配置支持XML与注解两种开发模式[^3][^5]。 --- #### 三、数据持久化实现 1. **实体类定义** 使用Lombok简化POJO类: ```java @Data public class User { private Long id; private String username; private String password; } ``` 2. **Mapper接口开发** - **注解模式**:直接在接口方法上编写SQL ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(Long id); } ``` - **XML模式**:分离SQL到XML文件 ```xml <!-- UserMapper.xml --> <select id="getUserByName" resultType="User"> SELECT * FROM user WHERE username = #{name} </select> ``` 引用[5]提到XML模式更适用于复杂SQL场景[^5]。 3. **Service层调用** ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.getUserById(id); } } ``` --- #### 四、高级功能扩展 1. **动态SQL** 使用MyBatis的`<if>`, `<foreach>`标签处理动态条件: ```xml <select id="searchUsers" resultType="User"> SELECT * FROM user <where> <if test="name != null">AND username LIKE #{name}</if> <if test="status != null">AND status = #{status}</if> </where> </select> ``` 引用[1]提到MyBatis支持定制化SQL的特性[^1]。 2. **分页插件** 集成PageHelper实现分页: ```java PageHelper.startPage(1, 10); // 第1页,每页10条 List<User> users = userMapper.getAllUsers(); PageInfo<User> pageInfo = new PageInfo<>(users); ``` 3. **多数据源配置** 通过`@Configuration`定义多个数据源,结合`@Primary`注解指定主库[^5]。 --- #### 五、测试与验证 1. **单元测试** 使用Spring Boot Test验证持久层操作: ```java @SpringBootTest public class UserServiceTest { @Autowired private UserService userService; @Test void testGetUserById() { User user = userService.getUserById(1L); Assertions.assertNotNull(user); } } ``` 引用[4]展示了测试用的编写方法[^4]。 --- ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值