两种测试分别工作,但不能一起工作

时间:2019-05-01 08:24:52

标签: python django django-tests

我在单独的类中有两个测试,它们测试相似的行为:

  1. 使用Client()在书签中添加产品。
  2. 使用ORM在书签中添加产品。

单独运行:

./manage.py test compare.test.CompareBookmarkTests

./manage.py test compare.test.CompareAccountTests

它们一起发射时会失败:

./manage.py test

我试图:

  • 使用setUptearDown方法
  • 更改类和方法的顺序
  • 在尝试诊断问题时使用--reverse
  • 在带有测试数据库的交互式django shell中启动代码行(并且正在运行)
  • 进行一些打印调试,可以从测试数据库正确调用我的对象。
  • 将这两个类合并为一个

到目前为止,我还没有找到关于stackoverflow的相关答案。 django文档中都没有。


from django.contrib.auth.models import User
from django.test import TestCase
from .models import Bookmark, Categorie, Product


class CompareBookmarkTests(TestCase):
    def setUp(self):
        Categorie.objects.create(id_categorie='someid', name='somename')
        p = Categorie.objects.get(pk='someid')
        p.product_set.create(id_product='1', product_name='orange', categorie='someid')
        User.objects.create_user('john@sign.in', 'john@sign.in', 'smith')

    def tearDown(self):
        User.objects.all().delete()
        Categorie.objects.all().delete()
        Bookmark.objects.all().delete()

    def test_redirect_logged(self):
        self.client.login(username='john@sign.in', password='smith')

        # Adding product with id 1 in bookmark
        response = self.client.get('/compare/1/bookmark/')

        # Get added product form bookmark
        bookmark = Bookmark.objects.get(pk=1)
        self.assertEqual(str(bookmark), 'Bookmark object (1)')
        self.assertEqual(response.status_code, 302)


class CompareAccountTests(TestCase):
    def setUp/tearDown: [same as previous class]

    def test_get_product(self):
        self.client.login(username='john@sign.in', password='smith')

        user = User.objects.get(pk=1)
        product = Product.objects.get(pk='1')

        # Adding product with id 1 in bookmark
        add_bookmark = Bookmark.objects.create(id_result=product, user=user)

        bookmark = Bookmark.objects.get(pk=1)
        response = self.client.get('/compare/account/')
        self.assertEqual(str(bookmark), 'Bookmark object (1)')
        self.assertEqual(response.status_code, 200)

我希望我的两个测试一起启动时都能成功,但是我得到了:

compare.models.Bookmark.DoesNotExist: Bookmark matching query does not exist.

使用--reverse:

django.contrib.auth.models.User.DoesNotExist

这可能是一个初学者的错误,但我看不到,谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我发现了可能是造成此问题的原因:

我使用了import cv2 import pytesseract import numpy as np import PIL.Image as Image pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract- OCR\\tesseract' def get_contour_precedence(contour, cols): tolerance_factor = 20 origin = cv2.boundingRect(contour) return ((origin[0] // tolerance_factor) * tolerance_factor) * cols + origin[1] img = cv2.imread("C:/Users/Akshatha/Desktop/text_detection_from _image/images/news1.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) kernel = np.ones((5, 5), np.uint8) erosion = cv2.erode(thresh, kernel, iterations=1) dilation = cv2.dilate(thresh, kernel, iterations=3) (contours, heirarchy,_) = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) heirarchy = heirarchy[0] print("start") print(np.array(heirarchy).shape, np.array(contours).shape) print("stop") contours.sort(key=lambda x: get_contour_precedence(x, img.shape[1])) # print(contours[0]) idx = 0 textlist = [] i = 0 rect_list = [] for c in contours: # get the bounding rect x, y, w, h = cv2.boundingRect(c) rect_list.append((x, y, x + w, y + h)) # draw a green rectangle to visualize the bounding rect cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3) roi = img[y:y + h, x:x + w] text = pytesseract.image_to_string(roi, lang='eng', config='--oem 1 -- psm 6 -c preserve_interword_spaces=1 ') print(text) cv2.putText(img, "#{}".format(i + 1), (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4) i += 1 cv2.namedWindow('Dilation', cv2.WINDOW_NORMAL) cv2.imshow('Dilation', img) cv2.waitKey(0) ,发现未删除的测试数据库:

psql

除了一个很奇怪的东西:

test_myprojet_1
test_myprojet_2
test_myprojet_3
test_myprojet_4
...and so on...

我在test_test_test_test_test_test_test_test_test_test_test_myprojec[truncated name] 中使用过:

psql

现在,我的测试正在按预期工作!

感谢awesoon,您的建议使我走上了正轨。