Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket4499 ioc launch without tests #238

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ By default the framework searches for tests inside `.\tests\`. If you wish to sp

>`python run_tests.py -tp C:\my_ioc_tests` will run tests in the `my_ioc_tests` folder.

### Run test but Ask before starting the tests but after the IOC and emmulator are running

It is sometimes useful to attach a debugger to the test using this option means that the framework will ask to run tests before it starts the setup for the test.
This gives you time to attach a debugger. It also allows you an easy way to set up the system with emmulator and ioc attached to each other for unscripted testing.

> `python run_tests.py -a will ask if you want to run test before it runs them.

## Troubleshooting

If all tests are failing then it is likely that the PV prefix is incorrect.
Expand Down
40 changes: 36 additions & 4 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sys
import traceback
import unittest

import six
import xmlrunner
import glob

Expand Down Expand Up @@ -90,13 +92,14 @@ def make_device_launchers_from_module(test_module, mode):
return device_launchers


def load_and_run_tests(test_names, failfast):
def load_and_run_tests(test_names, failfast, ask_before_running_tests):
"""
Loads and runs the dotted unit tests to be run.

Args:
test_names: List of dotted unit tests to run.
failfast: Determines if tests abort after first failure.
ask_before_running_tests: ask whether to run the tests before running them

Returns:
boolean: True if all tests pass and false otherwise.
Expand All @@ -121,12 +124,34 @@ def load_and_run_tests(test_names, failfast):
clean_environment()
device_launchers = make_device_launchers_from_module(module.file, mode)
test_results.append(
run_tests(arguments.prefix, module.tests, device_collection_launcher(device_launchers), failfast))
run_tests(arguments.prefix, module.tests, device_collection_launcher(device_launchers), failfast, ask_before_running_tests))

return all(test_result is True for test_result in test_results)


def run_tests(prefix, tests_to_run, device_launchers, failfast_switch):
def prompt_user_to_run_tests(test_names):
"""
Utility function to ask the user whether to begin the tests

Args:
test_names: List of IOC test names to be run

Returns:
None

"""
print("Run tests? [Y/N]: {}".format(test_names))
while True:
answer = six.moves.input()
if answer == "" or answer.upper()[0] not in ["N", "Y"]:
print("Answer must be Y or N")
elif answer.upper()[0] == "N":
print("Not running tests, emulator and IOC only. Ctrl+c to quit.")
elif answer.upper()[0] == "Y":
return


def run_tests(prefix, tests_to_run, device_launchers, failfast_switch, ask_before_running_tests=False):
"""
Runs dotted unit tests.

Expand All @@ -135,6 +160,7 @@ def run_tests(prefix, tests_to_run, device_launchers, failfast_switch):
tests_to_run: List of dotted unit tests to be run.
device_launchers: Context manager that launches the necessary iocs and associated emulators.
failfast_switch: Determines if test suit aborts after first failure.
ask_before_running_tests: ask whether to run the tests before running them

Returns:
bool: True if all tests pass and false otherwise.
Expand All @@ -149,6 +175,8 @@ def run_tests(prefix, tests_to_run, device_launchers, failfast_switch):
test_names = ["{}.{}".format(arguments.tests_path, test) for test in tests_to_run]

with modified_environment(**settings), device_launchers:
if ask_before_running_tests:
prompt_user_to_run_tests(test_names)

runner = xmlrunner.XMLTestRunner(output='test-reports', stream=sys.stdout, failfast=failfast_switch)

Expand Down Expand Up @@ -191,6 +219,9 @@ def run_tests(prefix, tests_to_run, device_launchers, failfast_switch):
Default is in the tests folder of this repo""")
parser.add_argument('-f', '--failfast', action='store_true',
help="""Determines if the rest of tests are skipped after the first failure""")
parser.add_argument('-a', '--ask-before-running', action='store_true',
help="""Pauses after starting emulator and ioc. Allows you to use booted
emulator/IOC or attach debugger for tests""")

arguments = parser.parse_args()

Expand Down Expand Up @@ -220,9 +251,10 @@ def run_tests(prefix, tests_to_run, device_launchers, failfast_switch):

tests = arguments.tests if arguments.tests is not None else package_contents(arguments.tests_path)
failfast = arguments.failfast
ask_before_running_tests = arguments.ask_before_running

try:
success = load_and_run_tests(tests, failfast)
success = load_and_run_tests(tests, failfast, ask_before_running_tests)
except Exception as e:
print("---\n---\n---\nAn Error occurred loading the tests: ")
traceback.print_exc()
Expand Down
2 changes: 1 addition & 1 deletion utils/channel_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _MonitorAssertion:
"""
def __init__(self, channel_access, pv):
"""
Initilise.
Initialise.
Args:
channel_access: channel_access to set up monitor
pv: name of pv to monitor
Expand Down