Python+Selenium-BDD框架之behave
(查看behave具体教程可以访问官网: http://pythonhosted.org/behave/)
1.安装behave
安装好python后,使用 pip install behave命令安装behave
2.简单实例
新建下面几个文件,文件结构如下
firstCase
firstCase/wordcheck.feature
firstCase/steps
firstCase/steps/wordcheck.py
wordcheck.feature的内容如下:
Feature: word check
Scenario: Check letters
Given I have a letter
When I input letter y
Then the inputed letter is Equal with y
wordcheck.py内容如下:
__author__ = 'Amy'
from behave import * @Given('I have a letter')
def step_impl(context):
pass @When('I input letter {letter}')
def step_impl(context,letter):
context.letter = letter @Then('the inputed letter is Equal with {TargetLetter}')
def step_impl(context,TargetLetter):
context.TargetLetter = TargetLetter
assert context.letter is context.TargetLetter
3.在cmd里面运行
4.Scenario Outlines
在测试同一个场景时,很多时候我们需要输入各种各样的数据来验证不同的结果输出,这时我们用Scenario Outlines就可以实现了。如下图分别输大小写字母来验证场景
Feature: word check
Scenario Outline: Check letters
Given I have a letter
When I input letter <keyword>
Then the inputed letter is Equal with <targetword> Examples: Lowercase letters
|keyword|targetword|
|a |a |
|b |b | Examples: Capital letters
|keyword|targetword|
|F |F |
|D |D |
结果如下:
5.使用table
在场景中可以一个表格作为context.table属性值,如下图在Given中加入表格:
Feature: show the table
Scenario: some scenario
Given a set of specific users
| name | department |
| Barry | Beer Cans |
| Pudey | Silly Walks |
| Two-Lumps | Silly Walks |
"""
This is a table,and this line will be displayed in result,it is context.text attribute
"""
When we count the number of people in each department
Then we will find two people in "Silly Walks"
But we will find one person in "Beer Cans"
6.Environment.py
Environment.py是个非常重要的文件,放在feature文件夹下,与.feature文件并列。下面是Environment.py中定义的一些方法:
before_step(context, step), after_step(context, step)
These run before and after every step.
before_scenario(context, scenario), after_scenario(context, scenario)
These run before and after each scenario is run.
before_feature(context, feature), after_feature(context, feature)
These run before and after each feature file is exercised.
before_tag(context, tag), after_tag(context, tag)
These run before and after a section tagged with the given name. They are invoked for each tag encountered in the order they’re found in the feature file.
before_all(context), after_all(context)
These run before and after the whole shooting match.
下面是一个简单的例子,大家可以根据自己的需要定义方法:
# coding:utf-8
__author__ = 'Amy'
import sys
from behave import *
from selenium import webdriver # 开始测试前,定义系统编码为utf-8
def before_all(context):
reload(sys)
sys.setdefaultencoding('utf-8') def before_feature(context):
context.driver = webdriver.Firefox() def after_feature(context):
context.driver.quit()
7.通过标签tag来控制测试执行
标签以@开头,如下示例:
Feature: find a look
@valid
Scenario: look up a book
Given I search for a valid book
Then the result page will include "success" @invalid
Scenario: look up an invalid book
Given I search for a invalid book
Then the result page will include "failure"
执行的时候在behave 后面加上tag 标签即可,如我只测试“valid”这个场景,那么就输入“behave --tags=valid”.
如果你想执行若干个不同标签的场景,你可以这么写“behave --tags=valid,invalid”;
如果你想执行除了@invalid外的所有场景,你可以这么写“behave --tags=-invalid”;
如果你要执行标签包含了 “valid”和“invalid”两个签标的场景,你可以这么写“behave --tags=valid --tags=invalid”
当然,tags在py文件中也起作用,例如
def before_feature(context):
if 'browser' in feature.tags:
context.driver = webdriver.Firefox() def after_feature(context):
if 'browser' in feature.tags:
context.driver.quit()
Python+Selenium-BDD框架之behave的更多相关文章
- python+selenium+unnittest框架
python+selenium+unnittest框架,以百度搜索为例,做了一个简单的框架,先看一下整个项目目录结构 我用的是pycharm工具,我觉得这个工具是天使,超好用也超好看! 这些要感谢原作 ...
- python+selenium之框架设计
一.自动化测试框架 1.什么是自动化测试框架 简单来说,自动化测试框架就是由一些标准,协议,规则组成,提供脚本运行的环境.自动化测试框架能够提供很多便利给用户高效完成一些事情,例如,结构清晰开发脚本, ...
- 从零开始到设计Python+Selenium自动化测试框架-如何开始
如何开始学习web ui自动化测试?如何选择一门脚本语言?选择什么自动化测试工具? 本人已经做测试快5年,很惭愧,感觉积累不够,很多测试都不会,三年多功能测试,最近两年才开始接触和学习自动化测试.打算 ...
- 《一头扎进》系列之Python+Selenium自动化测试框架实战篇6 - 价值好几K的框架,呦!这个框架还真牛叉哦!!!
1. 简介 本文开始介绍如何通过unittest来管理和执行测试用例,这一篇主要是介绍unittest下addTest()方法来加载测试用例到测试套件中去.用addTest()方法来加载我们测试用例到 ...
- BDD框架:behave学习记录
本人学习的时候基本上是按照behave的tutorial教程一步步学习的,这篇文章就当Behave教程的翻译版吧(*^__^*) 嘻嘻--. 1 安装behave 安装好python后 ...
- python selenium --unittest 框架
转自:http://www.cnblogs.com/fnng/p/3300788.html 学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习sele ...
- Python selenium自动化测试框架入门实战--登录测试案例
本文为Python自动化测试框架基础入门篇,主要帮助会写基本selenium测试代码又没有规划的同仁.本文应用到POM模型.selenium.unittest框架.configparser配置文件.s ...
- python+selenium 自动化测试框架-学习记录
本人小白一枚,想着把学习时的东西以博客的方式记录下来,文章中有不正确的地方请大佬多多指点!!共同学习 前期准备 安装python3.selenium.下载对应版本的webdriver:安装所需的第三 ...
- Eclipse+Python+Selenium自动化测试框架搭建
1.下载Eclipse:http://www.eclipse.org/downloads/ 2.下载JDK:http://www.oracle.com/technetwork/java/javaee/ ...
- python + selenium 自动化测试框架
分享一个网站自动化测试框架 结构如下: test_project|--logs|---pages |---register_page.py| |---base_page.py|---test ...
随机推荐
- App测试从入门到精通之兼容性和回归测试
兼容性测试需要考虑的APP测试点 1.不同网络环境下的兼容性测试 2.不同手机操作系统兼容性测试 3.不同应用软件的兼容性测试 4.不同的容量大小的SIM卡之间的互相兼容测试 5.当安装杀毒软件时,应 ...
- How Tomcat Works(二十)
要使用一个web应用程序,必须要将表示该应用程序的Context实例部署到一个host实例中.在tomcat中,context实例可以用war文件的形式来部署,也可以将整个web应用拷贝到Tomcat ...
- bzoj 3224/Tyvj 1728 普通平衡树(splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- HTML 5+CSS 3网页设计经典范例 (李俊民,黄盛奎) 随书光盘
<html 5+css 3网页设计经典范例(附cd光盘1张)>共分为18章,涵盖了html 5和css3中各方面的技术知识.主要内容包括html 5概述.html 5与html 4的区别. ...
- 复制构造函数被调用的三种情况------新标准c++程序设计
1.当用一个对象去初始化同类的另一个对象时,会引发复制构造函数被调用.例如,下面的两条语句都会引发复制构造函数的调用,用以初始化c2. C c2 (c1); C c2=c1; 这两条语句是等价的.注意 ...
- java中int转String 固定位数 不足补零
转载自:http://ych0108.iteye.com/blog/2174134 String.format("%010d", 25); //25为int型 0代表前面要补的字符 ...
- kali 插耳机没声音
打开终端,然后输入命令:下载pulseaudio音量控制软件: apt install pavucontrol 然后终端输入指令:pavucontrol打开软件,发现在输出设备中有两个输出设备:一个N ...
- python+echarts==pycharts
Django数据可视化 pyechats http://pyecharts.org/#/zh-cn/django
- 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分/LCT+贪心
Description 给你一个有n个点的树,每个点的包括一个位运算opt和一个权值x,位运算有&,l,^三种,分别用1,2,3表示. 每次询问包含三个数x,y,z,初始选定一个数v.然后v依 ...
- LOSKI,我
2019年入驻github以及博客园 在发现用github的issue写博客稍微有些奇怪后决定开辟这个更适合写博的空间 2019/4/1 目前大一,计算机专业,尚未分流 更多的时间花在了数据结构与算法 ...