diff --git a/__init__.py b/conftest.py similarity index 100% rename from __init__.py rename to conftest.py diff --git a/tests/__init__.py b/pytests/__init__.py similarity index 100% rename from tests/__init__.py rename to pytests/__init__.py diff --git a/tests/resources/__init__.py b/pytests/resources/__init__.py similarity index 100% rename from tests/resources/__init__.py rename to pytests/resources/__init__.py diff --git a/pytests/resources/test_stack.py b/pytests/resources/test_stack.py new file mode 100644 index 0000000000000000000000000000000000000000..2328e93d404be8142d5d5c339062ed2a0365d681 --- /dev/null +++ b/pytests/resources/test_stack.py @@ -0,0 +1,89 @@ +""" +Date: 09-16-19 +Class: CS5310 +Assignment: InClass03 - Custom Implementation of a stack. +Author: Brandon Rodriguez + + +PyTests for custom Stack implementation. +""" + + +# System Imports. + +# User Class Imports. +from resources.stack import Stack + + + +class TestStack(): + test_stack = Stack() + + def test_stack_creation(self): + assert isinstance(self.test_stack, Stack) + + def test_push(self): + # Test initial state. + assert len(self.test_stack._data_) == 0 + + # Test adding first element. + self.test_stack.push(4) + assert len(self.test_stack._data_) == 1 + assert self.test_stack._data_[0] == 4 + + # Test adding second element. + self.test_stack.push('testing') + assert len(self.test_stack._data_) == 2 + assert self.test_stack._data_[0] == 4 + assert self.test_stack._data_[1] == 'testing' + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. + + def test_pop(self): + # Test with two elements. + assert self.test_stack.pop() == 'testing' + + # Test with one element. + assert self.test_stack.pop() == 4 + + # Test with zero elements. + assert self.test_stack.pop() == None + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. + + def test_is_empty(self): + # Test with more than zero elements. + self.test_stack.push('') + assert self.test_stack.is_empty() == False + + # Test with zero elements. + self.test_stack.pop() + assert self.test_stack.is_empty() == True + + def test_top(self): + # Test with zero elements. + assert self.test_stack.top() == None + + # Test with one element. + self.test_stack.push(5) + assert self.test_stack.top() == 5 + + # Test with two elements. + self.test_stack.push('testing') + assert self.test_stack.top() == 'testing' + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. + + def test_size(self): + # Test with two elements. + assert self.test_stack.size() == 2 + self.test_stack.pop() + + # Test with one element. + assert self.test_stack.size() == 1 + self.test_stack.pop() + + # Test with zero elements. + assert self.test_stack.size() == 0 + + # Works with 0, 1, and 2 elements. Assuming it works for all further n+1 elements. diff --git a/readme.md b/readme.md index e9b985526f8833ad0d0ee93031322665690c30f3..efcee6253dc9c06f3efd5f88bdfa9ec31e3b79cd 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ In class example of a custom stack implementation. ## Python Environment This project is tested using Python3.7. -Written using PyCharm. +Written using PyCharm. Tested with Pycharm 2017.2.3 and 2019.2 versions. ### Creating a New Env Most installations of Python3 will allow you to simply run `python3.7 -m venv ./.venv` to create a new Python3.7 @@ -27,17 +27,64 @@ Run the program via `python ./main.py` while at the project root. None so far. -## Running Tests -The following are two methods to "run all tests in a given directory". +## Testing with UnitTest +Unittests come built into python. No need for third party packages. + +### Running a Single UnitTest +The following will run tests from a single file. + +NOTE: For the file path of these two commands, replace any `/` characters with `.` instead.<br> +So for example, `tests/my_tests/test_file.py` would turn into `tests.my_tests.test_file.py`. + +* Terminal: From a terminal, run `python -m unittest <path_to_file>` + +* Pycharm: Create a new `unittest` configuration with the properties: + * Target: `custom` + * Additional Arguments: `python -m unittest <path_to_file>` + * Python Interpreter: `<your_preferred_interpreter_environment>` + * Working Directory: `<project_root>` + + +### Running Multiple UnitTests +The following will "run all tests in a given directory".<br> NOTE: For these to work as intended, there needs to be an "__init__.py" file in each project subdirectory that the tests access (even despite using Python3). -* Terminal: From a terminal at the project root, run `python -m unittest discover ./tests/ -p "*.py" -t ./` -* Pycharm: Create a new unittest configuration with: +* Terminal: From a terminal at the project root, run `python -m unittest discover <path_to_folder> -p "*.py" -t ./` + +* Pycharm: Create a new `unittest` configuration with the properties: + * Target: `custom` + * Additional Arguments: `discover -s <path_to_folder> -p "*.py" -t ./` + * Python Interpreter: `<your_preferred_interpreter_environment>` + * Working Directory: `<project_root>` + +## PyTest +PyTest is an alternative to UnitTests, using a third party library. +To install, run `pip install pytest`. + +### Running a Single PyTest +The following will run all tests from a single file. + +* Terminal: From a terminal, run `pytest -q <path_to_file>`. + +* Pycharm: Create a new `py.test` or `pytest` configuration (depending on your Pycharm version) with the properties: + * Target: `custom` + * Additional Arguments: `-q <path_to_file>` + * Python Interpreter: `<your_preferred_interpreter_environment>` + * Working Directory: `<project_root>` + +### Running Multiple PyTests +The following will "run all tests in root folder". + +NOTE: You cannot have an `__init__.py` at project root.<br> +If you get a ModuleError/ImportError, you also may have to create a blank `conftest.py` at project root. + +* Terminal: From a terminal at the project root, run `pytest`. + +* Pycharm: Create a new `py.test` or `pytest` configuration (depending on your Pycharm version) with the properties: * Target: `custom` - * Additional Arguments: `discover -s tests -p "*.py" -t ./` - * Python Interpreter: `<your preferred interpreter environment>` - * Working Directory: `<project root>` + * Python Interpreter: `<your_preferred_interpreter_environment>` + * Working Directory: `<project_root>` ## References @@ -56,9 +103,20 @@ Logging files can be found in `resources/logs/`. Getting module imports to work correctly (-t arg): <https://docs.python.org/3/library/unittest.html#test-discovery> -Running Tests from Console: <https://stackoverflow.com/a/15630454>. +Running UnitTests from Console: <https://stackoverflow.com/a/15630454>. -Running Tests from Pycharm: Trial and error with modifying above references. +Running UnitTests from Pycharm: Trial and error with modifying above references. + +Running a single UnitTest: <https://stackoverflow.com/a/43029618> ### UnitTesting Subtests Using subtests: <https://www.caktusgroup.com/blog/2017/05/29/subtests-are-best/> + +### Using Pytest +General Documentation: <https://docs.pytest.org/en/latest/contents.html#toc> + +Running PyTests from Pycharm: <https://www.jetbrains.com/help/pycharm/run-debug-configuration-py-test.html> + +Running a single PyTest: <https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test> + +Issues with module imports: <https://stackoverflow.com/a/50610630> diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..12a04824dd526bec6a16691f5a5e6761131103ba --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ + +# Install via 'pip install -r requirements.txt' from project root. +pytest diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index be3289626d3570dcd6dba7a71c012a6a0d2d1108..0000000000000000000000000000000000000000 --- a/tests/test.py +++ /dev/null @@ -1,22 +0,0 @@ -""" -Date: 09-16-19 -Class: CS5310 -Assignment: InClass03 - Custom Implementation of a stack. -Author: Brandon Rodriguez - - -Tests for main.py. -""" - -# System Imports. -import unittest - -# User Class Imports. - - -class TestMain(unittest.TestCase): - def setUp(self): - pass - - def test_(self): - pass diff --git a/unittests/__init__.py b/unittests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/unittests/resources/__init__.py b/unittests/resources/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/resources/stack.py b/unittests/resources/stack.py similarity index 97% rename from tests/resources/stack.py rename to unittests/resources/stack.py index 990eea57d46413580f8f82a9a975211cc3ed2f38..1c06a7dce79dc59f2deadcfea678819be7acb20c 100644 --- a/tests/resources/stack.py +++ b/unittests/resources/stack.py @@ -5,9 +5,10 @@ Assignment: InClass03 - Custom Implementation of a stack. Author: Brandon Rodriguez -Tests for custom Stack implementation. +UnitTests for custom Stack implementation. """ + # System Imports. import unittest @@ -15,7 +16,7 @@ import unittest from resources.stack import Stack -class TestMain(unittest.TestCase): +class TestStack(unittest.TestCase): def setUp(self): self.test_stack = Stack()