Selenium自行整理【四十二】

泄露秘密 提交于 2019-11-26 13:07:59

7.2.6、单元测试中的概念

        Junit 单元测试的学习,临近收尾,我们来回顾它所包含的几个概念,当然这几个概念来自于 Python的 unittest 的文档中开篇介绍,我发现它同样适用于 Junit 单元测试框架,这几个概念分别是:test fixture, testcase, test suite, test runner,我觉得只有理解了这几个概念,才能真正的理解单元测试的基本原理。

1、  test case

        一个 TestCase 的实例就是一个测试用例。什么是测试用例呢?就是一个完整的测试流程,包括测试前准备环境的搭建(setUp),实现测试过程的代码(run),以及测试后环境的还原(tearDown)。元测试(unit test)的本质也就在这里,一个测试用例是一个完整的测试单元,通过运行这个测试单元,可以对某一个功能进行验证。

2、  test suite

         对一个功能的验证往往是需要多测试用例的,可以把多的测试用例集合在一起执行,这就产生了测试套件 TestSuite 的概念,它用来组装单个测试用例,而且 TestSuite 也可以嵌套 TestSuite。可以通过@SuiteClasses 加载 TestCase 到 TestSuite 中,再返回一个 TestSuite 实例。

3、  test runner

         @RunWith 是来执行测试套件中的测试用例的。测试的结果会保存到 TestResult 实例中,包括运行了多少测试用例,成功了多少,失败了多少,以及每条用例的耗时等信息。

4、  test fixture

         对一个测试用例环境的搭建和销毁,是一个 fixture,通过覆盖 TestCase 的 setUp()和 tearDown()方法来实现。这个有什么用呢?比如说在这个测试用例中需要访问数据库,那么可以在 setUp()中建立数据库连接以及进行一些初始化,在 tearDown()中清除在数据库中产生的数据,然后关闭连接。注意 tearDown 的过程很重要,要为以后的 TestCase 留下一个干净的环境。

 

7.3、用 Junit 编写 web 自动化

        通过学习,我们已经掌握了 Junit 单元测试框架中的一些基本概念与用法,下面要做的就是把单元测试的用例替换为 webdriver 自动化测试脚本,利用 Junit 的原理与特点来运行 web 自动化测试。

创建如下测试包和测试用例:

【源代码如下】:

package com.junit.web;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
    @RunWith(Suite.class)
    @SuiteClasses({
        TestBaidu.class,
        TestYoudao.class
        })
public class TestAll {

}

---------------------------------------------------------------------------------

package com.junit.web;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestBaidu {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "https://www.baidu.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    @Test
    public void testCase() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("kw")).clear();
        driver.findElement(By.id("kw")).sendKeys("junit");
        driver.findElement(By.id("su")).click();
        Thread.sleep(2000);
        String title =driver.getTitle();
        assertEquals(title, "junit_百度搜索");
    }
    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
        }
    }

-----------------------------------------------------------------------------------------------------------------

package com.junit.web;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestYoudao {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "https://www.youdao.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    @Test
    public void testCase() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("query")).clear();
        driver.findElement(By.id("query")).sendKeys("webdriver");
        driver.findElement(By.id("qb")).click();
        Thread.sleep(2000);
        String title =driver.getTitle();
        assertEquals(title, "webdriver - 有道搜索");
    }
    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

7.4、本章小结

         本章从分析 Selenium IDE 所录制导出的 Java/Junit/webdriver 脚本开如学习 Junit 单元测试框架,接下来详细的学习了 Junit 单元测试的概念,如何编写单元测试用例,以及通过@RunWith 方法来组织运行测试用例。最终又后回到原点,学习如何通过 Junit 编写 web 自动化测试。相信通过本章的学习,读者对 Junit以及单元测试已经基本掌握其概念和使用

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!