目录

自动化测试工具

目录

一、什么是自动化测试? what

  • 测试:程序测试是为了发现错误而执行的过程。
  • 自动化测试:就是把手工进行的测试过程,转变成机器自动执行的测试过程。该过程,依旧是为了发现错误而执行。因此自动化测试的关键在于“自动化”三个字。

二、为什么要进行自动化测试? why

  • 测试任务繁重-替代手工测试提高效率
  • 修改程序后,回归测试
  • 长时间的系统稳定性测试
  • 高并发场景的压力测试
  • 避免人工测试时的疏漏或错误
  • 兼容性测试 - 在多个浏览器上测试的重复工作

三、怎么进行自动化测试? how

  1. Selenium
    Selenium是基于WEB应用的验收测试工具集合,直接运行在浏览器中,通过一系列命令来模拟用户操作,然后Selenium可以将这些命令转化成实际的HTTP请求在浏览器中运行 。

  2. Evosuite
    EvoSuite是一种工具,可以自动生成Java类的测试用例集的工具,生成的测试用例均符合Junit的标准,可直接在Junit中运行。但是只能辅助测试,并不能完全取代人工,测试用例的正确与否还需人工判断。

  3. JMeter
    Apache JMeter是Apache组织开发的基于Java的压力测试工具。它最初被设计用于Web应用测试,但后来扩展到其他测试领域。

  4. 介绍链接

四、自动化测试工具:Selenium

(一) python

(二) Selenium

  1. 按照浏览器版本下载对应版本的webdriver

    • IE浏览器驱动:http://docs.seleniumhq.org/download/

    • Firfox浏览器驱动:https://github.com/mozilla/geckodriver/releases

    • Chrome浏览器驱动:http://chromedriver.storage.googleapis.com/index.html

    • Edge浏览器驱动:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

    • 淘宝开源镜像站:https://npm.taobao.org/mirrors

  2. 添加webdriver路径到环境变量

  3. 安装selenium

    • 方式一:命令行运行

      pip install selenium
      
    • 方式二:下载源码包安装

      • (1) 下载源码包 selenium-3.141.0.tar.gz

      • (2) 解压文件,并进入到setup.py文件所在目录

        //解压文件,然后切换cmd当前路径为解压后的selenium-3.141.0下
        cd selenium-3.141.0
        
      • (3) 安装模块

        python setup.py install
        
      • (4) 安装成功可以查看安装模块信息

        pip list
        
  4. 录制测试步骤并导出

    • 火狐浏览器可在附加组件中搜索 Selenium IDE,并添加。
    • 使用 Selenium IDE 录制步骤,导出python脚本,以搜狐邮箱注册为例
# Generated by Selenium IDE
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestDefaultSuite():
    def setup_method(self):
        self.driver = webdriver.Firefox()
        self.vars = {}
    
    def teardown_method(self):
        self.driver.quit()
    
    def test_1(self):
        self.driver.get("https://v4.passport.sohu.com/fe/mailRegister/lettersMail?fr=mail")
        self.driver.set_window_size(1114, 680)
        self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(1) .input").click()
        self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(1) .input").send_keys("hello")
        self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(2) .input").click()
        self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(2) .input").send_keys("123456")

# 添加代码
mytest = TestDefaultSuite()

mytest.setup_method()

mytest.test_1()

mytest.teardown_method()
  1. 安装并引入unittest库,以及BeautifulReport
  • (1) 安装unittest、BeautifulReport库
    pip install unittest
    
    pip install 
    
  • (2) 在脚本中引入unittest、BeautifulReport库,并修改代码
    # Ge# Generated by Selenium IDE
    import time
    import json
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    import unittest
    from BeautifulReport import BeautifulReport as bf
    
    class TestDefaultSuite(unittest.TestCase):
        @classmethod
        def setUpClass(self):
            self.driver = webdriver.Firefox()
            self.vars = {}
    
        @classmethod
        def tearDownClass(self):
            self.driver.quit()
    
        # @BeautifulReport.add_test_img('测试报告.png')
        def test_1(self):
            self.driver.get("https://v4.passport.sohu.com/fe/mailRegister/lettersMail?fr=mail")
            self.driver.set_window_size(1114, 680)
            self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(1) .input").click()
            self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(1) .input").send_keys("hello")
            self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(2) .input").click()
            self.driver.find_element(By.CSS_SELECTOR, ".input-body:nth-child(2) .input").send_keys("123456")
    
    if __name__ == '__main__':
    
        suite = unittest.TestSuite()
        suite.addTest(TestDefaultSuite("test_1"))
        run = bf(suite)
        run.report(filename='MyTestReport',description='我的测试报告')
    
    • 这边还可指定输出报告的位置以及主题:
    • 报告主题样式 theme_default theme_cyan theme_candy theme_memories,样例如下
    run.report(filename='MyTestReport',description='我的测试报告',report_dir="C:\\Users\\userz\\Desktop",theme='theme_cyan' )
    

五、自动化测试工具:EvoSuite

  • Maven工程集成EvoSuite官方教程:http://www.evosuite.org/documentation/tutorial-part-2/
  1. 新建一个普通Maven项目
mvn archetype:generate
  1. 在pom.xml中添加项目依赖和构建依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

    <dependencies>
        <!--JUnit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <!--log4j2日志组件-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!--EvoSuite运行时库-->
        <dependency>
            <groupId>org.evosuite</groupId>
            <artifactId>evosuite-standalone-runtime</artifactId>
            <version>1.0.6</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--构建依赖-EvoSuite插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.evosuite.plugins</groupId>
                <artifactId>evosuite-maven-plugin</artifactId>
                <version>1.0.6</version>
            </plugin>
        </plugins>
    </build>
</project>
  1. 编写代码并调用EvoSuite插件
  • 首次调用EvoSuite插件时,Maven将下载EvoSuite及其依赖项,需要些时间
    mvn evosuite:help                                 #获取帮助信息
    
    mvn evosuite:help -Ddetail=true -Dgoal=generate   #获取有关generate插件的详细信息
    
    mvn evosuite:generate                             #生成测试代码文件,默认在`.evosuite/best-tests`下
    
    mvn -Dcores=4 evosuite:generate                   #并行执行4个EvoSuite作业生成测试代码文件作业
    
    mvn evosuite:info                                 #查看EvoSuite生产状态
    
    mvn evosuite:export                               #输出测试代码文件到src/test/java目录
    
    mvn test                                          #执行测试
    
  1. 生成示例
  • 应用代码

    package com.example;
    
    /**
    * Hello world!
    *
    */
    public class App 
    {
        public static void main( String[] args )
        {
            System.out.println(plus(4,5));
        }
    
        public static int plus (int a, int b){
            return a + b;
        }
    }
    
  • 生成的测试代码

    package com.example;
    
    import org.junit.Test;
    import static org.junit.Assert.*;
    import com.example.App;
    import org.evosuite.runtime.EvoRunner;
    import org.evosuite.runtime.EvoRunnerParameters;
    import org.junit.runner.RunWith;
    
    @RunWith(EvoRunner.class) 
    @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true, useJEE = true) 
    public class App_ESTest extends App_ESTest_scaffolding {
    
        @Test(timeout = 4000)
        public void test0()  throws Throwable  {
            int int0 = App.plus(1, 0);
            assertEquals(1, int0);
        }
    
        @Test(timeout = 4000)
        public void test1()  throws Throwable  {
            int int0 = App.plus(0, (-1074));
            assertEquals((-1074), int0);
        }
    
        @Test(timeout = 4000)
        public void test2()  throws Throwable  {
            int int0 = App.plus(0, 0);
            assertEquals(0, int0);
        }
    
        @Test(timeout = 4000)
        public void test3()  throws Throwable  {
            String[] stringArray0 = new String[0];
            App.main(stringArray0);
            assertEquals(0, stringArray0.length);
        }
    
        @Test(timeout = 4000)
        public void test4()  throws Throwable  {
            App app0 = new App();
        }
    }
    

六、自动化测试工具:Jmeter

  1. 下载并解压
    apache-jmeter-5.2.1.zip

  2. 运行bin目录下的 ApacheJMeter.jar

  3. 添加线程组

    • 右击窗口左侧的测试计划
    • 选择 添加 =》线程(用户) =》线程组
  4. 添加HTTP请求以及HTTP请求头

    • 请求主要信息在HTTP请求下配置,请求头信息在HTTP信息头管理下配置
    • 右击 线程组
    • 添加 =》取样器 =》HTTP请求
    • 添加 =》配置元件 =》HTTP信息头管理
  5. 添加定时器

    • 同步定时器用于同步所有线程,在线程都创建好后,同时发送请求
    • 右击 线程组
    • 添加 =》定时器 =》同步定时器
      • 模拟用户组的数量 0
      • 超时时间以毫秒为单位 0
  6. 添加断言

    • 用响应断言来判断Http请求返回内容是否符合预期
    • 右击 线程组
    • 添加 =》断言 =》响应断言
  7. 添加监听器

    • 这些不同的监听器可以查看不同形式的测试结果
    • 添加 =》监听器 =》察看结果树
    • 添加 =》监听器 =》汇总报告
    • 添加 =》监听器 =》汇总图
    • 添加 =》监听器 =》断言结果
  8. 参考链接

  • JMeter性能测试,完整入门篇:https://blog.csdn.net/u012111923/article/details/80705141
← 返回技术