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 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 #2
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 #3
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 #4
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 #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_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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def pretend_to_be_nose_with_cover(self, erase=False, cd=False): """This is what the nose --with-cover plugin does.""" self.make_file("no_biggie.py", """\ a = 1 b = 2 if b == 1: c = 4 """) self.make_file("sub/hold.txt", "") cov = coverage.Coverage() if erase: cov.combine() cov.erase() cov.load() self.start_import_stop(cov, "no_biggie") if cd: os.chdir("sub") 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 """)) if cd: os.chdir("..")
Example #17
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_source_package_as_package_part_omitted(self): # https://bitbucket.org/ned/coveragepy/issues/638/run-omit-is-ignored-since-45 lines = self.coverage_usepkgs(source=["pkg1"], omit=["*/p1b.py"]) self.filenames_in(lines, "p1a") self.filenames_not_in(lines, "p1b") self.assertEqual(lines['p1c'], 0)
Example #18
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_omit_and_include(self): result = self.coverage_usepkgs(include=["*/p1*"], omit=["*/p1a.py"]) self.filenames_in(result, "p1b") self.filenames_not_in(result, "p1a p1c p2a p2b")
Example #19
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_omit_as_string(self): result = self.coverage_usepkgs(omit="*a.py") self.filenames_in(result, "p1b p2b otherb osb") self.filenames_not_in(result, "p1a p1c p2a othera osa")
Example #20
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_many_missing_branches(self): cov = coverage.Coverage(branch=True) self.make_file("missing.py", """\ def fun1(x): if x == 1: print("one") else: print("not one") print("done") # pragma: nocover def fun2(x): print("x") fun2(3) """) # Import the Python file, executing it. self.start_import_stop(cov, "missing") nums = cov._analyze("missing.py").numbers self.assertEqual(nums.n_files, 1) self.assertEqual(nums.n_statements, 7) self.assertEqual(nums.n_excluded, 1) self.assertEqual(nums.n_missing, 3) self.assertEqual(nums.n_branches, 2) self.assertEqual(nums.n_partial_branches, 0) self.assertEqual(nums.n_missing_branches, 2)
Example #21
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_config_doesnt_change(self): self.make_file("simple.py", "a = 1") cov = coverage.Coverage() self.start_import_stop(cov, "simple") self.assertEqual(cov.get_option("report:show_missing"), False) cov.report(show_missing=True) self.assertEqual(cov.get_option("report:show_missing"), False)
Example #22
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 #23
Source File: test_api.py From coveragepy 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")
Example #24
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_switch_context_testrunner(self): # This test simulates a coverage-aware test runner, # measuring labeled coverage via public API self.make_test_files() # Test runner starts cov = coverage.Coverage() cov.start() if "pragma: nested": # Imports the test suite suite = import_local_file("testsuite") # Measures test case 1 cov.switch_context('multiply_zero') suite.test_multiply_zero() # Measures test case 2 cov.switch_context('multiply_six') suite.test_multiply_six() # Runner finishes cov.save() cov.stop() # Labeled data is collected data = cov.get_data() self.assertEqual( [u'', u'multiply_six', u'multiply_zero'], sorted(data.measured_contexts()) ) filenames = self.get_measured_filenames(data) suite_filename = filenames['testsuite.py'] data.set_query_context("multiply_six") self.assertEqual([2, 8], sorted(data.lines(suite_filename))) data.set_query_context("multiply_zero") self.assertEqual([2, 5], sorted(data.lines(suite_filename)))
Example #25
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def make_test_files(self): """Create a simple file representing a method with two tests. Returns absolute path to the file. """ self.make_file("testsuite.py", """\ def timestwo(x): return x*2 def test_multiply_zero(): assert timestwo(0) == 0 def test_multiply_six(): assert timestwo(6) == 12 """)
Example #26
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 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() expected = textwrap.dedent("""\ Name Stmts Miss Cover --------------------------- b.py 1 0 100% """) self.assertEqual(expected, self.stdout())
Example #27
Source File: test_api.py From coveragepy 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 #28
Source File: test_api.py From coveragepy with Apache License 2.0 | 5 votes |
def test_two_getdata_only_warn_once(self): self.make_code1_code2() cov = coverage.Coverage(source=["."], omit=["code1.py"]) cov.start() import_local_file("code1") # pragma: nested cov.stop() # pragma: nested # We didn't collect any data, so we should get a warning. with self.assert_warnings(cov, ["No data was collected"]): cov.get_data() # But calling get_data a second time with no intervening activity # won't make another warning. with self.assert_warnings(cov, []): cov.get_data()
Example #29
Source File: test_api.py From coveragepy 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 #30
Source File: test_api.py From coveragepy 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 """)