Title: Test02- Requires Respondus LockDown Browser
Course: Intro to Scripting Language: Python (APT TEMPLATE) > Section: Test02

Test for Testing competency.

Q: The code in the top panel of Image1 produces the output shown in the bottom panel of Image 1.

Image 1
def listModifier(listA,listB):

  """Illustrates pass by value or reference"""

  listA.append(3.14159)

  print("New listA = " + str(listA))



  listB = ["A","new","list"]

  print("New listB = " + str(listB))



  return



aList = ["ab","cd","ef"]

bList = ["The","old","list"]



print("aList = " + str(aList))

print("bList = " + str(bList))



listModifier(aList,bList)

print("aList = " + str(aList))

print("bList = " + str(bList))

===============================================================================

aList = ['ab', 'cd', 'ef']

bList = ['The', 'old', 'list']

New listA = ['ab', 'cd', 'ef', 3.14159]

New listB = ['A', 'new', 'list']

aList = ['ab', 'cd', 'ef', 3.14159]

bList = ['The', 'new', 'list']
- True
- False
A: False
If wrong: The actual output is shown in Image 2. Image 2 aList = ['ab', 'cd', 'ef'] bList = ['The', 'old', 'list'] New listA = ['ab', 'cd', 'ef', 3.14159] New listB = ['A', 'new', 'list'] aList = ['ab', 'cd', 'ef', 3.14159] bList = ['The', 'old', 'list']

Q: The test file shown in the top panel of Image 3, when executed using the command shown in the middle panel of Image 3 produced a test report with the summary shown by the text in the bottom panel of Image 3.

Image 3
Beginning of first test

>>> 2 + 3

5



End of first test

Beginning of second test

>>> 2 + 3

6



End of second test

===============================================================================

python -m doctest Py1359_1720r_01.txt

===============================================================================

1 items had failures:

   2 of   2 in Py1359_1720r_01.txt

***Test Failed*** 2 failures.
- True
- False
A: False
If wrong: The actual output is shown in Image 4. The test file contained two tests, one of which passed and the other of which failed. Verbose output was not specified. Therefore, the report didn't include information on the test that passed. The test file contained explanatory text, which is okay under certain formatting conditions. Image 4 ********************************************************************** File "Py1359_1720r_01.txt", line 7, in Py1359_1720r_01.txt Failed example: 2 + 3 Expected: 6 Got: 5 ********************************************************************** 1 items had failures: 1 of 2 in Py1359_1720r_01.txt ***Test Failed*** 1 failures.

Q: The test file shown in the top panel of Image 5, when executed using the command shown in the middle panel of Image 5, produces the output shown in the bottom panel of Image 5.

Image 5
Beginning of test

>>> 2 + 3

5

End of test

===============================================================================

python -m doctest Py1359_1720r_02.txt -v

===============================================================================

Trying:

    2 + 3

Expecting:

    5

ok

1 items passed all tests:

   1 tests in Py1359_1720r_02.txt

1 tests in 1 items.

1 passed and 0 failed.

Test passed.
- True
- False
A: False
If wrong: The actual output is shown in Image 6. This is a formatting issue. If you include explanatory text in the text file, you must leave a blank line between the last line of specified output and the following line of explanatory text. Otherwise, the explanatory text will be included as part of required output as shown by the expected output value in Image 6. Image 6 Trying: 2 + 3 Expecting: 5 End of test ********************************************************************** File "Py1359_1720r_02.txt", line 2, in Py1359_1720r_02.txt Failed example: 2 + 3 Expected: 5 End of test Got: 5 ********************************************************************** 1 items had failures: 1 of 1 in Py1359_1720r_02.txt 1 tests in 1 items. 0 passed and 1 failed. ***Test Failed*** 1 failures.

Q: Given the Python module shown in the top panel of Image 7, the test file shown in the second panel of Image 7, when executed using the command shown in the third panel of Image 7, produces a report with the summary shown by the text in the bottom panel of Image 7.

Image 7
#module Py1359_1720r_03.py

def sum(parA,parB):

    return parA + parB



def product(parC,parD):

    return parC * parD

===============================================================================

>>> from Py1359_1720r_03 import sum

>>> sum(2,3)

5

>>> from Py1359_1720r_03 import product

>>> product(2,3)

6

===============================================================================

python -m doctest -v Py1359_1720r_03.txt

===============================================================================

4 tests in 1 items.

4 passed and 0 failed.

Test passed.
- True
- False
A: True
If right: Note that in addition to the function calls, the imports are also counted as tests and they passed.

Q: Given the Python module shown in the top panel of Image 8, execution of the module as a script using the command shown in the middle panel of Image 8 produces a report with the summary shown by the text in the bottom panel of Image 8.

Image 8
"""

Begin first test

>>> sum(2,3)

5



Begin second test

>>> product(2,3)

6

"""

def sum(parA,parB):

    return parA + parB



def product(parC,parD):

    return parC * parD



if __name__ == "__main__":

    import doctest

    doctest.testmod()

===============================================================================

python Py1359_1720r_04.py -v

===============================================================================

2 passed and 0 failed.

Test passed.
- True
- False
A: True

Q: Given the Python module shown in the top panel of Image 9, execution of the module as a script using the command shown in the middle panel of Image 9 produces a report with the summary shown by the text in the bottom panel of Image 9.

Image 9
"""

>>> divide(2,3)

0

>>> divide(2,0)

Traceback (most recent call last):

  ...

ZeroDivisionError: integer division or modulo by zero

"""

def divide(parA,parB):

    return parA//parB



if __name__ == "__main__":

    import doctest

    doctest.testmod()

===============================================================================

python Py1359_1720r_05.py -v

===============================================================================

2 passed and 0 failed.

Test passed.
- True
- False
A: True

Q: Given the Python module shown in the top panel of Image 10, execution of the module as a script using the command shown in the middle panel of Image 10 produces a report with the summary shown by the text in the bottom panel of Image 10.

Image 10
"""

>>> function(11)

Traceback (most recent call last):

  ...

ValueError: par must be <= 10

"""

def function(par):

    if par > 10:

        raise ValueError("par must be <= 10")

    else:

        return par



if __name__ == "__main__":

    import doctest

    doctest.testmod()

===============================================================================

python Py1359_1720r_06.py -v

===============================================================================

1 passed and 0 failed.

Test passed.
- True
- False
A: True

Q: Given the Python modules shown in the top two panels of Image 11 and the test file shown in the third panel of Image 11, execution of the command shown in the fourth panel of Image 11, produces the output shown in the bottom panel of Image 11. Note: If the answer is False, it will be due to a syntax issue, which may or may not exist.

Image 11
#module Py1359_1720r_07_runner.py

import doctest

doctest.testfile(Py1359_1720r_07.txt)

===============================================================================

#module Py1359_1720r_07.py

def sum(parA,parB):

    return parA + parB



def product(parC,parD):

    return parC * parD

===============================================================================

>>> from Py1359_1720r_07 import sum

>>> sum(2,3)

5

>>> from Py1359_1720r_07 import product

>>> product(2,3)

5

===============================================================================

python Py1359_1720r_07_runner.py

===============================================================================

File "Py1359_1720r_07.txt", line 5, in Py1359_1720r_07.txt

Failed example:

    product(2,3)

Expected:

    5

Got:

    6

**********************************************************************

1 items had failures:

   1 of   4 in Py1359_1720r_07.txt

***Test Failed*** 1 failures.
- True
- False
A: False
If wrong: The output produced by the procedure described in this question includes the following error message: NameError: name 'Py1359_1720r_07' is not defined This indicates a syntax error in the specification of the name of the test file. While the concept is correct and it is possible to use Python code in a module similar to that shown in the top panel of Image 11 to execute a test file such as that shown in Image 11, the name of the test file must be enclosed in quotation marks to be recognized as a test file. Enclosing the name of the test file in Image 11 in quotes would cause the procedure to produce the output shown in Image 11.

Q: Given the Python module shown in the top panel of Image 12 and the test file shown in the second panel of Image 12, execution of the command shown in the third panel of Image 12, produces a report with the summary shown by the text in the bottom panel of Image 12.

Image 12
#module Py1359_1720r_08.py

def sum(parA,parB):

    return parA + parB



def product(parC,parD):

    return parC * parD



if __name__ == "__main__":

    import doctest

    doctest.testfile("Py1359_1720r_08.txt")

===============================================================================

>>> from Py1359_1720r_08 import sum

>>> sum(2,3)

5

>>> from Py1359_1720r_08 import product

>>> product(2,3)

6

===============================================================================

python Py1359_1720r_08.py -v

===============================================================================

4 passed and 0 failed.

Test passed.
- True
- False
A: True
If right: The call to testfile at the end of the top panel in Image 12 illustrates another way to cause a test file to be executed.

Q: Given the Python version 3 module shown in the top panel of Image 13, execution of the module as a script using the command shown in the middle panel of Image 13, produces the output shown in the bottom panel of Image 13.

Image 13
"""

>>> divide(1,3)

0.333333

"""

def divide(parA,parB):

    return parA/parB



if __name__ == "__main__":

    import doctest

    doctest.testmod()

===============================================================================

python Py1359_1720r_09.py -v

===============================================================================

Trying:

    divide(1,3)

Expecting:

    0.3333333333333333

ok

1 items had no tests:

    __main__.divide

1 items passed all tests:

   1 tests in __main__

1 tests in 2 items.

1 passed and 0 failed.

Test passed.
- True
- False
A: False
If wrong: The actual output is shown in Image 14. This question was included to point out that testing floating point results can be problematic. Many floating point values are simply estimates. Although this example was purposely caused to fail, a given floating point computation may not produce exactly the same results on different platforms. Therefore a test that specifies a given floating point result may pass on one platform and fail on a different platform. The Python Standard Library -- 26.2 doctest -- Test interactive Python examples provides some suggestions for dealing with this issue. Image 14 Trying: divide(1,3) Expecting: 0.333333 ********************************************************************** File "Py1359_1720r_09.py", line 4, in __main__ Failed example: divide(1,3) Expected: 0.333333 Got: 0.3333333333333333 1 items had no tests: __main__.divide ********************************************************************** 1 items had failures: 1 of 1 in __main__ 1 tests in 2 items. 0 passed and 1 failed. ***Test Failed*** 1 failures.