当代码推给Jenkins时,随机测试失败

时间:2021-08-16 22:01:15

I have asked this one earlier too but am not satisfied with the answer.

我之前也问过这个问题,但是我不满意这个答案。

What I use:

我使用:

  • Working on Django/python website.
  • Django / python网站上工作。
  • Development done on python virtual envs locally.
  • 在本地python虚拟env上完成的开发。
  • using GIT as my SCM
  • 使用GIT作为我的SCM
  • have separate virtual servers deployed for Developer and Production branches for GIT
  • 是否为GIT部署了独立的虚拟服务器
  • Using Jenkins CI for continuous Integration. Separate Virtual server deployed for Jenkins
  • 使用Jenkins CI进行持续集成。为Jenkins部署的独立虚拟服务器

Working:

工作:

  • I have Unit tests, smoke tests and Integration tests for the website. Jenkins has been setup so that whenever code is pushed from my local git branch to Developer and Production branch on git repo, a build is triggered in Jenkins.
  • 我有单元测试,烟雾测试和网站集成测试。Jenkins已经设置好了,所以当代码从本地git分支推到git repo上的开发和生产分支时,Jenkins中就会触发一个构建。

Issue:

问题:

  • My tests are passing locally when I do a 'python manage.py test'
  • 当我进行“python管理”时,我的测试正在本地通过。py测试”
  • Random tests (mostly unit tests) FAIL in Jenkins when code is pushed to other branches (Developer and Production).
  • 当代码被推到其他分支(开发人员和产品)时,Jenkins中的随机测试(大部分是单元测试)将失败。
  • After a test failure, if I do do a build manually by pressing the 'Build Now' button in Jenkins, the tests usually pass and the build is successful.
  • 在测试失败之后,如果我通过在Jenkins中按下“现在构建”按钮来手动进行构建,那么测试通常会通过,构建是成功的。
  • Sometimes, when no changes are made to the code and code is still pushed to these branches, the tests are randomly failing in Jenkins.
  • 有时,当对代码不做任何更改,代码仍然被推到这些分支时,Jenkins中的测试就会随机失败。

Some common Errors:

一些常见的错误:

  • AssertionError: 302 != 200
  • AssertionError:302 ! = 200
  • TypeError: 'NoneType' object is not subscriptable
  • 类型错误:'NoneType'对象不是可下标的。
  • IndexError: list index out of range
  • 索引错误:列出超出范围的索引
  • AssertionError: datetime.datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime.datetime(2012, 12, 5, 0, 0, 27, 239884)
  • AssertionError:datetime。datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime。datetime(2012, 12, 5, 0, 0, 27, 239884)
  • AssertionError: Response redirected to 'x' expected 'y'
  • AssertionError:响应重定向到x' expected 'y'

Troubleshooting till date:

故障排除,直到日期:

  • Ran all the tests locally on my machine and also on the virtual server. They are running fine.
  • 在我的机器上和虚拟服务器上运行所有的测试。他们正在运行好。
  • Ran the individual failing tests locally and also on the virtual server. They are running fine.
  • 在本地和虚拟服务器上运行失败的测试。他们正在运行好。
  • Tried to recreate the failing conditions but as of now, the tests are passing.
  • 试图重新创建失败的条件,但到目前为止,测试正在通过。

The only problem I see is that whenever the code is pushed to the developer and production brnaches, the random test failure kicks in. Some tests fail repeatedly.

我看到的唯一问题是,每当代码被推给开发人员和产品时,就会出现随机测试失败。一些测试失败多次。

Can anyone tell me what more I can do to troubleshoot this problem. I tried googling the issue but in vain. I know xunitpatterns website has some good insights on the erratic tests behaviour but it is not helping since I tried most of the stuff there.

谁能告诉我我还能做些什么来解决这个问题吗?我试图在谷歌上搜索这个问题,但没有成功。我知道xunitpatterns网站对不稳定的测试行为有一些很好的见解,但它并没有帮助,因为我尝试了那里的大多数东西。

2 个解决方案

#1


1  

This is a really tough question to answer.

这是一个很难回答的问题。

It is possible there are some common pitfalls django developers falls into, but those I do not know.

django开发人员可能会遇到一些常见的缺陷,但是我不知道。

Outside of that, this is just normal debugging:

除此之外,这只是正常的调试:

  1. Find a way to reproduce the failure. If you can make the test fail on your own laptop, great. If you cannot, you have to debug it on the machine where it fails.

    找到重现失败的方法。如果你能在自己的笔记本电脑上测试失败,那太棒了。如果不能,则必须在发生故障的机器上进行调试。

  2. Get more information. Asserts can be made to print a custom message when they fail. Print values of relevant variables. Add debug printouts into your code and tests. See where things are not the way they are supposed to be. Google how to use the Python debugger.

    获得更多的信息。断言可以在失败时打印自定义消息。打印相关变量的值。在代码和测试中添加调试打印输出。看到事情不应该是这样的。如何使用Python调试器。

Keep an open mind. The bug can be anywhere: in the hardware, the software environment, your code or in the test code. But unless you are god, Linus Torvalds or Brian Kernighan it is a safe first hypothesis the bug originates somewhere between your keyboard and back of your seat. (And all the three hackers above have made bad bugs too.)

保持开放的心态。bug可以在任何地方:硬件、软件环境、代码或测试代码中。但除非你是上帝,莱纳斯·托瓦兹或布莱恩·克尼汉这是一个安全的第一个假设这个错误起源于你的键盘和座位之间。(以上三名黑客也制造了糟糕的bug。)

#2


0  

For the issue - AssertionError: datetime.datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime.datetime(2012, 12, 5, 0, 0, 27, 239884)

对于这个问题——AssertionError: datetime。datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime。datetime(2012, 12, 5, 0, 0, 27, 239884)

I have used the freezegun library to make date time more predictable. I am not sure why this issue comes but freezing date and time helps to some extent.

我使用了freezegun库使约会时间更可预测。我不知道为什么会出现这个问题,但在某种程度上,冻结日期和时间是有帮助的。

example:

例子:

from freezegun import freeze_time
import datetime
import unittest


@freeze_time("2012-01-14")

def test():

assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)

#1


1  

This is a really tough question to answer.

这是一个很难回答的问题。

It is possible there are some common pitfalls django developers falls into, but those I do not know.

django开发人员可能会遇到一些常见的缺陷,但是我不知道。

Outside of that, this is just normal debugging:

除此之外,这只是正常的调试:

  1. Find a way to reproduce the failure. If you can make the test fail on your own laptop, great. If you cannot, you have to debug it on the machine where it fails.

    找到重现失败的方法。如果你能在自己的笔记本电脑上测试失败,那太棒了。如果不能,则必须在发生故障的机器上进行调试。

  2. Get more information. Asserts can be made to print a custom message when they fail. Print values of relevant variables. Add debug printouts into your code and tests. See where things are not the way they are supposed to be. Google how to use the Python debugger.

    获得更多的信息。断言可以在失败时打印自定义消息。打印相关变量的值。在代码和测试中添加调试打印输出。看到事情不应该是这样的。如何使用Python调试器。

Keep an open mind. The bug can be anywhere: in the hardware, the software environment, your code or in the test code. But unless you are god, Linus Torvalds or Brian Kernighan it is a safe first hypothesis the bug originates somewhere between your keyboard and back of your seat. (And all the three hackers above have made bad bugs too.)

保持开放的心态。bug可以在任何地方:硬件、软件环境、代码或测试代码中。但除非你是上帝,莱纳斯·托瓦兹或布莱恩·克尼汉这是一个安全的第一个假设这个错误起源于你的键盘和座位之间。(以上三名黑客也制造了糟糕的bug。)

#2


0  

For the issue - AssertionError: datetime.datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime.datetime(2012, 12, 5, 0, 0, 27, 239884)

对于这个问题——AssertionError: datetime。datetime(2012, 12, 5, 0, 0, 27, 218397) != datetime。datetime(2012, 12, 5, 0, 0, 27, 239884)

I have used the freezegun library to make date time more predictable. I am not sure why this issue comes but freezing date and time helps to some extent.

我使用了freezegun库使约会时间更可预测。我不知道为什么会出现这个问题,但在某种程度上,冻结日期和时间是有帮助的。

example:

例子:

from freezegun import freeze_time
import datetime
import unittest


@freeze_time("2012-01-14")

def test():

assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)