FitNesse:变量赋值和简单算术

FitNesse: Variable assignment and simple arithmetic

根据user guide,可以为变量赋值,然后执行简单的算术运算。

想象一下,我有一个 fixture 设计用于获取页面上的一个元素并将数值提取为 Double(我现在使用 HSAC Slim BrowserTest fixture 和我自己的代码来执行此操作)

|script    |numbers extraction                         |
|$testval1=|numeric value of  |element1              | |
|$testval2=|numeric value of  |element2              | |

运行 这给了我这样的东西:

|script            |numbers extraction                         |
|$testval1<-[20.04]|numeric value of  |element1 ->[€ 20,04]  | |
|$testval2<-[5.1]  |numeric value of  |element2 ->[€ 5,1]    | |

现在假设我想将两个双精度值的总和与第三个元素的数值进行比较:

|script|numbers extraction                                           |
|check |numeric value of  |element3|{=${ ${testval1} + ${testval2} =}|

无论我在最后一个单元格中使用什么括号和美元符号的组合,我总是得到 'invalid expression'。

运行 最后一行(testval1 和 testval2 两边没有括号)returns:

|check|numeric value of|element3  ->[€ 25.14]| [25.14] expected [invalid expression: $testval1->[20.04] + $testval2->[5.1]] |

你好像是用浏览器测试的,我拿这个是HSAC安装的?请在问题中提及这一点,因为 HSAC 是 FitNesse 固定装置。

无论如何,删除大括号应该可以解决问题。使用大括号,它期望一个全局变量,那些使用 !define var {foo}

实现的变量

当使用本地定义的变量时,例如 |$bar=|value of|foo| 必须在测试中使用不带大括号的变量调用。

|$bar=|value of|foo|
|enter|$bar|as|inputField|

在此处查找有关 HSAC 使用的更多信息:https://github.com/fhoeben/hsac-fitnesse-fixtures/wiki/2.-Slim-Fixtures

旁注: 然后还有使用 @var@{var} 的 table 模板,其中首选使用 @{var},因为 @{var} 将查找列 var@var 将接受列 vva,如果你碰巧实现了。此处使用花括号可确保使用完整的变量名。

很遗憾,您不能做您正在寻找的事情。您为元素赋值的变量实际上是 SLIM symbols,而不是 wiki 级别的变量。如果您在问题中链接到的用户指南页面上向下滚动一点,您会发现一个名为 "Difference between variables and SLIM symbols":

的部分
  • Variables are evaluated at render time, before the test executes. This allows for values to be set based on page hierarchy and other things that are purely inputs to the tests.
  • Symbols only exist at execution time. They can be changed at runtime, so are distinct from variables, which cannot.

我发现 FitNesse/SLIM 中的三种类型的变量令人困惑,它们不同的用法、语法和可能性会导致很多问题。我的理解是:

  1. Markup variables (aka wiki variables). For instance ${myVar}, defined using !define. They get their value at page render time, so even before a test is started, so you see their value when you browse to a wiki page, and only in the page's source do you see it is a variable. These can be used in markup expressions,这就是你在问题中试图做的。
  2. Scenario parameters. For instance @{myVar} (or @myVar), defined in the first row of a SLIM scenario table. These are the 'formal parameters' to the scenario, which get their actual value based on the invocation of the scenario (i.e. each usage of the scenario, either from a script table, other scenario or row in a decision table defines their value). They get their value at the start of a test, before its first action is performed. You see the variable when you look at the scenario table that defines it. (When you use the 'table template' table type defined by hsac-fitnesse-plugin (which is included in hsac-fitnesse-fixtures 项目基线)您不需要在 table 的第一行定义变量名称,它们会根据它们的出现自动找到(例如 @{myVar} ) 在 table.)
  3. SLIM symbols。例如 $myVar,它们使用 $myVar= 赋值。这些 'runtime variables' 在测试执行期间获取它们的值,它们对于测试套件是全局的,并且它们的值可能在测试执行期间更改。这些是唯一一种可以从 'system under test' 获得的 属性 中获取其值的变量,它们是您在问题的 table 中使用的变量。 它们实际上是对 SLIM 进程内部对象的引用,因此 fixture 可能会更改变量所指对象的内部状态,而不会在变量的 wiki 表示中显示此更改(这只是对象的 toString()上次分配时的结果)。

P.S。当您使用 hsac-fitnesse-fixtures 时,将字符串转换为双精度不需要自定义夹具(如您的 numbers extraction)。您可以只使用库的 string fixture.

convert to double 方法