测试步骤
PnxTest比较重要的一个概念就是"测试步骤", 测试步骤是测试用例的最小基本单位,测试用例有一个或多个测试步骤组合而成。 设计"测试步骤"的主要目的: 1)复用 -- 可以方便地对所有步骤进行各种复用和组合 2)追踪 -- 所有的@Step方法都会被PnxTest追踪并记录到执行日志中
创建测试步骤仓库:
@Repository
public class UserServiceRepository{
@Step("使用用户名<{0}>和密码<{1}>登录")
public String loginWithUserNameAndPassword(String userName, String password){
//...
//return token
}
@Step("使用手机号和密码短信验证码登录")
public void loginWithMobile(String mobilePhone, String smsCode){
//...
}
@Step("使用token获取用户信息")
public UserInfo getUserInfoByToken(String token){
//...
//return userInfo
}
@Step(value="登出系统")
public boolean logout(){
//...
return true;
}
}
@Repository
public class MessageServiceRepository{
@Step(value="发送短信验证码", timeThreshold=3)
public boolean sendSmsCode(String mobilePhone){
//...
return true;
}
}
测试模块中使用@Steps:
@Controller(module="用户服务模块")
public class UserServiceTest{
@Steps
UserServiceRepository userService;
@Steps
MessageServiceRepository messageService;
@Test
@DisplayName("使用合法的用户名和密码应该能正确的登录系统")
void TestLoginUsingPassword(){
String token = userService.loginWithUserNameAndPassword("nicolas", "P@ssword1");
PnxAssert.assertThat(token).isNotNull();
}
@Test
@DisplayName("手机短信验证吗登录")
void TestLoginUsingMobile(){
String toMobile = "18212345678";
messageService.sendSmsCode(toMobile);
String token = userService.loginWithMobile(toMobile, "654321");
PnxAssert.assertThat(token).isNotNull();
}
@Test
@DisplayName("获取的用户信息应不包含用户密码等敏感信息)
void TestRetrievingUserInfo(){
String token = userService.loginWithUserNameAndPassword("nicolas", "P@ssword1");
PnxAssert.assertThat(token).isNotNull();
UserInfo currentUserInfo = userService.getUserInfoById(token);
PnxAssert.assertThat(currentUserInfo.getName()).isEqualTo("nicolas");
PnxAssert.assertThat(currentUserInfo.getPassword()).isNull();
}
@Test
@DisplayName("场景:登录,获取信息,登出")
void TestScenario(){
String token = userService.loginWithUserNameAndPassword("nicolas", "P@ssword1");
userService.getUserInfo(token);
userService.logout();
}
}
特别说明:
- @Step所在的类需要使用@Repository标注
- @Repository中也可以调用@Steps