Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Created June 27, 2016 04:19
Show Gist options
  • Save davidrichards/677e8885435a212e039074080ad8311f to your computer and use it in GitHub Desktop.
Save davidrichards/677e8885435a212e039074080ad8311f to your computer and use it in GitHub Desktop.
A kind of a cheatsheet to remind me some of the basics
import unittest
from unittest.mock import MagicMock
class Bar(object):
def thud(self):
return "thunk"
def baz(self):
return self.thud()
class TestDimensional(unittest.TestCase):
def setUp(self):
self.subject = "foo"
@unittest.skip("using skip")
def test_true(self):
self.assertTrue(True)
@unittest.expectedFailure
def test_weird(self):
self.assertTrue(False)
def test_foo(self):
self.assertTrue(True)
self.assertEqual("foo", self.subject)
def test_bar(self):
thing = Bar()
thing.thud = MagicMock(return_value="foo")
result = thing.baz()
self.assertEqual("foo", thing.baz())
def tearDown(self):
self.subject = None
# Just for example, no reason to tear down I should think (in this case)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestDimensional)
unittest.TextTestRunner(verbosity=2).run(suite)
# Simple:
# unittest.main()
@davidrichards
Copy link
Author

Running with find . | entr -c python -m unittest discover -s tests -v

The discover option on the command line is finding and organizing the suite for me. The -v makes it verbose and the -s is the start directory for discovery.

The if __name__ == '__main__' is idiomatic. It is courtesy to make things runnable without a test runner. If I just want to run the tests, the bottom line will work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment