From f3686fd728408c7aa96985b548882a94e9373e1f Mon Sep 17 00:00:00 2001 From: AdityaSavara <39929571+AdityaSavara@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:41:27 -0400 Subject: [PATCH 1/5] __init__ improvement --- ExampleFiles/__init__.py | 0 UnitTesterSG/pytestDriver.py | 25 ++++++++++++++++--------- test12/__init__.py | 0 test13/__init__.py | 0 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 ExampleFiles/__init__.py create mode 100644 test12/__init__.py create mode 100644 test13/__init__.py diff --git a/ExampleFiles/__init__.py b/ExampleFiles/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/UnitTesterSG/pytestDriver.py b/UnitTesterSG/pytestDriver.py index f689f1f..db3cf97 100644 --- a/UnitTesterSG/pytestDriver.py +++ b/UnitTesterSG/pytestDriver.py @@ -16,20 +16,27 @@ def runAllTests(failWithError=False): for directory in directoryList: print("Changing directory to "+directory) os.chdir(directory) + try: os.system("del __pycache__ /Q") #for windows os.system("rm -r __pycache__ /Q") #for linux except: pass - #Try to run the test. In the past, we we used an executable version of pytest, but now we are using "pytest.main()" so we can get the exit code, that way we can fail with error if a unit test doesn't pass. - import pytest - exitCode = pytest.main() - # os.system(sys.executable +" -m pytest") #this is like typing "python -m pytest" but uses whichever version of python should be used, important for virtual environments and different systems https://stackoverflow.com/questions/8338854/how-to-run-py-test-against-different-versions-of-python - if exitCode >= 1 and exitCode <5: - allTestsPassed = False - + + #We will check if __init__ exists, because for various situations, pytest.main( will have failures if there is no __init__. + initExists = os.path.exists('__init__.py') + if initExists: + #Try to run the test. In the past, we we used an executable version of pytest, but now we are using "pytest.main()" so we can get the exit code, that way we can fail with error if a unit test doesn't pass. + import pytest + exitCode = pytest.main() + # https://stackoverflow.com/questions/8338854/how-to-run-py-test-against-different-versions-of-python + if exitCode >= 1 and exitCode <5: + allTestsPassed = False + else: #if __init__ does not exist, we can usually still run the unit tests by running the pytest executable. + os.system(sys.executable +" -m pytest") #this is like typing "python -m pytest" but uses whichever version of python should be used, important for virtual environments and different systems os.chdir("..") - if failWithError == True: #if the failWithError flag is on due to the optional argument, we will check if all tests passed. If not, we'll raise an error. - if allTestsPassed == False: + if allTestsPassed == False: + print("At least one unit test failed.") + if failWithError == True: #if the failWithError flag is on due to the optional argument, we will check if all tests passed. If not, we'll raise an error. raise RuntimeError("At least one unit test failed.") #This is to intentionally create an error so that the Travis CI will fail. \ No newline at end of file diff --git a/test12/__init__.py b/test12/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test13/__init__.py b/test13/__init__.py new file mode 100644 index 0000000..e69de29 From 7edeeff6ecf890f50319656bcbffba0f16213084 Mon Sep 17 00:00:00 2001 From: AdityaSavara <39929571+AdityaSavara@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:46:16 -0400 Subject: [PATCH 2/5] Check that failures occur as expected with __init__ improvement --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a85894a..3465e08 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,6 @@ install: #remove the test_13.py before running the runPytestDriver.py since that python file is designed to give an error as downloaded. script: - cd test13 - - rm test_13.py + #- rm test_13.py - cd .. - python3 runPytestDriver.py failWithError #The "failWithError" optional argument will cause an error exitCode if any unit tests fail. \ No newline at end of file From 177c524e92f8a733943e690343c463167e325cc0 Mon Sep 17 00:00:00 2001 From: AdityaSavara <39929571+AdityaSavara@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:48:24 -0400 Subject: [PATCH 3/5] Changing .travis.yml back to normal situation to let Travis CI tests pass again Changing .travis.yml back to normal situation to let Travis CI tests pass again. This reverts commit 7edeeff6ecf890f50319656bcbffba0f16213084. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3465e08..a85894a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,6 @@ install: #remove the test_13.py before running the runPytestDriver.py since that python file is designed to give an error as downloaded. script: - cd test13 - #- rm test_13.py + - rm test_13.py - cd .. - python3 runPytestDriver.py failWithError #The "failWithError" optional argument will cause an error exitCode if any unit tests fail. \ No newline at end of file From 3bcd19a537f9e6b29f5381d2d5b54d85e27ad55c Mon Sep 17 00:00:00 2001 From: AdityaSavara <39929571+AdityaSavara@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:50:09 -0400 Subject: [PATCH 4/5] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 696b7ef..fc171e5 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ EMAIL = 'AditySavara2008@u.northwestern.edu' AUTHOR = 'Aditya Savara' REQUIRES_PYTHON = '>=3.0.0' -VERSION = '6.0.0' +VERSION = '6.1.0' LICENSE = 'BSD-3-Clause' # What packages are required for this module to be executed? From 28a6f1ea637f4beeb4aac6ffd442ec0db8b8d73a Mon Sep 17 00:00:00 2001 From: AdityaSavara <39929571+AdityaSavara@users.noreply.github.com> Date: Sat, 30 Jul 2022 00:56:27 -0400 Subject: [PATCH 5/5] Adding more instructions and slight changes regarding .travis.yml --- .travis.yml | 12 ++++++++---- README.txt | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index a85894a..9635dac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,13 @@ language: python python: - "3.8" # commands to install dependencies + +before_install: #this is only for UnitTesterSG. + #For UnitTesterSG, we remove the test_13.py before running the runPytestDriver.py since that python file is designed to give an error as downloaded. + - cd test13 + - rm test_13.py + - cd .. + install: #No installation is required for UnitTesterSG with what is already on the Travis CI, so commenting out the below to let the build test run faster. #- virtualenv -p /opt/pyenv/versions/3.8/bin/python3.8 venv @@ -9,9 +16,6 @@ install: #- pip install UnitTesterSG[COMPLETE] #this is mostly to get the dependencies. #- python setup.py install #now overwrite old installations with the current version. # command to run tests -#remove the test_13.py before running the runPytestDriver.py since that python file is designed to give an error as downloaded. + script: - - cd test13 - - rm test_13.py - - cd .. - python3 runPytestDriver.py failWithError #The "failWithError" optional argument will cause an error exitCode if any unit tests fail. \ No newline at end of file diff --git a/README.txt b/README.txt index f6344b0..4fec0f6 100644 --- a/README.txt +++ b/README.txt @@ -18,6 +18,10 @@ There are three ways to run unit tests: Note: For any individual test, set allowOverwrite to False when calling doTest if you want to skip UnitTesterSG from stopping to notify user when results match but result strings don't. +COMPATIBILITY WITH PYTEST AND TRAVIS CI: +Add an __init__.py file into each test directory (they can be empty) files +Then add .travis.yml to your root directory, and the script command "python3 runPytestDriver.py failWithError" within it (as in this repository). + PURPOSE OF MODULE: UnitTesterSG is a unit testing framework that is designed for nested and/or scientific/engineering data structures. It is designed primarily for testing the outputs if a single function or simulation run by storing the expected results file such that comparisons to the stored output can be made with unit tests after the function or software has been edited. However, the compare nested objects module can also be imported directly and is quite useful even outside of unit testing.