c 20022004 python software foundation a torture test of the email package this should not be run as part of the standard python test suite since it requires several meg of email messages collected in the wild these source messages are not checked into the python distro but are available as part of the standalone email package at http sf netprojectsmimelib prevent this test from running in the python distro mark crispin s torture test from the squirrelmail project probably the best way to verify the message is parsed correctly is to dump its structure and compare it against the known structure def testclasses mod sys modulesname return getattrmod name for name in dirmod if name startswith test def loadtestsloader tests pattern suite loader suiteclass for testclass in testclasses suite addtestloader loadtestsfromtestcasetestclass return suite if name main unittest main c 2002 2004 python software foundation a torture test of the email package this should not be run as part of the standard python test suite since it requires several meg of email messages collected in the wild these source messages are not checked into the python distro but are available as part of the standalone email package at http sf net projects mimelib prevent this test from running in the python distro mark crispin s torture test from the squirrelmail project probably the best way to verify the message is parsed correctly is to dump its structure and compare it against the known structure multipart mixed text plain message rfc822 multipart alternative text plain multipart mixed text richtext application andrew inset message rfc822 audio basic audio basic image pbm message rfc822 multipart mixed multipart mixed text plain audio x sun multipart mixed image gif image gif application x be2 application atomicmail audio x sun message rfc822 multipart mixed text plain image pgm text plain message rfc822 multipart mixed text plain image pbm message rfc822 application postscript image gif message rfc822 multipart mixed audio basic audio basic message rfc822 multipart mixed application postscript text plain message rfc822 multipart mixed text plain multipart parallel image gif audio basic application atomicmail message rfc822 audio x sun
import sys
import os
import unittest
from io import StringIO

from test.test_email import TestEmailBase

import email
from email import __file__ as testfile
from email.iterators import _structure

def openfile(filename):
    from os.path import join, dirname, abspath
    path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
    return open(path, 'r')


def setUpModule():
    try:
        openfile('crispin-torture.txt')
    except OSError:
        raise unittest.SkipTest



class TortureBase(TestEmailBase):
    def _msgobj(self, filename):
        fp = openfile(filename)
        try:
            msg = email.message_from_file(fp)
        finally:
            fp.close()
        return msg



class TestCrispinTorture(TortureBase):
    
    def test_mondo_message(self):
        eq = self.assertEqual
        neq = self.ndiffAssertEqual
        msg = self._msgobj('crispin-torture.txt')
        payload = msg.get_payload()
        eq(type(payload), list)
        eq(len(payload), 12)
        eq(msg.preamble, None)
        eq(msg.epilogue, '\n')
        
        
        fp = StringIO()
        _structure(msg, fp=fp)
        neq(fp.getvalue(), )

def _testclasses():
    mod = sys.modules[__name__]
    return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]


def load_tests(loader, tests, pattern):
    suite = loader.suiteClass()
    for testclass in _testclasses():
        suite.addTest(loader.loadTestsFromTestCase(testclass))
    return suite

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