Here assume we have a python script folder, and we want to put test scripts in tests just in the folder of scripts.
Step 0, Create testing target
Create pipeline.py and __init__.py in your folder. Below is the content of pipeline.py.
import logging
logger = logging.getLogger(__name__)
def even_filter(nums):
for num in nums:
if num % 2 == 0:
logger.debug("%s", num)
yield num
def multiply_by_three(nums):
for num in nums:
logger.debug("%s * 3 = %s", num, num * 3)
yield num * 3
def convert_to_string(nums):
for num in nums:
logger.debug("%s", num)
yield 'The Number: %s' % num
def pipeline_func(data, fns):
return reduce(lambda a, x: x(a), fns, data)
def main():
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#pipeline = convert_to_string(multiply_by_three(even_filter(nums)))
pipeline = pipeline_func(nums, [even_filter, multiply_by_three, convert_to_string])
for num in pipeline:
logger.info(num)
if __name__ == '__main__':
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)s.%(funcName)s: %(message)s")
main()
Step 1, Create runTests script
#!/usr/bin/python
import pytest
exit(pytest.main("-q ./tests"))
Step 2, Create test cases
$ mkdir tests
$ cd tests
$ vim test_sample.py
Use below contents in tests/test_sample.py script.
#!/usr/bin/python
import pytest
nums = []
@pytest.fixture(scope="module")
def data_setup(request):
print ("data_setup")
global nums
nums = range(10)
def data_teardown():
print ("data_teardown")
request.addfinalizer(data_teardown)
def test_pipeline_func(data_setup):
import pipeline
output = pipeline.pipeline_func(
nums, [
pipeline.even_filter, pipeline.multiply_by_three])
expect = [0, 6, 12, 18, 24]
assert(output, expect)
Step 3, Run runTests.py
C:\Users\Xiaoqiang\Desktop\pyWorks\funcprog>python runTests.py
.
1 passed in 0.04 seconds
Over