当前位置:   article > 正文

2022年全国职业院校技能大赛(高职组)“软件测试”赛项单元测试参考答案

2022年全国职业院校技能大赛(高职组)“软件测试”赛项单元测试参考答案

题目2:根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰年,输出2月份正确天数;如为世纪闰年,输出2月份正确天数;不为闰年输出2月份天数。返回结果格式:“year年month月份的天数是days天。”year、month为传入的值,days为判断得到的天数值。其中变量year、month均须为正整数。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断返回期望结果和实际返回是否一致。

  1. import org.junit.Test;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.hamcrest.Matchers.equalTo;
  4. public class MonthDaysTest {
  5. public int getMonthDays(int year, int month) {
  6. if (month < 1 || month > 12) {
  7. throw new IllegalArgumentException("月份输入不正确。");
  8. }
  9. int days;
  10. if (month == 2) {
  11. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  12. days = 29; // 闰年
  13. } else {
  14. days = 28; // 非闰年
  15. }
  16. } else if (month == 4 || month == 6 || month == 9 || month == 11) {
  17. days = 30;
  18. } else {
  19. days = 31;
  20. }
  21. return days;
  22. }
  23. @Test
  24. public void testGetMonthDays() {
  25. MonthDaysTest monthDaysTester = new MonthDaysTest();
  26. assertThat(monthDaysTester.getMonthDays(2023, 6), equalTo(30));
  27. assertThat(monthDaysTester.getMonthDays(2023, 2), equalTo(28));
  28. assertThat(monthDaysTester.getMonthDays(2024, 2), equalTo(29));
  29. assertThat(monthDaysTester.getMonthDays(2023, 13), equalTo(null));
  30. // 其他测试数据...
  31. }
  32. }

题目3:填写快递单时通常需要确定接收人的姓名、手机号和地址。其中要求手机号是 11 位数字字符,地址为字母开头的 10个(含10)以内字母或字母数字共同组成。填写正确则提示“OK”,否则根据实际情况提示“**不符合要求”(**为手机号或地址),退出。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足判定覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断输出文字期望结果值和实际返回值是否一致。

  1. import org.junit.Test;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.hamcrest.Matchers.equalTo;
  4. public class DeliveryFormTest {
  5. public String validateForm(String name, String phoneNumber, String address) {
  6. if (!phoneNumber.matches("\\d{11}")) {
  7. return "手机号不符合要求";
  8. }
  9. if (!address.matches("[a-zA-Z0-9]{1,10}")) {
  10. return "地址不符合要求";
  11. }
  12. // 填写正确
  13. return "OK";
  14. }
  15. @Test
  16. public void testValidateForm() {
  17. DeliveryFormTest formTester = new DeliveryFormTest();
  18. assertThat(formTester.validateForm("John", "12345678900", "Apt 123"), equalTo("OK"));
  19. assertThat(formTester.validateForm("Alice", "1234", "Apt 456"), equalTo("手机号不符合要求"));
  20. assertThat(formTester.validateForm("Bob", "12345678900", "This is a very long address"), equalTo("地址不符合要求"));
  21. // 其他测试数据...
  22. }
  23. }

题目4:输入小写的字符串。如字符串前缀为ab开头,则将前缀ab替换为ef并打印出替换后字符串,返回文字“替换前缀后的字符串为:”和替换后字符串值;如后缀为cd并且前缀不为ab,替换字符串中所有cd为gh并打印出替换后字符串,返回文字“替换cd后的字符串为:”和替换后字符串值;否则全部字母大写输出,返回文字“大写字母的字符串为:”和转换后的字符串值。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足条件覆盖测试,测试类使用参数化测试(@Parameters)完成测试。使用assertEquals判断期望结果值和实际返回值是否一致。

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.junit.runners.Parameterized;
  4. import static org.junit.Assert.assertEquals;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. @RunWith(Parameterized.class)
  8. public class StringTransformationTest {
  9. @Parameterized.Parameter(0)
  10. public String input;
  11. @Parameterized.Parameter(1)
  12. public String expectedOutput;
  13. @Parameterized.Parameters
  14. public static Collection<Object[]> data() {
  15. return Arrays.asList(new Object[][] {
  16. {"abcd", "efgh"}, // 替换前缀
  17. {"xyzcdxyz", "xyzghxyz"}, // 替换后缀
  18. {"HelloWorld", "HELLOWORLD"}, // 大写字母
  19. {"", ""} // 空字符串
  20. });
  21. }
  22. public String transformString(String input) {
  23. if (input.startsWith("ab")) {
  24. return "替换前缀后的字符串为: " + input.replaceFirst("ab", "ef");
  25. } else if (input.endsWith("cd") && !input.startsWith("ab")) {
  26. return "替换cd后的字符串为: " + input.replaceAll("cd", "gh");
  27. } else {
  28. return "大写字母的字符串为: " + input.toUpperCase();
  29. }
  30. }
  31. @Test
  32. public void testTransformString() {
  33. assertEquals(expectedOutput, transformString(input));
  34. }
  35. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号