虽然网上都说slim效率很高,无奈找不到支持python的方法,继续用pyfit

1 Column Fixture

特点:行表格展现形式,一条测试用例对应一行数据

Wiki

!define COMMAND_PATTERN {python "%m" %p}

!define TEST_RUNNER {C:\Python27\PyFIT-0.8a2\fit\FitServer.py}

!path E:\selfworkspaces\fitTest

!|ColumnFixtureTest.AddTest |

| a| b| add?|

|1|2|3|

|1|3|3|

Python:

from fit.ColumnFixture import ColumnFixture

class AddTest(ColumnFixture):

_typeDict = {

"a":"Int",

"b":"Int",

"add":"Int",

}

def __init__(self):

ColumnFixture.__init__(self)

self.a=''

self.b=''

def add(self):

c=self.a+self.b

return c

Action Fixture

主要用于测试一系列事件流,可直接调用其他类进行测试

Wiki:

第一行填入ActionFixture

Start:调用的类名

Enter:传入的参数

Press:执行一个没有返回值的方法

Check:检查结果

Wiki:

!define COMMAND_PATTERN {python "%m" %p}

!define TEST_RUNNER {C:\Python27\PyFIT-0.8a2\fit\FitServer.py}

!path E:\selfworkspaces\fitTest

!| ActionFixture |

| start | ActionFixtureTest.AddTest| |

|enter|firstPart|1|

|enter|secondPart|2|

|press|join|

|check|together|3|

python:

from fit.Fixture import Fixture

class AddTest(Fixture):

_typeDict = {}

def __init__(self):

Fixture.__init__(self)

self.__first  = ""    #< Private attributes (Python convention).

self.__second = ""

self.__both   = ""

_typeDict["firstPart"] = "Int"

def firstPart(self, s):

self.__first = s

_typeDict["secondPart"] = "Int"

def secondPart(self, s):

self.__second = s

_typeDict["join"] = "Default"      #< AUTO-DETECT: None = void

def join(self):

self.__both = self.__first+self.__second

_typeDict["together"] = "Int"

def together(self):

return self.__both

3 Row Fixture

非常适用于测试一个列表,和column想比,row验证属性多了missing和surplus,missing表示期望有值,但是实际结果没有返回, surplus表示期望无值,但实际结果返回有值

使用时,如果被测对象有主键(比如id之类的),将主键放在最左侧,便于查看错误报告

Row Fixture 用于检查没有序列的列表,检查有序列的对象,要用ArrayFixture

Wiki:
!|RowFixtureTest.PayRecordTest|

|id|name|

|1|nxy|

|2|nxy|

|3|nxy|

Python:
from fit.RowFixture import RowFixture

class
PayRecordTest(RowFixture):

def getTargetClass(self):

return PayRecord

def query(self):

PayRecord.addId(1,"nxy")

PayRecord.addId(3,"nnn")

PayRecord.addId(4,"nxy")

return PayRecord.idList #< Return copy of
players

class
PayRecord(object):

_typeDict = {

"id":"Int"
,

"name":"String"

}

def __init__(self,id=0,name=""):

self.id=id

self.name=name

idList=[]

@classmethod

def addId(cls,id,name):

cls.idList.append(PayRecord(id,name))

4 Table Fixture

可用于需要自定义表格的解析方式,自由设计表格样式

Wiki:
验证总分数和平均分

!| TableFixtureTest |

| name| score|

|math|90|

|chinese|95|

|english|91|

|total|276|

|avg|92|

Python:
from
fitnesse.fixtures.TableFixture import TableFixture

import
types

class
TableFixtureTest(TableFixture):

def doStaticTable(self,
rows):

total=0

for row in range(1, rows-2):

total+=int(self.getText(row,
2))

totalTable = int(self.getText(rows-2, 1))

avg=total/(rows-3)

avgTable=int(self.getText(rows-1, 1))

if total==totalTable:

self.right(self.getCell(rows-2, 1))

else:

self.wrong(self.getCell(rows-2, 1), str(totalTable))

if avg == avgTable:

self.right(self.getCell(rows-1, 1))

else:

self.wrong(self.getCell(rows-1, 1), str(avg))

Comment

protected abstract void doStaticTable(int rows)

Table Fixture is an abstract class that you must derive from. You must override doStaticTable to perform the functions of the fixture. The number of rows in the table is passed in rows.

protected Parse getCell(int row, int column)

Returns the addressed table cell as a Parse.

protected String getText(int row, int column)

Returns the text within the addressed table cell.

protected boolean blank(int row, int column)

Returns true if the addressed table cell is blank.

protected void wrong(int row, int column)

Turns the addressed table cell red.

protected void right(int row, int column)

Turns the addressed table cell green.

protected void wrong(int row, int column, String actual)

Turns the addressed table cell red, and annotates it with the actuall value.

protected void ignore(int row, int column)

Turns the addressed cell gray.

protected int getInt(int row, int column)

Converts the addressed cell to an int, and returns it.

5 SetUpFixture

可以用来做测试数据的准备

Wiki:

!| SetUpFixtureTest |

| player | post code | balance |

| John Smith | SW4 66Z | 10.00 |

| Michael Jordan | NE1 8AT | 12.00 |

Python:

from fitLib.SetUpFixture import SetUpFixture

from domain.Player import Player

class SetUpFixtureTest(SetUpFixture):

_typeDict = {}

def __init__(self):

Player.players = []

_typeDict["playerPostCodeBalance.types"] = [ None, "String", "String", "Float" ]

def playerPostCodeBalance(self, name, postCode, balance):

Player.addPlayer(name, postCode, balance)

Player 类见RowFixture示例

6 CalculateFixture

用来验证一个或多个列的组合值,可以用columnFixture来完成,但代码要比column少

Wiki:

!|CalculateFixtureTest|

|firstPart|secondPart||together|

|Hello|World||Hello, World|

|Houston|We Have a Problem||Houston, We Have a Problem|

Python:

from fitLib.CalculateFixture import CalculateFixture

class CalculateFixtureTest(CalculateFixture):

_typeDict = {}

# JAVA: String togetherFirstPartSecondPart(String firstPart,String secondPart){

_typeDict["togetherFirstPartSecondPart.types"] = ["String", "String", "String"]

def togetherFirstPartSecondPart(self, firstPart, secondPart):

return "%s, %s" % (firstPart, secondPart)

7 DoFixture

用来描述故事型的测试,比ActionFixture更高效

如果DoFixture是测试页的第一个表,则他管理着整个测试页面,允许你将测试表分成多个fixture类型的表,使测试具有可读性

Wiki:

!|DoFixtureTest|

|fill|10|times with|x|

|check|char at|4|x|

|set list|A,B,C,D|

|show|char at|2|

Python:

from fitLib.DoFixture import DoFixture

from typeadapter import buildListTypeAdapterFor

class DoFixtureTest(DoFixture):

_typeDict = {

"letters": "String"

}

def __init__(self):

DoFixture.__init__(self)

self.letters = ""

_typeDict["fillTimesWith.types"] = [None, "Integer", "Char" ]

def fillTimesWith(self, count, c):

self.letters = c * count    #< FILL: Repeat char ``count`` times.

_typeDict["charAtIs.types"] = ["Boolean", "Integer", "Char" ]

def charAtIs(self, position, c):

return self.letters[position] == c

ARRAY_OF_CHAR_TYPE_ADAPTER = buildListTypeAdapterFor("Char")

_typeDict["setList.types"] = [ None, ARRAY_OF_CHAR_TYPE_ADAPTER ]

def setList(self, array):

self.letters = "".join(array)

_typeDict["charAt.types"] = [ "Char", "Integer" ]

def charAt(self, position):

return self.letters[position]

from fit.taBase  import StringAdapter, ListAdapter

from fit.taTable import typeAdapterTable

from fit.FitException import FitException

import types

def buildListTypeAdapterFor(scalarType, typeName=""):

"""Utility method to build a TypeAdapter for a list of scalarType."""

if not typeName:

typeName = "ListOf%s" % scalarType

return ListAdapter(None, "", typeName, { ".scalarType": scalarType })

class CharTypeAdapter(StringAdapter):

"""

    TypeAdapter for Character.

    Characters are normally not of interest in Python (you normally use strings).

    Therefore, Python has no seperate datatype for a character.

    In addition, a TypeAdapter for Character is also not provided by PyFIT.

    """

def parse(self, text):

text = text.strip()

if len(text) != 1:

raise FitException, ("ParseError CharValue", text)

return text[0]

def toString(self, c):

if isinstance(c, (types.IntType, types.LongType)):

return chr(c)

elif isinstance(c, types.StringTypes):

assert len(c) >= 1

return c[0]

else:

raise FitException, ("toString.ValueError CharValue", c)

typeAdapterTable["Char"] = CharTypeAdapter

8 ArrayFixture

用于严格有序的序列测试

Wiki

!include -seamless SetUpFixture

!|ArrayFixtureTest|

|name|post code|credit limit|

|John Smith|SW4 66Z|10|

|Michael Jordan|NE1 8AT|12|

其中setupFixture见1.5,player类见之前例子

Python:

from fitLib.ArrayFixture import ArrayFixture

from domain.Player import Player

class ArrayFixtureTest(ArrayFixture):

def __init__(self):

ArrayFixture.__init__(self)

self.paramCollection = Player.players

self.setActualCollection()

def getTargetClass(self):

return Player   #< TYPE-HINT: For ValueObject class.

9 Combination Fixture

CombinationFixture用于描述有两个参数的计算规则,如下面两个例子,除法和乘法,其中theFirst指第一列各值,theSecond指第一行各值

Wiki:

!|CombinationFixtureTest.DivisionTest|

|  |1 |2|3|

|6 |6 |3|2|

|12|12|6|4|

!|CombinationFixtureTest.MultiplicationTest|

|  |1 |2|3|

|6 |6 |12|18|

|12|12|24|3|

Python:

from fitLib.CombinationFixture import CombinationFixture

class DivisionTest(CombinationFixture):

_typeDict = {}

# PY3K: combine(theFirst : int, theSecond : int) : int

_typeDict["combine.types"] = [ "Int", "Int", "Int" ]

def combine(self, theFirst, theSecond):

return theFirst / theSecond

class MultiplicationTest(CombinationFixture):

_typeDict = {}

# PY3K: combine(theFirst : int, theSecond : int) : int

_typeDict["combine.types"] = [ "Int", "Int", "Int" ]

def combine(self, theFirst, theSecond):

return theFirst * theSecond

10 ConstraintFixture

与calculatefixture不同 的是,constraintfixture每一次计算期望值都为true

Wiki中都为输入值,代码需有一个返回布尔类型的方法

Wiki
!|ConstraintFixtureTest|

|firstPart|secondPart|

|1|2|

|2|3|

Python:

from fitLib.ConstraintFixture import ConstraintFixture

class ConstraintFixtureTest(ConstraintFixture):

_typeDict = {}

# PY3K: firstPartSecondPart(firstPart : int, secondPart : int) : bool

_typeDict["firstPartSecondPart.types"] = [ "Boolean", "Int", "Int" ]

def firstPartSecondPart(self, firstPart, secondPart):

return firstPart < secondPart

11 Fixture Arguments

表的第一行表示类名,在类名后面添加单元格传递参数

Arguments 可以参数化字符串,并且重复使用,比如可以向RowFixture传递参数,他的参数不是在构造函数里初始化,而是通过加载类初始化

Wiki:
!|ArgumentsTest|Hello You Houston We Have a Problem|

|word|

|Hello|

|World|

|Houston|

|We|

|Have|

|a|

|Problem|

|Problem|

Python:

from
fitLib.SetFixture import
SetFixture

import
types

class
Word(object):

"""Simple ValueObject class
to store a word as string."""

_typeDict = { "word": "String" }

def __init__(self,
word):

assert isinstance(word, types.StringTypes)

self.word = word

class
ArgumentsTest(SetFixture):

def getTargetClass(self):

return Word #< CLASS-HINT: For _typeDict lookup.

def doTable(self,
table):

wordSet = set()

for s in self.args:#*argslist

for word in s.split(" "):

wordSet.add( Word(word) )

# -- WEIRD: setActualCollection() takes no arg -> Preassign
first.

self.paramCollection = wordSet

self.setActualCollection()

SetFixture.doTable(self, table)

fitnesse 中各类fit fixture的python实现的更多相关文章

  1. 利用Python中的mock库对Python代码进行模拟测试

    这篇文章主要介绍了利用Python中的mock库对Python代码进行模拟测试,mock库自从Python3.3依赖成为了Python的内置库,本文也等于介绍了该库的用法,需要的朋友可以参考下     ...

  2. python操作txt文件中数据教程[1]-使用python读写txt文件

    python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...

  3. MySQL中的分页操作结合python

    mysql中的分页操作结合python --分页: --方式1: ;-- 读取十行 , --从第十行读取 往后再读十行 --方式2: offset ; --从第二十行开始读取10行 -- 结合pyth ...

  4. beego中各类数据库连接方式

    beego 框架是优秀得go REST API开发框架.下面针对beego中各类数据库连接操作做一个总结. 1. orm连接方式 beego中的orm操作支持三种数据库:mysql,sqlite3,p ...

  5. 视频直播:Windows中各类画面源的截取和合成方法总结

    当今,视频直播技术和实时音视频技术已经是很多行业必备,典型的应用场景有教育直播.远程视频会议.互联网娱乐等.在移动端发起直播,其画面源的种类是十分有限的,无非是取摄像头.截屏等.PC端由于其系统资源充 ...

  6. 算法笔试过程中的几个输入输出python语句

    title: python在线笔试学习笔记 localimage: image1 urlname: writenexam categories: summary tags: [writen, exam ...

  7. 用配置文件里面的参数值替换yaml模板中的变量值【python】

    用配置文件里面的参数值替换yaml模板中的变量值[python] #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/9/20 1 ...

  8. 剑指offer-数字在排序数组中出现的次数-数组-python

    题目描述 统计一个数字在排序数组中出现的次数.   python 内置函数 count()一行就能搞定   解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...

  9. Linux中各类程序的配置文件位置

    目录 Linux中各类程序的配置文件位置 1.启动引导程序配置文件 2.系统启动文件核脚本 3.网络配置文件 4.超级服务程序配置文件和目录 5.硬件配置 6.硬件访问文件 7.扫描仪配置文件 8.打 ...

随机推荐

  1. Python的中文编码转换问题

    与server进行数据交换时,尤其是数据中含有中文时,要注意中文的编码问题. 要选择server接受的编码方式,否则会造成显示乱码. 经验: 实验室server的数据库,中文用UTF-8编码,但我提交 ...

  2. Android Resource介绍和使用

    1. 相关文件夹介绍 文件 取值方式 string.xml getResource().getString(resourceId)或者getResource().getText(resourceId) ...

  3. 高效的SQLSERVER分页查询(推荐)

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  4. 从零开始学习UNITY3D(GUI篇)

    邻近年底,心也有些散乱,加上工作忙了一阵,在达内培训的课程也落下了不少.对unity3d的热度似乎也有点点下降.痛定思痛,又在淘宝上买了写蛮牛网的视频.总之不管是用任何手段都要逼着自己不要浪费了培训的 ...

  5. samba服务器的安装及配置

    安装前首先查看服务器是否已经安装samba服务器 [root@bogon home]# rpm -qa|grep samba system-config-samba-docs-1.0.9-1.el6. ...

  6. zoj 2587 Unique Attack 最小割判定

    题目链接 让你判断最小割是否唯一. 判断方法是, 先求一遍最大流, 然后从源点dfs一次, 搜索未饱和边的数目. 从汇点dfs一次, 同样也是搜索未饱和边的数目, 看总和是否等于n. 如果等于n那么唯 ...

  7. poj 3270 置换

    poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...

  8. 《Pointers On C》读书笔记(第二章 基本概念)

    1.从源代码到生成可执行程序的过程整体上可以分为两个阶段:编译和链接.其中,编译过程大致上又可分为:预处理.编译和汇编.预处理阶段主要对源代码中的预处理指令(包含宏定义指令<如 #define& ...

  9. Oracle 游标Cursor 的基本用法

    查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT语句时,要与INTO子句一起使用,查询的 返回值被赋予INTO子句中的变量,变量的声明是在DELCARE中.SELECT ...

  10. Delphi获取与设置系统时间格式,即GetLocaleInfo和SetLocaleInfo

    在Delphi中,特别是在写管理系统软件时,经常要用到 FormatDateTime 以将 TDateTime 格式的日期时间转换成字符串形式的值显示或保存起来,或者用 StrToDateTime将字 ...