Best Python code snippet using green
tests.py
Source:tests.py
1from django.test import TestCase2# Create your tests here.3from django.contrib.auth import get_user_model4from django.test import TestCase5from django.urls import reverse6from rest_framework import status7from rest_framework.test import APITestCase8from .models import Food9class FoodModelTests(TestCase):10 @classmethod11 def setUpTestData(cls):12 test_user = get_user_model().objects.create_user(username='tester',password='pass')13 test_user.save()14 test_food = Food.objects.create(15 author = test_user,16 name = 'Shawerma',17 body = 'Fast Food'18 )19 test_food.save()20 21 def test_food_content(self):22 food = Food.objects.get(id=1)23 self.assertEqual(str(food.author), 'tester')24 self.assertEqual(food.name, 'Shawerma')25 self.assertEqual(food.body, 'Fast Food')26 27class APITest(APITestCase):28 def test_detail(self):29 test_user = get_user_model().objects.create_user(username='tester',password='pass')30 test_user.save()31 test_food = Food.objects.create(32 author = test_user,33 name = 'Shawerma',34 body = 'Fast Food'35 )36 test_food.save()37 response = self.client.get(reverse('food_detail', args=[1]))38 self.assertEqual(response.status_code, status.HTTP_200_OK)39 self.assertEqual(response.data, {40 'id':1,41 'name': test_food.name,42 'body': test_food.body,43 'author': test_user.id,44 })45 # def test_create(self):46 # test_user = get_user_model().objects.create_user(username='tester',password='pass')47 # test_user.save()48 # url = reverse('food_list')49 # data = {50 # "name":"Testing is Fun!!!",51 # "body":"when the right tools are available",52 # "author":test_user.id,53 # }54 # response = self.client.post(url, data, format='json')55 # self.assertEqual(response.status_code, status.HTTP_201_CREATED, test_user.id)56 # self.assertEqual(Food.objects.count(), 1)57 # self.assertEqual(Food.objects.get().name, data['name'])58 # def test_update(self):59 # test_user = get_user_model().objects.create_user(username='tester',password='pass')60 # test_user.save()61 # test_food = Food.objects.create(62 # author = test_user,63 # name = 'Shawerma',64 # body = 'Fast Food'65 # )66 # test_food.save()67 # url = reverse('food_detail',args=[test_food.id])68 # data = {69 # "name":"Testing is Still Fun!!!",70 # "author":test_food.author.id,71 # "body":test_food.body,72 # }73 # response = self.client.put(url, data, format='json')74 # self.assertEqual(response.status_code, status.HTTP_200_OK, url)75 # self.assertEqual(Food.objects.count(), test_food.id)76 # self.assertEqual(Food.objects.get().name, data['name'])77 # def test_delete(self):78 # """Test the api can delete a post."""79 # test_user = get_user_model().objects.create_user(username='tester',password='pass')80 # test_user.save()81 # test_food = Food.objects.create(82 # author = test_user,83 # name = 'Shawerma',84 # body = 'Fast Food'85 # )86 # test_food.save()87 # test_food.save()88 # post = Food.objects.get()89 # url = reverse('food_detail', kwargs={'pk': post.id})90 # response = self.client.delete(url)...
test.py
Source:test.py
1import unittest2from project.pet_shop import PetShop3class PetShopTests(unittest.TestCase):4 def setUp(self) -> None:5 self.pet_shop = PetShop("Test")6 def test_pet_shop_init_attributes(self):7 self.assertEqual("Test", self.pet_shop.name)8 self.assertEqual({}, self.pet_shop.food)9 self.assertEqual([], self.pet_shop.pets)10 def test_pet_shop_add_food_new_with_valid_quantity(self):11 self.assertEqual("Successfully added 1.50 grams of test_food.", self.pet_shop.add_food("test_food", 1.5))12 self.assertEqual({"test_food": 1.5}, self.pet_shop.food)13 def test_pet_shop_add_food_existing_with_valid_quantity(self):14 self.pet_shop.add_food("test_food", 1.5)15 self.assertEqual("Successfully added 1.50 grams of test_food.", self.pet_shop.add_food("test_food", 1.5))16 self.assertEqual({"test_food": 3}, self.pet_shop.food)17 def test_pet_shop_add_food_with_invalid_quantity(self):18 with self.assertRaises(ValueError) as ex:19 self.pet_shop.add_food("test_food", 0)20 self.assertEqual('Quantity cannot be equal to or less than 0', str(ex.exception))21 self.assertEqual({}, self.pet_shop.food)22 def test_pet_shop_add_pet_new(self):23 self.assertEqual("Successfully added Eeyore.", self.pet_shop.add_pet("Eeyore"))24 self.assertEqual(["Eeyore"], self.pet_shop.pets)25 def test_pet_shop_add_pet_existing_raises(self):26 self.pet_shop.add_pet("Eeyore")27 with self.assertRaises(Exception) as ex:28 self.pet_shop.add_pet("Eeyore")29 self.assertEqual("Cannot add a pet with the same name", str(ex.exception))30 self.assertEqual(["Eeyore"], self.pet_shop.pets)31 def test_pet_shop_feed_pet_enough_food_quantity(self):32 self.pet_shop.add_food("test_food", 1001.5)33 self.pet_shop.add_pet("Eeyore")34 self.assertEqual("Eeyore was successfully fed", self.pet_shop.feed_pet("test_food", "Eeyore"))35 self.assertEqual({"test_food": 901.5}, self.pet_shop.food)36 def test_pet_shop_feed_pet_not_enough_food_quantity(self):37 self.pet_shop.add_food("test_food", 1.5)38 self.pet_shop.add_pet("Eeyore")39 self.assertEqual("Adding food...", self.pet_shop.feed_pet("test_food", "Eeyore"))40 self.assertEqual({"test_food": 1001.5}, self.pet_shop.food)41 def test_pet_shop_feed_pet_not_existing_raises(self):42 self.pet_shop.add_food("test_food", 1.5)43 with self.assertRaises(Exception) as ex:44 self.pet_shop.feed_pet("test_food", "Eeyore")45 self.assertEqual("Please insert a valid pet name", str(ex.exception))46 self.assertEqual({"test_food": 1.5}, self.pet_shop.food)47 def test_pet_shop_feed_pet_not_existing_food(self):48 self.pet_shop.add_pet("Eeyore")49 self.assertEqual('You do not have test_food', self.pet_shop.feed_pet("test_food", "Eeyore"))50 self.assertEqual({}, self.pet_shop.food)51 def test_pet_shop_repr(self):52 self.pet_shop.add_pet("Eeyore")53 self.pet_shop.add_pet("Murphy")54 self.assertEqual(f"Shop Test:\nPets: Eeyore, Murphy", self.pet_shop.__repr__())55if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!