Best Python code snippet using django-test-plus_python
test_comment.py
Source: test_comment.py
...38 with self.login(username='user1', password='strong_password_1'):39 self.create_board()40 self.create_post(board_pk=self.board_pk-1)41 self.create_comment(board_pk=self.board_pk-1, post_pk=self.post_pk-1)42 self.assert_http_201_created()43 is_comment = Comment.objects.exists()44 self.assertTrue(is_comment)45 def test_comment_get(self):46 with self.login(username='user1', password='strong_password_1'):47 self.create_board()48 self.create_post(board_pk=self.board_pk)49 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)50 self.get_check_200(f'/board/{self.board_pk}/post/{self.post_pk}/comment/')51 self.get_check_200(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/')52 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/')53 self.assert_http_403_forbidden()54 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/')55 self.assert_http_403_forbidden()56 def test_comment_update(self):57 with self.login(username='user1', password='strong_password_1'):58 self.create_board()59 self.create_post(board_pk=self.board_pk)60 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)61 self.put(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/', data={62 'text': 'fix_test',63 })64 self.assert_http_200_ok()65 with self.login(username='user2', password='strong_password_2'):66 self.put(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/', data={67 'text': 'fix_second_test',68 })69 self.assert_http_403_forbidden()70 comment = Comment.objects.first()71 self.assertEqual(comment.text, 'fix_test')72 self.put(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/', data={73 'text': 'fix_third_test',74 })75 self.assert_http_403_forbidden()76 def test_comment_delete(self):77 with self.login(username='user1', password='strong_password_1'):78 self.create_board()79 self.create_post(board_pk=self.board_pk)80 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)81 with self.login(username='user2', password='strong_password_2'):82 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/')83 self.assert_http_403_forbidden()84 is_comment = Comment.objects.exists()85 self.assertTrue(is_comment)86 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/')87 self.assert_http_403_forbidden()88 is_comment = Comment.objects.exists()89 self.assertTrue(is_comment)90 with self.login(username='user1', password='strong_password_1'):91 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/')92 self.assert_http_204_no_content()93 is_comment = Comment.objects.exists()94 self.assertFalse(is_comment)95 def test_comment_like_create(self):96 with self.login(username='user1', password='strong_password_1'):97 self.create_board()98 self.create_post(board_pk=self.board_pk)99 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)100 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')101 self.assert_http_201_created()102 comment = Comment.objects.first()103 like_count = comment.like_comment.count()104 self.assertEqual(like_count, 1)105 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')106 self.assert_http_400_bad_request()107 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')108 self.assert_http_403_forbidden()109 comment = Comment.objects.first()110 like_count = comment.like_comment.count()111 self.assertEqual(like_count, 1)112 with self.login(username='user2', password='strong_password_2'):113 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')114 self.assert_http_201_created()115 comment = Comment.objects.first()116 like_count = comment.like_comment.count()117 self.assertEqual(like_count, 2)118 def test_comment_like_get(self):119 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')120 with self.login(username='user1', password='strong_password_1'):121 self.create_board()122 self.create_post(board_pk=self.board_pk)123 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)124 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')125 self.assert_http_400_bad_request()126 with self.login(username='user2', password='strong_password_2'):127 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')128 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')...
test_board.py
Source: test_board.py
...26 with self.login(username='user1', password='strong_password_1'):27 random_num = random.randint(3, 10)28 for _ in range(random_num):29 self.create_board()30 self.assert_http_201_created()31 boards = Boards.objects.all()32 board = Boards.objects.first()33 self.assertEqual(len(boards), random_num)34 self.assertEqual(board.author.username, 'user1')35 def test_board_put(self):36 with self.login(username='user1', password='strong_password_1'):37 self.create_board()38 # PUT method39 self.put('/board/1/', data={40 'title': 'fix'41 })42 self.assert_http_200_ok()43 board = Boards.objects.first()44 self.assertEqual(board.title, 'fix')45 with self.login(username='user2', password='strong_password_2'):46 self.create_board()47 self.put('/board/1/', data={48 'title': 'isNotAuthorFix'49 })50 self.assert_http_403_forbidden()51 board = Boards.objects.first()52 self.assertNotEqual(board.title, 'isNotAuthorFix')53 def test_board_patch(self):54 with self.login(username='user1', password='strong_password_1'):55 # PATCH method56 self.create_board()57 self.patch('/board/1/', data={58 'title': 'second fix'59 })60 self.assert_http_200_ok()61 second_board = Boards.objects.first()62 self.assertEqual(second_board.title, 'second fix')63 with self.login(username='user2', password='strong_password_2'):64 self.patch('/board/1/', data={65 'title': 'isNotAuthorFix'66 })67 self.assert_http_403_forbidden()68 board = Boards.objects.first()69 self.assertNotEqual(board.title, 'isNotAuthorFix')70 def test_board_delete(self):71 with self.login(username='user1', password='strong_password_1'):72 self.create_board()73 self.delete('/board/1/')74 self.assert_http_204_no_content()75 is_board = Boards.objects.exists()76 self.assertFalse(is_board)77 with self.login(username='user1', password='strong_password_1'):78 self.create_board()79 self.assert_http_201_created()80 with self.login(username='user2', password='strong_password_2'):81 self.delete('/board/2/')82 self.assert_http_403_forbidden()83 is_board = Boards.objects.exists()...
test_year.py
Source: test_year.py
...37 self.assert_http_400_bad_request(res)38 from_year, to_year = Year.MIN_YEAR, Year.MIN_YEAR + 2039 res = self.post(url, data={'from_year': from_year, 'to_year': to_year},40 extra={'format': 'json'})41 self.assert_http_201_created(res)42 res_years = [r['year'] for r in res.data]43 qs = Year.objects.values_list('year', flat=True)44 expected = list(range(from_year, to_year + 1))45 self.assertQuerysetEqual(qs, expected, ordered=False)46 self.assertQuerysetEqual(res_years, expected, ordered=False)47 res = self.post(url, data={'from_year': '2130', 'to_year': 2140,},48 extra={'format': 'json'})49 self.assert_http_201_created(res)50 res_years = [r['year'] for r in res.data]51 qs = Year.objects.values_list('year', flat=True)52 expected = list(range(2130, 2141))53 self.assertQuerysetEqual(qs, expected, ordered=False)54 self.assertQuerysetEqual(res_years, expected, ordered=False)55 res = self.post(url, data={'from_year': '2130.2', 'to_year': 2140,},56 extra={'format': 'json'})57 self.assert_http_400_bad_request(res)58 def test_query_params(self):59 """test the query params"""60 Year.objects.all().delete()61 for y in range(2020, 2050):62 year = Year.objects.create(year=y, is_prognosis=not(y % 5))63 url = 'years-list'...
Check out the latest blogs from LambdaTest on this topic:
“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.
While there is a huge demand and need to run Selenium Test Automation, the experts always suggest not to automate every possible test. Exhaustive Testing is not possible, and Automating everything is not sustainable.
It’s strange to hear someone declare, “This can’t be tested.” In reply, I contend that everything can be tested. However, one must be pleased with the outcome of testing, which might include failure, financial loss, or personal injury. Could anything be tested when a claim is made with this understanding?
Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.
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!!