2

我正在尝试使用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()
4

0 回答 0