{"id":3563,"date":"2022-06-30T08:46:18","date_gmt":"2022-06-30T08:46:18","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3563"},"modified":"2022-07-09T07:24:01","modified_gmt":"2022-07-09T07:24:01","slug":"python-test-fixtures","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-test-fixtures\/","title":{"rendered":"Python Test Fixtures"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python test fixtures including <code>setUp()<\/code> and <code>tearDown()<\/code> methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-test-fixtures'>Introduction to the Python Test fixtures <a href=\"#introduction-to-the-python-test-fixtures\" class=\"anchor\" id=\"introduction-to-the-python-test-fixtures\" title=\"Anchor for Introduction to the Python Test fixtures\">#<\/a><\/h2>\n\n\n\n<p>By definition, a test fixture is a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> or <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">method<\/a> that runs before and after a block of test code executes. In other words, it is a step carried out before or after a test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='module-level-fixtures'>Module-level fixtures <a href=\"#module-level-fixtures\" class=\"anchor\" id=\"module-level-fixtures\" title=\"Anchor for Module-level fixtures\">#<\/a><\/h3>\n\n\n\n<p>Suppose you have a test module called <code>test_my_module.py<\/code>. In the <code>test_my_module.py<\/code>, the <code>setUpModule()<\/code> and <code>tearDownModule()<\/code> functions are the module-level fixtures. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>setUpModule()<\/code> function runs before all test methods in the test module.<\/li><li>The <code>tearDownModule()<\/code> function runs after all methods in the test module.<\/li><\/ul>\n\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUpModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running setUpModule'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDownModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running tearDownModule'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestMyModule<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_1<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertEqual(<span class=\"hljs-number\">5<\/span>+<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_2<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertEqual(<span class=\"hljs-number\">1<\/span>+<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you run the test: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Running setUpModule\ntest_case_1 (test_my_module.TestMyModule) ... ok\ntest_case_2 (test_my_module.TestMyModule) ... ok\nRunning tearDownModule\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">2<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>setUpModule()<\/code> function runs before all the test methods and the <code>tearDownModule()<\/code> function runs after all the test methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='class-level-fixtures'>Class-level fixtures <a href=\"#class-level-fixtures\" class=\"anchor\" id=\"class-level-fixtures\" title=\"Anchor for Class-level fixtures\">#<\/a><\/h3>\n\n\n\n<p>The <code>setUpClass()<\/code> and <code>tearDownClass()<\/code> are class-level fixtures:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>setUpClass()<\/code> runs before all test methods of a class<\/li><li>The <code>tearDownClass()<\/code> runs after all test methods of a class.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUpModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running setUpModule'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDownModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running tearDownModule'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestMyModule<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUpClass<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running setUpClass'<\/span>)\n\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDownClass<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running tearDownClass'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_1<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertEqual(<span class=\"hljs-number\">5<\/span>+<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_2<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertEqual(<span class=\"hljs-number\">1<\/span>+<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we added the class methods: <code>setUpClass()<\/code> and <code>tearDownClass()<\/code> to the <code>TestMyModule<\/code> class.<\/p>\n\n\n\n<p>If you run the test, you&#8217;ll see the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Running setUpModule\nRunning setUpClass\ntest_case_1 (test_my_module.TestMyModule) ... ok\ntest_case_2 (test_my_module.TestMyModule) ... ok\nRunning tearDownClass\nRunning tearDownModule\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">2<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='method-level-fixtures'>Method-level fixtures <a href=\"#method-level-fixtures\" class=\"anchor\" id=\"method-level-fixtures\" title=\"Anchor for Method-level fixtures\">#<\/a><\/h3>\n\n\n\n<p>The <code>setUp()<\/code> and <code>tearDown()<\/code> are method-level fixtures:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>setUp()<\/code> runs before every test method in the test class.<\/li><li>The <code>tearDown()<\/code> runs after every test method in the test class.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUpModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running setUpModule'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDownModule<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Running tearDownModule'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestMyModule<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUpClass<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running setUpClass'<\/span>)\n\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDownClass<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running tearDownClass'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUp<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">''<\/span>)\n        print(<span class=\"hljs-string\">'Running setUp'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDown<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running tearDown'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_1<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running test_case_1'<\/span>)\n        self.assertEqual(<span class=\"hljs-number\">5<\/span>+<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_case_2<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'Running test_case_2'<\/span>)\n        self.assertEqual(<span class=\"hljs-number\">1<\/span>+<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows the test result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Running setUpModule\nRunning setUpClass\ntest_case_1 (test_my_module.TestMyModule) ...\nRunning setUp\nRunning test_case_1\nRunning tearDown\nok\ntest_case_2 (test_my_module.TestMyModule) ...\nRunning setUp\nRunning test_case_2\nRunning tearDown\nok\nRunning tearDownClass\nRunning tearDownModule\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">2<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.002<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>setUp()<\/code> and <code>tearDown()<\/code> executes before and after each test method including  <code>test_case_1()<\/code> and <code>test_case_2()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-test-fixtures-example'>Python test fixtures example <a href=\"#python-test-fixtures-example\" class=\"anchor\" id=\"python-test-fixtures-example\" title=\"Anchor for Python test fixtures example\">#<\/a><\/h2>\n\n\n\n<p>First, define classes called <code>BankAccount<\/code> and <code>InsufficientFund<\/code> classes in the <code>bank_account.py<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">InsufficientFund<\/span><span class=\"hljs-params\">(Exception)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">pass<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, balance: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> balance &lt; <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'balance cannot be negative'<\/span>)\n        self._balance = balance\n\n<span class=\"hljs-meta\">    @property<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">balance<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self._balance\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">deposit<\/span><span class=\"hljs-params\">(self, amount: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> amount &lt;= <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The amount must be positive'<\/span>)\n\n        self._balance += amount\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">withdraw<\/span><span class=\"hljs-params\">(self, amount: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> amount &lt;= <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The withdrawal amount must be more than 0'<\/span>)\n\n        <span class=\"hljs-keyword\">if<\/span> amount &gt; self._balance:\n            <span class=\"hljs-keyword\">raise<\/span> InsufficientFund(<span class=\"hljs-string\">'Insufficient ammount for withdrawal'<\/span>)\n\n        self._balance -= amount<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define the <code>TestBankAccount<\/code> class in the <code>test_bank_account.py<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n<span class=\"hljs-keyword\">from<\/span> bank_account <span class=\"hljs-keyword\">import<\/span> BankAccount\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestBankAccount<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_deposit<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account = BankAccount(<span class=\"hljs-number\">100<\/span>)\n        self.bank_account.deposit(<span class=\"hljs-number\">100<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">200<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_withdraw<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account = BankAccount(<span class=\"hljs-number\">100<\/span>)\n        self.bank_account.withdraw(<span class=\"hljs-number\">50<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">50<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>TestBankAccount<\/code> class has two test methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>test_<code>deposit()<\/code><\/code> &#8211; test the <code>deposit()<\/code> method of the bank account.<\/li><li><code>test_<code>withdraw()<\/code><\/code> &#8211; test the <code>withdraw()<\/code> method of the bank account.<\/li><\/ul>\n\n\n\n<p>Both methods create a new instance of the <code>BankAccount<\/code>. It&#8217;s redundant. <\/p>\n\n\n\n<p>To avoid redundancy, you can create an instance of the BankAccount class in <code>setUp()<\/code> method and use it in all the test methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n<span class=\"hljs-keyword\">from<\/span> bank_account <span class=\"hljs-keyword\">import<\/span> BankAccount\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestBankAccount<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUp<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        self.bank_account = BankAccount(<span class=\"hljs-number\">100<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_deposit<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account.deposit(<span class=\"hljs-number\">100<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">200<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_withdraw<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account.withdraw(<span class=\"hljs-number\">50<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">50<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code><code>setUp()<\/code><\/code> method:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, create an instance of the <code>BankAccount<\/code> class and assign it to the instance variable <code>self.bank_account<\/code>.<\/li><li>Then, use <code>self.bank_account<\/code> instance in both <code>test_deposit()<\/code> and <code>test_withdraw()<\/code> methods.<\/li><\/ul>\n\n\n\n<p>When running test methods <code>test_deposit()<\/code> and <code>test_withdraw()<\/code>, the <code>setUp()<\/code> runs before each test method.<\/p>\n\n\n\n<p>For <code>test_deposit()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">setUp()\ntest_deposit()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For <code>test_withdraw()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">setUp()\ntest_withdraw()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you run the test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;ll output the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_deposit (test_bank_account.TestBankAccount) ... ok\ntest_withdraw (test_bank_account.TestBankAccount) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">2<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following adds the <code>tearDown()<\/code> method to the <code>TestBankAccount<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n<span class=\"hljs-keyword\">from<\/span> bank_account <span class=\"hljs-keyword\">import<\/span> BankAccount, InsufficientFund\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestBankAccount<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">setUp<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        self.bank_account = BankAccount(<span class=\"hljs-number\">100<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_deposit<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account.deposit(<span class=\"hljs-number\">100<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">200<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_withdraw<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.bank_account.withdraw(<span class=\"hljs-number\">50<\/span>)\n        self.assertEqual(self.bank_account.balance, <span class=\"hljs-number\">50<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">tearDown<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        self.bank_account = <span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>tearDown()<\/code> method assigns <code>None<\/code> to the <code>self.bank_account<\/code> instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Fixtures are functions and methods that execute before and after test code blocks execute.<\/li><li>The <code>setUpModule()<\/code> and <code>tearDownModule()<\/code> run before and after all test methods in the module.<\/li><li>The <code>setUpclass()<\/code> and <code>tearDownClass()<\/code> run before and after all test methods in a test class.<\/li><li>The <code>setUp()<\/code> and <code>tearDown()<\/code> run before and after each test method of a test class.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3563\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-test-fixtures\/\"\n\t\t\t\tdata-post-title=\"Python Test Fixtures\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3563\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-test-fixtures\/\"\n\t\t\t\tdata-post-title=\"Python Test Fixtures\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about Python test fixtures including setUp() and tearDown() methods.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3563","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3563"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3563\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3553"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}