How to use istestfunction method in pytest-play

Best Python code snippet using pytest-play_python

classes.py

Source:classes.py Github

copy

Full Screen

...8# NOTE: these are defined here for reuse by both pytest's own machinery and our9# internal bits.10def istestclass(name):11 return not name.startswith("_")12def istestfunction(name):13 return not (name.startswith("_") or name in ("setup", "teardown"))14# All other classes in here currently inherit from PyCollector, and it is what15# defines the default istestfunction/istestclass, so makes sense to inherit16# from it for our mixin. (PyobjMixin, another commonly found class, offers17# nothing of interest to us however.)18class RelaxedMixin(PyCollector):19 """20 A mixin applying collection rules to both modules and inner/nested classes.21 """22 # TODO:23 # - worth calling super() in these? Difficult to know what to do with it;24 # it would say "no" to lots of stuff we want to say "yes" to.25 # - are there other tests to apply to 'obj' in a vacuum? so far only thing26 # we test 'obj' for is its membership in a module, which must happen inside27 # SpecModule's override.28 def istestclass(self, obj, name):29 return istestclass(name)30 def istestfunction(self, obj, name):31 return istestfunction(name)32class SpecModule(RelaxedMixin, Module):33 def _is_test_obj(self, test_func, obj, name):34 # First run our super() test, which should be RelaxedMixin's.35 good_name = getattr(super(SpecModule, self), test_func)(obj, name)36 # If RelaxedMixin said no, we can't really say yes, as the name itself37 # was bad - private, other non test name like setup(), etc38 if not good_name:39 return False40 # Here, we dig further based on our own wrapped module obj, by41 # rejecting anything not defined locally.42 if inspect.getmodule(obj) is not self.obj:43 return False44 # No other complaints -> it's probably good45 return True46 def istestfunction(self, obj, name):47 return self._is_test_obj("istestfunction", obj, name)48 def istestclass(self, obj, name):49 return self._is_test_obj("istestclass", obj, name)50 def collect(self):51 # Get whatever our parent picked up as valid test items (given our52 # relaxed name constraints above). It'll be nearly all module contents.53 items = super(SpecModule, self).collect()54 collected = []55 for item in items:56 # Replace Class objects with recursive SpecInstances (via57 # SpecClasses, so we don't lose some bits of parent Class).58 # TODO: this may be way more than is necessary; possible we can59 # recurse & unpack here, as long as we preserve parent/child60 # relationships correctly?61 # NOTE: we could explicitly skip unittest objects here (we'd want62 # them to be handled by pytest's own unittest support) but since63 # those are almost always in test_prefixed_filenames anyways...meh64 if isinstance(item, Class):65 item = SpecClass(item.name, item.parent)66 collected.append(item)67 return collected68# NOTE: no need to inherit from RelaxedMixin here as it doesn't do much by69# its lonesome70class SpecClass(Class):71 def collect(self):72 items = super(SpecClass, self).collect()73 collected = []74 # Replace Instance objects with SpecInstance objects that know how to75 # recurse into inner classes.76 # TODO: is this ever not a one-item list? Meh.77 for item in items:78 item = SpecInstance(name=item.name, parent=item.parent)79 collected.append(item)80 return collected81class SpecInstance(RelaxedMixin, Instance):82 def _getobj(self):83 # Regular object-making first84 obj = super(SpecInstance, self)._getobj()85 # Then decorate it with our parent's extra attributes, allowing nested86 # test classes to appear as an aggregate of parents' "scopes".87 # NOTE: need parent.parent due to instance->class hierarchy88 # NOTE: of course, skipping if we've gone out the top into a module etc89 if (90 not hasattr(self, "parent")91 or not hasattr(self.parent, "parent")92 or not isinstance(self.parent.parent, SpecInstance)93 ):94 return obj95 parent_obj = self.parent.parent.obj96 # Obtain parent attributes, etc not found on our obj (serves as both a97 # useful identifier of "stuff added to an outer class" and a way of98 # ensuring that we can override such attrs), and set them on obj99 delta = set(dir(parent_obj)).difference(set(dir(obj)))100 for name in delta:101 value = getattr(parent_obj, name)102 # Pytest's pytestmark attributes always get skipped, we don't want103 # to spread that around where it's not wanted. (Besides, it can104 # cause a lot of collection level warnings.)105 if name == "pytestmark":106 continue107 # Classes get skipped; they'd always just be other 'inner' classes108 # that we don't want to copy elsewhere.109 if isinstance(value, six.class_types):110 continue111 # Functions (methods) may get skipped, or not, depending:112 if isinstance(value, types.MethodType):113 # If they look like tests, they get skipped; don't want to copy114 # tests around!115 if istestfunction(name):116 continue117 # Non-test == they're probably lifecycle methods118 # (setup/teardown) or helpers (_do_thing). Rebind them to the119 # target instance, otherwise the 'self' in the setup/helper is120 # not the same 'self' as that in the actual test method it runs121 # around or within!122 # TODO: arguably, all setup or helper methods should become123 # autouse class fixtures (see e.g. pytest docs under 'xunit124 # setup on steroids')125 func = six.get_method_function(value)126 setattr(obj, name, six.create_bound_method(func, obj))127 # Anything else should be some data-type attribute, which is copied128 # verbatim / by-value.129 else:...

Full Screen

Full Screen

test_python_function_task.py

Source:test_python_function_task.py Github

copy

Full Screen

...14 assert isnested(inner_foo) is True15 # Uses tasks.tasks method16 with pytest.raises(ValueError):17 tasks.tasks()18def test_istestfunction():19 assert istestfunction(foo) is True20 assert istestfunction(isnested) is False21 assert istestfunction(tasks.tasks) is False22def test_container_image_conversion():23 default_img = Image(name="default", fqn="xyz.com/abc", tag="tag1")24 other_img = Image(name="other", fqn="xyz.com/other", tag="tag-other")25 cfg = ImageConfig(default_image=default_img, images=[default_img, other_img])26 assert get_registerable_container_image(None, cfg) == "xyz.com/abc:tag1"27 assert get_registerable_container_image("", cfg) == "xyz.com/abc:tag1"28 assert get_registerable_container_image("abc", cfg) == "abc"29 assert get_registerable_container_image("abc:latest", cfg) == "abc:latest"30 assert get_registerable_container_image("abc:{{.image.default.version}}", cfg) == "abc:tag1"31 assert (32 get_registerable_container_image("{{.image.default.fqn}}:{{.image.default.version}}", cfg) == "xyz.com/abc:tag1"33 )34 assert (35 get_registerable_container_image("{{.image.other.fqn}}:{{.image.other.version}}", cfg)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-play automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful