Python random.py() Examples
The following are 30
code examples of random.py().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
random
, or try the search function
.

Example #1
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_unexecuted_file(self): cov = coverage.Coverage() self.make_file("mycode.py", """\ a = 1 b = 2 if b == 3: c = 4 d = 5 """) self.make_file("not_run.py", """\ fooey = 17 """) # Import the Python file, executing it. self.start_import_stop(cov, "mycode") _, statements, missing, _ = cov.analysis("not_run.py") self.assertEqual(statements, [1]) self.assertEqual(missing, [1])
Example #2
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_include_can_measure_stdlib(self): self.make_file("mymain.py", """\ import colorsys, random a = 1 r, g, b = [random.random() for _ in range(3)] hls = colorsys.rgb_to_hls(r, g, b) """) # Measure without the stdlib, but include colorsys. cov1 = coverage.Coverage(cover_pylib=False, include=["*/colorsys.py"]) self.start_import_stop(cov1, "mymain") # some statements were marked executed in colorsys.py _, statements, missing, _ = cov1.analysis("colorsys.py") self.assertNotEqual(statements, missing) # but none were in random.py _, statements, missing, _ = cov1.analysis("random.py") self.assertEqual(statements, missing)
Example #3
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_combining_corrupt_data(self): # If you combine a corrupt data file, then you will get a warning, # and the file will remain. self.make_good_data_files() self.make_bad_data_file() cov = coverage.Coverage() warning_regex = ( r"Couldn't read data from '.*\.coverage\.foo': " r"CoverageException: Doesn't seem to be a coverage\.py data file" ) with self.assert_warnings(cov, [warning_regex]): cov.combine() # We got the results from code1 and code2 properly. self.check_code1_code2(cov) # The bad file still exists. self.assert_exists(".coverage.foo")
Example #4
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_combining_twice(self): self.make_good_data_files() cov1 = coverage.Coverage() cov1.combine() cov1.save() self.check_code1_code2(cov1) cov2 = coverage.Coverage() with self.assertRaisesRegex(CoverageException, r"No data to combine"): cov2.combine(strict=True) cov3 = coverage.Coverage() cov3.combine() # Now the data is empty! _, statements, missing, _ = cov3.analysis("code1.py") self.assertEqual(statements, [1]) self.assertEqual(missing, [1]) _, statements, missing, _ = cov3.analysis("code2.py") self.assertEqual(statements, [1, 2]) self.assertEqual(missing, [1, 2])
Example #5
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_warnings(self): self.make_file("hello.py", """\ import sys, os print("Hello") """) cov = coverage.Coverage(source=["sys", "xyzzy", "quux"]) self.start_import_stop(cov, "hello") cov.get_data() out = self.stdout() self.assertIn("Hello\n", out) err = self.stderr() self.assertIn(textwrap.dedent("""\ Coverage.py warning: Module sys has no Python source. (module-not-python) Coverage.py warning: Module xyzzy was never imported. (module-not-imported) Coverage.py warning: Module quux was never imported. (module-not-imported) Coverage.py warning: No data was collected. (no-data-collected) """), err)
Example #6
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_source_and_include_dont_conflict(self): # A bad fix made this case fail: https://bitbucket.org/ned/coveragepy/issues/541 self.make_file("a.py", "import b\na = 1") self.make_file("b.py", "b = 1") self.make_file(".coveragerc", """\ [run] source = . """) # Just like: coverage run a.py cov = coverage.Coverage() self.start_import_stop(cov, "a") cov.save() # Run the equivalent of: coverage report --include=b.py cov = coverage.Coverage(include=["b.py"]) cov.load() # There should be no exception. At one point, report() threw: # CoverageException: --include and --source are mutually exclusive cov.report() self.assertEqual(self.stdout(), textwrap.dedent("""\ Name Stmts Miss Cover --------------------------- b.py 1 0 100% """))
Example #7
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def coverage_usepkgs(self, **kwargs): """Run coverage on usepkgs and return the line summary. Arguments are passed to the `coverage.Coverage` constructor. """ cov = coverage.Coverage(**kwargs) cov.start() import usepkgs # pragma: nested # pylint: disable=import-error, unused-variable cov.stop() # pragma: nested data = cov.get_data() summary = data.line_counts() for k, v in list(summary.items()): assert k.endswith(".py") summary[k[:-3]] = v return summary
Example #8
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def pretend_to_be_nose_with_cover(self, erase): """This is what the nose --with-cover plugin does.""" cov = coverage.Coverage() self.make_file("no_biggie.py", """\ a = 1 b = 2 if b == 1: c = 4 """) if erase: cov.combine() cov.erase() cov.load() self.start_import_stop(cov, "no_biggie") cov.combine() cov.save() cov.report(["no_biggie.py"], show_missing=True) self.assertEqual(self.stdout(), textwrap.dedent("""\ Name Stmts Miss Cover Missing -------------------------------------------- no_biggie.py 4 1 75% 4 """))
Example #9
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_unexecuted_file(self): cov = coverage.Coverage() self.make_file("mycode.py", """\ a = 1 b = 2 if b == 3: c = 4 d = 5 """) self.make_file("not_run.py", """\ fooey = 17 """) # Import the Python file, executing it. self.start_import_stop(cov, "mycode") _, statements, missing, _ = cov.analysis("not_run.py") self.assertEqual(statements, [1]) self.assertEqual(missing, [1])
Example #10
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_include_can_measure_stdlib(self): self.make_file("mymain.py", """\ import colorsys, random a = 1 r, g, b = [random.random() for _ in range(3)] hls = colorsys.rgb_to_hls(r, g, b) """) # Measure without the stdlib, but include colorsys. cov1 = coverage.Coverage(cover_pylib=False, include=["*/colorsys.py"]) self.start_import_stop(cov1, "mymain") # some statements were marked executed in colorsys.py _, statements, missing, _ = cov1.analysis("colorsys.py") self.assertNotEqual(statements, missing) # but none were in random.py _, statements, missing, _ = cov1.analysis("random.py") self.assertEqual(statements, missing)
Example #11
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_combining_twice(self): self.make_good_data_files() cov1 = coverage.Coverage() cov1.combine() cov1.save() self.check_code1_code2(cov1) self.assert_file_count(".coverage.*", 0) self.assert_exists(".coverage") cov2 = coverage.Coverage() with self.assertRaisesRegex(CoverageException, r"No data to combine"): cov2.combine(strict=True) cov3 = coverage.Coverage() cov3.combine() # Now the data is empty! _, statements, missing, _ = cov3.analysis("code1.py") self.assertEqual(statements, [1]) self.assertEqual(missing, [1]) _, statements, missing, _ = cov3.analysis("code2.py") self.assertEqual(statements, [1, 2]) self.assertEqual(missing, [1, 2])
Example #12
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_warnings(self): self.make_file("hello.py", """\ import sys, os print("Hello") """) cov = coverage.Coverage(source=["sys", "xyzzy", "quux"]) self.start_import_stop(cov, "hello") cov.get_data() out = self.stdout() self.assertIn("Hello\n", out) err = self.stderr() self.assertIn(textwrap.dedent("""\ Coverage.py warning: Module sys has no Python source. (module-not-python) Coverage.py warning: Module xyzzy was never imported. (module-not-imported) Coverage.py warning: Module quux was never imported. (module-not-imported) Coverage.py warning: No data was collected. (no-data-collected) """), err)
Example #13
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_warnings_suppressed(self): self.make_file("hello.py", """\ import sys, os print("Hello") """) self.make_file(".coveragerc", """\ [run] disable_warnings = no-data-collected, module-not-imported """) cov = coverage.Coverage(source=["sys", "xyzzy", "quux"]) self.start_import_stop(cov, "hello") cov.get_data() out = self.stdout() self.assertIn("Hello\n", out) err = self.stderr() self.assertIn( "Coverage.py warning: Module sys has no Python source. (module-not-python)", err ) self.assertNotIn("module-not-imported", err) self.assertNotIn("no-data-collected", err)
Example #14
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def coverage_usepkgs(self, **kwargs): """Run coverage on usepkgs and return the line summary. Arguments are passed to the `coverage.Coverage` constructor. """ cov = coverage.Coverage(**kwargs) cov.start() import usepkgs # pragma: nested # pylint: disable=import-error, unused-import cov.stop() # pragma: nested data = cov.get_data() summary = line_counts(data) for k, v in list(summary.items()): assert k.endswith(".py") summary[k[:-3]] = v return summary
Example #15
Source File: test_api.py From coveragepy with Apache License 2.0 | 6 votes |
def test_moving_stuff_with_relative(self): # When using relative file names, moving the source around is fine. self.make_file("foo.py", "a = 1") self.make_file(".coveragerc", """\ [run] relative_files = true """) cov = coverage.Coverage(source=["."]) self.start_import_stop(cov, "foo") res = cov.report() assert res == 100 os.remove("foo.py") self.make_file("new/foo.py", "a = 1") shutil.move(".coverage", "new/.coverage") shutil.move(".coveragerc", "new/.coveragerc") with change_dir("new"): cov = coverage.Coverage() cov.load() res = cov.report() assert res == 100
Example #16
Source File: randcrack.py From Python-random-module-cracker with MIT License | 5 votes |
def predict_randrange(self, start, stop=None, step=1, _int=int): # Adopted messy code from random.py module # In fact only changed _randbelow() method calls to predict_randbelow() istart = _int(start) if istart != start: raise ValueError("non-integer arg 1 for randrange()") if stop is None: if istart > 0: return self.predict_randbelow(istart) raise ValueError("empty range for randrange()") # stop argument supplied. istop = _int(stop) if istop != stop: raise ValueError("non-integer stop for randrange()") width = istop - istart if step == 1 and width > 0: return istart + self.predict_randbelow(width) if step == 1: raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) # Non-unit step argument supplied. istep = _int(step) if istep != step: raise ValueError("non-integer step for randrange()") if istep > 0: n = (width + istep - 1) // istep elif istep < 0: n = (width + istep + 1) // istep else: raise ValueError("zero step for randrange()") if n <= 0: raise ValueError("empty range for randrange()") return istart + istep * self.predict_randbelow(n)
Example #17
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def assertFiles(self, files): """Assert that the files here are `files`, ignoring the usual junk.""" here = os.listdir(".") here = self.clean_files(here, ["*.pyc", "__pycache__", "*$py.class"]) self.assertCountEqual(here, files)
Example #18
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_filenames(self): self.make_file("mymain.py", """\ import mymod a = 1 """) self.make_file("mymod.py", """\ fooey = 17 """) # Import the Python file, executing it. cov = coverage.Coverage() self.start_import_stop(cov, "mymain") filename, _, _, _ = cov.analysis("mymain.py") self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis("mymod.py") self.assertEqual(os.path.basename(filename), "mymod.py") filename, _, _, _ = cov.analysis(sys.modules["mymain"]) self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis(sys.modules["mymod"]) self.assertEqual(os.path.basename(filename), "mymod.py") # Import the Python file, executing it again, once it's been compiled # already. cov = coverage.Coverage() self.start_import_stop(cov, "mymain") filename, _, _, _ = cov.analysis("mymain.py") self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis("mymod.py") self.assertEqual(os.path.basename(filename), "mymod.py") filename, _, _, _ = cov.analysis(sys.modules["mymain"]) self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis(sys.modules["mymod"]) self.assertEqual(os.path.basename(filename), "mymod.py")
Example #19
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_ignore_stdlib(self): self.make_file("mymain.py", """\ import colorsys a = 1 hls = colorsys.rgb_to_hls(1.0, 0.5, 0.0) """) # Measure without the stdlib. cov1 = coverage.Coverage() self.assertEqual(cov1.config.cover_pylib, False) self.start_import_stop(cov1, "mymain") # some statements were marked executed in mymain.py _, statements, missing, _ = cov1.analysis("mymain.py") self.assertNotEqual(statements, missing) # but none were in colorsys.py _, statements, missing, _ = cov1.analysis("colorsys.py") self.assertEqual(statements, missing) # Measure with the stdlib. cov2 = coverage.Coverage(cover_pylib=True) self.start_import_stop(cov2, "mymain") # some statements were marked executed in mymain.py _, statements, missing, _ = cov2.analysis("mymain.py") self.assertNotEqual(statements, missing) # and some were marked executed in colorsys.py _, statements, missing, _ = cov2.analysis("colorsys.py") self.assertNotEqual(statements, missing)
Example #20
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_datafile_specified(self): # You can specify the data file name. self.make_file("datatest2.py", """\ fooey = 17 """) self.assertFiles(["datatest2.py"]) cov = coverage.Coverage(data_file="cov.data") self.start_import_stop(cov, "datatest2") cov.save() self.assertFiles(["datatest2.py", "cov.data"])
Example #21
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_datafile_and_suffix_specified(self): # You can specify the data file name and suffix. self.make_file("datatest3.py", """\ fooey = 17 """) self.assertFiles(["datatest3.py"]) cov = coverage.Coverage(data_file="cov.data", data_suffix="14") self.start_import_stop(cov, "datatest3") cov.save() self.assertFiles(["datatest3.py", "cov.data.14"])
Example #22
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_datafile_from_rcfile(self): # You can specify the data file name in the .coveragerc file self.make_file("datatest4.py", """\ fooey = 17 """) self.make_file(".coveragerc", """\ [run] data_file = mydata.dat """) self.assertFiles(["datatest4.py", ".coveragerc"]) cov = coverage.Coverage() self.start_import_stop(cov, "datatest4") cov.save() self.assertFiles(["datatest4.py", ".coveragerc", "mydata.dat"])
Example #23
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def make_code1_code2(self): """Create the code1.py and code2.py files.""" self.make_file("code1.py", """\ code1 = 1 """) self.make_file("code2.py", """\ code2 = 1 code2 = 2 """)
Example #24
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def check_code1_code2(self, cov): """Check the analysis is correct for code1.py and code2.py.""" _, statements, missing, _ = cov.analysis("code1.py") self.assertEqual(statements, [1]) self.assertEqual(missing, []) _, statements, missing, _ = cov.analysis("code2.py") self.assertEqual(statements, [1, 2]) self.assertEqual(missing, [])
Example #25
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_two_getdata_only_warn_once_nostop(self): self.make_code1_code2() cov = coverage.Coverage(source=["."], omit=["code1.py"]) cov.start() import_local_file("code1") # pragma: nested # We didn't collect any data, so we should get a warning. with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested cov.get_data() # pragma: nested # But calling get_data a second time with no intervening activity # won't make another warning. with self.assert_warnings(cov, []): # pragma: nested cov.get_data() # pragma: nested # Then stop it, or the test suite gets out of whack. cov.stop() # pragma: nested
Example #26
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_two_getdata_warn_twice(self): self.make_code1_code2() cov = coverage.Coverage(source=["."], omit=["code1.py", "code2.py"]) cov.start() import_local_file("code1") # pragma: nested # We didn't collect any data, so we should get a warning. with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested cov.save() # pragma: nested import_local_file("code2") # pragma: nested # Calling get_data a second time after tracing some more will warn again. with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested cov.get_data() # pragma: nested # Then stop it, or the test suite gets out of whack. cov.stop() # pragma: nested
Example #27
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_explicit_namespace_module(self): self.make_file("main.py", "import namespace_420\n") cov = coverage.Coverage() self.start_import_stop(cov, "main") with self.assertRaisesRegex(CoverageException, r"Module .* has no file"): cov.analysis(sys.modules['namespace_420'])
Example #28
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_bug_572(self): self.make_file("main.py", "import namespace_420\n") # Use source=namespace_420 to trigger the check that used to fail, # and use source=main so that something is measured. cov = coverage.Coverage(source=["namespace_420", "main"]) with self.assert_warnings(cov, []): self.start_import_stop(cov, "main") cov.report()
Example #29
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_include(self): result = self.coverage_usepkgs(include=["*/p1a.py"]) self.filenames_in(result, "p1a") self.filenames_not_in(result, "p1b p1c p2a p2b othera otherb osa osb")
Example #30
Source File: test_api.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_include_2(self): result = self.coverage_usepkgs(include=["*a.py"]) self.filenames_in(result, "p1a p2a othera osa") self.filenames_not_in(result, "p1b p1c p2b otherb osb")