在pytest中运行smtpd.DebuggingServer失败但单元测试工作?

时间:2017-06-30 22:49:25

标签: python testing smtpd

我正在尝试使用smtpd包对我的电子邮件功能进行单元测试。该软件包允许使用调试服务器。

当我使用pytest运行此测试时,它会在收集过程中产生错误:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtpd.py:646: in __init__
type=socket.SOCK_STREAM)
E   TypeError: getaddrinfo() got an unexpected keyword argument 'type'

但是,如果我使用unittest运行此测试,它会设法通过罚款。它们都使用Python3.6在同一个虚拟环境中运行

这是我用来实例化调试服务器和测试函数的代码:

class DebuggingServer(smtpd.DebuggingServer):

    def __init__(self, addr, port):
        smtpd.DebuggingServer.__init__(self, localaddr=(addr, port), remoteaddr=None)


class DebuggingServerThread(threading.Thread):

    def __init__(self, addr='localhost', port=1025):
        threading.Thread.__init__(self)
        self.server = DebuggingServer(addr, port)

    def run(self):
        asyncore.loop(use_poll=True)

    def stop(self):
        self.server.close()
        self.join()


class TestMyEmail(unittest.TestCase):

    server = DebuggingServerThread()

    def setUp(self):

        self.server.start()
        print('Server has started')

    def tearDown(self):

        self.server.stop()
        print('Server has stopped')

    def test_png(self):

        png_files = [os.path.join(DATA_DIR, 'page1.png'),
                 os.path.join(DATA_DIR, 'page2.png')]

        with open(os.path.join(DATA_DIR, 'test_png.txt'), 'w+') as f:
            with redirect_stdout(f):
                success = myemail.mail(recipients=['email@email'],
                                       message="Test email sent from server to test inline attachment of png files",
                                       attachments=png_files,
                                       subject="TestMyEmail.test_png")

        with open(os.path.join(DATA_DIR, 'test_png.eml'), 'w+') as email_file:

            gen = generator.Generator(email_file)
            gen.flatten(success)

        assert type(success)

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

0 个答案:

没有答案