【问题标题】:Time format on Python testsPython 测试的时间格式
【发布时间】:2016-04-30 03:52:25
【问题描述】:

我想在 python 测试中测试格式是否类似于“03:10 ~ 04:10”,但是我无法正确获取它。 这是我的代码

def test_activity_today_time_format(self):  
    """Test time format is HH:MM    """     
    driver = self.driver    
    # Get page by URL   
    driver.get(self.base_url + "/userk/inbox/")     
    # Check time format     
    self.assertEqual("%H:%M ~ %H:%M",  driver.find_element_by_xpath("//td[3]").text)

我觉得这个逻辑有问题

"%H:%M ~ %H:%M"

【问题讨论】:

    标签: python django selenium


    【解决方案1】:

    您实际上是在将应该是时间格式的字符串与日期本身进行比较;不能那样做。您可以使用正则表达式。像这样的:

    text = driver.find_element_by_xpath("//td[3]").text
    pattern = re.compile(r"((1?\d)|2[0-4]):[1-5]?\d ~ ((1?\d)|2[0-4]):[1-5]?\d")
    self.assertRegexpMatches(text, pattern, "Date format not correct")
    

    【讨论】:

    • 非常感谢...我想这就是我一直在寻找的
    • 我真的是测试新手...我收到此错误消息... Traceback(最近一次调用最后):文件“testing/actuser_layouts.py”,第 35 行,在 test_activity_today_time_format 模式 = re .compile(r"((1?\d)|2[0-4]):[1-5]?\d ~ ((1?\d)|2[0-4]):[1-5 ]?\d") NameError: 未定义全局名称're'
    • 你需要import re
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多