小编blt*_*yer的帖子

Python unittest 模拟类和类方法

我觉得这可能相对简单,但我正在拉我的头发以使其工作。我想模拟整个类,然后指定此类方法之一的返回值。

我已经看过了这里,还有其他几个问题,当然还有docs。我仍然无法让它发挥作用。请看我下面的简单例子。

目录内容tmp

tmp
??? __init__.py
??? my_module.py
??? test_my_module.py
Run Code Online (Sandbox Code Playgroud)

内容my_module.py

tmp
??? __init__.py
??? my_module.py
??? test_my_module.py
Run Code Online (Sandbox Code Playgroud)

内容test_my_module.py

class MyClass:
    def __init__(self):
        # Do expensive operations that will be mocked in testing.
        self.a = 7

    def my_method(self):
        # For sake of simple example, always return 1.
        return 1


def create_class_call_method():
    """Create MyClass instance and call its my_method method, returning
    the result."""
    instance = MyClass()
    value = instance.my_method()
    return value …
Run Code Online (Sandbox Code Playgroud)

python unit-testing mocking python-3.x python-unittest

6
推荐指数
3
解决办法
5430
查看次数

向 EmailMessage 添加附件会引发 TypeError:set_text_content() 得到了意外的关键字参数“maintype”

按照Python 电子邮件示例中的示例,添加附件似乎应该非常简单。但是,以下方法不起作用:

import smtplib
from email.message import EmailMessage
import json

# Initialize message.
msg = EmailMessage()
msg['Subject'] = 'Testing, testing 1-2-3'
msg['To'] = 'fake@example.com'
msg['From'] = 'extrafake@example.com'
msg.set_content('Trying to attach a .json file')

# Create json attachment.
attachment = json.dumps({'This': 'is json'})

# Attempt to attach. This raises an exception.
msg.add_attachment(attachment, maintype='application', subtype='json', filename='test.json')
Run Code Online (Sandbox Code Playgroud)

这是例外:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.7/email/message.py", line 1147, in add_attachment
    self._add_multipart('mixed', *args, _disp='attachment', **kw)
  File "/usr/local/lib/python3.7/email/message.py", …
Run Code Online (Sandbox Code Playgroud)

python email encoding python-3.x

5
推荐指数
1
解决办法
2130
查看次数

在 Sphinx HTML 输出中排除 reStructuredText 注释

我想在 reStructuredText 文档中添加一些注释,这些注释在我构建文档时不会显示。例如,我想将以下内容放入文件中:

This is the source of my .rst file. I would like the following block to be excluded when the docs are built.
..
    This should not show up in the HTML.
    This should not either.

This text would show up in the HTML, as we're past the comment block.
Run Code Online (Sandbox Code Playgroud)

根据Sphinx 文档,“根据输出格式化程序,注释可能会从处理后的输出中删除。” 但是,我在HTML 输出配置文档中没有看到任何选项。

我正在使用 Sphinx ( sphinx-build) 来构建我的文档。我使用sphinx-quickstart进行设置,从而通过make html.

提前致谢!

python restructuredtext python-3.x python-sphinx

0
推荐指数
1
解决办法
1268
查看次数