feat: use pytest for testing (#6)

* feat: use pytest for testing

* fix: improve wording

Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>

* feat: add tests with two problems

Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
This commit is contained in:
gikf 2021-08-26 09:08:31 +02:00 committed by GitHub
parent 96469913d0
commit c4921d2d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 44 deletions

View File

@ -38,7 +38,7 @@ Output:
### Rules ### Rules
The function will return the correct conversion if the supplied problems are properly formatted, otherwise, it will **return** a **string** that describes an error that is meaningful to the user. The function will return the correct conversion if the supplied problems are properly formatted, otherwise, it will **return** a **string** that describes an error that is meaningful to the user.
* Situations that will return an error: * Situations that will return an error:
@ -60,9 +60,9 @@ The function will return the correct conversion if the supplied problems are pro
Write your code in `arithmetic_arranger.py`. For development, you can use `main.py` to test your `arithmetic_arranger()` function. Click the "run" button and `main.py` will run. Write your code in `arithmetic_arranger.py`. For development, you can use `main.py` to test your `arithmetic_arranger()` function. Click the "run" button and `main.py` will run.
### Testing ### Testing
The unit tests for this project are in `test_module.py`. We imported the tests from `test_module.py` to `main.py` for your convenience. The tests will run automatically whenever you hit the "run" button. The unit tests for this project are in `test_module.py`. We are running the tests from `test_module.py` in `main.py` for your convenience. The tests will run automatically whenever you hit the "run" button. Alternatively you may run the tests by inputting `pytest` in the console.
### Submitting ### Submitting

View File

@ -1,10 +1,11 @@
# This entrypoint file to be used in development. Start by reading README.md # This entrypoint file to be used in development. Start by reading README.md
from pytest import main
from arithmetic_arranger import arithmetic_arranger from arithmetic_arranger import arithmetic_arranger
from unittest import main
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])) print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
# Run unit tests automatically # Run unit tests automatically
main(module='test_module', exit=False) main()

15
pyproject.toml Normal file
View File

@ -0,0 +1,15 @@
[tool.poetry]
name = "fcc-arithmetic-formatter"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.8"
pytest = "^6.2.4"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

View File

@ -1,43 +1,77 @@
import unittest import pytest
from arithmetic_arranger import arithmetic_arranger from arithmetic_arranger import arithmetic_arranger
test_cases = [
# the test case pytest.param(
class UnitTests(unittest.TestCase): [['3801 - 2', '123 + 49']],
def test_arrangement(self): ' 3801 123\n'
actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]) '- 2 + 49\n'
expected = " 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----" '------ -----',
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]') 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]',
id='test_two_problems_arrangement1'),
actual = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]) pytest.param(
expected = " 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------" [['1 + 2', '1 - 9380']],
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]') ' 1 1\n'
'+ 2 - 9380\n'
def test_too_many_problems(self): '--- ------',
actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]) 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]',
expected = "Error: Too many problems." id='test_two_problems_arrangement2'),
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."') pytest.param(
[['3 + 855', '3801 - 2', '45 + 43', '123 + 49']],
def test_incorrect_operator(self): ' 3 3801 45 123\n'
actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]) '+ 855 - 2 + 43 + 49\n'
expected = "Error: Operator must be '+' or '-'." '----- ------ ---- -----',
self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''') 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]',
id='test_four_problems_arrangement'),
def test_too_many_digits(self): pytest.param(
actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]) [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']],
expected = "Error: Numbers cannot be more than four digits." ' 11 3801 1 123 1\n'
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."') '+ 4 - 2999 + 2 + 49 - 9380\n'
'---- ------ --- ----- ------',
def test_only_digits(self): 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]',
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]) id='test_five_problems_arrangement'),
expected = "Error: Numbers must only contain digits." pytest.param(
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."') [['44 + 815', '909 - 2', '45 + 43', '123 + 49',
'888 + 40', '653 + 87']],
def test_solutions(self): 'Error: Too many problems.',
actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True) 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."',
expected = " 32 1 45 123\n- 698 - 3801 + 43 + 49\n----- ------ ---- -----\n -666 -3800 88 172" id='test_too_many_problems'),
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.') pytest.param(
[['3 / 855', '3801 - 2', '45 + 43', '123 + 49']],
"Error: Operator must be '+' or '-'.",
'''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''',
id='test_incorrect_operator'),
pytest.param(
[['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']],
'Error: Numbers cannot be more than four digits.',
'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."',
id='test_too_many_digits'),
pytest.param(
[['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']],
'Error: Numbers must only contain digits.',
'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."',
id='test_only_digits'),
pytest.param(
[['3 + 855', '988 + 40'], True],
' 3 988\n'
'+ 855 + 40\n'
'----- -----\n'
' 858 1028',
'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.',
id='test_two_problems_with_solutions'),
pytest.param(
[['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True],
' 32 1 45 123 988\n'
'- 698 - 3801 + 43 + 49 + 40\n'
'----- ------ ---- ----- -----\n'
' -666 -3800 88 172 1028',
'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.',
id='test_five_problems_with_solutions'),
]
if __name__ == "__main__": @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
unittest.main() def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
assert actual == expected_output, fail_message