Best Python code snippet using autotest_python
base.py
Source:base.py
...7 """8 Base TestCase class that provides utilities throughout the raspberryio9 project.10 """11 def create_instance(self, ModelClass, **kwargs):12 """13 Given ModelClass, validate, create and return an model instance of that14 type. Takes `defaults` as a dictionary of default values. Any15 other kwargs override the provided defaults. E.g. defaults can be16 provided in test helper methods but overriden when called, as needed:17 Example:18 create_item = create_instance(Item, default={'title': 'generic'})19 ...20 a = create_item()21 b = create_item(title='flower')22 print (a.title, b.title)23 Out: ('generic', 'flower')24 """25 defaults = kwargs.pop('defaults', {})26 defaults.update(kwargs)27 instance = ModelClass(**defaults)28 instance.clean()29 instance.save()30 return instance31 def create_site(self, **kwargs):32 defaults = {33 'name': self.get_random_string(),34 'domain': self.get_random_string(),35 }36 return self.create_instance(DjangoSite, defaults=defaults, **kwargs)37 def create_superuser(self, data=None):38 data = data or {}39 user = self.create_user(data=data)40 user.is_superuser = True41 user.is_staff = True42 user.save()43 return user44 def get_current_site(self):45 return DjangoSite.objects.get(id=current_site_id())46class ProjectBaseTestCase(RaspberryIOBaseTestCase):47 def create_project(self, **kwargs):48 defaults = {49 'title': self.get_random_string(length=500),50 'site': kwargs.pop('site', self.create_site()),51 'user': kwargs.pop('user', self.create_user()),52 }53 instance = self.create_instance(54 project.Project, defaults=defaults, **kwargs55 )56 if 'status' in kwargs:57 instance.status = kwargs['status']58 instance.save()59 return instance60 def create_project_step(self, **kwargs):61 defaults = {62 'project': kwargs.pop('project', self.create_project()),63 'title': self.get_random_string(length=500),64 'content': self.get_random_string(),65 }66 return self.create_instance(67 project.ProjectStep, defaults=defaults, **kwargs68 )69 def create_project_category(self, **kwargs):70 defaults = {71 'title': kwargs.pop('title', self.get_random_string()),72 }73 return self.create_instance(74 project.ProjectCategory, defaults=defaults, **kwargs75 )76 def create_file(self, **kwargs):77 filename = kwargs.pop('filename', 'test.jpg')78 content = kwargs.pop('content', self.get_random_string())79 temp_file = ContentFile(content)80 temp_file.name = filename81 return temp_file82 def create_project_image(self, **kwargs):83 defaults = {84 'file': kwargs.pop('file', self.create_file()),85 }86 project_step = kwargs.pop('project_step', None)87 project_image = self.create_instance(88 project.ProjectImage, defaults=defaults, **kwargs89 )90 if project_step:91 project_step.gallery.add(project_image)...
test_category_controller.py
Source:test_category_controller.py
...4from backend.controller.base_controller import BaseController5from backend.controller.category_controller import CategoryController6from backend.models.category import Category7@pytest.fixture8def create_instance():9 category = CategoryController()10 return category11def test_category_controller_instance(create_instance):12 assert isinstance(create_instance, BaseController)13 assert isinstance(create_instance, CategoryController)14def test_create_category(create_instance):15 name = 'Category'16 description = 'Test'17 category = Category(name, description)18 result = create_instance.save(category)19 assert result.id is not None20 assert result.name == name21 assert result.description == description22 create_instance.delete(result)...
behavior_tests.py
Source:behavior_tests.py
2from freezegun import freeze_time3class BehaviorTestCaseMixin:4 def get_model(self):5 return getattr(self, 'model')6 def create_instance(self, **kwargs):7 raise NotImplementedError('Implement method')8class TimestampableTests(BehaviorTestCaseMixin):9 def test_created_date(self):10 now = timezone.now()11 with freeze_time(now):12 obj = self.create_instance()13 assert now == obj.created_date14 def test_modified_date(self):15 obj = self.create_instance()16 now = timezone.now()17 with freeze_time(now):18 obj.save()19 assert now == obj.modified_date20class PermalinkableTests(BehaviorTestCaseMixin):21 def test_givenAnObjectWithUsername_theSlugIsTheSlugifiedUsername(self, default_user, client):22 obj = self.create_instance(username='test')...
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!!