.. _xunitsetup: =================================================== æ¡å¼µããã xUnit ã¹ã¿ã¤ã«ã®ã»ããã¢ãããã£ã¯ã¹ã㣠=================================================== .. ==================================== Extended xUnit style setup fixtures ==================================== .. _`funcargs`: funcargs.html .. _`test parametrization`: funcargs.html#parametrizing-tests .. _`unittest plugin`: plugin/unittest.html .. _`xUnit`: http://en.wikipedia.org/wiki/XUnit .. Python, Java and many other languages support xUnit_ style testing. This typically involves the call of a ``setup`` ("fixture") method before running a test function and ``teardown`` after it has finished. ``py.test`` supports a more fine-grained model of setup/teardown handling by optionally calling per-module and per-class hooks. PythonãJava ããã³ä»ã®å¤ãã®è¨èªã¯ xUnit_ ã¹ã¿ã¤ã«ã®ãã¹ãã«å¯¾å¿ãã¦ãã¾ããããã¯ãã¹ãé¢æ°ã®å®è¡åã« ``setup`` ("ãã£ã¯ã¹ãã£") ã¡ã½ãããããã¹ãé¢æ°ã®å®è¡å¾ã« ``teardown`` ã¡ã½ãããå¼ã³åºãå ¸åçãªã¹ã¿ã¤ã«ã§ãã ``py.test`` ã¯ãã¢ã¸ã¥ã¼ã«åä½ãã¯ã©ã¹åä½ã®ããã¯ãå¿ è¦ã«å¿ãã¦å¼ã³åºãã¦å¦çãããããç´°ååããã setup/teardown ã®ã¢ãã«ã«å¯¾å¿ãã¦ãã¾ãã .. Module level setup/teardown ============================================= ã¢ã¸ã¥ã¼ã«ã¬ãã«ã® setup/teardown ================================= .. If you have multiple test functions and test classes in a single module you can optionally implement the following fixture methods which will usually be called once for all the functions:: 1ã¤ã®ã¢ã¸ã¥ã¼ã«ã«è¤æ°ã®ãã¹ãé¢æ°ããã¹ãã¯ã©ã¹ãããå ´åãå¿ è¦ã«å¿ãã¦ãå ¨ã¦ã®é¢æ°ã«å¯¾ãã¦é常1度ã ãå¼ã³åºããããã£ã¯ã¹ãã£ã¡ã½ãããå®è£ ã§ãã¾ã:: def setup_module(module): """ ã¢ã¸ã¥ã¼ã«ã®å®è¡ã«é¢ãã¦ä»»æã®ç¶æ ãã»ããã¢ãããã """ def teardown_module(module): """ setup_module ã§äºåã«ã»ããã¢ããããç¶æ ã解ä½ãã """ .. Class level setup/teardown ============================================= ã¯ã©ã¹ã¬ãã«ã® setup/teardown ============================= .. Similarly, the following methods are called at class level before and after all test methods of the class are called:: åæ§ã«ãã¯ã©ã¹ã®å ¨ã¦ã®ãã¹ãã¡ã½ãããå¼ã³åºãããåå¾ã«ãã¯ã©ã¹ã¬ãã«ã§æ¬¡ã®ã¡ã½ãããå¼ã°ãã¾ã:: @classmethod def setup_class(cls): """ (é常ã¯ãã¹ããå«ã) ã¯ã©ã¹ã®å®è¡ã«é¢ãã¦ä»»æã®ç¶æ ãã»ããã¢ãããã """ @classmethod def teardown_class(cls): """ setup_class ã§äºåã«ã»ããã¢ããããç¶æ ã解ä½ãã """ .. Method and function level setup/teardown ============================================= ã¡ã½ãããé¢æ°ã¬ãã«ã® setup/teardown ===================================== .. Similarly, the following methods are called around each method invocation:: åæ§ã«ãããããã®ã¡ã½ããå¼ã³åºãã®åå¾ã§æ¬¡ã®ã¡ã½ãããå¼ã°ãã¾ã:: def setup_method(self, method): """ ã¯ã©ã¹å ã®ã¡ã½ããã®å®è¡ã«é¢ãã¦ä»»æã®ç¶æ ãã»ããã¢ãããã setup_method ã¯ã¯ã©ã¹ã®ãã¹ãã¡ã½ããåä½ã§å®è¡ããã """ def teardown_method(self, method): """ setup_method ã§äºåã«ã»ããã¢ããããç¶æ ã解ä½ãã """ .. If you would rather define test functions directly at module level you can also use the following functions to implement fixtures:: ã¢ã¸ã¥ã¼ã«ã¬ãã«ã§ç´æ¥çã«ãã¹ãé¢æ°ãå®ç¾©ããããªãã次ã®é¢æ°ããã£ã¯ã¹ãã£ãå®è£ ããã®ã«ä½¿ãã¾ã:: def setup_function(function): """ é¢æ°ã®å®è¡ã«é¢ãã¦ä»»æã®ç¶æ ãã»ããã¢ãããã ã¢ã¸ã¥ã¼ã«å ã®é¢æ°åä½ã§å®è¡ããã """ def teardown_function(function): """ setup_function ã§äºåã«ã»ããã¢ããããç¶æ ã解ä½ãã """ .. Note that it is possible for setup/teardown pairs to be invoked multiple times per testing process. ãã¹ãããã»ã¹ã«ã¤ãè¤æ°åå®è¡ããã setup/teardown ã®çµã¿åããã«ä½¿ãããã¨ãè¦ãã¦ããã¦ãã ããã .. _`unittest.py module`: http://docs.python.org/library/unittest.html