基本的django-pytest测试

时间:2017-02-16 21:24:53

标签: python django testing pytest pytest-django

即使只是学习django和testwriting,所以我需要一些确认。我想知道以下测试是"正确"拼写它。

test_view.py

@interface RLMObject ()

// unmanaged initializer
- (instancetype)initWithValue:(id)value schema:(RLMSchema *)schema NS_DESIGNATED_INITIALIZER;

// live accessor initializer
- (instancetype)initWithRealm:(__unsafe_unretained RLMRealm *const)realm
                       schema:(RLMObjectSchema *)schema NS_DESIGNATED_INITIALIZER;

@end

forms.py

@pytest.mark.django_db
def test_create_form(client):
    form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': 'New Content for lambs'}
    response = client.post(reverse("public:create"), form_data, follow=True)
    assert b'Test Title' in response.content

@pytest.mark.django_db
def test_context_create(client):
   form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': 'New Content for lambs'}
   response = client.post(reverse("public:create"), form_data)
   assert response.context['form'].cleaned_data['title'] == 'Test Title'

@pytest.mark.django_db
def test_TITLE_is_blank(client):
    form_data = {'title': '', 'tags': 'HTML', 'content': 'New Content for lambs'}
    response = client.post(reverse("public:create"), form_data)
    assert response.context['form'].cleaned_data['title'] == ''

@pytest.mark.django_db
def test_CONTENT_is_blank(client):
    form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': ''}
    response = client.post(reverse("public:create"), form_data)
    assert response.context['form'].cleaned_data['content'] == ''

@pytest.mark.django_db
def test_CONTENT_and_TITLE_is_blank(client):
    form_data = {'title': '', 'tags': 'HTML', 'content': ''}
    response = client.post(reverse("public:create"), form_data)
    assert (not response.context['form'].is_valid())

这是模型

class PostForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args,**kwargs)

        self.fields['title'].required = False
        self.fields['content'].required = False

    class Meta:
        model = Post
        fields = ['title', 'tags', 'content']

    def clean(self):
        form_data = self.cleaned_data

        if (form_data['title'] is not "") or (form_data['content'] is not ""):
            return form_data
        else:
           self._errors["title"] = ["Two field is empty"]


    def save(self, *args, **kwargs):
        instance = super(PostForm, self).save(commit=False)
        instance.slug = slugify(instance.title)
        instance.save()

        return instance

感谢您的帮助和建议。

0 个答案:

没有答案