Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Changes for code review
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyanegg committed Feb 6, 2020
1 parent fff2832 commit a28c823
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 43 deletions.
16 changes: 6 additions & 10 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
from pathlib import Path
from pkgutil import ModuleInfo

from tests.test_util import make_project
from ward import test, fixture, raises
from ward.collect import (
search_generally,
is_test_module,
get_module_path,
is_excluded_module,
remove_excluded_paths,
handled_within,
)
from ward import fixture, raises, test
from ward.collect import (get_module_path, handled_within, is_excluded_module,
is_test_module, remove_excluded_paths,
search_generally)
from ward.testing import Test, each

from tests.test_util import make_project


def named():
assert "fox" == "fox"
Expand Down
2 changes: 1 addition & 1 deletion ward/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def rewrite_assertion(test: Test) -> Test:
# We dedented the code so that it was a valid tree, now re-apply the indent
for child in ast.walk(new_tree):
if hasattr(child, "col_offset"):
child.col_offset = getattr(child, 'col_offset', 0) + col_offset
child.col_offset = getattr(child, "col_offset", 0) + col_offset

# Reconstruct the test function
new_mod_code_obj = compile(new_tree, code_obj.co_filename, "exec")
Expand Down
15 changes: 9 additions & 6 deletions ward/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import click
from colorama import init
from ward._ward_version import __version__
from ward.collect import (get_info_for_modules, get_tests_in_modules,
load_modules, search_generally)
from ward.collect import (
get_info_for_modules,
get_tests_in_modules,
load_modules,
search_generally,
)
from ward.config import set_defaults_from_config
from ward.rewrite import rewrite_assertions_in_tests
from ward.suite import Suite
Expand Down Expand Up @@ -71,8 +75,7 @@
help="Look for tests in PATH.",
)
@click.option(
"-d",
"--duration",
"--show-slowest",
type=int,
help="Record and display duration of n longest running tests",
default=0,
Expand All @@ -88,7 +91,7 @@ def run(
order: str,
capture_output: bool,
config: str,
duration: int,
show_slowest: int,
):
start_run = default_timer()
paths = [Path(p) for p in path]
Expand All @@ -110,7 +113,7 @@ def run(
test_results, time_to_collect=time_to_collect, fail_limit=fail_limit
)
time_taken = default_timer() - start_run
writer.output_test_result_summary(results, time_taken, duration)
writer.output_test_result_summary(results, time_taken, show_slowest)

exit_code = get_exit_code(results)

Expand Down
4 changes: 2 additions & 2 deletions ward/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def generate_test_runs(self, order="standard") -> Generator[TestResult, None, No
for test in self.tests:
generated_tests = test.get_parameterised_instances()
num_tests_per_module[test.path] -= 1
for idx, generated_test in enumerate(generated_tests):
yield generated_test.run(self.cache, idx)
for generated_test in generated_tests:
yield generated_test.run(self.cache)
self.cache.teardown_fixtures_for_scope(
Scope.Test, scope_key=generated_test.id
)
Expand Down
22 changes: 9 additions & 13 deletions ward/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,10 @@ def format_test_id(test_result: TestResult) -> (str, str):


def format_test_location(test_result: TestResult) -> str:
"""
"""

return f"{test_result.test.module_name}:{test_result.test.line_number}"


def format_test_case_number(test_result: TestResult) -> str:
"""
"""
# If we're executing a parameterised test
param_meta = test_result.test.param_meta
if param_meta.group_size > 1:
Expand Down Expand Up @@ -406,10 +401,10 @@ def result_checkbox(self, expect):
return result_marker

def output_test_result_summary(
self, test_results: List[TestResult], time_taken: float, duration: int
self, test_results: List[TestResult], time_taken: float, show_slowest: int
):
if duration:
self._output_longest_durations(test_results, duration)
if show_slowest:
self._output_slowest_tests(test_results, show_slowest)
outcome_counts = self._get_outcome_counts(test_results)
if test_results:
chart = self.generate_chart(
Expand Down Expand Up @@ -447,16 +442,17 @@ def output_test_result_summary(

print(output)

def _output_longest_durations(self, test_results: List[TestResult], num_tests: int):
def _output_slowest_tests(self, test_results: List[TestResult], num_tests: int):
test_results = sorted(
test_results, key=lambda r: r.test.timer.duration, reverse=True
)
print("Longest Running Tests\n")
self.print_divider()
heading = f"{colored('Longest Running Tests:', color='cyan', attrs=['bold'])}\n"
print(indent(heading, INDENT))
for result in test_results[:num_tests]:
test_id = format_test_id(result)
print(
f"{result.test.timer.duration:.2f} sec {test_id} {result.test.description} "
)
message = f"{result.test.timer.duration:.2f} sec {test_id} {result.test.description} "
print(indent(message, DOUBLE_INDENT))
print()

def output_captured_stderr(self, test_result: TestResult):
Expand Down
21 changes: 10 additions & 11 deletions ward/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class Test:
serr: StringIO = field(default_factory=StringIO)
ward_meta: WardMeta = field(default_factory=WardMeta)
timer: Optional["Timer"] = None
args: Optional[Dict[str, Any]] = None

def run(self, cache: FixtureCache, idx: int = 0) -> "TestResult":

Expand All @@ -117,8 +116,6 @@ def run(self, cache: FixtureCache, idx: int = 0) -> "TestResult":
stack.enter_context(redirect_stderr(self.serr))

if isinstance(self.marker, SkipMarker):
# TODO:onlyanegg: Do sout and serr need to be part of the class? If they weren't we could have
# the ExitStack close them
with closing(self.sout), closing(self.serr):
result = TestResult(self, TestOutcome.SKIP)
return result
Expand All @@ -127,9 +124,11 @@ def run(self, cache: FixtureCache, idx: int = 0) -> "TestResult":
# TODO:onlyanegg: I don't love this. We're setting up the
# fixture within the testing module, but cleaning it up in the
# suite module.
self.resolve_args(cache, iteration=idx)
self.format_description()
self.fn(**self.args)
resolved_args = self.resolve_args(
cache, iteration=self.param_meta.instance_index
)
self.format_description(resolved_args)
self.fn(**resolved_args)
except FixtureError as e:
outcome = TestOutcome.FAIL
error: Optional[Exception] = e
Expand Down Expand Up @@ -233,7 +232,7 @@ def get_parameterised_instances(self) -> List["Test"]:
def deps(self) -> MappingProxyType:
return inspect.signature(self.fn).parameters

def resolve_args(self, cache: FixtureCache, iteration: int = 0) -> None:
def resolve_args(self, cache: FixtureCache, iteration: int = 0) -> Dict[str, Any]:
"""
Resolve fixtures and return the resultant name -> Fixture dict.
If the argument is not a fixture, the raw argument will be used.
Expand All @@ -242,9 +241,9 @@ def resolve_args(self, cache: FixtureCache, iteration: int = 0) -> None:
"""
if self.capture_output:
with redirect_stdout(self.sout), redirect_stderr(self.serr):
self.args = self._resolve_args(cache, iteration)
return self._resolve_args(cache, iteration)
else:
self.args = self._resolve_args(cache, iteration)
return self._resolve_args(cache, iteration)

def _resolve_args(self, cache: FixtureCache, iteration: int) -> Dict[str, Any]:
if not self.has_deps:
Expand Down Expand Up @@ -385,7 +384,7 @@ def _unpack_resolved(self, fixture_dict: Dict[str, Any]) -> Dict[str, Any]:
resolved_vals[k] = arg
return resolved_vals

def format_description(self) -> str:
def format_description(self, args: Dict[str, Any]) -> str:
"""
Applies any necessary string formatting to the description,
given a dictionary `arg_map` of values that will be injected
Expand All @@ -395,7 +394,7 @@ def format_description(self) -> str:
Returns the newly updated description.
"""

format_dict = FormatDict(**self.args)
format_dict = FormatDict(**args)
if not self.description:
self.description = ""

Expand Down

0 comments on commit a28c823

Please sign in to comment.