Skip to content

Commit

Permalink
Merge pull request #46 from AdityaSavara/init-improvement
Browse files Browse the repository at this point in the history
Init improvement
  • Loading branch information
AdityaSavara authored Jul 30, 2022
2 parents 0f8a52e + 28a6f1e commit 28887ea
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ 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
#- source venv/bin/activate
#- 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.
Empty file added ExampleFiles/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
25 changes: 16 additions & 9 deletions UnitTesterSG/pytestDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
EMAIL = '[email protected]'
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?
Expand Down
Empty file added test12/__init__.py
Empty file.
Empty file added test13/__init__.py
Empty file.

0 comments on commit 28887ea

Please sign in to comment.