[Python] UnitTest๐Ÿšฅ

Inah-_-ยท2021๋…„ 4์›” 2์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
20/21
post-thumbnail

What is Unit Test?

UnitTest๋ž€, ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋ฅผ ํ…Œ์ŠคํŠธ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ
์ž‘์„ฑํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ํŒŒ์ด์ฌ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ ์•„์ฃผ ์œ ์šฉํ•œ ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ์ด๋‹ค.

์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋ž€ ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค.

Python Unit Test

TestCate - UnitTest framework์˜ ํ…Œ์ŠคํŠธ ์กฐ์ง์˜ ๊ธฐ๋ณธ ๋‹จ์œ„
Fixture - ํ…Œ์ŠคํŠธ ์ง„ํ–‰์‹œ ํ•„์š”ํ•œ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ ํ˜น์€ ์„ค์ •, ์ฃผ๋กœ ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํ–‰ ๋˜๊ธฐ ์ „์ด๋‚˜ ํ›„์— ์ƒ๊ธด๋‹ค.
Assertion - UnitTest์—์„œ ํ…Œ์ŠคํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ๋๋Š”์ง€ ํ™•์ธ

ํšŒ์›๊ฐ€์ž… API UnitTest๊ฐ€ ์ฐธ์ผ ๋•Œ

class UserSignUp(TestCase):
    def setUp(self):
    	User.objects.create(
            email    = 'inah@unit.test',
            password = '^-^test1234'
        )
        
    def tearDown(self):
    	User.objects.all().delete()
        
    def test_signup_success(self_:
        client = Client()
        user   = {
            'email'    : 'rara@unit.test',
            'password' : 'rarara^^777'
        }

        response = client.post('user/signup', json.dumps(user), content_type='application/json')
        self.assertEqual(response.status_code, 201)

ํšŒ์›๊ฐ€์ž… API UnitTest๊ฐ€ ๊ฑฐ์ง“์ผ ๋•Œ

1. database์— ์ด๋ฏธ ์žˆ๋Š” email์ผ ๋•Œ

def test_signup_email_exists(self):
    client = Client()
    
    user = {
        'email'    : 'rara@unit.test',
        'password' : 'rarara^^777'
    }
    
    response = client.post('user/signup', json.dumps(user), content_type='application/json')
    self.assertEqual(response.status_code, 400)
    self.assertEqual(response.json(), {'message' : 'ALREADY_EXISTS_EMAIl'})
profile
Backend Developer

0๊ฐœ์˜ ๋Œ“๊ธ€