如何使Robot Framework函数接受参数作为字符串而不是默认的unicode类型

bin*_*thb 0 python unicode configparser robotframework

我正在编写一个新的RF库,需要使用字符串参数,因为我使用的(预先存在的)Python库需要字符串,而不是unicode.当然,我可以在调用仅支持字符串的现有函数之前将每个unicode转换为字符串.

import ConfigParser

class RFConfigParser:

def get (self,section, option):
    print type (section) #prints unicode
    section = str (section) #works but I dont want to do this
    return self._config.get (section, option) #this pre-existing function expect a string input
Run Code Online (Sandbox Code Playgroud)

问题是我有很多类似的函数,在每个函数中我都要将这个unicode称为字符串转换马戏团.

有没有直接的方法这样做,因此RF功能将直接接受字符串格式

另一个问题是默认的unicode支持Robot Framework功能还是RIDE功能?(我正在使用RIDE,这就是我遇到这个问题的原因)

Har*_*rri 6

在将这些Unicode字符串传递给库之前,可以使用Evaluate关键字将这些Unicode字符串强制转换为普通字符串.

像这样的东西:

lib.py:

def foo(foo):
    print type(foo)
Run Code Online (Sandbox Code Playgroud)

的test.txt

*** Settings ***
Library           lib.py

*** Test Cases ***
demo
    ${bar}    Evaluate    str('bar')
    foo    ${bar}
Run Code Online (Sandbox Code Playgroud)

最佳解决方案取决于具体情况.也许一个解决方案是编写一个关键字,为您执行该转换,然后调用库函数.也许最好的选择仍然是修改你的库以接受Unicode字符串.这取决于.