我有一个Flask后端,我正在设置测试 . 它处于早期阶段,因此目前只设置了4个测试,但每个测试都会命中一个重定向 endpoints ,然后应该重定向到特定位置(取决于登录,会话等) . 所有4个测试都在一个新的 pipenv
环境中本地传递,使用与Travis相同的 Pipfile
设置 . 出于某种原因,它在Travis中的重定向上表现不尽如人意 . 作为参考,这是我的测试结构:
class TestApp(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def test_blind_home_redirect(self):
result = self.app.get('/')
self.assertEqual(result.status_code, 302)
result = self.app.get('/', follow_redirects=True)
self.assertEqual(result.status_code, 200)
第一个断言不会抛出错误,但是第二个断言检查它是否遵循重定向返回的是 status_code
,而不是200.对不同 endpoints 使用相同逻辑的第二个测试也通过302检查但是200与另一个失败404.检查所有"protected" endpoints 重定向到登录的结果是导致返回意外的对象:
for endpoint in known_login_check:
result = self.app.get('/{}'.format(endpoint))
self.assertEqual(result.status_code, 302)
# Make sure all are sent to the index
result = self.app.get('/{}'.format(endpoint),
follow_redirects=True)
sender = pathlib.Path(result.response.file.name)
self.assertEqual(sender.name, 'index.html')
这个在 sender = ...
上失败 AttributeError: 'ClosingIterator' object has no attribute 'file'
,但又在本地传递 .
这些都似乎只在重定向时失败 . 是否有一些东西应该设置为允许这在Travis中工作,由于某种原因我的本地机器正在处理?再次,这是完全相同的 Pipfile
正在使用,我删除了我的本地环境并重新设置以确保,但它仍然在本地传递 .