美文网首页
Lettuce:命令行

Lettuce:命令行

作者: Ailsa的简书 | 来源:发表于2017-09-25 21:57 被阅读0次

    Lettuce是用来作为一个命令行实用程序,这意味着目前使用它通过shell是唯一途径。只要在一个shell里,你可以通过2种方式使用Lettuce:
    通常的方式,这意味着任何路径文件夹下的项目,有简单的features/ step_definitions。另外一个,是Django方式的项目。
    它们之间的区别是,使用Django,你有更多的选择,但都有以下共同的选择:

    执行一个特定的feature文件

    user@machine:~/projects/myproj$ lettuce path/to/some/file.feature
    

    使用这种方式,你的feature文件可以不在默认的features文件下。

    只运行特定feature文件下面的某些场景

    user@machine:~/projects/myproj$ lettuce path/to/some/file.feature -s 3,5,9
    

    这种方式,只运行path/to/some/file.feature文件下的3、5和9场景。

    运行所有feature文件下的某些场景

    也许你会发现它毫无意义,但它就是这么工作的,到目前为止没有问题:)

    user@machine:~/projects/myproj$ lettuce -s 3,5,9
    

    猜到了吗?
    这个命令行将执行myproj/features目录下的所有features的场景3、5和9。

    信息级别

    级别1-每个feature输出点

    user@machine:~/projects/myproj$ lettuce --verbosity=1
    

    这是Lettuce的最低信息等级。它用点显示每一步的运行,无论目前运行什么场景或什么feature。
    例如,如果你有一个feature,看起来像:

    Feature: Manipulate strings
      Scenario: Uppercased strings
        Given I have the string "lettuce leaves"
        When I put it in upper case
        Then I see the string is "LETTUCE LEAVES"
    

    输出将是:

    user@machine:~/projects/myproj$ lettuce -v 1
    ...
    
    1 feature (1 passed)
    1 scenario (1 passed)
    3 steps (3 passed)
    

    级别2-输出场景名字

    user@machine:~/projects/myproj$ lettuce --verbosity=2
    

    在这种模式下,Lettuce将打印当前正在运行的每个场景名称,然后根据该场景中的步骤的状态,带上OK、FAILED或ERROR。
    例如,如果你有一个feature,看起来像:

    Feature: Manipulate strings
      Scenario: Uppercased strings
        Given I have the string "lettuce leaves"
        When I put it in upper case
        Then I see the string is "LETTUCE LEAVES"
    
      Scenario: basic math
        Given I sum 2 and 5
        Then I see the result is 9
    

    将输出:

    user@machine:~/projects/myproj$ lettuce -v 2
    Uppercased strings ... OK
    basic math ... FAILED
    
    1 feature (1 passed)
    2 scenarios (2 passed)
    5 steps (4 passed)
    

    级别3-打印所有的feature,但无颜色

    user@machine:~/projects/myproj$ lettuce --verbosity=3
    

    这个模式比后面的模式冗长多了。它打印每一个feature非常有用的信息,比如:
    正在运行的feature文件的相对路径和该文件中的当前行
    执行步骤的步骤定义的相对路径,也包括当前行。
    当一些feature失败,内联回溯
    “ready-to-use”未定义步骤的片段
    例如,假设您有下面的feature,但是只有定义了步骤Given I have the string "lettuce leaves"

    Feature: Manipulate strings
      Scenario: Uppercased strings
        Given I have the string "lettuce leaves"
        When I put it in upper case
        Then I see the string is "LETTUCE LEAVES"
    

    你的输出结果将是这样:

    user@machine:~/projects/myproj$ lettuce -v 2
    
    Feature: Manipulate strings                   # features/strings.feature:1
    
      Scenario: Uppercased strings                # features/strings.feature:2
        Given I have the string "lettuce leaves"  # features/step_definitions/example-steps.py:5
        When I put it in upper case               # features/strings.feature:4 (undefined)
        Then I see the string is "LETTUCE LEAVES" # features/strings.feature:5 (undefined)
    
    1 feature (0 passed)
    1 scenario (0 passed)
    3 steps (2 undefined, 1 passed)
    
    You can implement step definitions for undefined steps with these snippets:
    
    # -*- coding: utf-8 -*-
    from lettuce import step
    
    @step(u'When I put it in upper case')
    def when_i_put_it_in_upper_case(step):
        assert False, 'This step must be implemented'
    @step(u'Then I see the string is "(.*)"')
    def then_i_see_the_string_is_group1(step, group1):
        assert False, 'This step must be implemented'
    

    级别4-打印所有feature,且有颜色

    这个模式几乎与级别3完全相同,不同之处在于它是彩色的。


    如果你打算把Lettuce放在一个连续的集成服务器中,比如Hudson。您可以选择1, 2或3级,这样输出不会显得凌乱。

    与持续集成结合

    Lettuce可以使用Subunit输出测试结果。Subunit是一个可以复用的流的格式,实时展示或转换为不同的格式(如xUnit / JUnit XML格式)。

    user@machine:~/projects/myproj$ lettuce --with-subunit > output.log
    user@machine:~/projects/myproj$ subunit2junitxml < subunit.bin > lettucetests.xml
    

    Subunit的文件标签,可用于指定subunit.bin之外的其他文件名,这很重要,如果你结合测试执行。

    包括覆盖

    你还可以使用覆盖包得到测试覆盖信息。

    user@machine:~/projects/myproj$ coverage run lettuce --with-subunit
    user@machine:~/projects/myproj$ coverage xml
    

    通过shell得到帮助

    user@machine:~/projects/myproj$ lettuce -h
    

    展示所有的选项。


    上一篇:Lettuce: Calling steps from step definitions
    下一篇:Lettuce: Features, Scenarios and Steps reference

    相关文章

      网友评论

          本文标题:Lettuce:命令行

          本文链接:https://www.haomeiwen.com/subject/dqodextx.html