Skip to content
Snippets Groups Projects
Verified Commit a4f8eb1a authored by David Beniamine's avatar David Beniamine
Browse files

Pytests and gitlab-ci

parent 8526d3e6
No related branches found
No related tags found
No related merge requests found
Pipeline #31 failed
*.egg-info
.eggs
__pycache__
.ropeproject
.pytest_cache
image: python:3.6
stages:
- build
- test
- deploy
installSetupTools:
stage: build
script: pip install setuptools
installPackage:
stage: build
script: pip install -e .
simpleTest:
stage: test
script: python setup.py test
...@@ -17,13 +17,11 @@ ...@@ -17,13 +17,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import multiprocessing as mp import multiprocessing as mp
import os
import traceback import traceback
from pyprind import ProgBar from pyprind import ProgBar
from time import sleep, time
from random import randint
from contextlib import ExitStack from contextlib import ExitStack
from sys import stderr from sys import stderr
from time import time
MAX_PROC = int(mp.cpu_count()) MAX_PROC = int(mp.cpu_count())
...@@ -68,7 +66,7 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu ...@@ -68,7 +66,7 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu
f.write(message + '\n') f.write(message + '\n')
f.flush() f.flush()
progressBar.update() if(bar): progressBar.update()
# Report status # Report status
errmsg= '{} errors / {} tasks written to file "{}" \n'.format(numErrors, len(tasks), errFileName) if(errFileName) else '' errmsg= '{} errors / {} tasks written to file "{}" \n'.format(numErrors, len(tasks), errFileName) if(errFileName) else ''
...@@ -83,29 +81,3 @@ def worker(args): ...@@ -83,29 +81,3 @@ def worker(args):
if(handleErrors): if(handleErrors):
return (False,traceback.format_exc()) return (False,traceback.format_exc())
raise raise
# Testing
def testFunc(arg):
if(randint(0,100)%2 ==0):
raise Exception("COUCOU"+ret)
return testFunc2(arg)
def testFunc2(arg):
ret=str(arg)+str(os.getpid())
sleep(randint(0,3))
return ret
def test():
print("Error file + debug")
parallelRunAndWrite(testFunc, range(10), "testplop", numProc=2, errFileName="errors", debug=True)
print("Error file")
parallelRunAndWrite(testFunc, range(10), "testplop", numProc=2, errFileName="errors")
print("No error file/ no rror")
parallelRunAndWrite(testFunc2, range(10), "testplop", numProc=2)
print("No error file")
parallelRunAndWrite(testFunc, range(10), "testplop", numProc=2)
if __name__ == "__main__":
test()
[aliases]
test=pytest
from setuptools import setup, find_packages
setup(
name="pyParallelRunAndWrite",
packages=find_packages(),
setup_requires=["pytest-runner"],
tests_require=["pytest"],
)
from pyParallelRunAndWrite import parallelRunAndWrite as prw
from time import sleep, time
import os
from random import randint
import re
rangeSize = 10
outFname = "output.txt"
errFname = "err.txt"
errRegex='prw arg \d+ pid \d+'
def raiseOrWork(arg):
if(randint(0,100)%2 ==0):
raise Exception("prw arg {} pid {} ".format(arg, os.getpid()))
return work(arg)
def work(arg):
ret=str(arg)+str(os.getpid())
sleep(randint(0,3))
return ret
def verifyOutput():
ok=True
with open(outFname, 'r') as f:
for line in f.read():
if(re.match('\d*',line) is None) :
ok=False
assert ok
def verifyErrFile():
found=False
with open(errFname, 'r') as f:
for line in f.read():
if(re.match('Exception: '+errRegex,line) is None) :
found=True
assert found
def verifyFiles():
verifyOutput()
verifyErrFile()
def testErrFileDebug():
prw.parallelRunAndWrite(raiseOrWork, range(rangeSize), outFname, errFileName=errFname, debug=True, bar=False)
verifyFiles()
def testErrFile():
prw.parallelRunAndWrite(raiseOrWork, range(rangeSize), outFname, errFileName=errFname)
verifyFiles()
def testNoErrFile():
prw.parallelRunAndWrite(work, range(rangeSize), outFname)
verifyOutput()
def testRaise():
from pytest import raises
with raises(Exception, match=r''+errRegex):
prw.parallelRunAndWrite(raiseOrWork, range(rangeSize), outFname)
verifyFiles()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment