Django测试打印或日志失败

时间:2016-12-28 18:41:13

标签: django django-testing django-tests

我有一个django_rest_framework测试(问题与常规django测试相同),如下所示:

from rest_framework.test import APITestCase

class APITests(APITestCase):

    # tests for unauthorized access
    def test_unauthorized(self):
        ...
        for api in apipoints:
            response = self.client.options(api)
            self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

我有一个失败的网址,终端显示:

  

失败:test_unauthorized(app.misuper.tests.APITests)   -------------------------------------------------- -------------------- Traceback(最近一次调用最后一次):文件   " /家庭/亚历/...&# 34;,

     

第64行,在test_unauthorized

中      

self.assertEqual(response.status_code,status.HTTP_403_FORBIDDEN)AssertionError:200!= 403

好的,我怎么知道哪个网址未通过测试?我正在遍历所有需要登录的网址,即许多网址,如何打印未通过测试的网址?

1 个答案:

答案 0 :(得分:1)

对于简单的快速修复,您可以在断言方法的第三个参数中传递apipoint

>>> from unittest import TestCase
>>> TestCase('__init__').assertEqual(1, 2, msg='teh thing is b0rked')
AssertionError: teh thing is b0rked

在单元测试的精神中,这些应该是每种不同的测试方法,而不是只有一种测试方法。查看nose_parameterized以获取更多干燥的帮助。你会像这样装饰测试方法:

from nose_parameterized import parameterized

@parameterized.expand(apipoints)
def test_unauthorized(self, apipoint):
    response = self.client.options(apipoint)
    self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

装饰器将为每个端点生成不同的测试方法,以便它们可以彼此独立地通过/失败。

虽然此软件包名称中包含nose unittest,例如py.test#import "ViewController.h" #import "Popup.h" @interface ViewController ()<UIPopoverPresentationControllerDelegate> { Popup * popupController; UIPopoverPresentationController *popupPresentationController; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)popupOnClick:(id)sender { popupController=[self.storyboard instantiateViewControllerWithIdentifier:@"Popup"]; popupController.modalPresentationStyle=UIModalPresentationPopover; popupPresentationController= [popupController popoverPresentationController]; popupPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; popupController.preferredContentSize=CGSizeMake(150, 300); popupPresentationController.delegate = self; [self presentViewController:popupController animated:YES completion:NULL]; // in case we don't have a bar button as reference popupPresentationController.sourceView = _popupButton; popupPresentationController.sourceRect = _popupButton.frame; } - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } @end

相关问题