Best Python code snippet using tox_python
old_weather.py
Source: old_weather.py
...114 Conditions: %(condition)s""" % forecast115 report.append(forecast_str)116 report_str = "\n".join(report)117 return report_str118def create_cli_parser():119 """120 Creates a command line interface parser.121 """122 cli_parser = ArgumentParser(description=__doc__)123 cli_parser.add_argument('location_code',124 help="The location code for the region you want to retrieve weather for. See http://developer.yahoo.com/weather/#req""")125 # Add the CLI options126 cli_parser.add_argument('-n', '--nocurr', action='store_true',127 help="suppress reporting the current weather conditions",128 default=False)129 cli_parser.add_argument('-d', '--delim', action='store',130 help="use the given string as a delimiter between the temperature and the conditions",131 default=" and ")132 cli_parser.add_argument('-f', '--forecast', action='store', type=int,133 help="show the forecast for DAYS days",134 default=0)135 cli_parser.add_argument('-l', '--location', action='store_true',136 help="print the location of the weather",137 default=False)138 cli_parser.add_argument('-m', '--metric', action='store_true',139 help="show the temperature in metric units (C)",140 default=False)141 cli_parser.add_argument('-v', '--verbose', action='store_true',142 help="print the weather section headers",143 default=False)144 cli_parser.add_argument('-t', '--temperature', action="store_true",145 help="print only the current temperature",146 default=False)147 cli_parser.add_argument('-c', '--conditions', action="store_true",148 help="print only the current conditions",149 default=False)150 cli_parser.add_argument('-o', '--output', action='store',151 help="print the weather conditions to a specified file name",152 default="")153 return cli_parser154def main(argv):155 """156 Main entry point of this file. Parses argv, gets weather, then emits output157 """158 # Create the command line parser.159 cli_parser = create_cli_parser()160 # Get the options and arguments.161 args = cli_parser.parse_args(argv)162 # Limit the requested forecast days.163 if args.forecast > DAYS_LIMIT or args.forecast < 0:164 cli_parser.error("Days to forecast must be between 0 and %d"165 % DAYS_LIMIT)166 # Get the weather.167 weather = get_weather(args.location_code, args)168 # Create the report.169 report = create_report(weather, args)170 if report is None:171 return -1172 else:173 if args.output == '':...
test_cli_parser.py
Source: test_cli_parser.py
1import unittest2class CLIParser(object):3 def __init__(self, schema):4 self.flags = {}5 self.schema = schema6 def set_cli(self, cli):7 parsed_cli = self.extract_flags(cli)8 self.evaluate_all_flags(parsed_cli)9 def evaluate_all_flags(self, parsed_cli):10 for item in parsed_cli:11 split_flag = item.split(" ")12 flag = split_flag[0]13 if flag not in self.schema.keys():14 raise Exception(f"Unknown flag has been passed: flag -{flag} is not in the schema")15 value = True16 if len(split_flag) > 1:17 if split_flag[1]:18 value = split_flag[1]19 self.flags[flag] = value20 @staticmethod21 def extract_flags(cli):22 parsed_cli = []23 i = 024 block = ""25 for char in cli:26 if char == "-" and not cli[i + 1].isnumeric():27 parsed_cli.append(block)28 i += 129 block = ""30 continue31 block += char32 i += 133 parsed_cli.append(block)34 return parsed_cli[1:]35 def evaluate_flag(self, flag):36 flag_type = self.schema[flag]37 if type(flag_type) == list:38 flag_type = flag_type[0]39 return [self.cast(val, flag_type) for val in self.flags[flag].split(",")]40 if flag not in self.flags.keys():41 return False42 return self.cast(self.flags[flag], flag_type)43 def cast(self, value, new_type):44 return new_type(value)45class TestCLIParser(unittest.TestCase):46 def test_should_return_true_when_single_boolean_flag(self):47 cli_parser = CLIParser({"l": bool})48 cli_parser.set_cli("-l")49 self.assertEqual(True, cli_parser.evaluate_flag("l"))50 def test_should_return_false_when_no_single_boolean(self):51 cli_parser = CLIParser({"l": bool})52 cli_parser.set_cli("")53 self.assertEqual(False, cli_parser.evaluate_flag("l"))54 def test_should_return_integer_when_single_integer_flag(self):55 cli_parser = CLIParser({"p": int})56 cli_parser.set_cli("-p 8080")57 self.assertEqual(8080, cli_parser.evaluate_flag("p"))58 def test_should_return_string_when_single_string_flag(self):59 cli_parser = CLIParser({"d": str})60 cli_parser.set_cli("-d /usr/logs")61 self.assertEqual("/usr/logs", cli_parser.evaluate_flag("d"))62 def test_should_parse_several_different_flags_type_at_once(self):63 cli_parser = CLIParser({"l": bool, "p": int, "d": str})64 cli_parser.set_cli("-l -p 8080 -d /usr/logs")65 self.assertEqual(True, cli_parser.evaluate_flag("l"))66 self.assertEqual(8080, cli_parser.evaluate_flag("p"))67 self.assertEqual("/usr/logs", cli_parser.evaluate_flag("d"))68 def test_should_return_collection_when_single_collection_flag(self):69 cli_parser = CLIParser({"g": [str]})70 cli_parser.set_cli("-g this,is,a,list")71 self.assertEqual(["this", "is", "a", "list"], cli_parser.evaluate_flag("g"))72 def test_cli_is_split_correctly_with_minus_in_values(self):73 cli_parser = CLIParser({"d": int})74 cli_parser.set_cli("-d -3")75 self.assertEqual(-3, cli_parser.evaluate_flag("d"))76 def test_should_return_multiple_collections_when_multiple_collection_flag(self):77 cli_parser = CLIParser({"g": [str], "d": [int]})78 cli_parser.set_cli("-g this,is,a,list -d 1,2,-3,5")79 self.assertEqual(["this", "is", "a", "list"], cli_parser.evaluate_flag("g"))80 self.assertEqual([1, 2, -3, 5], cli_parser.evaluate_flag("d"))81 def test_should_raise_an_exception_if_flag_is_not_in_schema(self):82 cli_parser = CLIParser({"g": str, "d": [int]})83 with self.assertRaises(Exception) as context:84 cli_parser.set_cli("-f this,is,not,a,flag")85 self.assertEqual("Unknown flag has been passed: flag -f is not in the schema", context.exception.args[0])86if __name__ == '__main__':...
test_inspect.py
Source: test_inspect.py
1from __future__ import absolute_import, division, print_function2import pytest3import requests_mock4from appr.client import DEFAULT_PREFIX5from appr.commands.inspect import InspectCmd6def get_inspectcmd(cli_parser, args=[]):7 options = cli_parser.parse_args(["inspect"] + args)8 return InspectCmd(options)9def test_inspect_init(cli_parser):10 inspectcmd = get_inspectcmd(cli_parser, ["kpm.sh/foo/bar", "-t", "helm"])11 assert inspectcmd.version == "default"12 assert inspectcmd.registry_host == "kpm.sh"13 assert InspectCmd.name == "inspect"14def test_inspect_tree(cli_parser, package_blob, capsys):15 inspectcmd = get_inspectcmd(cli_parser, ["kpm.sh/foo/bar@1.0.0", "-t", "helm", "--tree"])16 with requests_mock.mock() as m:17 response = package_blob18 m.get("https://kpm.sh" + DEFAULT_PREFIX + "/api/v1/packages/foo/bar/1.0.0/helm/pull", content=response)19 inspectcmd.exec_cmd()20 out, err = capsys.readouterr()21 default_out = ["README.md", "manifest.yaml", "templates/rocketchat-rc.yml", "templates/rocketchat-svc.yml\n"]22 default_out.sort()23 assert out == "\n".join(default_out)24def test_inspect_default(cli_parser, package_blob, capsys):25 """ Default is the tree view """26 inspectcmd = get_inspectcmd(cli_parser, ["kpm.sh/foo/bar@1.0.0", "-t", "helm", "--tree"])27 inspectcmd_default_file = get_inspectcmd(cli_parser, ["kpm.sh/foo/bar@1.0.0", "-t", "helm"])28 with requests_mock.mock() as m:29 response = package_blob30 m.get("https://kpm.sh" + DEFAULT_PREFIX + "/api/v1/packages/foo/bar/1.0.0/helm/pull", content=response)31 inspectcmd.exec_cmd()32 out, err = capsys.readouterr()33 inspectcmd_default_file.exec_cmd()34 default_out, default_err = capsys.readouterr()35 assert out == default_out36def test_inspect_file(cli_parser, package_blob, capsys):37 inspectcmd = get_inspectcmd(cli_parser, ["kpm.sh/foo/bar@1.0.0", "-t", "helm", "--file", "README.md"])38 with requests_mock.mock() as m:39 response = package_blob40 m.get("https://kpm.sh" + DEFAULT_PREFIX + "/api/v1/packages/foo/bar/1.0.0/helm/pull", content=response)41 inspectcmd.exec_cmd()42 out, err = capsys.readouterr()43 readme = "\nrocketchat\n===========\n\n# Install\n\nkpm install rocketchat\n\n"44 assert out == readme + "\n"...
Check out the latest blogs from LambdaTest on this topic:
Hey Testers! We know it’s been tough out there at this time when the pandemic is far from gone and remote working has become the new normal. Regardless of all the hurdles, we are continually working to bring more features on-board for a seamless cross-browser testing experience.
Manual cross browser testing is neither efficient nor scalable as it will take ages to test on all permutations & combinations of browsers, operating systems, and their versions. Like every developer, I have also gone through that ‘I can do it all phase’. But if you are stuck validating your code changes over hundreds of browsers and OS combinations then your release window is going to look even shorter than it already is. This is why automated browser testing can be pivotal for modern-day release cycles as it speeds up the entire process of cross browser compatibility.
With new-age project development methodologies like Agile and DevOps slowly replacing the old-age waterfall model, the demand for testing is increasing in the industry. Testers are now working together with the developers and automation testing is vastly replacing manual testing in many ways. If you are new to the domain of automation testing, the organization that just hired you, will expect you to be fast, think out of the box, and able to detect bugs or deliver solutions which no one thought of. But with just basic knowledge of testing, how can you be that successful test automation engineer who is different from their predecessors? What are the skills to become a successful automation tester in 2019? Let’s find out.
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.
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!!