【问题标题】:How to test if two dates are different using RobotFrameWork如何使用 RobotFrameWork 测试两个日期是否不同
【发布时间】:2022-03-09 00:34:08
【问题描述】:

我编写了一个代码,该代码调用了两次网络服务并恢复了一个名为 lastLocationDate 的日期字段。在我的代码中,我将两个日期存储在两个不同的变量 date1 和 date2 中。

现在我想要确保我的两个日期不一样。 当我记录我拥有的日期的值时:

Date1=    2019-10-08T06:54:06.118Z
Date2=    2019-10-08T06:54:05.783Z

我尝试使用减法它没有用我尝试使用不相等并寻找不同的东西。

${DateFinale}=    Subtract Time From Date    ${Date2}     ${Date1}     result_format=%y-%m-%d     exculse_millies=True    date_Format=%Y-%m-%dT%H:%M:%S.%f

我也尝试在代码中添加后面的 Z 它也不起作用

date_Format=%Y-%m-%dT%H:%M:%S.%fZ

我希望有一个 TimeDate,但我收到了这条消息:
关键字 ${DateFinale} = 日期时间。从日期 ${Date2}、${Date1}、result_format=%y-%m-%d、exculse_millies=True、date_Format=%Y-%m-%dT%H:%M:%S.%f 减去时间 文档:
从日期中减去时间并返回结果日期。

开始/结束/经过:20191008 09:08:18.494 / 20191008 09:08:18.494 / 00:00:00.000 09:08:18.494 FAIL Keyword 'DateTime.Subtract Time From Date' 在命名参数之后得到了位置参数。

【问题讨论】:

  • 为什么使用 jenkins 和 squash 标签?
  • 如果你看过DateTime库文档,你可以看到Subtract Time From Date中的第二个参数不能使用DateTime格式。文档说“以支持的时间格式之一减去的时间”。但是,使用 Subtract Date From Date 关键字使用日期格式非常好。

标签: robotframework


【解决方案1】:

比之前的答案更短的版本:

Compare Dates Are Different
    ${date1}=    Get Current Date
    ${date2}=    Get Current Date
    Run Keyword If    '${date1}' == '${date2}'    Fail    Dates are same

【讨论】:

    【解决方案2】:

    你可以这样试试:

    ${isdateequal}= Run Keyword And Return Status Should Be Equal ${date1} ${date2} Run Keyword If '${isdateequal}' == 'True' Test Failed

    【讨论】:

    • 无法将 True 与 'True' 进行比较...如果变量中已有布尔值,则只需执行 Run Keyword If ${isdateequal} Test Failed
    • 对不起,我在运行关键字后忘记了''如果,我更正了它
    • 非常感谢它的工作,我不必使用减法
    • @Hopeful,然后将答案标记为“已接受的答案”:stackoverflow.com/help/someone-answers
    【解决方案3】:

    我找到了解决问题的简单方法我所要做的就是将我的日期作为字符串进行比较

        Should Not Be Equal As Strings    ${Date1}    ${Date2}    msg=Aucune localisation n'a été recue 
    

    感谢您的帮助

    【讨论】:

      【解决方案4】:

      我会使用评估关键字

      ${date1}=    Convert Date    2019-10-08T06:54:06.118Z
      ${date2}=    Convert Date    2019-10-08T06:54:05.783Z
      ${date_same}=    Evaluate    '${date1}' == '${date2}'
      Log    ${date_same}
      Should Not Be True    ${date_same}
      

      【讨论】:

        猜你喜欢
        • 2012-06-20
        • 2023-03-07
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        • 2012-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多