ãã¼ã 1. Python ãå§ãã 2. åºæ¬ä»æ§ 3. ã¯ã©ã¹ 4. ã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸ 5. ãã¡ã¤ã«æä½ 6. ä¾å¤ 7. ã¸ã§ãã¬ã¼ã¿ 8. ãã¹ã
æ¦è¦ pytest ã®åºæ¬çãªä½¿ãæ¹ãè¨è¼ãã¾ãã é©å®åç §ããããã®å ¬å¼ããã¥ã¡ã³ããã¼ã¸ Full pytest documentation API Reference ã¤ã³ã¹ãã¼ã« é©å½ãªããã±ã¼ã¸ããã¼ã¸ã£çã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã sudo apt install python-pip pip install pytest which pytest /home/vagrant/.local/bin/pytest å®è¡æ¹æ³ test_sample.py #!/usr/bin/python # -*- coding: utf-8 -*- class TestClass(object): def test_one(self): x = "this" assert 'h' in x def test_two(self): self.y = "hello" assert hasattr(s
Pytestãå®è¡ããã¨ãã«ç¥ã£ã¦ããã¨ä¾¿å©ãªã³ãã³ãï¼ãªãã·ã§ã³ãã¾ã¨ãã¾ããã ç°å¢ Python3.6 pytest 4.3.0 venv ç¹å®ã®ãã¹ãã®ã¿ãå®è¡ãã ã¢ã¸ã¥ã¼ã«ãæå®ãã¦å®è¡ ã¢ã¸ã¥ã¼ã«æ¯ã«ãã¹ããå®è¡ãã¾ãã pytest test_1.py ãã¹ãæå®ãã¦å®è¡ ãã©ã«ãå¥ã«ãã¹ããå®è¡ãã¾ãã pytest testing/ ãã¼ã¯ã¼ããæå®ãã¦å®è¡ æååã«å«ã¾ãããã¼ã¯ã¼ãã«ããããããã®ã®ã¿ãå®è¡ãã¾ãã ãããã³ã°å¯¾è±¡ã¯ãã¡ã¤ã«åãã¯ã©ã¹åãé¢æ°åã§ãã pytest -k "MyClass and not method" ä¸ã®ä¾ã®å ´åãâTestMyClassâã®ãã¼ã¯ã¼ããå«ããã¹ããå®è¡ãã¾ãããâmethodâãã¼ã¯ã¼ããå«ããã®ã¯é¤ãã¾ãã ä¾ãã°ã "TestMyClass.test_something" ã¯å®è¡ããã¾ããã âTestMyC
Python + pytestã«ã¦ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã追å ãã¦ãã³ãã³ãã©ã¤ã³ããå¤ãåãåã£ãæã®ã¡ã¢ãæ®ãã¾ãã ç®æ¬¡ ç°å¢ pytestã®ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è¿½å æ¹æ³ conftest.pyã®å®è£ ãã¹ãã³ã¼ãã®å®è£ ãã¹ãã®å®è¡ å¿ç¨ï¼ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã§æå®ãããã¹ãã³ã¼ã以å¤ã¯ã¹ããããã conftest.pyã®å®è£ ãã¹ãã³ã¼ãã®å®è£ ãã¹ãã®å®è¡ conftest.pyã«ã¤ã㦠ã½ã¼ã¹ã³ã¼ã ç°å¢ Python 3.6.3 pytest 3.2.3 pytestã®ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è¿½å æ¹æ³ å ¬å¼ããã¥ã¡ã³ãã«æ å ±ãããã¾ããã Basic patterns and examples â pytest documentation conftest.py ã«ã¦ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è¿½å ãå®è£ ãã¹ãã³ã¼ãã«ã¦ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®å¤ãå
pytestã®ãã¹ãé¢æ°ã«ãã©ã¡ã¼ã¿ã¤ãã¦ã¿ã(parametrize, pytest_generate_tests)Pythonãã¹ãèªååpytest åä½ãã¹ãæ¸ãã¦ã¾ãã? ããã©ãããã¦ãµãããã¡ã«ãªã£ã¦ãã¾ãã¾ãããã¦ãããããµãã£ã¦ã¾ãããä»å¾ãã³ã¼ãã触ãå¯è½æ§ããããªããåä½ãã¹ããæ¸ãä½æ¥ã¯å¿ ããã¤ãã¾ããä½ãªã¹ã¯ãå©ççæ°ç¾ãã¼ã»ã³ãã¨ãã夢ã®ãããªæè³ã§ãã ããèªã¿ã«æ¥ã¦ã人ã¯ããããªã®åãã£ã¦ãã¨ä¿¡ãã¦ã ãã¹ãã³ã¼ããã³ããããä½æ¥ã«é£½ãã人ã®ããã«ãpytestã®ãã¹ãé¢æ°ã«ãã©ã¡ã¼ã¿ä»ãããã¦ã¿ã¾ãã ä»åã¯ãç§ãéçºãã¦ããéåã³ã³ãã¥ã¼ã¿ç¨ã©ã¤ãã©ãªBlueqatã§å®éã«ä½¿ããã¦ãããã¹ãã³ã¼ããããå®éã®ä¾ãæãåºãã¦ã¿ã¾ãã pytest.mark.parametrizeã使ã è¦ãæ¹ãæ©ãã®ã§ããããªãå®ä¾ã§ãã @pytest.mark.par
Pythonã§ãã¹ãããã©ã¡ã¼ã¿åããæã®æ¸ãæ¹ã unittest 㨠pytest ã使ã£ãå ´åã§ç´¹ä»ããæå¾ã«ãããã§ä½¿ããparameterizedã¨ããã©ã¤ãã©ãªãç´¹ä»ãã¾ãã ã¡ãªã¿ã«ç§ã¯pytestãããunittestæ´¾ã§ãã ã¯ããã« ãã¹ããæ¸ãã¦ããã¨å¤ãéãã ãã®ã±ã¼ã¹ãè¤æ°ç¢ºèªããããªãæãããã¾ãã ä¾ãã°ã1 + 1 = 2, 2 + 3 = 5, 32 + (-32) = 0, â¦ãªã©ã®ãããªã±ã¼ã¹ã§ãã ããããã¹ã¦å¥ã®ãã¹ãã±ã¼ã¹ã¨ãã¦æ¸ãä¸ãã®ã¯é¢åã§ããã¡ã³ãã大å¤ãªãã¨ã«ãªãã®ã§ããã¹ããã¬ã¼ã ã¯ã¼ã¯ã¯å¤§æµãããªãã¹ããç°¡åã«ã§ããä»çµã¿ãæä¾ãã¦ãã¾ãã ä»åã¯Pythonã§ãã使ããã¦ããunittestï¼æ¨æºã©ã¤ãã©ãªï¼ã¨pytestã§ãããã®ãã¹ãã®æ¸ãæ¹ãç´¹ä»ãã¾ãã ãªãã§ãã¾ããï¼ã¨æããããã§ãããpytestã¯ã¨ãããunitte
â»ãã¡ãã¯Zeals AdventCalendar 12æ¥ç®ã®è¨äºã§ãã ããã«ã¡ã¯ï¼ Pythonã¨ã³ã¸ãã¢ã®èæ¨ã§ãã å¤é ã«ç¤¾å ã®éçºããã¸ã§ã¯ãã«Pythonã¡ã³ãã¼ã¨ãã¦ã¢ãµã¤ã³ãããæ¬æ ¼çã«Pythonãæ¥åã§æ¸ãããã«ãªããæè¿ã¯ã»ã¼æ¯æ¥Pythonãæ¸ãã¦ãã¾ãã Pythonã®æ¥åã¨ãã¦ã¯ä¸»ã«æ°æ©è½å®è£ ã¨DBãªãã¡ã¯ã¿ãªã³ã°ããã£ã¦ãã¾ãã ä»æ¥ã¯pytestã®parametrizeã®åºæ¬çãªä½¿ãæ¹ã¨ parametrizeã§ã¡ãã£ã¨è©°ã¾ã£ãã¨ããããã£ãã®ã§ããããã«ã¤ãã¦æ¸ãã¾ãã ç®æ¬¡ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã åæ parametrizeã«ã¤ã㦠parametrize ã¨ã¯ parametrize ã®ä½¿ãæ¹ fixtureãparametrizeãã fixtureã¨ã¯ fixtureã®parametrize çµããã« åæ ã¾ãã以åã«ãã¡ãã®è¨äºã§ç´¹ä»ãããã
ã¯ããã« ä½äºãã¾ãã¯æ¨æºè£ åã®æ©è½ããã¡ããã¨ä½¿ããããã«ãªããã¨æã£ã¦ãPythonã®Unittestãã¡ããã¡ãã触ã£ã¦ãããã§ãããæ¡ä»¶ã§ã¯pytestã使ã£ã¦ãã¾ããpytestã®æ¸ãæ¹ã«ãæ £ãã¦ãã¦ãæ¯æ¥èªãã ãæ¸ããããã¦ãã¾ãããåã身ä¸æ¹ã§èº«ã®åãã«ããã³ã¼ãä¾ããç¥ããªããããã¯ããã¾ãè¯ããªãã¨æã£ã¦ãã¡ããã¨èªåã§ãã£ã°ã£ã¦ä½¿ããããªã³ãã³ãã¨ãæ©è½ãçºæãããã¨ããã¨æãç«ã¡ã¾ããããã¨ãpytest.Parameterizeã¨ä»²è¯ããªãããã¦ãParameterizeãæ¬é¡ã§ãã ææç© pytest.Parametarize ã®åºæ¬ ããã«é¢æ°ãããã¾ããStringãIntã«å¤æãã¦ãããã ãã®ãã¤ã§ããä¸çä¸ä½ãæå³ã®ç¡ãé¢æ°ã®å ã®ï¼ã¤ã§ããã def convert_str_into_int(x): return int(x) ãã¦ããã®é¢æ°ããã¹ããã
ãã®è¨äºã¯BASE Advent Calendar 2019ã®20æ¥ç®ã®è¨äºã§ãã devblog.thebase.in PAYæ ªå¼ä¼ç¤¾ã§ããã¯ãªã¼ããåããæ±ã¨ç³ãã¾ãã 主ã«ããã¯ã¨ã³ãå ¨è¬ã«åºãæºãã£ã¦ãã¾ããæè¿ã¯ãµã¼ãã¼ã¢ããªã°ããæ¸ãã¦ãã¾ããã¤ã³ãã©ãããã¨ããã¾ãã å½ããã°ã®èªè ã®æ¹ã«ã¯å¼ç¤¾ã®ãã¨ããåããªãæ¹ãããããããã£ããããã¨æãã¾ãã®ã§ãç°¡åã«ç¤¾ã®ç´¹ä»ãããã¦ããã ãã¾ãã PAYæ ªå¼ä¼ç¤¾ã¯BASEæ ªå¼ä¼ç¤¾ã®100%åä¼ç¤¾ã§ããªã³ã©ã¤ã³æ±ºæ¸ãµã¼ãã¹ãPAY.JPãã¨ID決æ¸ãµã¼ãã¹ãPAY IDããªã©ã®æ±ºæ¸ãµã¼ãã¹ãéçºã»éå¶ãã¦ããä¼ç¤¾ã§ãã ãæ¯æãã®ãã¹ã¦ãã·ã³ãã«ã«ããããã·ã§ã³ã«æ²ãããéãæ±ããã¹ã¦ã®äºæ¥è ã»å人ããã£ã¨è±ããªçæ´»ãã§ãããã¨ãç®æãã¦ãã¾ãã ãã¦ã決æ¸ã¨ããããã·ã§ã³ã¯ãªãã£ã«ã«ãªãã¼ããæ±ãã«ããã£ã¦ãå質ä¿è¨¼ã¯æãéè¦ãªèª²é¡
ç¡æ°ã«åå¨ããpytestã®ãªãã·ã§ã³ã«ã¤ãã¦ãä»æ¥ã¯æä½éç¥ã£ã¦ãããã便å©ãªãã®ã4ã¤ãç´¹ä»ãã¾ãã å®éã«èªåãè²ã 試ãã¦ã¿ãã®ã§ãããçµå±ãã使ã£ã¦ãã®ã¯ãã®4ã¤ããããªã®ã§ãæããããã ãç¥ã£ã¦ããã°å¤§ä½ä½ã¨ããªããã¨ã§ãããâ¦ã ãªãã·ã§ã³ã®ä½¿ãæ¹ã«ã¤ã㦠以ä¸ã§ç´¹ä»ãããªãã·ã§ã³ãå©ç¨ããå ´åã¯ã$pytestãå®è¡ããéã«ããªãã·ã§ã³å¼æ°ãä»ä¸ãã¾ããä¾ãã°ã1ã¤ç®ã®ä¾ã ã¨$pytest -vã¾ãã¯$pytest --verboseã¨ããããã«å®è¡ãã¾ãã -v/--verbose ãã¹ãçµæã«ã¤ãã¦ãããã©ã«ãããã詳ããæ å ±ãåºåã -s/--capture=method ãã¹ãå®è¡ä¸ã«printæã®åºåãæ¨æºåºåã«æ¸ãåºãã --lf/--last-failed ååã®ãã¹ãã§å¤±æãããã¹ãã®ã¿åã³å®è¡ãä½åº¦ããã¹ãè¡ãéã«ä¾¿å©ã§ãã -m MARKEXPR ã½ã¼ã¹ã³
ã¯ããã« pytestã¯Pythonç¨ã®ãã¹ããã¼ã«ã§ãã æ¨æºã®unittestã«æ¯ã¹ããã¹ãã¨ã©ã¼æã®çµæãåãããããã®ãç¹å¾´ã§ãã ä¾ã¨ãã¦ãè¾æ¸ãªãã¸ã§ã¯ããæ¯è¼ãã以ä¸ã®ãã¹ãã³ã¼ããunittestã¨pytestããããã§å®è¡ãã¦ã¿ã¾ãã # test_dict.py import unittest dict1 = {'name': 'Tom', 'age': 20} dict2 = {'name': 'John', 'age': 23} class TestUnitTest(unittest.TestCase): def test_one(self): assert dict1 == dict2 $ python -m unittest F ===================================================================
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ã©ã³ãã³ã°
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}