Skip to content

Commit

Permalink
use match feature of pytest.raises to check error message
Browse files Browse the repository at this point in the history
  • Loading branch information
CunliangGeng committed Jul 4, 2024
1 parent 81c801e commit 09173b5
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 18 deletions.
3 changes: 1 addition & 2 deletions tests/unit/genomics/test_antismash_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def test_default(self, tmp_path):
def test_error_nonempty_path(self, tmp_path):
nonempty_path = tmp_path / "extracted" / "antismash" / f"{self.antismash_id}" / "subdir"
nonempty_path.mkdir(parents=True)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Nonempty directory"):
download_and_extract_antismash_data(self.antismash_id, tmp_path, tmp_path / "extracted")
assert "Nonempty directory" in e.value.args[0]

# test a non-existent ID, which can be either a fake ID, non-existent in NCBI
# or a valid NCBI genome ID but it does not have BGC data in antismash database
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/genomics/test_antismash_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,5 @@ def test_parse_bgc_genbank():

def test_parse_bgc_genbank_error():
gbk_file = str(DATA_DIR / "fake_antismash.region001.gbk")
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Not found product prediction in antiSMASH Genbank file"):
parse_bgc_genbank(gbk_file)
assert "Not found product prediction in antiSMASH Genbank file" in e.value.args[0]
8 changes: 4 additions & 4 deletions tests/unit/genomics/test_mibig_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_version(self, tmp_path):
assert metadata.is_file()

def test_error_same_path(self, tmp_path):
with pytest.raises(ValueError) as e:
with pytest.raises(
ValueError, match="Identical path of download directory and extract directory"
):
mibig.download_and_extract_mibig_metadata(tmp_path, tmp_path)
assert e.value.args[0] == "Identical path of download directory and extract directory"

def test_error_nonempty_path(self, tmp_path):
nonempty_path = tmp_path / "metadata" / "subdir"
nonempty_path.mkdir(parents=True)

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Nonempty directory"):
mibig.download_and_extract_mibig_metadata(tmp_path, nonempty_path.parent)
assert "Nonempty directory" in e.value.args[0]
4 changes: 1 addition & 3 deletions tests/unit/genomics/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ def test_add_strain_to_bgc_error(bgcs):
strains.add(strain1)
strains.add(strain2)

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Multiple strain objects found .*"):
add_strain_to_bgc(strains, bgcs)

assert "Multiple strain objects found for BGC id 'BGC_01'" in e.value.args[0]


def test_add_bgc_to_gcf(bgcs):
"""Test add_bgc_to_gcf function."""
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/metabolomics/test_gnps_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ def setup_with_fixture(gnps_website_is_down):


def test_unknown_workflow(tmpdir):
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Unknown workflow type for GNPS task .*"):
GNPSDownloader("0ad6535e34d449788f297e712f43068a", tmpdir)
assert "Unknown workflow type for GNPS task" in str(e.value)


@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/metabolomics/test_gnps_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@


def test_unknown_workflow(gnps_zip_files, tmpdir):
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Unknown workflow type for GNPS archive .*"):
GNPSExtractor(gnps_zip_files[GNPSFormat.Unknown], tmpdir)
assert "Unknown workflow type for GNPS archive" in str(e.value)


@pytest.mark.parametrize("workflow", [GNPSFormat.FBMN, GNPSFormat.SNETS, GNPSFormat.SNETSV2])
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/strain/test_strain_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_add_different_id_different_alias(strain: Strain, collection: StrainColl

def test_remove(strain: Strain):
sc = StrainCollection()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
sc.remove(strain)
assert strain not in sc

Expand All @@ -151,7 +151,7 @@ def test_remove_same_id_different_alias(collection: StrainCollection):

def test_remove_different_id(collection: StrainCollection):
strain = Strain("strain_2")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
collection.remove(strain)
assert len(collection) == 1
assert strain not in collection
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_has_name(collection: StrainCollection):
def test_lookup(collection: StrainCollection, strain: Strain):
for name in strain.names:
assert collection.lookup(name) == [strain]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
collection.lookup("strain_not_exist")


Expand Down

0 comments on commit 09173b5

Please sign in to comment.