Created
June 30, 2022 19:49
-
-
Save kingbuzzman/a5101a77152c55e8bcbce84bc354db17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import coverage | |
import json | |
from contextlib import redirect_stdout | |
def functions_execution_is_identical(func1, func2, file_to_compare): | |
""" | |
Compare the coverage of two functions to see if they're equivalent/same. | |
Say you have `test_thing()` and someone else wrote a `test_thing_better()` and you want to see if they | |
are indeed the same. | |
In the same file that test_thing and test_thing_better are, add a new function `test_are_things_the_same`: | |
def test_thing(...) | |
... | |
utils.thing(...) | |
... | |
def test_thing_better(...) | |
... | |
utils.thing(...) | |
... | |
def test_are_things_the_same(): | |
from functools import partial | |
functions_execution_is_identical(partial(test_thing, arg1, kwarg1=123), | |
partial(test_thing_better, arg1, kwarg1=123) | |
'path/to/utils.py') | |
# if `test_are_things_the_same` does not crash, that means the two tests are equivalent. | |
""" | |
# Run coverage for the first function | |
func1_cov = coverage.Coverage() | |
func1_cov.start() | |
func1() | |
func1_cov.stop() | |
# Run coverage for the second function | |
func2_cov = coverage.Coverage() | |
func2_cov.start() | |
func2() | |
func2_cov.stop() | |
# Generate the json reports | |
with io.StringIO() as buf, redirect_stdout(buf): | |
func1_cov.json_report(pretty_print=True, outfile='-') | |
func1_json = json.loads(buf.getvalue()) | |
with io.StringIO() as buf, redirect_stdout(buf): | |
func2_cov.json_report(pretty_print=True, outfile='-') | |
func2_json = json.loads(buf.getvalue()) | |
# Compare that the functions 1 and 2 executed the exact lines of code in the fine in question | |
assert func1_json['files'][file_to_compare] == func2_json['files'][file_to_compare] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment