lib.py
from django.core.urlresolvers import reverse
def render_reverse(f, kwargs):
"""
kwargs is a dictionary, usually of the form {'args': [cbid]}
"""
return reverse(f, **kwargs)
tests.py
from lib import render_reverse, print_ls
class LibTest(unittes
我仍然在使用Django 1.2.1,我认为在较新的Django中,我们不需要先使用import unittest,然后再使用unittest.TestCase。
插图
import unittest
class TestThis(unittest.TestCase):
from django.utils.unittest import TestCase
class TestThis(TestCase):
from django.test import TestCase
class TestThis(TestCase):
根据的说法,第二种方法是稍微更有效。
下面是显示这些关系的:
我编写了这个测试,但是为了不延迟测试,我模拟time.sleep,测试将遇到失败。
from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase
class CommandsTest(TestCase):
@patch('time.sleep', return_value=None)
def test_wait
我想在我的单元测试中统一一个异常处理,并且我使用assertRaises来检查是否抛出异常。
class Add():
def __init__(self, x, y):
if type(x) != int or type(y) != int:
raise Exception('Not a valid number')
Add(None, None)
import unittest
from test2 import Add
class TestAdd(unittest.TestCase):
我真的不明白protectAsyncN方法的目的。
有没有人能给我解释一下它是怎么工作的?例如,想象一下下面的测试用例:
import "package:unittest/unittest.dart";
import "dart:async";
void main() {
test("Protect async", () {
// given
var controller = new StreamController();
// when
controller.add("This is correc
我有一个函数测试'y1.py‘,我试图从python/django函数中调用它。在调用函数中,我拥有:
import unittest
import ft1.y1
unittest.main(module=ft1.y1.py)
y1.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
当使用python的unittest 时,我很困惑参数是如何在子测试中命名和作用域的。
上面的链接中给出的规范示例似乎意味着可以将with self.subtest()子句中使用的参数作为关键字参数传递给subTest()。供参考的例子如下:
class NumbersTest(unittest.TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
fo
>>> d = {'AUD': 1, 'GBP': 2}
>>> Currency.objects.filter(currency_code__in=d)
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]
>>> Currency.objects.filter(currency_code__in=d.keys())
[<Currency: Australia Dollar (A
如果使用patch.multiple作为方法/函数修饰器,则如果修补对象为mock.DEFAULT或非mock.DEFAULT,则行为不同。
示例:
from unittest import mock
class A: pass
@mock.patch.multiple('__main__', A=mock.DEFAULT)
def with_default(*args,**kwargs):
if 'A' not in kwargs:
print("with_default: A not passed")
@mock
我试图理解为什么显式指定签名参数不起作用,而只是盲目地执行*args,**kwargs有效!我真的看不出这两者有多大区别?
不起作用的例子:
from django.db.models import CharField as _CharField
class CharField(_CharField):
def get_db_prep_value(self, value, connection, prepared=False):
if self.blank == self.null == self.unique == True and value == '
这是我的发情器,在导入的测试类中有一些情况。
from apa_login import Apa_login
from processmap import Show_processmap
import unittest,HTMLTestRunner,apa_login,processmap
def suite(self):
suite = unittest.TestSuite()
suite = unittest.TestLoader().loadTestsFromTestCase(Apa_login)
suite = unittest.TestLoader().l
我想创建一个允许user (扩展OneToOne by UserProfile)成为translator的表单。这意味着如果User是登录的,他/她可以点击Become a Translator按钮将它们重定向到预填充的表单。如果UserProfile中没有空这样的属性,则应预先填充此表单。
因此,如果有人已经填写了他的last_name,姓氏应该在这个表格中预先填写,否则,他必须填写它,因为对翻译是必要的。
因此,我尝试将user.userprofile参数作为form类的属性,但它会引发:
'UserProfile' object has no attribute
我正在为Python模块编写测试文件。
class A:
def func(self):
B().sub_func()
class B:
def sub_func(self):
pass
在测试A时,我需要B块的副作用。我的问题是如何替换测试文件中的B类。
class ATest(unittest.TestCase):
def test_a(self):
a=A()
a.func()
#now object a will invoke a func that B is a mock clas