WEEK15

【声明!】
本博客内容仅为学校要求提交的内容,所有包含着此声明的博文或多或少为学业要求的产品,与个人所想展现的想法无关,所有含此声明的博文也将在毕业后存档删除。
若因此行为而给大家带来了不快,本人深表歉意。 


 

个人博客25


 

前景提要:
在软件测试章节中中,我们介绍了不少VSTS的 软件测试工具,请使用一些其他平台上的测试工具,并写博客介绍如何在你的项目中具体使用。

但是我们还没动【我超诚实的】又由于我们是不联网记录型APP,所一我们也不需要过多测试,所以在预演之后我决定向大家介绍Junit,这是一个基于Java的测试插件。以下正文。

1Junit 是什么?

  JUnit是一个Java语言的单元测试框架。它由Kent BeckErich Gamma建立,逐渐成为源于Kent BecksUnitxUnit家族中最为成功的一个JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。

  注意:Junit 测试也是程序员测试,即所谓的白盒测试,它需要程序员知道被测试的代码如何完成功能,以及完成什么样的功能

 

2Junit 能做什么?

  我们知道 Junit 是一个单元测试框架,那么使用 Junit 能让我们快速的完成单元测试。

  通常我们写完代码想要测试这段代码的正确性,那么必须新建一个类,然后创建一个 main() 方法,然后编写测试代码。如果需要测试的代码很多呢?那么要么就会建很多main() 方法来测试,要么将其全部写在一个 main() 方法里面。这也会大大的增加测试的复杂度,降低程序员的测试积极性。而 Junit 能很好的解决这个问题,简化单元测试,写一点测一点,在编写以后的代码中如果发现问题可以较快的追踪到问题的原因,减小回归错误的纠错难度。

 

3Junit 的用法

  一、首先下载 Junit jar 包,这里给两个版本的百度云下载地址:

  ①、Junit 4.12版本    链接:http://pan.baidu.com/s/1c2cRqdM 密码:hfix

  ②、Junit 4.8 版本     链接:http://pan.baidu.com/s/1qY8WVGK 密码:ma2u

  【但是实际上很多eclipse社区版都已经自带了这个功能,一般不用下载而且现在出了Junit5,使用更加顺手

  二、下载完成之后,在项目中将 下载的 jar 包放进去,然后右键,Build--->Add Build Path  即可。

   

   如果你是用 eclipse 开发,也可以不用下载那些jar包,eclipse内部集成了,我们只需要引入即可:

    ①、选中项目,右键Build--->Add Library

      

    ②、弹出来的界面,选中 JUnit,点击 next

      

     ③、选中 Junit 的版本,一般我们都用 4.0 以上的。点击 Finish

       

  三、我们先看下面这个例子,看一下 Junit 的用法

  ①、编写代码(需要测试的类)

package com.example.tests;

 

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;                            //Server用来启动浏览器,接受Client端的请求

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

import com.thoughtworks.selenium.*;           //client,Client端用来向Server端发送请求

 

public class 1 {

  private WebDriver driver;

  private String baseUrl;

  private boolean acceptNextAlert = true;

  private StringBuffer verificationErrors = new StringBuffer();

 

  @Before

  public void setUp() throws Exception {

    driver = new FirefoxDriver();

    baseUrl = "https://www.katalon.com/";

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

 

  @Test                              //案例一

  public void test1() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.id("search-icon-legacy")).click();

    // ERROR: Caught exception [ERROR: Unsupported command [doubleClick | xpath=(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2] | ]]

    // ERROR: Caught exception [ERROR: Unsupported command [doubleClick | xpath=(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2] | ]]

    // ERROR: Caught exception [ERROR: Unsupported command [doubleClick | xpath=(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2] | ]]

    // ERROR: Caught exception [ERROR: Unsupported command [doubleClick | xpath=(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2] | ]]

    // ERROR: Caught exception [ERROR: Unsupported command [doubleClick | xpath=(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2] | ]]

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2]")).click();

  }

  @Test                             //案例二

  public void test2() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.name("search_query")).click();

    driver.findElement(By.name("search_query")).click();

    driver.findElement(By.name("search_query")).clear();

    driver.findElement(By.name("search_query")).sendKeys("......");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='过滤'])[1]/following::ytd-background-promo-renderer[1]")).click();

  }

  @Test                              //案例三

  public void test3() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.name("search_query")).click();

    driver.findElement(By.name("search_query")).clear();

    driver.findElement(By.name("search_query")).sendKeys("ninja");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2]")).click();

  }

  @Test                              //案例四

  public void test4() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.name("search_query")).click();

    driver.findElement(By.name("search_query")).clear();

    driver.findElement(By.name("search_query")).sendKeys("shroud");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='跳过导航'])[1]/following::yt-icon[2]")).click();

  }

  @Test                              //案例五

  public void test5() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.id("img")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='语言:'])[1]/following::yt-formatted-string[1]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='选择您的语言'])[2]/following::paper-item[11]")).click();

  }

@Test                              //案例六

  public void test6() throws Exception {

    driver.get("https://www.youtube.com/watch?v=Xp7t9fWz62Q");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Switch camera'])[1]/following::button[1]")).click();

  }

 @Test                              //案例七

  public void test7() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Scroll for details'])[1]/following::button[8]")).click();

  }

@Test                              //案例八

  public void test8() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='滚动浏览详情'])[1]/following::button[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='关闭'])[2]/following::div[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='HD'])[2]/following::div[6]")).click();

  }

  @Test                              //案例九

  public void test9() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='滚动浏览详情'])[1]/following::button[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='画质'])[1]/following::div[1]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='HD'])[1]/following::div[3]")).click();

  }

 @Test                              //案例十

  public void test10() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='滚动浏览详情'])[1]/following::button[6]")).click();

  }

  @Test                              //案例十一

  public void test11() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='画质'])[1]/following::button[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='画质'])[1]/following::div[31]")).click();

  }

  @Test                              //案例十二

  public void test12() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='画质'])[1]/following::button[2]")).click();

  }

 @Test                              //案例十三

  public void test13() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='滚动浏览详情'])[1]/following::button[1]")).click();

  }

  @Test                              //案例十四

 public void test14() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='分享'])[3]/preceding::yt-icon-button[1]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='电子邮件'])[1]/following::yt-icon[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='电子邮件'])[1]/following::yt-icon[2]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Mix'])[1]/following::yt-icon[1]")).click();

  }

 @Test                              //案例十五

  public void test15() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='http://bit.ly/SubscribeNinja'])[1]/preceding::span[1]")).click();

  }

  @Test                              //案例十六

  public void test16() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='分享'])[3]/following::yt-formatted-string[1]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='保存到…'])[1]/following::yt-formatted-string[1]")).click();

  }

 @Test                              //案例十七

  public void test17() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.id("simplebox-placeholder")).click();

    driver.findElement(By.id("contenteditable-root")).click();

    driver.findElement(By.id("contenteditable-root")).click();

    // ERROR: Caught exception [unknown command [editContent]]

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='取消'])[2]/following::yt-formatted-string[1]")).click();

  }

  @Test                              //案例十八

   public void test18() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='在“Ninja”商店购物'])[1]/following::img[1]")).click();

    // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]]

    // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | index=2 | ]]

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='YES, SEND IT!'])[1]/following::span[3]")).click();

  }

  @Test                              //案例十九

  public void test19() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU&t=7s");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='自动播放'])[2]/following::img[1]")).click();

  }

 @Test                              //案例二十

  public void test20() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.id("guide-icon")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='游戏'])[3]/following::span[2]")).click();

  }

  @Test                              //案例二十一

  public void test21() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.id("guide-icon")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='YouTube Premium'])[1]/following::span[2]")).click();

  }

  @Test                              //案例二十二

  public void test22() throws Exception {

    driver.get("https://www.youtube.com/");

    driver.findElement(By.xpath("(//button[@id='button']/yt-icon)[5]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='创建'])[1]/following::paper-item[1]")).click();

    driver.findElement(By.id("upload-prompt-box")).click();

  }

 @Test                              //案例二十三

  public void test23() throws Exception {

    driver.get("https://www.youtube.com/watch?v=zSNEh8zMoVU");

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='保存'])[1]/following::img[1]")).click();

    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='上传的视频'])[1]/preceding::div[33]")).click();

  }

  @After

  public void tearDown() throws Exception {

    driver.quit();

    String verificationErrorString = verificationErrors.toString();

    if (!"".equals(verificationErrorString)) {

      fail(verificationErrorString);

    }

  }

 

  private boolean isElementPresent(By by) {

    try {

      driver.findElement(by);

      return true;

    } catch (NoSuchElementException e) {

      return false;

    }

  }

 

  private boolean isAlertPresent() {

    try {

      driver.switchTo().alert();

      return true;

    } catch (NoAlertPresentException e) {

      return false;

    }

  }

 

  private String closeAlertAndGetItsText() {

    try {

      Alert alert = driver.switchTo().alert();

      String alertText = alert.getText();

      if (acceptNextAlert) {

        alert.accept();

      } else {

        alert.dismiss();

      }

      return alertText;

    } finally {

      acceptNextAlert = true;

    }

  }

}
View Code

 

  那么我们可以看到,不用 Junit 只能写在 main()方法中,通过运行结果来判断测试结果是否正确。这里需要测试的只有两个方法,如果有很多方法,那么测试代码就会变得很混乱。

 

  二、使用 Junit(看不懂 Assert.assertEquals()方法没关系,可以自己写 if()语句来判断)


 
 如何运行
Junit呢?鼠标放在需要测试的方法中,右键,Run As ---->JUnit Test

 

  结果出现如下的绿色横条,则测试通过,红色横条,则测试失败

  

 

 

那么由上面可以看到,使用 Junit 不需要创建 main() 方法,而且每个测试方法一一对应,逻辑特别清晰。可能有读者会问,这样写代码量也并不会减少啊,那么你接着往下看:

 

首先介绍 Junit 的几种类似于 @Test 的注解:

  1.@Test: 测试方法

    a)(expected=XXException.class)如果程序的异常和XXException.class一样,则测试通过
    b)(timeout=100)如果程序的执行能在100毫秒之内完成,则测试通过

  2.@Ignore: 被忽略的测试方法:加上之后,暂时不运行此段代码

  3.@Before: 每一个测试方法之前运行

  4.@After: 每一个测试方法之后运行

  5.@BeforeClass: 方法必须必须要是静态方法(static 声明),所有测试开始之前运行,注意区分before,是所有测试方法

  6.@AfterClass: 方法必须要是静态方法(static 声明),所有测试结束之后运行,注意区分 @After

 

-注意:编写测试类的原则: 

    ①测试方法上必须使用@Test进行修饰

        ②测试方法必须使用public void 进行修饰,不能带任何的参数

        ③新建一个源代码目录来存放我们的测试代码,即将测试代码和项目业务代码分开

        ④测试类所在的包名应该和被测试类所在的包名保持一致

        ⑤测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖

        ⑥测试类使用Test作为类名的后缀(不是必须)

        ⑦测试方法使用test作为方法名的前缀(不是必须)

转载于:https://www.cnblogs.com/von-wunder-er/p/10981362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值