当前位置:文档之家› 对比介绍自动化测试语言Tcl和Python

对比介绍自动化测试语言Tcl和Python


控制流—for
Tcl: for {set i 0} {$i<5} {incr i} { puts $i } Python: for i in range(5): print i
#range起始缺省是0,步长缺省是1,这里给出序列[0,1,2,3,4]
Tcl控制流程switch
set result FLASE switch -exact -- $result { "TRUE" { puts "TRUE" } "FALSE" { puts "FALSE" } "UNKNOWN" default { puts "UNKNOW or unknown value" } }
数学运算
Tcl: set a 1 set b 2 set c [expr $a+$b] puts $c #通过命令expr可以实现对数学表达式的分析和计算 Python: a=1 b=2 c=a+b print c
转义字符—”\”
Tcl: puts "The book I borrowed is \"1984\"." #实际输出为:The book I borrowed is "1984". Python: print 'Jane\'s dog is missing‘ #实际输出为:Jane's dog is missing
Python:
a = [1,2,3,4,'apple'] for i in a: print i a.append('banana') print a a.insert(0,0) print a print a.index('apple') a.remove('apple') #只会删除匹配的第一个 print a del a[0] print a print a[4] #序列的索引操作符 print a[0:3] #序列的切片操作符
2013.2
目录
1. 2. 3. 4. 5. 6. 7. 8. 9.
语言特点 版本介绍 注释
13. 列表 14. 字典 15. 元组(Python) 16. 数组(Tcl) 17. 文件操作 18. 名字空间(Tcl) 19. 模块化 20. 面向对象编程 21. 多线程编程 22. Python中嵌入Tcl
while { $running == True } { if { $guess == $number } { puts "Congratulations, you guessed it." break } if { $guess < $number } { puts "Sorry, number is too small." incr guess continue } puts "Sorry, number is too big." incr guess -1 }
ቤተ መጻሕፍቲ ባይዱ
函数(过程)
Tcl: proc MyPet { pet {number 1} } { ; # number缺省值为1 puts "I have $number $pet\s" return } MyPet dog MyPet cat 5 MyPet mouse 3
函数(过程)
Python: def MyPet(pet, number=1): '''print the number of my pets''' print 'I have %d %ss' % (number,pet) return
print MyPet.__doc__ MyPet('dog') MyPet('cat', 5) MyPet(number=3, pet='mouse')
局部和全局变量
Tcl: proc func { } { global x puts "x is $x" set x 2 puts "Changed local x to $x" } set x 50 func puts "Value of x is $x“ #全局变量在过程内部不会自动可见,需要通过global 命令 来事先声明,也可在变量前加::表示全局变量
列表
Tcl:
set a [list 1 2 3 4 apple] ;#或者set a "1 2 3 4 apple" foreach i $a { puts $i } lappend a banana puts $a set a [linsert $a 0 0] #不改变原列表的内容,返回一个新列表 puts $a set i [lsearch -exact $a apple] puts $i set a [lreplace $a $i $i] puts $a set a [lreplace $a 0 0] #不改变原列表的内容,返回一个新列表 puts $a
Python:解释性的脚本语言,具有编译过程(脚本编译成字节
码),面向过程且面向对象,支持多线程。 Python语言非常干 净,设计优雅,具有出色的模块化特性,可以与C和Java紧密整 合。Python的支持库的代码水平也比较高,对于软件开发的各 个方面的第三方库(如图像处理,网络通信,Web技术等)都 有非常好的支持。
局部和全局变量
Python: def func(): global x print 'x is', x x=2 print 'Changed local x to', x x = 50 func() print 'Value of x is', x
控制流—if
Tcl:
set love False if {$love==True} { puts "I love you" } elseif {$love==False} { puts "I don't love you" } else {puts "I don't know you"}
Tcl:解释执行的脚本语言,不需要编译,面向过程,iTcl(incr
Tcl)扩展包及8.6版本提供面向对象支持,thread扩展包支持多 线程。Tcl是老牌的自动化测试语言,最初就是基于整合测试系 统而开发出来的,80年代开始就在摩托罗拉使用,后来被思科 采纳,并在自动化测试领域得到了广泛的应用。
注释
Tcl: #注释1 set a 1 ;#注释2(注意与命令同一行时#前要加;) Python: #注释1 a=1 #注释2 ''' 注释3 '''
数据类型
Tcl:只有字符串一种类型,不需要声明类型 Python:有数字(整型、长整型、浮点数、复数), 字符串和布尔值(True,False)这几种类型,也不需要 声明类型
输出
Tcl: puts "hello $b world" puts {hello $b world} set b "computer" puts $b #双引号里字符串会做置换处理,花括号里的置换则有可能会被阻止
Python: print 'hello world' # 或 print “hello world” 或 print ‘’’hello world’’’ b='computer' print b
Python: number = 23 guess = 0 running = True while running: if guess == number: print 'Congratulations, you guessed it.' break if guess < number: print 'Sorry, number is too small.' guess+=1 continue print 'Sorry, number is too big.' guess-=1
变量定义和赋值
Tcl: set a 1 set b "hello world" set c Hello 或 set c "Hello" Python: a=1 b = "hello world" c = "Hello" 或 c='Hello' 或 c='''Hello''' a = True #布尔值 print int(a)
字符串操作
Python: cmd = 'APPLY MUTEX:SW_ID=2,NE_ID=2,NETYPE=\"RNC\"‘ index = cmd.find(':') print cmd[0:index]
#输出APPLY MUTEX
print len(cmd[0:index]) #打印字符串长度
语言特点
Perl原本是专门设计处理文本的,这方面能力最强,但是
不适合编写大程序,语法晦涩难懂。Perl在网络管理员的 圈子里应用的很广,被称作是the duct tape of the Internet, 但在自动化测试领域应用的范围不是很广泛。
Ruby是日本人发明的完全面向对象的脚本语言,在自动化
版本介绍
ActiveTcl是ActiveState公司提供的免费Tcl开发集成支持包, ActiveTcl目前有8.4,8.5和8.6Beta三个版本。这里介绍的是8.4和 8.5版本,IDE使用Eclipse+dltk 8.4版本包含完整的扩展包,例如Expect 8.5是内核改进版本,运行速度比8.4快10%。8.5不包含绝大部分 的扩展包,用户需要通过内置的teacup在线升级安装各种需要的 扩展包。如果无法在线升级,可先安装8.4版本共用8.4的扩展包 8.6目前还是beta版本,支持面向对象,暂不考虑 Python的版本很多,而2008年底推出Python3.0不向下兼容2.x版本。 这里介绍的是2.x版本,使用的也是ActiveState公司提供的免费 Python开发集成支持包ActivePython,IDE使用Eclipse+Pydev
相关主题