更多的pythonic方式来测试

问题描述 投票:1回答:2

好的我正在为我的Django应用程序编写测试。我试图找出一种方法来迭代测试ajax寄存器调用以获得适当的响应。它工作正常,但我知道它可以以更有效的方式完成。

def test_ajax_register(self):
    c = Client()
    # Check register success
    response = c.post('/register/', {
        'register-username': 'testuser',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'success')
    self.assertEqual(response.status_code, 200)

    # Check register failed  username taken
    response = c.post('/register/', {
        'register-username': 'testuser',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'username already in use')

    # Check register failed email in use
    response = c.post('/register/', {
        'register-username': 'testuser1',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'email already in use')

    # Check register failed  password length
    response = c.post('/register/', {
        'register-username': 'testuser2',
        'register-email': '[email protected]',
        'register-password': 'pass'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'password must be atleast 8 characters long')
python django django-testing
2个回答
0
投票

只需将测试分成不同的方法即可。只要它们具有前缀test_,它们中的每一个都将被触发,并且每个测试也可以作为独立测试运行。如果需要预设任何东西,请使用setUp和tearDown方法。

def test_ajax_register(self):
    # Test registration 
    c = Client()
    # Check register success
    response = c.post('/register/', {
        'register-username': 'testuser',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'success')
    self.assertEqual(response.status_code, 200)


def test_failed_registration(self):
    # Check register failed  username taken
    response = c.post('/register/', {
        'register-username': 'testuser',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'username already in use')


def test_email_in_use(self):
    # Check register failed email in use
    response = c.post('/register/', {
        'register-username': 'testuser1',
        'register-email': '[email protected]',
        'register-password': 'password'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'email already in use')


def test_password_length(self):
    # Check register failed  password length
    response = c.post('/register/', {
        'register-username': 'testuser2',
        'register-email': '[email protected]',
        'register-password': 'pass'
    })
    self.assertEqual(json.loads(response.content)['status'], 'fail')
    self.assertEqual(json.loads(response.content)['error_msg'], 'password must be atleast 8 characters long')

0
投票

一般来说,你应该逻辑地拆分大测试并遵循UnitOfWork_StateUnderTest_ExpectedBehavior test method naming practice

数据驱动测试的概念也是一个相关的想法,我个人过去曾亲自使用ddt package然后切换到pytest parameterized tests - 这是一个很好的方法,使用不同的输入数据测试被测函数的相同特定“症状” - 这有助于防止代码重复(DRY principle)并使您的测试更有条理和可读性。

© www.soinside.com 2019 - 2024. All rights reserved.