Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Add unit tests to the "commentAnalysis.py" #17

Merged
merged 2 commits into from
Jan 20, 2022

Conversation

pramodrj07
Copy link
Contributor

@pramodrj07 pramodrj07 commented Jan 4, 2022

  1. Adds unit tests using the 'unittest' framework.
  2. Adds documentation to run all the unit tests and individual modules

Fixes: #8

""" positive test """
self.assertEqual(result, 0)
""" negative test """
self.assertNotEqual(result, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertNotEqual(result, 1)
self.assertNotEqual(result, 1)
Suggested change
self.assertNotEqual(result, 1)
self.assertNotEqual(result, 1)

Copy link
Contributor

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.

Copy link
Contributor Author

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})
Copy link
Contributor

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.

Copy link
Contributor Author

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):
Copy link
Contributor

@ericwb ericwb Jan 4, 2022

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

Copy link
Contributor Author

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 """
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,64 @@
import unittest

from ..commentAnalysis import CommentAnalyzer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from ..commentAnalysis import CommentAnalyzer
import commentAnalysis

Copy link
Contributor

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__)))
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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"?

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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

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
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ericwb
Copy link
Contributor

ericwb commented Jan 13, 2022

Please also expand on the PR message to be more descriptive. Also include a reference to #8, the issue you're fixing. Thanks

@pramodrj07 pramodrj07 changed the title WIP: Add unit tests to the "commentAnalysis.py" Add unit tests to the "commentAnalysis.py" Jan 18, 2022
README.md Outdated
## Running tests
### Run tests with a virtual environment
```python
python3 -m venv virtualenv-ml-conversational
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!

Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@annajung annajung left a 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
Copy link
Contributor

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
@pramodrj07 pramodrj07 force-pushed the unit-tests branch 2 times, most recently from 5e55812 to ed5698f Compare January 19, 2022 23:46
@pramodrj07
Copy link
Contributor Author

just commented on the README clea

Done!! Moved the testing under 'build and run' section

@pramodrj07 pramodrj07 changed the title Add unit tests to the "commentAnalysis.py" Add unit tests to the "commentAnalysis.py" https://github.com/vmware-labs/ml-conversational-analytic-tool/issues/8 Jan 20, 2022
@pramodrj07 pramodrj07 changed the title Add unit tests to the "commentAnalysis.py" https://github.com/vmware-labs/ml-conversational-analytic-tool/issues/8 Add unit tests to the "commentAnalysis.py" Jan 20, 2022
update the README.md with the commands to run unit tests
Copy link
Contributor

@annajung annajung left a 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

@pramodrj07 pramodrj07 requested a review from ericwb January 20, 2022 21:02
Copy link
Contributor

@ericwb ericwb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ericwb ericwb merged commit 5262fea into vmware-archive:main Jan 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test coverage
4 participants