diff --git a/src/pytest_benchmark/cli.py b/src/pytest_benchmark/cli.py index 03bfa1d..30e847b 100644 --- a/src/pytest_benchmark/cli.py +++ b/src/pytest_benchmark/cli.py @@ -133,7 +133,10 @@ def __getattr__(self, item): def main(): parser = make_parser() args = parser.parse_args() - logger = Logger(args.verbose) + level = Logger.QUIET if args.quiet else Logger.NORMAL + if args.verbose: + level = Logger.VERBOSE + logger = Logger(level) storage = load_storage(args.storage, logger=logger, netrc=args.netrc) hook = HookDispatch() diff --git a/src/pytest_benchmark/logger.py b/src/pytest_benchmark/logger.py index 94d94b8..6e2c8fa 100644 --- a/src/pytest_benchmark/logger.py +++ b/src/pytest_benchmark/logger.py @@ -13,8 +13,10 @@ class PytestBenchmarkWarning(PytestWarning): class Logger(object): - def __init__(self, verbose, config=None): - self.verbose = verbose + VERBOSE, NORMAL, QUIET = range(3) + + def __init__(self, level=NORMAL, config=None): + self.level = level self.term = py.io.TerminalWriter(file=sys.stderr) self.suspend_capture = None self.resume_capture = None @@ -29,7 +31,7 @@ def __init__(self, verbose, config=None): getattr('capman', 'resumecapture', None)) def warn(self, text, warner=None, suspend=False): - if self.verbose: + if self.level == self.VERBOSE: if suspend and self.suspend_capture: self.suspend_capture(in_=True) self.term.line("") @@ -50,14 +52,15 @@ def error(self, text): self.term.sep("-", red=True, bold=True) def info(self, text, newline=True, **kwargs): - if not kwargs or kwargs == {'bold': True}: - kwargs['purple'] = True - if newline: - self.term.line("") - self.term.line(text, **kwargs) + if self.level >= self.NORMAL: + if not kwargs or kwargs == {'bold': True}: + kwargs['purple'] = True + if newline: + self.term.line("") + self.term.line(text, **kwargs) def debug(self, text, newline=False, **kwargs): - if self.verbose: + if self.level == self.VERBOSE: if self.suspend_capture: self.suspend_capture(in_=True) self.info(text, newline=newline, **kwargs) diff --git a/src/pytest_benchmark/plugin.py b/src/pytest_benchmark/plugin.py index 40a261d..f485835 100644 --- a/src/pytest_benchmark/plugin.py +++ b/src/pytest_benchmark/plugin.py @@ -121,6 +121,11 @@ def add_global_options(addoption, prefix="benchmark-"): action="store_true", default=False, help="Dump diagnostic and progress information." ) + addoption( + "--{0}quiet".format(prefix), *[] if prefix else ['-q'], + action="store_true", default=False, + help="Disable reporting. Verbose mode takes precedence." + ) def pytest_addoption(parser): diff --git a/src/pytest_benchmark/session.py b/src/pytest_benchmark/session.py index 0741b9c..0f79efe 100644 --- a/src/pytest_benchmark/session.py +++ b/src/pytest_benchmark/session.py @@ -31,7 +31,11 @@ class BenchmarkSession(object): def __init__(self, config): self.verbose = config.getoption("benchmark_verbose") - self.logger = Logger(self.verbose, config) + self.quiet = False if self.verbose else config.getoption("benchmark_quiet") + level = Logger.QUIET if self.quiet else Logger.NORMAL + if self.verbose: + level = Logger.VERBOSE + self.logger = Logger(level, config=config) self.config = config self.performance_regressions = [] self.benchmarks = [] @@ -222,9 +226,11 @@ def display(self, tr): scale_unit=partial(self.config.hook.pytest_benchmark_scale_unit, config=self.config), ) progress_reporter = report_progress if self.verbose else report_noprogress - results_table.display(tr, self.groups, progress_reporter=progress_reporter) + if not self.quiet: + results_table.display(tr, self.groups, progress_reporter=progress_reporter) self.check_regressions() - self.display_cprofile(tr) + if not self.quiet: + self.display_cprofile(tr) def check_regressions(self): if self.compare_fail and not self.compared_mapping: