Best Python code snippet using Testify_python
test_loading.py
Source:test_loading.py  
1"""2pkg13pkg1.test4pkg1.test.test_things5pkg1.test.test_things.test_func6pkg1.test.test_things.test_gen7pkg1.test.test_things.test_gen:38pkg1.test.test_things.SomeTests9pkg1.test.test_things.SomeTests.test_ok10# generator method11# generator method index12# param func13# param func index14# param method15# param method index16"""17from nose2.tests._common import FunctionalTestCase, support_file18class TestLoadTestsFromPackage(FunctionalTestCase):19    def test_module_name(self):20        proc = self.runIn(21            'scenario/tests_in_package',22            '-v',23            'pkg1.test.test_things')24        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')25        self.assertEqual(proc.poll(), 1)26    def test_package_name(self):27        proc = self.runIn(28            'scenario/tests_in_package',29            '-v',30            'pkg1')31        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')32        self.assertEqual(proc.poll(), 1)33    def test_module_name_with_start_dir(self):34        proc = self.runIn(35            '.', '-v', '-s', support_file('scenario/tests_in_package'),36            'pkg1.test.test_things')37        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')38        self.assertEqual(proc.poll(), 1)39    def test_package_name_with_start_dir(self):40        proc = self.runIn(41            '.', '-v', '-s', support_file('scenario/tests_in_package'), 'pkg1')42        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')43        self.assertEqual(proc.poll(), 1)44    def test_function_name(self):45        proc = self.runIn(46            'scenario/tests_in_package',47            '-v',48            'pkg1.test.test_things.test_func')49        self.assertTestRunOutputMatches(50            proc, stderr='test_func')51        self.assertTestRunOutputMatches(52            proc, stderr='Ran 1 test')53        self.assertTestRunOutputMatches(54            proc, stderr='OK')55        self.assertEqual(proc.poll(), 0)56    def test_generator_function_name(self):57        proc = self.runIn(58            'scenario/tests_in_package',59            '-v',60            'pkg1.test.test_things.test_gen')61        self.assertTestRunOutputMatches(proc, stderr='test_gen')62        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')63        self.assertEqual(proc.poll(), 0)64    def test_generator_function_index(self):65        proc = self.runIn(66            'scenario/tests_in_package',67            '-v',68            'pkg1.test.test_things.test_gen:3')69        self.assertTestRunOutputMatches(proc, stderr='test_gen')70        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')71        self.assertEqual(proc.poll(), 0)72    def test_generator_function_index_1_based(self):73        proc = self.runIn(74            'scenario/tests_in_package',75            '-v',76            'pkg1.test.test_things.test_gen:1')77        self.assertTestRunOutputMatches(proc, stderr='test_gen')78        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')79        self.assertTestRunOutputMatches(proc, stderr='OK')80        self.assertEqual(proc.poll(), 0)81    def test_testcase_name(self):82        proc = self.runIn(83            'scenario/tests_in_package',84            '-v',85            'pkg1.test.test_things.SomeTests')86        self.assertTestRunOutputMatches(proc, stderr='SomeTests')87        self.assertTestRunOutputMatches(proc, stderr='Ran 8 tests')88        self.assertEqual(proc.poll(), 1)89    def test_testcase_method(self):90        proc = self.runIn(91            'scenario/tests_in_package',92            '-v',93            'pkg1.test.test_things.SomeTests.test_ok')94        self.assertTestRunOutputMatches(proc, stderr='SomeTests')95        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')96        self.assertTestRunOutputMatches(proc, stderr='OK')97        self.assertEqual(proc.poll(), 0)98    def test_generator_method(self):99        proc = self.runIn(100            'scenario/tests_in_package',101            '-v',102            'pkg1.test.test_things.SomeTests.test_gen_method')103        self.assertTestRunOutputMatches(proc, stderr='test_gen_method')104        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')105        self.assertEqual(proc.poll(), 1)106    def test_generator_method_index(self):107        proc = self.runIn(108            'scenario/tests_in_package',109            '-v',110            'pkg1.test.test_things.SomeTests.test_gen_method:1')111        self.assertTestRunOutputMatches(proc, stderr='test_gen_method')112        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')113        self.assertTestRunOutputMatches(proc, stderr='OK')114        self.assertEqual(proc.poll(), 0)115    def test_parameterized_method(self):116        proc = self.runIn(117            'scenario/tests_in_package',118            '-v',119            'pkg1.test.test_things.SomeTests.test_params_method')120        self.assertTestRunOutputMatches(proc, stderr='test_params_method')121        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')122        self.assertEqual(proc.poll(), 1)123    def test_parameterized_method_index(self):124        proc = self.runIn(125            'scenario/tests_in_package',126            '-v',127            'pkg1.test.test_things.SomeTests.test_params_method:1')128        self.assertTestRunOutputMatches(proc, stderr='test_params_method')129        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')130        self.assertTestRunOutputMatches(proc, stderr='OK')131        self.assertEqual(proc.poll(), 0)132    def test_parameterized_func(self):133        proc = self.runIn(134            'scenario/tests_in_package',135            '-v',136            'pkg1.test.test_things.test_params_func')137        self.assertTestRunOutputMatches(proc, stderr='test_params_func')138        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')139        self.assertEqual(proc.poll(), 1)140    def test_parameterized_func_index(self):141        proc = self.runIn(142            'scenario/tests_in_package',143            '-v',144            'pkg1.test.test_things.test_params_func:1')145        self.assertTestRunOutputMatches(proc, stderr='test_params_func')146        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')147        self.assertTestRunOutputMatches(proc, stderr='OK')148        self.assertEqual(proc.poll(), 0)149class TestLoadTestsOutsideOfPackage(FunctionalTestCase):150    def test_module_name(self):151        proc = self.runIn(152            'scenario/package_in_lib',153            '-v',154            'tests')155        self.assertTestRunOutputMatches(proc, stderr='Ran 3 tests')156        self.assertEqual(proc.poll(), 1)157    def test_function_name(self):158        proc = self.runIn(159            'scenario/package_in_lib',160            '-v',161            'tests.test')162        self.assertTestRunOutputMatches(proc, stderr='test')163        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')164        self.assertTestRunOutputMatches(proc, stderr='OK')165        self.assertEqual(proc.poll(), 0)166    def test_module_name_with_start_dir(self):167        proc = self.runIn(168            '.', '-v', '-s', support_file('scenario/package_in_lib'), 'tests')169        self.assertTestRunOutputMatches(proc, stderr='Ran 3 tests')170        self.assertEqual(proc.poll(), 1)171class TestLoadingErrors(FunctionalTestCase):172    def test_import_error_module(self):173        proc = self.runIn(174            'scenario/module_import_err',175            '-v',176            'test_import_err')177        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')178        self.assertEqual(proc.poll(), 1)179    def test_import_error_func(self):180        proc = self.runIn(181            'scenario/module_import_err',182            '-v',183            'test_import_err.test')184        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')185        self.assertEqual(proc.poll(), 1)186    def test_import_error_testcase(self):187        proc = self.runIn(188            'scenario/module_import_err',189            '-v',190            'test_import_err.Test')191        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')192        self.assertEqual(proc.poll(), 1)193    def test_import_error_testcase_method(self):194        proc = self.runIn(195            'scenario/module_import_err',196            '-v',197            'test_import_err.Test.test')198        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')199        self.assertEqual(proc.poll(), 1)200class TestTestClassLoading(FunctionalTestCase):201    def test_load_testclass_by_name(self):202        proc = self.runIn(203            'scenario/test_classes',204            '-v',205            'test_classes.Test')206        self.assertTestRunOutputMatches(proc, stderr='Ran 8 tests')207        self.assertEqual(proc.poll(), 0)208    def test_load_testclass_method_by_name(self):209        proc = self.runIn(210            'scenario/test_classes',211            '-v',212            'test_classes.Test.test')213        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')214        self.assertEqual(proc.poll(), 0)215    def test_load_testclass_generator_method_by_name(self):216        proc = self.runIn(217            'scenario/test_classes',218            '-v',219            'test_classes.Test.test_gen')220        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')221        self.assertEqual(proc.poll(), 0)222    def test_load_testclass_params_method_by_name(self):223        proc = self.runIn(224            'scenario/test_classes',225            '-v',226            'test_classes.Test.test_params')227        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')228        self.assertEqual(proc.poll(), 0)229    def test_class_level_fixtures_supported(self):230        proc = self.runIn(231            'scenario/test_classes',232            '-v',233            'test_fixtures')234        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')235        self.assertEqual(proc.poll(), 0)236    def test_error_in_test_class(self):237        proc = self.runIn(238            'scenario/test_class_fail',239            '-v',240            'test_class_fail')241        self.assertTestRunOutputMatches(proc, stderr='nose2.loader.LoadTestsFailure')242        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')243        self.assertTestRunOutputMatches(proc, stderr='FAILED')244        self.assertEqual(proc.poll(), 1)245    246    def test_expected_failures(self):247        proc = self.runIn(248            'scenario/expected_failures',249            '-v',250            'expected_failures')251        self.assertTestRunOutputMatches(proc, stderr=r'FAILED \(failures=1, expected failures=1, unexpected successes=1\)')...tests.py
Source:tests.py  
1from django import test2from django.contrib.auth import get_user_model3from django.test import TestCase4from django.urls import reverse5# Create your tests here.6from rest_framework.test import APITestCase7from .models import Things8from rest_framework import status9class ThingsModelTests(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_thing=Things.objects.create(15            title='Title of thing',16            purchaser=test_user,17            description='Words'18        )19        test_thing.save()20    def test_thing_content(self):21        thing = Things.objects.get(id=1)22        self.assertEqual(thing.title, 'Title of thing')23        self.assertEqual(str(thing.purchaser), 'tester')24        self.assertEqual(thing.description, 'Words')25class APITest(APITestCase):26    def test_list(self):27        response = self.client.get(reverse('things_list'))28        self.assertEqual(response.status_code, status.HTTP_200_OK)29    def test_detail(self):30        test_user = get_user_model().objects.create_user(username='tester',password='pass')31        test_user.save()32        test_things = Things.objects.create(33            purchaser = test_user,34            title = 'Title of Blog',35            description = 'Words'36        )37        test_things.save()38        response = self.client.get(reverse('things_detail', args=[1]))39        self.assertEqual(response.status_code, status.HTTP_200_OK)40        self.assertEqual(response.data, {41            'id':1,42            'title': test_things.title,43            'description': test_things.description,44            'purchaser': test_user.id,45        })46    def test_create(self):47        test_user = get_user_model().objects.create_user(username='tester',password='pass')48        test_user.save()49        url = reverse('things_list')50        data = {51            "title":"Testing is Fun!!!",52            "description":"when the right tools are available",53            "purchaser":test_user.id,54        }55        response = self.client.post(url, data, format='json')56        self.assertEqual(response.status_code, status.HTTP_201_CREATED, test_user.id)57        self.assertEqual(Things.objects.count(), 1)58        self.assertEqual(Things.objects.get().title, data['title'])59    def test_update(self):60        test_user = get_user_model().objects.create_user(username='tester',password='pass')61        test_user.save()62        test_things= Things.objects.create(63            purchaser = test_user,64            title = 'Title of Blog',65           description = 'Words'66        )67        test_things.save()68        url = reverse('things_detail',args=[test_things.id])69        data = {70            "title":"Testing is Still Fun!!!",71            "purchaser":test_user.id,72            "description":test_things.description,73        }74        response = self.client.put(url, data, format='json')75        self.assertEqual(response.status_code, status.HTTP_200_OK, url)76        self.assertEqual(Things.objects.count(), test_things.id)77        self.assertEqual(Things.objects.get().title, data['title'])78    def test_delete(self):79        """Test the api can delete a post."""80        test_user = get_user_model().objects.create_user(username='tester',password='pass')81        test_user.save()82        test_things = Things.objects.create(83            purchaser = test_user,84            title = 'Title of Blog',85            description = 'Words'86        )87        test_things.save()88        things = Things.objects.get()89        url = reverse('things_detail', kwargs={'pk': things.id})90        response = self.client.delete(url)...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!!
