Django 2.1测试问题 [英] Django 2.1 Test Issue

查看:81
本文介绍了Django 2.1测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您的时间,并为我的英语感到抱歉.

在学习Django时,我用python进行了几年的开发,因此决定开始检查此Framework.而且我得到了一个奇怪的答复.我在编写TestCase时,在Test外部完全可以正常工作.

那是代码:

class BoardTopicsTests(TestCase):
    # Hago las acciones necesarias para empezar el test
    def setUp(self):
        self.board = Board(name="Django", description="Django board.")
        # self.board.save()

    # Compruebo el status_code 200
    def test_board_topics_view_status_code(self):
        # self.board.save()
        url = reverse("board_topics", kwargs={"pk":1})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    # Compruebo el status_code 404
    def test_board_topics_view_not_found_status_code(self):
        url = reverse("board_topics", kwargs={"pk" : 99})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    # Compruebo que resuelve bien el board
    def test_board_topics_url_resolves_board_topics_views(self):
        view = resolve("/boards/1/")
        self.assertEqual( view.func.__name__, BoardTopics.as_view().__name__ )

如果我将木板保存在setUp中,则test_board_topics_view_status_code方法返回404,如果我将木板保存在方法中,则返回200并通过测试.

我想我丢失了一些东西,因为我认为必须通过setUp方法保存它!

请,有人可以帮忙吗?这只是出于学习目的,因为我想知道那里发生了什么.

如果我在test_board_topics_view_status_code内进行print(self.board.id),它会返回1.

非常感谢!

解决方案

经过研究,我得出了解决方案:

@classmethod
def setUpTestData(cls):
    Board.objects.create(name="Django", description="Django board.")

代替:

def setUp(self):
    self.board = Board(name="Django", description="Django board.")
    self.board.save()

它可以工作,但是我试图对另一个类做同样的事情,但404仍然返回...在那个特定的TestCase工作中,在另一个不起作用的情况下,o

我正在做更多研究,如果找到最终"解决方案,我会回信.

更新:

最后我发现了问题!解决方案是下一个解决方案:

@classmethod
def setUpTestData(cls):
    self.board = Board.objects.create(name="Django", description="Django board.")

# Compruebo el status_code 200
def test_board_topics_view_status_code(self):
    url = reverse("board_topics", kwargs={"pk":self.board.pk})
    response = self.client.get(url)
    self.assertEqual(response.status_code, 200)

请注意,在kwargs中,pk的值为 self.board.pk ,而不是(这里是错误所在的地方)硬编码为1 ,因为我们不知道PK是否会成为1 !!!哦,那个愚蠢的错误使我整夜无法入睡.

因此,最后我们可以在下一个继续:

  • 使用setUpTestData代替setUp添加数据
  • 不对pk进行硬编码,存储您已经插入的对象并返回pk

希望答案会有所帮助!

您的S3yk0

first of all thank you for your time and sorry about my english.

Im learning Django, I had several years developing with python and decided to start to check this Framework. And I'm getting an weird responses. Im writting a TestCase, wich works perfectly outside Test.

That is the code:

class BoardTopicsTests(TestCase):
    # Hago las acciones necesarias para empezar el test
    def setUp(self):
        self.board = Board(name="Django", description="Django board.")
        # self.board.save()

    # Compruebo el status_code 200
    def test_board_topics_view_status_code(self):
        # self.board.save()
        url = reverse("board_topics", kwargs={"pk":1})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    # Compruebo el status_code 404
    def test_board_topics_view_not_found_status_code(self):
        url = reverse("board_topics", kwargs={"pk" : 99})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    # Compruebo que resuelve bien el board
    def test_board_topics_url_resolves_board_topics_views(self):
        view = resolve("/boards/1/")
        self.assertEqual( view.func.__name__, BoardTopics.as_view().__name__ )

If I save the board in setUp the method test_board_topics_view_status_code returns 404, if I save the board inside the method it returns 200 and pass the test.

I think I'm missing something because I think it have to work saving from setUp method!

Please, can somebody help with that? It's just for learning purposes because I want to know whats happening there.

If I do print(self.board.id) inside test_board_topics_view_status_code it returns 1 as it suposed to be.

Thank you very much!

解决方案

After researching a bit I came with the solution:

@classmethod
def setUpTestData(cls):
    Board.objects.create(name="Django", description="Django board.")

Instead of:

def setUp(self):
    self.board = Board(name="Django", description="Django board.")
    self.board.save()

It works BUT I'm trying to do the same with another class and the 404 still returns... In that particular TestCase works, in another ones dont,oO

I'm researching a bit more, I will write back if I found the "ultimate" solution hehehe.

UPDATE:

Well at the end I found the problem! The solution is the next one:

@classmethod
def setUpTestData(cls):
    self.board = Board.objects.create(name="Django", description="Django board.")

# Compruebo el status_code 200
def test_board_topics_view_status_code(self):
    url = reverse("board_topics", kwargs={"pk":self.board.pk})
    response = self.client.get(url)
    self.assertEqual(response.status_code, 200)

Note that in kwargs the value of pk is self.board.pk insteadof (and here where the error was) hardcoded 1 because we dont know if the PK would be 1!!! Oh that silly error took me one night without sleep.

So at the end we can resume in the next:

  • Use setUpTestData instead setUp to add Data
  • Dont hardcode pk, store the object you already inserted and return the pk

Hope the answer helps!

Your's S3yk0

这篇关于Django 2.1测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆