Feb-13-2024, 07:14 AM
(Feb-12-2024, 05:43 PM)snippsat Wrote: As mention you most name function wih unique name eg add1 add2.
# file: add_functions.py # solution1 def add1(matrix1, matrix2): """Add corresponding numbers in given 2-D matrices.""" combined = [] for i in range(len(matrix1)): row = [] for j in range(len(matrix1[i])): row.append(matrix1[i][j] + matrix2[i][j]) combined.append(row) return combined #solution2: def add2(matrix1, matrix2): """Add corresponding numbers in given 2-D matrices.""" combined = [] for rows in zip(matrix1, matrix2): row = [] for items in zip(rows[0], rows[1]): row.append(items[0] + items[1]) combined.append(row) return combinedNow to give a example using pytest
A better tool than unittest as deanhystad mention.
# file: test_add.py import pytest from add_functions import add1, add2 test_cases = [ (add1, [[[5]], [[-2]]], [[3]]), (add2, [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[6, 8], [10, 12]]), ] @pytest.mark.parametrize('add_func, input_matrices, expected_output', test_cases) def test_add(add_func, input_matrices, expected_output): actual_output = add_func(input_matrices[0], input_matrices[1]) print(f"\nRunning {add_func.__name__} with input {input_matrices}:") print(f"Output: {actual_output}") assert actual_output == expected_output, ( f"Expected {expected_output}, got {actual_output}." )So if add-s
it will also show output,and not just passed or failed.
Output:G:\div_code\matx λ pytest -v -s =========test session starts ============ platform win32 -- Python 3.11.3, pytest-7.4.2, pluggy-1.3.0 -- C:\python311\python.exe cachedir: .pytest_cache rootdir: G:\div_code\matx collected 2 items test_add.py::test_add[add1-input_matrices0-expected_output0] Running add1 with input [[[5]], [[-2]]]: Output: [[3]] PASSED test_add.py::test_add[add2-input_matrices1-expected_output1] Running add2 with input [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]: Output: [[6, 8], [10, 12]] PASSED
hi
thanks for reply
i copied and runned the two your codes in idle and Thonny. no error message has been displayed but also nothing in output has been displayed.
then i went in cmd( also i did in Thonny>>tools>>open system shell). in cmd, i wrote the below commands and got the mentioned output:
( i renamed your pytest.py to pytest_usage_for_test.py)
>> pytest_usage_for_test.py
output was: nothing
>>python pytest_usage_for_test.py
output was : nothing
>>pytest_usage_for_test.py -v -s
output was : nothing
>>pytest_usage_for_test -v -s
output was : nothing
>>python pytest_usage_for_test.py -v -s
output was : nothing
>>python pytest_usage_for_test -v -s
output was : python: can't open file 'D:\\Desktop\\pythonmorsels_add\\pytest_usage_for_test': [Errno 2] No such file or directory
why can't see your output?
thanks