持续集成
(Continuous integration,简称 CI)是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译、发布、自动化测试)来验证,从而尽快地发现集成错误。许多团队发现这个过程可以大大减少集成的问题,让团队能够更快地开发内聚的软件。
Jenkins
Jenkins 官方网站:https://jenkins.io/
Jenkins 主要用于监视执行重复工作,如建立一个软件项目或工作运行的计划任务:
- 不断地进行项目的构建/测试软件:就像 CruiseControl 或 DamageControl。概括地说,Jenkins 提供了一个易于使用的所谓的持续集成系统,使开发人员更容易修改整合到项目中,并使它更容易为用户获得一个新的版本。自动连续生成提高了生产效率。
- 监控外部运行的作业:如计划任务作业和 Qrocmail 的工作,即使是那些在远程机器上运行的计划任务。 Jenkins 生成这些日志并且很容易让你注意到错误的出现。
Jenkins 构建任务
构建项目类型
点击 Jenkins 首页 创建一个新任务,选择第一个:构建一个自由风格的软件项目,输入项目名称:Python Test Project,点击 OK 按钮。
Jenkins 提供六种类型的任务:
- 构建一个自由风格的软件项目
这是Jenkins的主要功能。Jenkins 会结合任何 SCM 和任何构建系统来构建项目, 甚至可以构建软件以外的系统。
- Pipeline
Orchestrates long-running activities that can span multiple build slaves. Suitable for building pipelines (formerly known as workflows) and/or organizing complex activities that do not easily fit in free-style job type.
- 构建一个多配置项目
适用于多配置项目,例如多环境测试、平台指定构建,等等。
- GitHub Organization
Scans a GitHub organization (or user account) for all repositories matching some defined markers. 主要针对由 Github 托管的项目。
- Multibranch Pipeline
Creates a set of Pipeline projects according to detected branches in one SCM repository. 根据一个SCM存储库中检测到的分支创建一组 Pipeline 项目。
- 文件夹
创建一个可以嵌套存储的容器。利用它可以进行分组。视图仅仅是一个过滤器,而文件夹则是一个独立的命名空间,因此可以有多个相同名称的内容,只要它们在不同的文件夹里即可。
构建 Windows 测试任务
假设,我们有一个 Python 编写的测试脚本 py_tests.py,位于电脑 D盘 根目录,内容如下:
import unittest, time, subprocess
from selenium import webdriver
class TestClass(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.base_url = "http://www.baidu.com"
def tearDown(self):
time.sleep(2)
self.driver.quit()
def test_case1(self):
self.driver.get(self.base_url)
search_input = self.driver.find_element_by_name("wd")
search_input.send_keys("appblog.cn")
search_input.submit()
time.sleep(5)
def test_case2(self):
self.driver.get(self.base_url)
search_input = self.driver.find_element_by_name("wd")
search_input.send_keys("APP开发技术博客")
search_input.submit()
time.sleep(5)
if __name__ == '__main__':
unittest.main()
注:执行此Python脚本需要对应浏览器驱动支持,当Selenium升级到3.0之后,对不同的浏览器驱动进行了规范。如果想使用Selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动。下载chromedriver.exe或geckodriver.exe,然后放在与python.exe同一级目录下
各浏览器下载地址:
- Firefox浏览器驱动:[geckodriver](https://github.com/mozilla/geckodriver/releases geckodriver)
- Chrome浏览器驱动:[chromedriver](https://sites.google.com/a/chromium.org/chromedriver/home chromedriver), [taobao备用地址](https://npm.taobao.org/mirrors/chromedriver taobao备用地址)
- IE浏览器驱动:[IEDriverServer](http://selenium-release.storage.googleapis.com/index.html IEDriverServer)
- Edge浏览器驱动:[MicrosoftWebDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver MicrosoftWebDriver)
- Opera浏览器驱动:[operadriver](https://github.com/operasoftware/operachromiumdriver/releases operadriver)
- PhantomJS浏览器驱动:[phantomjs](http://phantomjs.org/ phantomjs)
在 Windows 下打开命令提示符(CMD)执行该测试用例:
> python d:/py_tests.py
DevTools listening on ws://127.0.0.1:12320/devtools/browser/c5d02f9a-6484-4c80-8f76-21080cd2f87b
.
DevTools listening on ws://127.0.0.1:12815/devtools/browser/c737abb2-f4ef-4a50-8bf9-1d787b2de288
.
----------------------------------------------------------------------
Ran 2 tests in 32.941s
OK
>
接下来回到 Jenkins 的配置过程中:
1、添加项目的描述:Selenium 自动化测试项目。
2、剩下的选项都不要管,拖到页面底部,构建 选项。选择 Execute Windows batch command 选项,执行Windows批处理命令。
3、输入在 Windows 命令提示符下所输的命令 python d:/py_tests.py。点击 保存。
一个极简的,基于 Windows 系统的,Python 脚本测试的 持续集成项目就创建完成了。
Jenkins 配置构建执行状态
运行构建
在项目 左侧列表点击 立即构建,在 Build History 列表,可能会得到一个红色的小圆点,表示构建失败。
点击 构建失败的任务(红色的小圆点)。然后点击 Console Output 即可查看失败的日志。
若提示:'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。
明明在 Windows 提示符下运行是没有问题的。这是因为 Jenkins 缺少环境配置。
配置构建执行状态
回到 Jenkins 首页,点击 构建执行状态 ,右侧会列出本机信息,点击 最右侧配置图标,节点属性 -> 环境变量勾选 -> 添加键值对列表,配置 Python 的 Path 环境变量。同时还需要添加浏览器驱动文件所在目录,参考:
键:path
值:C:\Program Files\Python36
配置完成,点击 保存,再来运行 立即构建,这次看到 py_tests.py 自动化脚本被执行了。至此一个简单的 Selenium + Python 自动化测试的构建任务就创建完成了。




