Skip to content

Commit

Permalink
Merge pull request #1607 from fabianegli/use-pytest-raises
Browse files Browse the repository at this point in the history
use pytest.raises to test for raised exception
  • Loading branch information
fabianegli authored Jun 13, 2022
2 parents 51f4b1b + a0b6e31 commit dd7b59b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
8 changes: 4 additions & 4 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def test_get_release_hash_branch(self):
== "https://github.com/nf-core/exoseq/archive/819cbac792b76cf66c840b567ed0ee9a2f620db7.zip"
)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_get_release_hash_non_existent_release(self):
wfs = nf_core.list.Workflows()
wfs.get_remote_workflows()
Expand All @@ -71,7 +70,8 @@ def test_get_release_hash_non_existent_release(self):
download_obj.wf_revisions,
download_obj.wf_branches,
) = nf_core.utils.get_repo_releases_branches(pipeline, wfs)
download_obj.get_revision_hash()
with pytest.raises(AssertionError):
download_obj.get_revision_hash()

#
# Tests for 'download_wf_files'
Expand Down Expand Up @@ -150,7 +150,6 @@ def test_matching_md5sums(self, tmpfile):
download_obj.validate_md5(tmpfile.name, val_hash)

@with_temporary_file
@pytest.mark.xfail(raises=IOError, strict=True)
def test_mismatching_md5sums(self, tmpfile):
download_obj = DownloadWorkflow(pipeline="dummy")
test_hash = hashlib.md5()
Expand All @@ -160,7 +159,8 @@ def test_mismatching_md5sums(self, tmpfile):
with open(tmpfile.name, "w") as f:
f.write("test")

download_obj.validate_md5(tmpfile.name, val_hash)
with pytest.raises(IOError):
download_obj.validate_md5(tmpfile.name, val_hash)

#
# Tests for 'singularity_pull_image'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def test_pretty_datetime(self):
now_ts = time.mktime(now.timetuple())
nf_core.list.pretty_date(now_ts)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_local_workflows_and_fail(self):
"""Test the local workflow class and try to get local
Nextflow workflow information"""
loc_wf = nf_core.list.LocalWorkflow("myWF")
loc_wf.get_local_nf_workflow_details()
with pytest.raises(AssertionError):
loc_wf.get_local_nf_workflow_details()

def test_local_workflows_compare_and_fail_silently(self):
"""Test the workflow class and try to compare local
Expand Down
45 changes: 22 additions & 23 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,18 @@ def test_load_lint_schema(self):
self.schema_obj.get_schema_path(self.template_dir)
self.schema_obj.load_lint_schema()

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_load_lint_schema_nofile(self):
"""Check that linting raises properly if a non-existant file is given"""
self.schema_obj.get_schema_path("fake_file")
self.schema_obj.load_lint_schema()
with pytest.raises(AssertionError):
self.schema_obj.get_schema_path("fake_file")

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_load_lint_schema_notjson(self):
"""Check that linting raises properly if a non-JSON file is given"""
self.schema_obj.get_schema_path(os.path.join(self.template_dir, "nextflow.config"))
self.schema_obj.load_lint_schema()
with pytest.raises(AssertionError):
self.schema_obj.load_lint_schema()

@with_temporary_file
@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_load_lint_schema_noparams(self, tmp_file):
"""
Check that linting raises properly if a JSON file is given without any params
Expand All @@ -64,7 +62,8 @@ def test_load_lint_schema_noparams(self, tmp_file):
with open(tmp_file.name, "w") as fh:
json.dump({"type": "fubar"}, fh)
self.schema_obj.get_schema_path(tmp_file.name)
self.schema_obj.load_lint_schema()
with pytest.raises(AssertionError):
self.schema_obj.load_lint_schema()

def test_get_schema_path_dir(self):
"""Get schema file from directory"""
Expand All @@ -74,22 +73,22 @@ def test_get_schema_path_path(self):
"""Get schema file from a path"""
self.schema_obj.get_schema_path(self.template_schema)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_get_schema_path_path_notexist(self):
"""Get schema file from a path"""
self.schema_obj.get_schema_path("fubar", local_only=True)
with pytest.raises(AssertionError):
self.schema_obj.get_schema_path("fubar", local_only=True)

def test_get_schema_path_name(self):
"""Get schema file from the name of a remote pipeline"""
self.schema_obj.get_schema_path("atacseq")

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_get_schema_path_name_notexist(self):
"""
Get schema file from the name of a remote pipeline
that doesn't have a schema file
"""
self.schema_obj.get_schema_path("exoseq")
with pytest.raises(AssertionError):
self.schema_obj.get_schema_path("exoseq")

def test_load_schema(self):
"""Try to load a schema from a file"""
Expand Down Expand Up @@ -135,10 +134,10 @@ def test_load_input_params_yaml(self, tmp_file):
yaml.dump({"input": "fubar"}, fh)
self.schema_obj.load_input_params(tmp_file.name)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_load_input_params_invalid(self):
"""Check failure when a non-existent file params file is loaded"""
self.schema_obj.load_input_params("fubar")
with pytest.raises(AssertionError):
self.schema_obj.load_input_params("fubar")

def test_validate_params_pass(self):
"""Try validating a set of parameters against a schema"""
Expand All @@ -163,11 +162,11 @@ def test_validate_schema_pass(self):
self.schema_obj.load_schema()
self.schema_obj.validate_schema(self.schema_obj.schema)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_validate_schema_fail_noparams(self):
"""Check that the schema validation fails when no params described"""
self.schema_obj.schema = {"type": "invalidthing"}
self.schema_obj.validate_schema(self.schema_obj.schema)
with pytest.raises(AssertionError):
self.schema_obj.validate_schema(self.schema_obj.schema)

def test_validate_schema_fail_duplicate_ids(self):
"""
Expand Down Expand Up @@ -327,37 +326,37 @@ def test_build_schema_from_scratch(self, tmp_dir):

param = self.schema_obj.build_schema(test_pipeline_dir, True, False, None)

@pytest.mark.xfail(raises=AssertionError, strict=True)
@mock.patch("requests.post")
def test_launch_web_builder_timeout(self, mock_post):
"""Mock launching the web builder, but timeout on the request"""
# Define the behaviour of the request get mock
mock_post.side_effect = requests.exceptions.Timeout()
self.schema_obj.launch_web_builder()
with pytest.raises(AssertionError):
self.schema_obj.launch_web_builder()

@pytest.mark.xfail(raises=AssertionError, strict=True)
@mock.patch("requests.post")
def test_launch_web_builder_connection_error(self, mock_post):
"""Mock launching the web builder, but get a connection error"""
# Define the behaviour of the request get mock
mock_post.side_effect = requests.exceptions.ConnectionError()
self.schema_obj.launch_web_builder()
with pytest.raises(AssertionError):
self.schema_obj.launch_web_builder()

@pytest.mark.xfail(raises=AssertionError, strict=True)
@mock.patch("requests.post")
def test_get_web_builder_response_timeout(self, mock_post):
"""Mock checking for a web builder response, but timeout on the request"""
# Define the behaviour of the request get mock
mock_post.side_effect = requests.exceptions.Timeout()
self.schema_obj.launch_web_builder()
with pytest.raises(AssertionError):
self.schema_obj.launch_web_builder()

@pytest.mark.xfail(raises=AssertionError, strict=True)
@mock.patch("requests.post")
def test_get_web_builder_response_connection_error(self, mock_post):
"""Mock checking for a web builder response, but get a connection error"""
# Define the behaviour of the request get mock
mock_post.side_effect = requests.exceptions.ConnectionError()
self.schema_obj.launch_web_builder()
with pytest.raises(AssertionError):
self.schema_obj.launch_web_builder()

def mocked_requests_post(**kwargs):
"""Helper function to emulate POST requests responses from the web"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ def test_get_repo_releases_branches_not_nf_core(self):
raise AssertionError("MultiQC release v1.10 not found")
assert "master" in wf_branches.keys()

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_get_repo_releases_branches_not_exists(self):
wfs = nf_core.list.Workflows()
wfs.get_remote_workflows()
pipeline, wf_releases, wf_branches = nf_core.utils.get_repo_releases_branches("made_up_pipeline", wfs)
with pytest.raises(AssertionError):
nf_core.utils.get_repo_releases_branches("made_up_pipeline", wfs)

@pytest.mark.xfail(raises=AssertionError, strict=True)
def test_get_repo_releases_branches_not_exists_slash(self):
wfs = nf_core.list.Workflows()
wfs.get_remote_workflows()
pipeline, wf_releases, wf_branches = nf_core.utils.get_repo_releases_branches("made-up/pipeline", wfs)
with pytest.raises(AssertionError):
nf_core.utils.get_repo_releases_branches("made-up/pipeline", wfs)

0 comments on commit dd7b59b

Please sign in to comment.