[英]Python unit test passes locally, but fails in Jenkins when comparing dates
The following test passes locally, because Locally it's EST:以下测试在本地通过,因为本地是 EST:
def test_get_local_ts(self):
dt = Utils.get_local_ts().strftime('%Y-%m-%d')
dt1 = datetime.now().strftime('%Y-%m-%d')
self.assertEqual(dt, dt1)
But fails when running in a Jenkins pipeline.但在 Jenkins 管道中运行时失败。
Because Jenkins server is running on UTC time, but the method get_local_ts
is returning the local time.因为 Jenkins 服务器在 UTC 时间运行,但
get_local_ts
方法返回的是本地时间。
And so this error happens:所以这个错误发生了:
self.assertEqual(dt, dt1)
self.assertEqual(dt, dt1)
AssertionError: '2021-12-16' != '2021-12-17'
断言错误:“2021-12-16”!=“2021-12-17”
Also, this will only fail when running it after 7 PM EST, when UTC rolls over to next day.此外,这只会在美国东部标准时间晚上 7 点之后运行它时失败,此时 UTC 会滚动到第二天。
How can I get this to pass locally and in Jenkins at any time?我怎样才能让它随时在本地和 Jenkins 中通过?
Thanks!谢谢!
If your unit tests involve datetime objects, then you should probably set the time zone as part of your test setup.如果您的单元测试涉及 datetime 对象,那么您可能应该将时区设置为测试设置的一部分。 A unit test should run under controlled conditions without depending on external factors.
单元测试应该在受控条件下运行,而不依赖于外部因素。
https://docs.python.org/3/library/time.html#time.tzset https://docs.python.org/3/library/time.html#time.tzset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.