Django Test Mock Datetime Now

时间:2017-06-25 07:08:33

标签: python django unit-testing datetime python-mock

我有一个Django测试,我需要模拟datetime.now(),因为它测试的视图使用datetime.now()

我正在使用Michael Foord的模拟库1.0.1版 我正在寻找一种解决方案而不使用其他库,例如freezegun。

大多数示例如thisthis都会导入日期时间并覆盖它,但我正在导入datetime.datetime并尝试覆盖它,并且由于某种原因这不起作用。

覆盖日期时间:

    <meta http-equiv="Cache-control" content="public, max-age=3600">
    <meta http-equiv="Expires" content="public, max-age=3600">
    <meta http-equiv="Last-Modified" content="public, max-age=3600">

但我想导入datetime.datetime并执行以下操作:

    Creating myapp-debug-unaligned.apk and signing it with a debug key...
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:960: The following error occurred while executing this line:
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:971: The following error occurred while executing this line:
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:314: 
java.lang.NoClassDefFoundError: Could not initialize class sun.security.x509.X500Name
    at com.android.sdklib.internal.build.SignedJarBuilder.writeSignatureBlock(SignedJarBuilder.java:384)
    at com.android.sdklib.internal.build.SignedJarBuilder.close(SignedJarBuilder.java:273)
    at com.android.sdklib.build.ApkBuilder.sealApk(ApkBuilder.java:795)
    at com.android.ant.ApkBuilderTask.execute(ApkBuilderTask.java:371)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
    at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
    at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
BUILD FAILED (total time: 2 seconds)

这会导致TypeError:需要一个有效的目标来修补。您提供了:'datetime'。

Mock库还声明:

  

target应该是'package.module.ClassName'形式的字符串。导入目标并将指定的对象替换为新对象,因此目标必须可从您调用补丁的环境中导入。

有没有办法只追踪日期时间而不是datetime.datetime?

的Nb。我也看到this示例,但它对我不起作用,因为我没有一个返回日期时间的函数,但我的视图使用datetime.now()

1 个答案:

答案 0 :(得分:1)

您正在测试的模块中应该有patch个对象,而不是带有测试的模块中的对象。

基于代码的最小工作示例:

<强> app.py

from datetime import datetime

def my_great_func():
    # do something
    return datetime.now()

tests.py (请注意修补<{strong> datetime中的app.py 的方式)

from datetime import datetime
from unittest import mock
from app import my_great_func

@mock.patch('app.datetime')
def test_my_great_func(mocked_datetime):
    mocked_datetime.now.return_value = datetime(2010, 1, 1)
    assert my_great_func() == datetime(2010, 1, 1)

测试执行结果:

$ pytest -vvv tests.py 
======================= test session starts =========================
platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
cachedir: .cache
rootdir: /home/kris/projects/tmp, inifile:
plugins: mock-1.6.2, celery-4.1.0
collected 1 item                                                   

tests.py::test_my_great_func PASSED

==================== 1 passed in 0.00 seconds =======================