From 29d63d7065ade4941347b3fffdc959b8c1e3c8d0 Mon Sep 17 00:00:00 2001 From: Benjamin Forehand Jr Date: Tue, 7 Nov 2023 06:03:58 -0600 Subject: [PATCH] Bug 1863606 - Add smoke test generation to experiment integration test suite --- .../generate_smoke_tests.py | 82 +++++++++++++++++++ .../experimentintegration/gradlewbuild.py | 9 +- .../experimentintegration/variables.yaml | 2 + 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/org/mozilla/fenix/experimentintegration/generate_smoke_tests.py diff --git a/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/generate_smoke_tests.py b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/generate_smoke_tests.py new file mode 100644 index 0000000000..3114304859 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/generate_smoke_tests.py @@ -0,0 +1,82 @@ +from pathlib import Path +import subprocess + +import yaml + + +def search_for_smoke_tests(tests_name): + """Searches for smoke tests within the requested test module.""" + path = Path("../ui") + files = sorted([x for x in path.iterdir() if x.is_file()]) + locations = [] + file_name = None + test_names = [] + + for name in files: + if tests_name in name.name: + file_name = name + break + + with open(file_name, "r") as file: + code = file.read().split(" ") + code = [item for item in code if item != ""] + + for count, item in enumerate(code): + if "class" in item or "@SmokeTest" in item: + locations.append(count) + + for location in locations: + if len(test_names) == 0: + class_name = code[location + 1] + test_names.append(class_name) + else: + test_names.append(f"{class_name}#{code[location+3].strip('()')}") + return test_names + + +def create_test_file(): + """Create the python file to hold the tests.""" + + path = Path("tests/") + filename = "test_smoke_scenarios.py" + final_path = path / filename + + if final_path.exists(): + print("File Exists, you need to delete it to create a new one.") + return + # file exists + subprocess.run([f"touch {final_path}"], encoding="utf8", shell=True) + assert final_path.exists() + with open(final_path, "w") as file: + file.write("import pytest\n\n") + + +def generate_smoke_tests(tests_names=None): + """Generate pytest code for the requested tests.""" + pytest_file = "tests/test_smoke_scenarios.py" + tests = [] + + for test in tests_names[1:]: + test_name = test.replace("#", "_").lower() + tests.append( + f""" +@pytest.mark.smoke_test +def test_smoke_{test_name}(setup_experiment, gradlewbuild, load_branches, check_ping_for_experiment): + setup_experiment(load_branches) + gradlewbuild.test("{test}", smoke=True) + assert check_ping_for_experiment +""" + ) + with open(pytest_file, "a") as file: + for item in tests: + file.writelines(f"{item}") + + +if __name__ == "__main__": + test_modules = None + create_test_file() + with open("variables.yaml", "r") as file: + test_modules = yaml.safe_load(file) + for item in test_modules.get("smoke_tests"): + tests = search_for_smoke_tests(item) + generate_smoke_tests(tests) diff --git a/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/gradlewbuild.py b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/gradlewbuild.py index 77fc18e58c..721d4ff3fa 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/gradlewbuild.py +++ b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/gradlewbuild.py @@ -16,12 +16,17 @@ class GradlewBuild(object): def __init__(self, log): self.log = log - def test(self, identifier): + def test(self, identifier, smoke=None): # self.adbrun.launch() # Change path accordingly to go to root folder to run gradlew os.chdir("../../../../../../../..") - cmd = f"adb shell am instrument -w -e class org.mozilla.fenix.experimentintegration.{identifier} org.mozilla.fenix.debug.test/androidx.test.runner.AndroidJUnitRunner" + test_type = "ui" if smoke else "experimentintegration" + cmd = f"adb shell am instrument -w -e class org.mozilla.fenix.{test_type}.{identifier} org.mozilla.fenix.debug.test/androidx.test.runner.AndroidJUnitRunner" + # if smoke: + # cmd = f"adb shell am instrument -w -e class org.mozilla.fenix.ui.{identifier} org.mozilla.fenix.debug.test/androidx.test.runner.AndroidJUnitRunner" + # else: + # cmd = f"adb shell am instrument -w -e class org.mozilla.fenix.experimentintegration.{identifier} org.mozilla.fenix.debug.test/androidx.test.runner.AndroidJUnitRunner" self.logger.info("Running cmd: {}".format(cmd)) diff --git a/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/variables.yaml b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/variables.yaml index 47404b3b99..286a36eae0 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/variables.yaml +++ b/app/src/androidTest/java/org/mozilla/fenix/experimentintegration/variables.yaml @@ -6,3 +6,5 @@ urls: stage_server: "https://stage.experimenter.nonprod.dataops.mozgcp.net" prod_server: "https://experimenter.services.mozilla.com" telemetry_server: "http://172.25.58.187:5000" +smoke_tests: + - "AddressAutofillTest"