-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert existing tests to pytest + increase coverage #1255
Merged
marcelotduarte
merged 31 commits into
marcelotduarte:main
from
TechnicalPirate:feature/convert_existing_tests_to_pytest
Oct 9, 2021
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
37ede11
Initial commit / skip pycharm config
36628ae
Added essential dependancies
7dcfb8a
Merge remote-tracking branch 'origin/main' into feature/pytest_implem…
TechnicalPirate 3407d13
Added another developer dependancy + a readme for test execution
TechnicalPirate 9b77e1c
Merge branch 'marcelotduarte:main' into feature/pipenv_dep_management…
TechnicalPirate 34f480d
Fixes for all but one unit test
TechnicalPirate a27ad4d
Merge branch 'marcelotduarte:main' into feature/fix_tests
TechnicalPirate 1074d9c
Merge branch 'marcelotduarte:main' into feature/pipenv_dep_management…
TechnicalPirate 34bbf76
Added pycharm idea folder to excluded dirs
TechnicalPirate df9dac9
Updated lock file
TechnicalPirate ca2b440
Merge branch 'feature/fix_tests' into feature/convert_existing_tests_…
TechnicalPirate 1f7a75e
Initial PyTest setup + import namespace test
TechnicalPirate 07cf1bc
Ported first nose test
TechnicalPirate a3e9c59
Fully ported test_finder
TechnicalPirate f3c482f
Moved remaining files
TechnicalPirate af93b30
Delete old test suite
TechnicalPirate c28fed2
added some more very basic tests
TechnicalPirate de7d0ef
Generated dev-requirements
TechnicalPirate c0642a8
Remove pipenv files
TechnicalPirate 6530818
Ignore pipfiles
TechnicalPirate 2df9638
Merge branch 'main' into feature/pipenv_dep_management_and_test_readme
TechnicalPirate 9087a34
Update Readme.md
TechnicalPirate efb4dba
Merge branch 'feature/pipenv_dep_management_and_test_readme' into fea…
TechnicalPirate c48a771
Merge branch 'main' into feature/convert_existing_tests_to_pytest
TechnicalPirate 8e7152e
Cleaning up reports + ignores
TechnicalPirate 001aefd
Removed unimplemented tests + improved param call
TechnicalPirate 9057119
Improve test to avoid ns pollution
TechnicalPirate 55bd656
Merge remote-tracking branch 'origin/main' into feature/convert_exist…
TechnicalPirate e3833d5
Fix failing test + nuke uneeded file + gitignore update
TechnicalPirate 93dc1c5
Switch approach for handling namesapce test
TechnicalPirate 7b2d3e4
Disabled test temporarily
TechnicalPirate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[run] | ||
branch = True | ||
include = | ||
../cx_Freeze/* | ||
|
||
omit = | ||
|
||
[html] | ||
directory = coverage_html_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Test Execution | ||
To Execute the tests in this folder do the following steps | ||
|
||
1 - Install the `dev-requirements` dependencies using `pip install -r dev-requirements.txt` | ||
|
||
2 - First install the module in development ( as seen in `setup.py`'s doc string ) | ||
|
||
``` | ||
Use one of the following commands to use the development mode: | ||
pip install -e . | ||
python setup.py develop | ||
``` | ||
|
||
3 - Call the tests (debuggable) with: | ||
``` | ||
python -m pytest tests | ||
``` | ||
|
||
or with coverage (not-debuggable) with: | ||
``` | ||
python -m pytest tests -m "not long" --cov="cx_Freeze" --cov-report=html | ||
``` | ||
The coverage report will be saved into `/htmlcov` - open the `index.html` to navigate the report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
import os | ||
|
||
|
||
@pytest.fixture() | ||
def fix_test_dir(): | ||
""" This fixture returns the root of the test folder """ | ||
return os.path.dirname(__file__) | ||
|
||
|
||
@pytest.fixture() | ||
def fix_test_samples_dir(fix_test_dir): | ||
""" This fixture returns the samples folder for the tests """ | ||
return os.path.join(fix_test_dir, "samples") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sys | ||
import importlib | ||
import pytest | ||
import cx_Freeze | ||
|
||
|
||
@pytest.mark.skip(reason="needs updating to handle/mock platform specific imports") | ||
@pytest.mark.parametrize( | ||
"platform, extra_modules", [ | ||
(None, []), | ||
("win32", ["bdist_msi"]), | ||
("darwin", ["bdist_dmg", "bdist_mac"]), | ||
("linux", []) | ||
] | ||
) | ||
def test_exposed_namespaces(mocker, platform, extra_modules): | ||
""" This test asserts that all the namespaces that should be exposed when `importing cx_Freeze` are available """ | ||
|
||
if platform: # Mock platform before import :) | ||
mocker.patch.object(sys, "platform", platform) | ||
|
||
try: | ||
importlib.reload(cx_Freeze) | ||
except ImportError: | ||
pass # Pass import errors as some modules are platform specific, we're testing what's exposed in `__all__` here | ||
|
||
expected_namespaces = [ # These namespaces are there regardless of platform | ||
"bdist_rpm", | ||
"build", | ||
"build_exe", | ||
"install", | ||
"install_exe", | ||
"setup", | ||
"ConfigError", | ||
"ConstantsModule", | ||
"Executable", | ||
"Freezer", | ||
"Module", | ||
"ModuleFinder", | ||
] | ||
for ns in expected_namespaces: | ||
assert ns in dir(cx_Freeze) | ||
|
||
if platform: | ||
for ns in extra_modules: | ||
assert ns in dir(cx_Freeze) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import pytest | ||
from cx_Freeze.exception import ConfigError, DarwinException | ||
|
||
|
||
@pytest.mark.parametrize("custom_exception", [ConfigError, DarwinException]) | ||
def test_raise_exceptions(custom_exception): | ||
""" This method tests that exceptions can be raised + caught and the __str__ value is correctly called """ | ||
# TODO : Discuss with maintainer, `ConfigError` dosen't seem like it needs a custom implementation? | ||
try: | ||
raise custom_exception("Something Bad Happened") | ||
except custom_exception as err: | ||
print(str(err)) # Force invokation of the __str__ method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import os | ||
import sys | ||
import shutil | ||
import zipfile | ||
import pytest | ||
from cx_Freeze import ModuleFinder, Module, ConstantsModule | ||
|
||
|
||
class TestModuleFinderWithConvertedNoseTests: | ||
""" This class provides test cases that are conversions of the old NoseTests in `test_finder` | ||
that predated usage of the PyTest Framework""" | ||
|
||
@pytest.fixture() | ||
def fix_module_finder(self): | ||
constants = ConstantsModule() | ||
mf = ModuleFinder(constants_module=constants) | ||
return mf | ||
|
||
def test_ScanCode(self, mocker, fix_test_samples_dir, fix_module_finder): | ||
any3 = (mocker.ANY,) * 3 | ||
import_mock = mocker.patch.object(fix_module_finder, "_import_module", return_value=None) | ||
fix_module_finder.IncludeFile(os.path.join(fix_test_samples_dir, "imports_sample.py")) | ||
import_mock.assert_has_calls( | ||
[ | ||
mocker.call("moda", *any3), | ||
mocker.call("modb", *any3), | ||
mocker.call("", *any3), | ||
mocker.call("modd", *any3), | ||
mocker.call("mode", *any3), | ||
mocker.call("modf", *any3), | ||
mocker.call("modg.submod", *any3), | ||
mocker.call("modh", *any3), | ||
] | ||
) | ||
|
||
def test_not_import_invalid_module_name(self, mocker, fix_test_samples_dir, fix_module_finder): | ||
""" testpkg1 contains not.importable.py, which shouldn't be included.""" | ||
fix_module_finder.path.insert(0, fix_test_samples_dir) | ||
module = fix_module_finder.IncludePackage( | ||
"testpkg1" | ||
) # Threw ImportError before the bug was fixed | ||
assert ("invalid-identifier" in module.global_names), \ | ||
"submodules whose names contain invalid identifiers should still be imported" | ||
|
||
def test_invalid_syntax(self, mocker, fix_test_samples_dir): | ||
"""Invalid syntax (e.g. Py2 or Py3 only code) should not break freezing.""" | ||
constants = ConstantsModule() | ||
mf = ModuleFinder(path=[fix_test_samples_dir] + sys.path, constants_module=constants) | ||
with pytest.raises(ImportError): | ||
mf.IncludeModule( | ||
"invalid_syntax" | ||
) # Threw SyntaxError before the bug was fixed | ||
|
||
@pytest.mark.skip("Test skipped, uncertain if no longer supported - waiting for maintainer to comment on:" | ||
"https://github.com/marcelotduarte/cx_Freeze/pull/1234") | ||
def test_FindModule_from_zip(self, fix_module_finder, fix_samples_dir, tmpdir): | ||
# ----------------- | ||
# Helper Methods from `test_zip_packages` | ||
def clean_pyc_files(): | ||
for dirpath, dirnames, filenames in os.walk(fix_samples_dir): | ||
for filename in filenames: | ||
if filename.endswith((".pyc", ".pyo")): | ||
os.unlink(os.path.join(dirpath, filename)) | ||
if "__pycache__" in dirnames: | ||
dirnames.remove("__pycache__") | ||
shutil.rmtree(os.path.join(dirpath, "__pycache__")) | ||
|
||
def prepare_zip_file(): | ||
clean_pyc_files() | ||
tmpd = tmpdir.mkdir() | ||
egg = os.path.join(tmpd, "testpkg1.egg") | ||
eggzip = zipfile.PyZipFile(egg, "w", zipfile.ZIP_DEFLATED) | ||
eggzip.writepy(os.path.join(fix_samples_dir, "testmod1.py")) | ||
eggzip.writepy(os.path.join(fix_samples_dir, "testpkg1")) | ||
eggzip.close() | ||
return egg | ||
# End Helper Methods | ||
# ----------------- | ||
|
||
|
||
# Original test | ||
egg = prepare_zip_file() | ||
try: | ||
fix_module_finder.path = [egg] | ||
mod = fix_module_finder._internal_import_module( | ||
"testpkg1.submod", deferred_imports=[] | ||
) | ||
assert isinstance(mod, Module) | ||
finally: | ||
os.unlink(egg) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 test can be marked as skipped for now. In the rewrite of ModuleFinder, I ignored this method, but similar support for importing directing from the wheel (.whl)... Maybe we can delete it. I'll think about it
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.
Your shout - the current usage of
@pytest.mark.skip
disables execution of the test - if you want to remove the underlying code + test go for it. I didn't want to remove existing tests without discussion with you :)