简体   繁体   English

Python测试的时间格式

[英]Time format on Python tests

I would like to test if the format is something like '03:10 ~ 04:10' in python tests, however I have a problem getting it correctly. 我想在python测试中测试格式是否为“ 03:10〜04:10”,但是我在正确获取格式时遇到问题。 This is my code 这是我的代码

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)

I think i got a problem with this logic 我认为我对此逻辑有疑问

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

You are literally comparing the string that is supposed to be time format with the date itself; 您实际上是在将应该是时间格式的字符串与日期本身进行比较。 can't do that. 无法做到这一点。 You can use regular expressions. 您可以使用正则表达式。 Something like this: 像这样:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM