Skip to content

Instantly share code, notes, and snippets.

@k0emt k0emt/Experiment.py
Last active Feb 28, 2020

Embed
What would you like to do?
Basic Hello world in Python with corresponding unittest
__author__ = 'k0emt'
class Greeter:
def __init__(self):
self.message = 'Hello world'
# print self.message
__author__ = 'k0emt'
import unittest
from Experiment import Greeter
class MyTestCase(unittest.TestCase):
def test_default_greeting_set(self):
greeter = Greeter()
# this test will fail until you change the Greeter to return this expected message
self.assertEqual(greeter.message, 'Hello world!')
if __name__ == '__main__':
unittest.main()
@IsraelTorres

This comment has been minimized.

Copy link

IsraelTorres commented Sep 21, 2012

Thank you k0emt - totally what I needed to get a basic understanding of what unittest was about!

To help others understand in ExperimentTests.py change 'self.assertEqual(greeter.message, 'Hello world!')' to expect something slightly different such as 'self.assertEqual(greeter.message, 'Hello world?')' to get a failure message :) This means that the expected message was not received thus the failure, otherwise if the expected message was received (i.e. they match) then it prints out OK after the test is run.

@k0emt

This comment has been minimized.

Copy link
Owner Author

k0emt commented Feb 2, 2013

IsraelTorres thanks for the suggestion. I updated the gists to be initially failing. Change the greeter code to match the test and they pass.

@SchofieldChris

This comment has been minimized.

Copy link

SchofieldChris commented Jan 21, 2018

Thanks for this, I was looking for a simple example and this is exactly what I needed.

@ronilpatel

This comment has been minimized.

Copy link

ronilpatel commented Jan 8, 2020

https://gist.github.com/ronilpatel/4b28e0f821ef119c8a8d223c89987f8a

Have a look at this with additional basic test cases appended to the above python files.
Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.