def test_is_hypothesis_file_not_confused_by_prefix(monkeypatch): # Errors in third-party extensions such as `hypothesis-trio` or # `hypothesis-jsonschema` used to be incorrectly considered to be # Hypothesis internal errors, which could result in confusing error # messages. This test makes sure that files like: # `[...]/python3.7/site-packages/hypothesis_something/[...]` # are not considered as hypothesis files. root = os.path.dirname(hypothesis.__file__) assert esc.is_hypothesis_file(hypothesis.__file__) assert esc.is_hypothesis_file(esc.__file__) assert not esc.is_hypothesis_file(pytest.__file__) assert not esc.is_hypothesis_file(root + "-suffix") assert not esc.is_hypothesis_file(root + "-suffix/something.py")
def test_is_hypothesis_file_not_confused_by_prefix(monkeypatch): # Errors in third-party extensions such as `hypothesis-trio` or # `hypothesis-jsonschema` used to be incorrectly considered to be # Hypothesis internal errors, which could result in confusing error # messages. This test makes sure that files like: # `[...]/python3.7/site-packages/hypothesis_something/[...]` # are not considered as hypothesis files. root = os.path.dirname(hypothesis.__file__) assert esc.is_hypothesis_file(hypothesis.__file__) assert esc.is_hypothesis_file(esc.__file__) # Ignore `Path.resolve` for python 3.5 because it is strict by default # and some paths in this test are made up (this behavior has been changed # since python 3.6) if sys.version_info < (3, 6): monkeypatch.setattr("pathlib.Path.resolve", lambda x: x, raising=False) assert not esc.is_hypothesis_file(pytest.__file__) assert not esc.is_hypothesis_file(root + "-suffix") assert not esc.is_hypothesis_file(root + "-suffix/something.py")
def evaluate_test_data(self, data): try: if self.collector is None: result = self.execute(data) else: # pragma: no cover # This should always be a no-op, but the coverage tracer has # a bad habit of resurrecting itself. original = sys.gettrace() sys.settrace(None) try: self.collector.data = {} result = self.execute(data, collect=True) finally: sys.settrace(original) covdata = CoverageData() self.collector.save_data(covdata) self.coverage_data.update(covdata) for filename in covdata.measured_files(): if is_hypothesis_file(filename): continue data.tags.update( arc(filename, source, target) for source, target in covdata.arcs(filename)) if result is not None and self.settings.perform_health_check: fail_health_check( self.settings, ('Tests run under @given should return None, but ' '%s returned %r instead.') % (self.test.__name__, result), HealthCheck.return_value) self.at_least_one_success = True return False except UnsatisfiedAssumption: data.mark_invalid() except ( HypothesisDeprecationWarning, FailedHealthCheck, StopTest, ) + exceptions_to_reraise: raise except Exception as e: escalate_hypothesis_internal_error() data.__expected_traceback = traceback.format_exc() data.__expected_exception = e verbose_report(data.__expected_traceback) error_class, _, tb = sys.exc_info() origin = traceback.extract_tb(tb)[-1] filename = origin[0] lineno = origin[1] data.mark_interesting((error_class, filename, lineno))
def test_is_hypothesis_file_does_not_error_on_invalid_paths_issue_2319(fname): assert not esc.is_hypothesis_file(fname)
def should_trace_file(fname): # fname.startswith("<") indicates runtime code-generation via compile, # e.g. compile("def ...", "<string>", "exec") in e.g. attrs methods. return not (is_hypothesis_file(fname) or fname.startswith("<"))
def hypothesis_check_include(filename): # pragma: no cover if is_hypothesis_file(filename): return False return filename.endswith('.py')
def evaluate_test_data(self, data): if ( time.time() - self.start_time >= HUNG_TEST_TIME_LIMIT ): fail_health_check(self.settings, ( 'Your test has been running for at least five minutes. This ' 'is probably not what you intended, so by default Hypothesis ' 'turns it into an error.' ), HealthCheck.hung_test) try: if self.collector is None: result = self.test_runner(data, reify_and_execute( self.search_strategy, self.test, )) else: # pragma: no cover # This should always be a no-op, but the coverage tracer has # a bad habit of resurrecting itself. original = sys.gettrace() sys.settrace(None) try: self.collector.data = {} result = self.test_runner(data, reify_and_execute( self.search_strategy, self.test, collector=self.collector )) finally: sys.settrace(original) covdata = CoverageData() self.collector.save_data(covdata) self.coverage_data.update(covdata) for filename in covdata.measured_files(): if is_hypothesis_file(filename): continue data.tags.update( arc(filename, source, target) for source, target in covdata.arcs(filename) ) if result is not None and self.settings.perform_health_check: fail_health_check(self.settings, ( 'Tests run under @given should return None, but ' '%s returned %r instead.' ) % (self.test.__name__, result), HealthCheck.return_value) self.at_least_one_success = True return False except UnsatisfiedAssumption: data.mark_invalid() except ( HypothesisDeprecationWarning, FailedHealthCheck, StopTest, ) + exceptions_to_reraise: raise except Exception as e: escalate_hypothesis_internal_error() data.__expected_traceback = traceback.format_exc() data.__expected_exception = e verbose_report(data.__expected_traceback) error_class, _, tb = sys.exc_info() origin = traceback.extract_tb(tb)[-1] filename = origin[0] lineno = origin[1] data.mark_interesting((error_class, filename, lineno))
def evaluate_test_data(self, data): if (time.time() - self.start_time >= HUNG_TEST_TIME_LIMIT): fail_health_check( self.settings, ('Your test has been running for at least five minutes. This ' 'is probably not what you intended, so by default Hypothesis ' 'turns it into an error.'), HealthCheck.hung_test) try: if self.collector is None: result = self.test_runner( data, reify_and_execute( self.search_strategy, self.test, )) else: # pragma: no cover # This should always be a no-op, but the coverage tracer has # a bad habit of resurrecting itself. original = sys.gettrace() sys.settrace(None) try: try: self.collector.data = {} self.collector.start() result = self.test_runner( data, reify_and_execute( self.search_strategy, self.test, )) finally: self.collector.stop() finally: sys.settrace(original) covdata = CoverageData() self.collector.save_data(covdata) self.coverage_data.update(covdata) for filename in covdata.measured_files(): if is_hypothesis_file(filename): continue for lineno in covdata.lines(filename): data.add_tag(Line(filename, lineno)) for source, target in covdata.arcs(filename): data.add_tag(Arc(filename, source, target)) if result is not None and self.settings.perform_health_check: fail_health_check( self.settings, ('Tests run under @given should return None, but ' '%s returned %r instead.') % (self.test.__name__, result), HealthCheck.return_value) self.at_least_one_success = True return False except UnsatisfiedAssumption: data.mark_invalid() except ( HypothesisDeprecationWarning, FailedHealthCheck, StopTest, ) + exceptions_to_reraise: raise except Exception: escalate_hypothesis_internal_error() data.__expected_exception = traceback.format_exc() verbose_report(data.__expected_exception) error_class, _, tb = sys.exc_info() origin = traceback.extract_tb(tb)[-1] filename = origin[0] lineno = origin[1] data.mark_interesting((error_class, filename, lineno))