Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
HTTP Request Testing in Pytest

So I am very new to coding in general and I have written the following code to pass HTTP requests to Python/Django methods to test them with Pytest:

import django
django.setup()
from login.views import delete_song, delete_playlist, create_song, logout_user, add_preferences, create_playlist, detail, login_user
from django.shortcuts import render, get_object_or_404
from login.models import Playlist
from django.test import TestCase
from django.test import Client


class test_songs(TestCase):

    def setUpClass(self):
        self.client = Client()
        self.post = ('POST /docs/index.html HTTP/1.1 Host: www.nowhere123.com Accept: image/gif, image/jpeg, / Accept-Language: en-us Accept-Encoding: gzip, deflate Content-Type: application/json User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) (blank line) {"foo": "bar"}')

    def test_login_pass(self):
        assert login_user(self) == render(self, 'index.html', {'playlists': Playlist.objects.filter(user=self.user)})

    def test_login_fail(self):
        assert login_user(self) == render(self, 'login.html', {'error_message': 'Invalid login'})

    def test_create_playlist(self):
        assert create_playlist(self) == render(self.post, 'create_playlist.html', context)

    def test_create_song(self):
        assert create_song(self,111) == render(self, 'detail.html', {'playlist': get_object_or_404(Playlist, pk=111)})

    def test_add_preference(self):
        assert add_preferences(x, 111) == render(x, 'add_preferences.html', context)

    def test_delete_song(self):
        assert delete_song(x,111,222) == render(x,'detail.html', {'playlist': get_object_or_404(Playlist, pk=111)})

    def test_detail(self):
        assert detail(self, 111) == render(self, 'detail.html',{'playlist': get_object_or_404(Playlist, pk=111), 'user': self.user})

    def test_detail_not_auth(self):
        assert detail('nonuser', 111)== render('nonuser', 'login.html')

    def test_delete_playlist(self):
        assert delete_playlist(x, 111) == render(x, 'detail.html', {'playlist': Playlist.objects.get(pk=111)})

    def test_logout_user(self):
        assert logout_user(x) == render(x, 'login.html', context)

but I keep getting the error when running them:

TypeError: setUpClass() missing 1 required positional argument: 'self'

I know there is probably a ton wrong with this code but getting past this hurdle would get me moving.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUpClass

I guess it's because setUpClass must be a class method

@classmethod
def setUpClass(cls):
    ...
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda