Created
July 10, 2012 17:35
-
-
Save alexanderjulo/3084938 to your computer and use it in GitHub Desktop.
Example unittest testcase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class ExampleTestCase(unittest.TestCase): | |
def setUp(self): | |
# open files, set up database and stuff | |
pass | |
def test_1(self): | |
# start all tests with test, so unittest finds it | |
assert something | |
# assert something you like | |
def test_2(self): | |
# add as many of them as you like | |
assert another thing | |
# you will need to adjust this asserts to something that makes sense | |
def tearDown(self): | |
# close files, database connections and so on | |
pass | |
def suite(): | |
# make a suite for testing (I do not know how made this system up, sorry..) | |
suite = unittest.TestSuite() | |
# add all your cases to the suite, you could make a loop or whatever | |
suite.addTest(unittest.makeSuite(ExampleTestCase)) | |
# return your suite | |
return suite | |
def run(): | |
# create a runner that will run all your tests | |
test = unittest.TextTestRunner() | |
# run your tests | |
result = test.run(suite()) | |
# return true, if the tests were successful or false if something failed | |
return result.wasSuccessful() | |
# you can import the run function from this module and just execute it as you like | |
# i use it in flask-script to attach the test command to some unittesting | |
# with unittest main() i just had circular import problems | |
# have fun! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment