EMAN2 Testing Framework

EMAN2 uses python unit test and regression test as its testing framework. For features not testable or not easily to be tested in python, they will be tested in C++.

EMAN2 Python Test Framework FAQ

1. what are the testing guidelines ?
2. Can you give me an example on how to write unittest and regression test?

import unittest
from test import test_support

class MyTestCase1(unittest.TestCase):

# Only use setUp() and tearDown() if necessary

def setUp(self):
... code to execute in preparation for tests ...

def tearDown(self):
... code to execute to clean up after tests ...

def test_feature_one(self):
# Test feature one.
... testing code ...

def test_feature_two(self):
# Test feature two.
... testing code ...

... more test methods ...

class MyTestCase2(unittest.TestCase):
... same structure as MyTestCase1 ...

... more test classes ...

def test_main():
test_support.run_unittest(MyTestCase1,
MyTestCase2,
... list other tests ...
)

if __name__ == '__main__':
test_main()


3. Can you give me an detailed example on how to write  unit test?

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.seq = range(10)

def tearDown(self):
# do clean up here

def testshuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))

def testchoice(self):
element = random.choice(self.seq)
self.assert_(element in self.seq)

def testsample(self):
self.assertRaises(ValueError, random.sample, self.seq, 20)
for element in random.sample(self.seq, 5):
self.assert_(element in self.seq)

if __name__ == '__main__':
unittest.main()



4. unittest details: How to assert values and conditions?

The crux of each test is a call to assertEqual() to check for an expected result; assert_() to verify a condition; or assertRaises() to verify that an expected exception gets raised.

5. unittest details: how to set up and clean up a test?
setUp() is for set up. tearDown() is for clean up.






Last Modified by Liwei Peng (lpeng@bcm.tmc.edu) on 1/30/2005.