Python - ãã¹ããã¼ã¿ã®å®ç¾©ã¯ã©ãããã®ããããèãã¦ã¿ã
ã¾ãã¯Pythonã§DataProviderã使ãæ¹æ³ã
ãã£ãã
ãã¹ããã¼ã¿ã¯setUpã§å®£è¨ããã¹ãå¹¾ã¤ãã®çç±ãèªã¿ãããã¯DataProviderã®æ¹ãé©åã§ã¯ãªããã¨ããæ°ããã¾ããã
DataProvider(ãã¼ã¿ãããã¤ãã¼)ã¨ã¯
ç°¡åã«è¨ãã°ãããã¹ããã¼ã¿ã®éåãè¿ãé¢æ°(orã¡ã½ãã)ãã®ãã¨ã§ãã
å
è¨äºã«ããã¦ã¯ 代ç¨: ãã¹ããã¼ã¿ãè¿ãã¦ãããé¢æ°ã宣è¨ãã
ã«ãããã¾ãã
PHPUnit(PHP)ãTestNG(Java)ã«ã¯æ¨æºã§ä»ãã¦ããæ©æ§ã§ãã
(ã°ã°ã£ãããããªã«æ
å ±ãªãã£ãã®ã ãã©ããã¾ãæå/æç¨ãªæ©è½ãããªãã®ããª?)
- JUnitã«ã¯ãªãTestNGã®â5âã¤ã®å¼·åãªãã¹ãç¨æ©è½
- ã³ã¼ãå質ã追æ±ãã: JUnit 4 対 TestNG
- ãã¼ã¿ãããã¤ã
åèè¨äºãè¦ã¦åããéããDataProviderã¯ã¢ããã¼ã·ã§ã³ãä»ãã¦ãã¹ãã¡ã½ããã¨ç´ä»ãããã¾ãã
ããã«ããããã¹ããã¼ã¿ãæ±ãã¤ã¤ãã¹ãæ¬ä½ã¯ç°¡æ½ããä¿ã¤ãã¨ãã§ãã¾ãã
ãã¤ãsetUpçã§åé¡è¦ããã¦ãããé¢ä¿ãªããã¹ãã§ãæ¯åä½ã£ã¡ãããã¨ãããã¨ãããã¾ããã
Pythonã§DataProvider(æºåç·¨)
Pythonã«ã¯ãã³ã¬ã¼ã¿ã¨ããã¤ã«ããæ©è½ãããã¾ãã®ã§ãããã使ãã°ç°¡åã«åºæ¥ãããªæ°ããã¾ãã
ã¨ããæãã§ã°ã°ã£ãã¨ãããããpackageãè¦ã¤ããã¾ããã
ããã使ãã°å®ç¾ããããªæ°ãããååã§ãã
installãã¦è©¦ããã¨ãã¾ããããç§ã®ç°å¢ã§ã¯DistributionNotFoundãåºã¦ãã¾ãæ念ã§ãã (Pythonåå¼·ããã¦ã§pipã¨ãã¾ã ããåãã£ã¦ããªãã®ã§æ·±ã調æ»ãã)
幸ãã«ããµã¤ãã«ã¯ã³ã¼ããè¼ã£ã¦ãã¾ãã®ã§ããããæåãã¦å®éã«åãã試ãã¦ã¿ã¾ãã
Pythonã§DataProvider(ã試ãç·¨)
ããã§ã¯unittest.pyã使ãã¾ããããã³ã¬ã¼ã¿ã§ãããã¶ãä»ã®ãã¹ãã§ã大ä¸å¤«ã§ãããã
次ã®åç´ãªè¶³ãç®ã¡ã½ããã¨å¼ãç®ã¡ã½ããããã¹ããã¾ãã
ãã¹ããã¼ã¿ã®ä½ãæ¹ããã¹ãã¡ã½ããå
å®ç¾©ããã«ãã¼ã¡ã½ãããsetUpããã¼ã¿ãããã¤ãã¼ã§åãã¦ã¿ã¾ãã
def plus(a, b): return a + b def minus(a, b): return a - b
ãã¹ãã¡ã½ããå ã§å®ç¾©
import unittest class TestDataInTestMethod(unittest.TestCase): def test_plus(self): testdata_integers = [(1, 2), (-1, 2), (0, 0)] for a, b in testdata_integers: self.assertEqual(plus(a, b), a + b) def test_minus(self): testdata_integers = [(1, 2), (-1, 2), (0, 0)] for a, b in testdata_integers: self.assertEqual(minus(a, b), a - b)
- ã¡ãªãã
- ãã¹ãã¡ã½ããå ã§å®çµãã¦ããã®ã§ããã®ç¨åº¦ã®éã§ããã°è¦éãã¯ãã
- å¿ è¦ãªãã¹ãã±ã¼ã¹ã§ã®ã¿ä½æããã
- ãã¡ãªãã
- DRYãããªã
- ãã¹ããã¼ã¿çæã®å¦çãè¤éã«ãªãã¨å¯èªæ§ãä¸ããããã¹ãã®æ¬è³ªãåããã«ãããªã
ãã«ãã¼ã¡ã½ãã
import unittest class TestDataHelperMethod(unittest.TestCase): def test_plus(self): for a, b in self.__testdata_integers(): self.assertEqual(plus(a, b), a + b) def test_minus(self): for a, b in self.__testdata_integers(): self.assertEqual(minus(a, b), a - b) def __testdata_integers(self): return [(1, 2), (-1, 2), (0, 0)]
- ã¡ãªãã
- DRY
- å¿ è¦ãªãã¹ãã±ã¼ã¹ã§ã®ã¿ä½æããã
- ãã¡ãªãã
- å è¨äº(ãã¹ããã¼ã¿ã¯setUpã§å®£è¨ããã¹ãå¹¾ã¤ãã®çç±)ãåè
setUp
import unittest class TestDataInSetUp(unittest.TestCase): def setUp(self): self.__testdata_integers = [(1, 2), (-1, 2), (0, 0)] def test_plus(self): for a, b in self.__testdata_integers: self.assertEqual(plus(a, b), a + b) def test_minus(self): for a, b in self.__testdata_integers: self.assertEqual(minus(a, b), a - b)
- ã¡ãªãã
- DRY
- ãã¡ãªãã
- å è¨äº(ãã¹ããã¼ã¿ã¯setUpã§å®£è¨ããã¹ãå¹¾ã¤ãã®çç±)ãåè
- ä¸è¨åç §
ã¾ããå è¨äºã«ãã次ã®ç¹ã«é¢ãã¦ç§ã¯ãã¾ãè³æã§ãã¾ããã§ããã
ã©ã®ãããªãã¹ããã¼ã¿ã使ããã¦ããããä¸ç®çç¶ã«ãªã
ç§ã®è§£éãééã£ã¦ããå¯è½æ§ãããã¾ãããsetUpã«å¯ããå ´åããããã®ç¹ã¯å¯èªæ§ãè½ã¡ãã®ã§ã¯ãªãã§ããããã
ãã¹ãã¯ã©ã¹ã®åä½ã«å¯ãã¾ããæ®éã«1ã¯ã©ã¹1ãã¹ãã¯ã©ã¹ã§ä½ãã¨ãã¦ã
setUpã«ãã¹ããã¼ã¿ãã¾ã¨ããããã¡ããã¡ããã¡ããæ°ããã¾ããã
å
è¨äºããã®è¨äºã®ãµã³ãã«ã¯ãã¹ããã¼ã¿ã®éãå°ãªãã®ã§å
¨ç¶åé¡ã«ã¯ãªãã¾ãããã
ããä¸ã¤ã¯ãsetUpé¢æ°ãè¨ãããã¨ã«å¯¾ãã¦ã©ãã¢ããã¼ããåããã¨ããåé¡ã§ãã
1è¡ã§å®ç¾©ãæ¸ããããªãã¹ããã¼ã¿ã®é£ç¶ãªãã°ãããªã«æ°ã«ãªããªãããããã¾ãããã
ããããæºåã«æ°å¦çå¿
è¦ãªãã¹ããã¼ã¿ãè¤æ°å®ç¾©ããã¨ãsetUpã¡ã½ããå
ãè¨ãã¾ãã
ããã¨å¯èªæ§ãä¸ããããã«ãã®ä¸ããã¾ãå¦çãåãã¦ã¡ã½ããåããã°ããã£ã¦ãªãããããã¾ãããã ãããªãæåãããã¹ããã¼ã¿é¢æ°ã§ããã®ã§ã¯ã¨ããæ°ããã¾ãã
æå¾ã«ããã¹ãã¯ã©ã¹ãè·¨ãã§ã®ãã¹ããã¼ã¿å
±æã«é¢ãã¦ã
ãã¹ãã¡ã½ããéã§å
±æã§ãããã¹ããã¼ã¿ã¨ããã®ã¯ãæ±ç¨çãªãã¼ã¿ã®å ´åãå¤ããã¨ã§ãããã
ãã¨ãã°æ´æ°ãã¹ããã¼ã¿ã¨ãã¦[è² æ°, 0, æ£æ°]ã®ãããªã
æ±ç¨çãªãã¼ã¿ã®å ´åãè¤æ°ã®ãã¹ãã¯ã©ã¹ã§ä½¿ãããå¯è½æ§ãããã¾ãã
ãã®éã«setUpæ¹å¼ã ã¨ãçµå±ã¯ãã³ããã§æ¸ã¾ããããäºæ
ãçºçãã¾ãã
TestCaseãç¶æ¿ããã¯ã©ã¹ããã¾ãã°ããã¨ããæè¦ãããããããã¾ããã
ãããã¢ãªããããã¾ãããããã¹ããã¼ã¿ã«é¢ãã¦è¨ãã°ãã¯ãä¸è¨åé¡ç¹1ã¤ç®ããããæ°ã«ãªãã¾ãã
å è¨äºã§ãä»°ããã¦ãã¾ããã
å½ç¶ã®ãã¨ãªããããã¼ã¿ããã®ã±ã¼ã¹ã®è¿ãã«ãã£ãã»ããããå ´åãããã¾ããã¨ããã®ãããã®ã»ããåç §ãæ©ããsetUpã«ãããã¨ãããã¾ã§é¡ããªããã°ãããªããªã£ããããã¾ãã
ã¨ãããã¨ã§ããã¹ãã¡ã½ããããé ãããã»ã©å¯èªæ§ãè½ã¡ãå±éºæ§ãããã¾ãã
DataProvider
ä¸è¿°ã®unittest-data-providerãinstallåºæ¥ãã°ãã¦ããã¾ãã
ä¸è¨ã³ã¼ãã§ã¯installåºæ¥ãªãå ´åã®ããã«ããã³ã¬ã¼ã¿ãèªåå®ç¾©ãã¦ãã¾ãã
import unittest # see https://pypi.python.org/pypi/unittest-data-provider/1.0.0 def data_provider(fn_data_provider): def test_decorator(fn): def repl(self, *args): for i in fn_data_provider(): try: fn(self, *i) except AssertionError: print "Assertion error caught with data set ", i raise return repl return test_decorator class TestDataByDecorator(unittest.TestCase): __testdata_integers = lambda: [(1, 2), (-1, 2), (0, 0)] @data_provider(__testdata_integers) def test_plus(self, a, b): self.assertEqual(plus(a, b), a + b) @data_provider(__testdata_integers) def test_minus(self, a, b): self.assertEqual(minus(a, b), a - b)
- ã¡ãªãã
- DRY
- å¿ è¦ãªãã¹ãã±ã¼ã¹ã§ã®ã¿ä½æããã
- ãã¡ãªãã
- ãã«ãã¼ã¡ã½ããã¨åæ§
ãããããã¹ããã¼ã¿ãå ±éåãããã¨ã®é£ãã
ç§ã¯æ¥åã§PHPUnit使ã£ã¦ããã¤ãã®ã¦ããããã¹ããæ¸ãããã¨ãããã¾ãã
PHPUnitã¯æ¨æºã§DataProviderãããã¾ãã®ã§ãæåã¯ãããé »ç¹ã«ä½¿ç¨ãã¦ãã¾ããã
ããããã¹ããä½æ/ã¡ã³ããã³ã¹ãã¦ãããã¡ã«ãDataProviderã¯ä½¿ãã©ãããé£ããã¨æããããã«ãªãã¾ããã
ã¨ããã®ããåºæ¬çã«å
¥å(ãã¼ã¿)ã¨åºå(å®è¡çµæ)ã¯ã²ãã¥ãã¦ããããã§ãã
ãAãã¨ãããã¼ã¿ãä¸ããã°ãXããè¿ããã¨ãããã¦ããã¯åºæ¥ãã ãåºå®å¤ã®æ¹ãããã¨æã£ã¦ãã¾ãã
ä¸è¨ãµã³ãã«ã³ã¼ãã§ã¯assertã®å¯¾è±¡ã¨ãã¦ããã¨ãa + bãã¨ãã§æ¸ãã¾ãããã
æ¬å½ã¯ããã¯å¾®å¦ãããªãããªã¨æã£ã¦ãã¾ãã
æ¬æ¥ã¯assertEqual(plus(a, b), 3)
ãªã©æ¥µååºå®å¤ã®æ¹ãããã¨èãã¦ãã¾ãã
ããã«å¯¾å¿ããããã«ãDataProviderã渡ããã¼ã¿ã¨ãã¦[å¼æ°ãæå¾
å¤]ã®ãããªãã¢ã使ããã¨ãããã¾ããã
ä¸è¨ãµã³ãã«ã§è¨ãã°ãä¾ãã°ãã®ããã«ãªãã¾ãã
class TestDataByDecorator2(unittest.TestCase): __testdata_integers = lambda: [[1, 2, 3], [-1, 2, 1], [0, 0, 0]] @data_provider(__testdata_integers) def test_plus(self, a, b, expected): self.assertEqual(plus(a, b), expected)
ãã®å¯¾å¿ã®å¤§ããªåé¡ç¹ã¯ãçµå±ãã¹ãã¡ã½ãããã¨ã«ãã¼ã¿ãç¨æããã®ã¨å¤ãããªããã¨ãããã¨ã§ãã
ãã¨ãã°ä¸è¨ã¯ãã®ã¾ã¾test_minusã«é©ç¨ããã¨ãå½ããåã§ãããã¹ãã失æãã¾ãã
ã¾ã¨ã
以ä¸ãè¸ã¾ãã¦ãç§å人ã®èãã¯ä»¥ä¸ã®ã¨ããã§ãã
- DRYã«ãããã¦ããã¤ç´ç²ã«ããã¹ããã¼ã¿ãã ãã®åãåºãã ãã§æ§ããªã
=> DataProviderãããã¯ãã«ãã¼ã¡ã½ãã
ä¾ãã°ãç¹å®ç¯å²ã®ãã¼ã¿ã«é¢ãã¦ã¯å¿ ã決ã¾ã£ãå¤ãè¿ããé¢æ°ã®ãã¹ããã¼ã¿ãªã©
- ãã¹ããã¼ã¿ä»¥å¤ã§å ±éå¦çãè¡ãããã
=> setUp()
- ãã以å¤
=> ãã¹ãã¡ã½ããå ã§å®ç¾©
setUpã®ãã¡ãªããã§ãããããè¿°ã¹ã¾ããããç§ã¯ãã¹ãã³ã¼ãã«é¢ãã¦ã¯å¿
ãããDRYãããªãã¦ãããã¨æã£ã¦ãã¾ãã
(DRYã«ãã¦ã¯ãããªãã§ã¯ãªã DRYã«åºå·ããªãã¦ãããã¨ããæå³ã§ã)
ããã°ã©ã5,6å¹´ã¨æµ
ãã§ãããçµé¨ä¸ç¡çã«DRYã«ããã¨éã«å¯èªæ§ãã¡ã³ããã³ã¹æ§ãä¸ãããã¨ãããã¾ããã®ã§...ã
ãã¡ããç§ã®ãã¹ãã®æ¸ãæ¹ãã¾ã ã¾ã ä¸æãªå¯è½æ§ãååã«ããã¾ãã
(念ã®ããè£è¶³ãã¾ãããå®è£
ã³ã¼ãã¯çµ¶å¯¾DRYãããã§ã)
ããããã¨æ¸ãã¾ãããå¿ ãããä¸è¨ã®éãã§ã¯ãªããçµå±ã¯ãã©ã³ã¹ãã¨æãã¾ãã(便å©ãªéãè¨è)
ãã®è¨äºã®ãµã³ãã«ã³ã¼ãã¯Gistã«ããã£ã¦ãã¾ãã