-
Notifications
You must be signed in to change notification settings - Fork 10
Add unit tests to the "commentAnalysis.py" #17
Conversation
""" positive test """ | ||
self.assertEqual(result, 0) | ||
""" negative test """ | ||
self.assertNotEqual(result, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.assertNotEqual(result, 1) | |
self.assertNotEqual(result, 1) |
self.assertNotEqual(result, 1) | |
self.assertNotEqual(result, 1) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newlines at the end of files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
def test_CommentAnalyzer_init(self): | ||
analyzer = CommentAnalyzer(['test']) | ||
""" positive test """ | ||
self.assertEqual(analyzer.word_count, {'test': 0}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syntax for assertEqual is (expected, actual). It's common to mistakenly have these swapped, but it matters when test failures are reported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
""" negative test """ | ||
self.assertNotEqual(analyzer.word_count, {'test': 1}) | ||
|
||
def test_analyze_comments(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming scheme for tests is "test_" + name of the function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
def test_preProcess(self): | ||
analyzer = CommentAnalyzer(['test']) | ||
result = analyzer.preProcess("This is a test comment") | ||
""" positive test """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stating positive or negative isn't necessary. It's also not that informative. Instead I suggest each function have a docstring explaining the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense!! will change this accordingly
@@ -0,0 +1,53 @@ | |||
import unittest | |||
from commentAnalysis import CommentAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8 standard: add blank line between standard imports and local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
e6238cd
to
5ff3311
Compare
@@ -0,0 +1,64 @@ | |||
import unittest | |||
|
|||
from ..commentAnalysis import CommentAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from ..commentAnalysis import CommentAnalyzer | |
import commentAnalysis |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why those ".." are included. But it is much preferred to import modules, not classes.
@@ -0,0 +1 @@ | |||
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not clear why the system path is being updated here. But also, the last line of a file should end with a newline. And use one import per module to simplify the import section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is required to make the modules(commentAnalysis.py in this case) visible to the testing modules. There are a couple of ways to do it and this seems to work.
README.md
Outdated
@@ -148,6 +149,10 @@ Both `BaseCNN` and `BaseLSTM` also have prediction explanation mechanisms that c | |||
If you have ideas on how to improve the framework to assess text conversation for constructive and inclusive | |||
communication, we welcome your contributions! | |||
|
|||
## Running tests | |||
```python | |||
python3 -m unittest ml-conversational-analytic-tool/tests/<test_file>.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this has to be manually run for each unit test file? How about an automated test suite runner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use python3 -m unittest discover -s tests
to run all the tests in one go. Did you mean to say we need to use something like tox when you refer "automated test runner"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example given in your README change states how to test file-by-file, not as a collection of tests. Tox isn't a requirement, but it can be effective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be helpful to include both
- a command for executing all tests
- a command for executing one specific file
and you shouldn't need to specify python3
here as that's already defined in the virtualenv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. Will do update the README.md for both!
README.md
Outdated
@@ -148,6 +149,10 @@ Both `BaseCNN` and `BaseLSTM` also have prediction explanation mechanisms that c | |||
If you have ideas on how to improve the framework to assess text conversation for constructive and inclusive | |||
communication, we welcome your contributions! | |||
|
|||
## Running tests | |||
```python | |||
python3 -m unittest ml-conversational-analytic-tool/tests/<test_file>.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be helpful to include both
- a command for executing all tests
- a command for executing one specific file
and you shouldn't need to specify python3
here as that's already defined in the virtualenv
5ff3311
to
b874070
Compare
README.md
Outdated
@@ -148,6 +149,16 @@ Both `BaseCNN` and `BaseLSTM` also have prediction explanation mechanisms that c | |||
If you have ideas on how to improve the framework to assess text conversation for constructive and inclusive | |||
communication, we welcome your contributions! | |||
|
|||
## Running tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't a virtualenv required here? Could you include that in the directions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
@@ -0,0 +1,4 @@ | |||
import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think something is incorrect in how the test suite is configured or run if an init.py is required in the root of the project. I've never seen the need for this before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed a PATH in my local env variable which fixes this. We do not need init.py now for the imports!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Please also expand on the PR message to be more descriptive. Also include a reference to #8, the issue you're fixing. Thanks |
b874070
to
502e2a3
Compare
README.md
Outdated
## Running tests | ||
### Run tests with a virtual environment | ||
```python | ||
python3 -m venv virtualenv-ml-conversational |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent use of "python" vs "python3" in the README here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python3 is required when creating a virtual env and is no longer needed when running commands in a virtualenv.
There are already steps in the "Installation" on how to create a virtual env and activate it.
Can we move the "running tests" section inside the "Build and Run" section to leverage the installation step so there are no duplicates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue that python3 is unnecessary as python2 is end-of-life. So depending on the users environment, python could map to python3 anyway. It depends on how many python versions are installed on your system. MacOS comes with a Python 2.7 as a system default, but other operating systems default with a Py3.x version. On debian for example, you can define with version python executable points to via update-alternatives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we specify in the prerequisite section that the python version needed is 3.6+
, I agree that python3
is not necessary. I think with the minimum version written, we can safely assume that python will be python3.
@pramodrj07 could you also help clean up python3 from line 56?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of changing line 56 is a separate PR. But I can do it in the next change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command here does not match the title. This only creates a virtual environment and does not run any tests. Also, I believe you would need an extra step to install requirements.txt as well to be able to run the tests.
I suggested adding "Running test" as part of the "Environment Setup" before. This could help remove the duplicate steps in the test section. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I did move it to the 'Environment setup' section
@@ -0,0 +1,52 @@ | |||
import unittest | |||
from unittest.mock import MagicMock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best practice is to import only modules, not classes (ie MagicMock).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
502e2a3
to
0b9cb9b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mostly looks good to me, just commented on the README clean up
README.md
Outdated
## Running tests | ||
### Run tests with a virtual environment | ||
```python | ||
python3 -m venv virtualenv-ml-conversational |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python3 is required when creating a virtual env and is no longer needed when running commands in a virtualenv.
There are already steps in the "Installation" on how to create a virtual env and activate it.
Can we move the "running tests" section inside the "Build and Run" section to leverage the installation step so there are no duplicates?
1.'unittest' framework of python is used to develop unit tests 2. adds the __init__.py to have standard packaging template
5e55812
to
ed5698f
Compare
Done!! Moved the testing under 'build and run' section |
ed5698f
to
2353db0
Compare
update the README.md with the commands to run unit tests
2353db0
to
3d4c96a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All comments addressed. Thank you!! LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes: #8