Python Testing by Arbuckle Daniel

Python Testing by Arbuckle Daniel

Author:Arbuckle, Daniel [Daniel Arbuckle]
Language: eng
Format: epub
Publisher: Packt Publishing


Time for action – creating a module fixture

We'll build a test module with a module-level fixture. In the fixture, we'll replace the datetime.date.today function, which normally returns an object representing the current date. We want it to return a specific value, so that our tests can know what to expect.

Create a directory called tests. We'll use this directory in this Time for action, as well as in the next one.

Within the tests directory, create a file called module_fixture_tests.py containing the following code:from unittest import TestCase from mocker import Mocker from datetime import date mocker = Mocker() def setup(): fake_date = mocker.replace(date) fake_date.today() mocker.result(date(year = 2009, month = 6, day = 12)) mocker.count(1, None) mocker.replay() def teardown(): mocker.restore() mocker.verify() class first_tests(TestCase): def test_year(self): self.assertEqual(date.today().year, 2009) def test_month(self): self.assertEqual(date.today().month, 6) def test_day(self): self.assertEqual(date.today().day, 12) class second_tests(TestCase): def test_isoformat(self): self.assertEqual(date.today().isoformat(), '2009-06-12')



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.