Posts Tagged ‘Method’
현재 함수의 이름 얻기
# 현재 함수의 이름 얻기
def whoami():
import sys
return sys._getframe(1).f_code.co_name
def whoami():
import sys
return sys._getframe(1).f_code.co_name
# 현재 함수의 caller 이름 얻기
def callersname():
import sys
return sys._getframe(2).f_code.co_name
출처 : Python Recipe 66062: Determining Current Function Name
python class의 메소드별 단위 테스트 (unittest pyUnit vs py.test)
여러가지 단위 테스트 프로그램이 존재하나,
그 중, standard library로 포함되어 있는 pyUnit (unittest) 과 py.test를 비교하여 간략하게
비교한다.
ㅁ 테스트를 위한 클래스
class A:
def getA(self):
return ‘a’
def getA(self):
return ‘a’
def getWrongA(self):
return ‘b’
return ‘b’
if __name__ == ‘__main__’:
aa = A()
print aa.getA()
aa = A()
print aa.getA()
* 위의 코드에서 보는 바와 같이, 테스트 메소드는 두 개(getA와 getWrongA)이다.
* 각 메소드는 정상 상황(‘a’를 리턴할 것이라 예상하는 상황에서 실제로 ‘a’를 리턴)과 오류 상황(‘a’를 리턴할 것이라 예상하는 상황에서 실제로는 ‘a’가 아닌 다른 값을 리턴)을 나타낸다.
ㅁ 설치 방법
1. pyUnit : 별도의 설치 과정 불필요
2. py.test : 별도의 설치 과정 필요 (http://pypi.python.org/pypi/py 참고)
ㅁ 테스트 코드 작성 방법
1. pyUnit
from a import A
import unittest
class ATester(unittest.TestCase):
def setUp(self):
print ‘setUp called..’
self.aa = A()
def setUp(self):
print ‘setUp called..’
self.aa = A()
def testGetA(self):
print ‘testGetA..’
self.assertEquals(self.aa.getA(), ‘a’)
print ‘testGetA..’
self.assertEquals(self.aa.getA(), ‘a’)
def testGetWrongA(self):
print ‘testGetWrongA..’
self.assertEquals(self.aa.getWrongA(), ‘a’)
print ‘testGetWrongA..’
self.assertEquals(self.aa.getWrongA(), ‘a’)
def tearDown(self):
print ‘tearDown called..’
print ‘tearDown called..’
if __name__ == ‘__main__’:
testSuite = unittest.TestSuite()
for testmethod in (‘testGetA’, ‘testGetWrongA’):
testSuite.addTest(ATester(testmethod))
testSuite = unittest.TestSuite()
for testmethod in (‘testGetA’, ‘testGetWrongA’):
testSuite.addTest(ATester(testmethod))
runner = unittest.TextTestRunner()
runner.run(testSuite)
runner.run(testSuite)
2. py.test
from a import A
def test_getA():
aa = A()
assert aa.getA() == ‘a’
aa = A()
assert aa.getA() == ‘a’
def test_getWrongA():
aa = A()
assert aa.getWrongA() == ‘b’
aa = A()
assert aa.getWrongA() == ‘b’
ㅁ 실행 방법
1. pyUnit
python test_with_unittest.py (위의 예제 파일 이름이 test_with_unittest.py임)
2. py.test
py.test (이름 지정하지 않아도 찾아서 테스트 실시)
ㅁ 실행 결과
1. pyUnit
setUp called..
testGetA..
tearDown called..
.setUp called..
testGetWrongA..
FtearDown called..
testGetA..
tearDown called..
.setUp called..
testGetWrongA..
FtearDown called..
======================================================================
FAIL: testGetWrongA (__main__.ATester)
———————————————————————-
Traceback (most recent call last):
File “test_with_unittest.py”, line 16, in testGetWrongA
self.assertEquals(self.aa.getWrongA(), ‘a’)
AssertionError: ‘b’ != ‘a’
FAIL: testGetWrongA (__main__.ATester)
———————————————————————-
Traceback (most recent call last):
File “test_with_unittest.py”, line 16, in testGetWrongA
self.assertEquals(self.aa.getWrongA(), ‘a’)
AssertionError: ‘b’ != ‘a’
———————————————————————-
Ran 2 tests in 0.000s
Ran 2 tests in 0.000s
FAILED (failures=1)
2. py.test
================================ test process starts ================================
executable: /usr/bin/python (2.5.2-final-0)
using py lib: /root/jhkim/lib/py-0.9.2/py <rev unknown>
executable: /usr/bin/python (2.5.2-final-0)
using py lib: /root/jhkim/lib/py-0.9.2/py <rev unknown>
test_with_py.py[2] .F
======================================================================================
________________________________entrypoint: test_getWrongA _____________________________
def test_getWrongA():
aa = A()
E assert aa.getWrongA() == ‘a’
> assert ‘b’ == ‘a’
+ where ‘b’ = <a.A instance at 0x838392c>.getWrongA()
________________________________entrypoint: test_getWrongA _____________________________
def test_getWrongA():
aa = A()
E assert aa.getWrongA() == ‘a’
> assert ‘b’ == ‘a’
+ where ‘b’ = <a.A instance at 0x838392c>.getWrongA()
/root/test/test_with_py.py:9: AssertionError
_______________________________________________________________________________________
tests finished: 1 passed, 1 failed in 0.18 seconds ========================================================
_______________________________________________________________________________________
tests finished: 1 passed, 1 failed in 0.18 seconds ========================================================
사용 목적이나 규모에 따라, 사용자 기호에 따라 각각 장단점이 존재한다!