Best Python code snippet using hypothesis
test_labeled_data_unpacking.py
Source:test_labeled_data_unpacking.py
...243 assert_raises(RuntimeError, f)244def test_function_call_with_replace_all_args():245 """Test with a "replace_all_args" argument, all *args should be replaced"""246 data = {"a": [1, 2], "b": [8, 9], "x": "xyz"}247 def funcy(ax, *args, **kwargs):248 all_args = [None, None, "x", None, "NOT"]249 for i, v in enumerate(args):250 all_args[i] = v251 for i, k in enumerate(["x", "y", "ls", "label", "w"]):252 if k in kwargs:253 all_args[i] = kwargs[k]254 x, y, ls, label, w = all_args255 return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (256 list(x), list(y), ls, w, label)257 func = unpack_labeled_data(replace_all_args=True, replace_names=["w"],258 label_namer="y")(funcy)259 assert_equal(func(None, "a", "b", w="x", label="", data=data),260 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")261 assert_equal(func(None, "a", "b", w="x", label="text", data=data),262 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")263 func2 = unpack_labeled_data(replace_all_args=True, replace_names=["w"],264 label_namer="y",265 positional_parameter_names=["x", "y", "ls",266 "label", "w"])(267 funcy)268 assert_equal(func2(None, "a", "b", w="x", data=data),269 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")270 assert_equal(func2(None, "a", "b", w="x", label="", data=data),271 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")272 assert_equal(func2(None, "a", "b", w="x", label="text", data=data),273 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")274def test_docstring_addition():275 @unpack_labeled_data()276 def funcy(ax, *args, **kwargs):277 """Funcy does nothing"""278 pass279 assert_regex(funcy.__doc__,280 r".*All positional and all keyword arguments\.")281 assert_not_regex(funcy.__doc__, r".*All positional arguments\.")282 assert_not_regex(funcy.__doc__,283 r".*All arguments with the following names: .*")284 @unpack_labeled_data(replace_all_args=True, replace_names=[])285 def funcy(ax, x, y, z, bar=None):286 """Funcy does nothing"""287 pass288 assert_regex(funcy.__doc__, r".*All positional arguments\.")289 assert_not_regex(funcy.__doc__,290 r".*All positional and all keyword arguments\.")291 assert_not_regex(funcy.__doc__,292 r".*All arguments with the following names: .*")293 @unpack_labeled_data(replace_all_args=True, replace_names=["bar"])294 def funcy(ax, x, y, z, bar=None):295 """Funcy does nothing"""296 pass297 assert_regex(funcy.__doc__, r".*All positional arguments\.")298 assert_regex(funcy.__doc__,299 r".*All arguments with the following names: 'bar'\.")300 assert_not_regex(funcy.__doc__,301 r".*All positional and all keyword arguments\.")302 @unpack_labeled_data(replace_names=["x", "bar"])303 def funcy(ax, x, y, z, bar=None):304 """Funcy does nothing"""305 pass306 # lists can print in any order, so test for both x,bar and bar,x307 assert_regex(funcy.__doc__,308 r".*All arguments with the following names: '.*', '.*'\.")309 assert_regex(funcy.__doc__, r".*'x'.*")310 assert_regex(funcy.__doc__, r".*'bar'.*")311 assert_not_regex(funcy.__doc__,312 r".*All positional and all keyword arguments\.")313 assert_not_regex(funcy.__doc__, r".*All positional arguments\.")314def test_positional_parameter_names_as_function():315 # Also test the _plot_arg_replacer for plot...316 from matplotlib.axes._axes import _plot_args_replacer317 @unpack_labeled_data(replace_names=["x", "y"],318 positional_parameter_names=_plot_args_replacer)319 def funcy(ax, *args, **kwargs):320 return "{args} | {kwargs}".format(args=args, kwargs=kwargs)321 # the normal case...322 data = {"x": "X", "y1": "Y"}323 assert_equal(funcy(None, "x", "y1", data=data),324 "('X', 'Y') | {}")325 assert_equal(funcy(None, "x", "y1", "c", data=data),326 "('X', 'Y', 'c') | {}")327 # no arbitrary long args with data328 def f():329 assert_equal(funcy(None, "x", "y", "c", "x", "y", "x", "y", data=data),330 "('X', 'Y', 'c', 'X', 'Y', 'X', 'Y') | {}")331 assert_raises(ValueError, f)332 # In the two arg case, if a valid color spec is in data, we warn but use333 # it as data...334 data = {"x": "X", "y": "Y", "ro": "!!"}335 with assert_produces_warning(RuntimeWarning):336 assert_equal(funcy(None, "y", "ro", data=data),...
test_preprocess_data.py
Source:test_preprocess_data.py
...220 func(None, "a", "b", "z", "z", data=data)221def test_function_call_with_replace_all_args():222 """Test with a "replace_all_args" argument, all *args should be replaced"""223 data = {"a": [1, 2], "b": [8, 9], "x": "xyz"}224 def funcy(ax, *args, **kwargs):225 all_args = [None, None, "x", None, "NOT"]226 for i, v in enumerate(args):227 all_args[i] = v228 for i, k in enumerate(["x", "y", "ls", "label", "w"]):229 if k in kwargs:230 all_args[i] = kwargs[k]231 x, y, ls, label, w = all_args232 return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (233 list(x), list(y), ls, w, label)234 func = _preprocess_data(replace_all_args=True, replace_names=["w"],235 label_namer="y")(funcy)236 assert (func(None, "a", "b", w="x", label="", data=data) ==237 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")238 assert (func(None, "a", "b", w="x", label="text", data=data) ==239 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")240 func2 = _preprocess_data(replace_all_args=True, replace_names=["w"],241 label_namer="y",242 positional_parameter_names=["x", "y", "ls",243 "label", "w"])(funcy)244 assert (func2(None, "a", "b", w="x", data=data) ==245 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")246 assert (func2(None, "a", "b", w="x", label="", data=data) ==247 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")248 assert (func2(None, "a", "b", w="x", label="text", data=data) ==249 "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")250def test_docstring_addition():251 @_preprocess_data()252 def funcy(ax, *args, **kwargs):253 """Funcy does nothing"""254 pass255 assert re.search(r".*All positional and all keyword arguments\.",256 funcy.__doc__)257 assert not re.search(r".*All positional arguments\.", funcy.__doc__)258 assert not re.search(r".*All arguments with the following names: .*",259 funcy.__doc__)260 @_preprocess_data(replace_all_args=True, replace_names=[])261 def funcy(ax, x, y, z, bar=None):262 """Funcy does nothing"""263 pass264 assert re.search(r".*All positional arguments\.",265 funcy.__doc__)266 assert not re.search(r".*All positional and all keyword arguments\.",267 funcy.__doc__)268 assert not re.search(r".*All arguments with the following names: .*",269 funcy.__doc__)270 @_preprocess_data(replace_all_args=True, replace_names=["bar"])271 def funcy(ax, x, y, z, bar=None):272 """Funcy does nothing"""273 pass274 assert re.search(r".*All positional arguments\.", funcy.__doc__)275 assert re.search(r".*All arguments with the following names: 'bar'\.",276 funcy.__doc__)277 assert not re.search(r".*All positional and all keyword arguments\.",278 funcy.__doc__)279 @_preprocess_data(replace_names=["x", "bar"])280 def funcy(ax, x, y, z, bar=None):281 """Funcy does nothing"""282 pass283 # lists can print in any order, so test for both x,bar and bar,x284 assert re.search(r".*All arguments with the following names: '.*', '.*'\.",285 funcy.__doc__)286 assert re.search(r".*'x'.*", funcy.__doc__)287 assert re.search(r".*'bar'.*", funcy.__doc__)288 assert not re.search(r".*All positional and all keyword arguments\.",289 funcy.__doc__)290 assert not re.search(r".*All positional arguments\.",291 funcy.__doc__)292def test_positional_parameter_names_as_function():293 # Also test the _plot_arg_replacer for plot...294 from matplotlib.axes._axes import _plot_args_replacer295 @_preprocess_data(replace_names=["x", "y"],296 positional_parameter_names=_plot_args_replacer)297 def funcy(ax, *args, **kwargs):298 return "{args} | {kwargs}".format(args=args, kwargs=kwargs)299 # the normal case...300 data = {"x": "X", "hy1": "Y"}301 assert funcy(None, "x", "hy1", data=data) == "('X', 'Y') | {}"302 assert funcy(None, "x", "hy1", "c", data=data) == "('X', 'Y', 'c') | {}"303 # no arbitrary long args with data304 with pytest.raises(ValueError):305 assert (funcy(None, "x", "y", "c", "x", "y", "x", "y", data=data) ==306 "('X', 'Y', 'c', 'X', 'Y', 'X', 'Y') | {}")307 # In the two arg case, if a valid color spec is in data, we warn but use308 # it as data...309 data = {"x": "X", "y": "Y", "ro": "!!"}310 with pytest.warns(RuntimeWarning):...
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!!