vendredi 21 février 2020

How do you refactor the pytest to use attributes of class?

Given that I have the below test:

class VideoGamesTest:


    def test_create_a_new_videoGame(self):

        protocol = 'http://'
        domain = 'localhost:'
        port = '8080'
        path = '/app/videogames'
        self.url = protocol+domain+port+path

        response = requests.get(self.url,headers={'Accept': 'application/json'})

        payload = {
                "id": 16,
                "name": "I am my Hero",
                "releaseDate": "2020-02-21T16:54:03.200Z",
                "reviewScore": 9.0,
                "category": "Biography",
                "rating": "9.0"
            }


        response = requests.post(self.url,data=json.dumps(payload),headers={'Content-Type': 'application/json'} )
        response = json.loads(response.text)
        assert 'status' in response.keys()


    def test_get_all_videoGames(self):

        protocol = 'http://'
        domain = 'localhost:'
        port = '8080'
        path = '/app/videogames'
        self.url = protocol+domain+port+path
        response = requests.get(self.url,headers={'Accept': 'application/json'})

        response = json.loads(response.text)[0]

        print(response)

        assert 'id' in response.keys()

As you can see, the below part is repetitive:

    protocol = 'http://'
    domain = 'localhost:'
    port = '8080'
    path = '/app/videogames'
    self.url = protocol+domain+port+path

I don't know, how to refactor the this class so that i can have the url as the attribute of the class. But, given that, i cannot use __init__ in my pytest class, then it is not doable.

Question is:

How can i omit the repetitive part of the tests?

Aucun commentaire:

Enregistrer un commentaire