55
66import io
77import os
8+ import re
89import subprocess
910import tempfile
1011from contextlib import redirect_stderr
1112from pathlib import Path
12- from unittest import SkipTest , TestCase
1313from urllib .error import URLError
1414from urllib .request import Request , urlopen
1515
16+ import pytest
1617from typer .testing import CliRunner
1718
1819from ..classes import Segment
2122VERBOSE_OVERRIDE = bool (os .environ .get ("EVERYVOICE_VERBOSE_TESTS" , False ))
2223
2324
24- class CLITest ( TestCase ):
25- def setUp ( self ) -> None :
26- self .runner = CliRunner ()
25+ @ pytest . fixture ( scope = "class" )
26+ def runner ( request ) -> None :
27+ request . cls .runner = CliRunner ()
2728
28- def test_main_help (self ):
29+
30+ @pytest .mark .usefixtures ("runner" )
31+ class TestCLI :
32+ def test_main_help (self , subtests ):
2933 for help in "-h" , "--help" :
30- with self . subTest (help = help ):
34+ with subtests . test (help = help ):
3135 result = self .runner .invoke (app , [help ])
32- self . assertEqual ( result .exit_code , 0 )
33- self . assertIn ( "align" , result .stdout )
34- self . assertIn ( "extract" , result .stdout )
36+ assert result .exit_code == 0
37+ assert "align" in result .stdout
38+ assert "extract" in result .stdout
3539
36- def test_sub_help (self ):
40+ def test_sub_help (self , subtests ):
3741 for cmd in "align" , "extract" :
3842 for help in "-h" , "--help" :
39- with self . subTest (cmd = cmd , help = help ):
43+ with subtests . test (cmd = cmd , help = help ):
4044 result = self .runner .invoke (app , [cmd , help ])
41- self . assertEqual ( result .exit_code , 0 )
42- self . assertIn ( "Usage:" , result .stdout )
43- self . assertIn ( cmd , result .stdout )
45+ assert result .exit_code == 0
46+ assert "Usage:" in result .stdout
47+ assert cmd in result .stdout
4448
45- def test_align_empty_file (self ):
46- with self . subTest ("empty file" ):
49+ def test_align_empty_file (self , subtests ):
50+ with subtests . test ("empty file" ):
4751 result = self .runner .invoke (app , ["align" , os .devnull , os .devnull ])
48- self . assertNotEqual ( result .exit_code , 0 )
49- self . assertRegex ( result . output , r"(?s)is.*empty" )
52+ assert result .exit_code != 0
53+ assert re . search ( r"(?s)is.*empty" , result . output )
5054
51- with self . subTest ("file with only empty lines" ):
55+ with subtests . test ("file with only empty lines" ):
5256 with tempfile .TemporaryDirectory () as tmpdir :
5357 textfile = os .path .join (tmpdir , "emptylines.txt" )
5458 with open (textfile , "w" , encoding = "utf8" ) as f :
5559 f .write ("\n \n \n " )
5660 result = self .runner .invoke (app , ["align" , textfile , os .devnull ])
57- self . assertNotEqual ( result .exit_code , 0 )
58- self . assertRegex ( result . output , r"(?s)is.*empty" )
61+ assert result .exit_code != 0
62+ assert re . search ( r"(?s)is.*empty" , result . output )
5963
6064 def fetch_ras_test_file (self , filename , outputdir ):
6165 repo , path = "https://github.com/ReadAlongs/Studio/" , "/tests/data/"
@@ -65,15 +69,15 @@ def fetch_ras_test_file(self, filename, outputdir):
6569 with open (os .path .join (outputdir , filename ), "wb" ) as f :
6670 f .write (response .read ())
6771
68- def test_align_something (self ):
72+ def test_align_something (self , subtests ):
6973 with tempfile .TemporaryDirectory () as tmpdir :
7074 tmppath = Path (tmpdir )
7175 try :
7276 with redirect_stderr (io .StringIO ()):
7377 self .fetch_ras_test_file ("ej-fra.txt" , tmpdir )
7478 self .fetch_ras_test_file ("ej-fra.m4a" , tmpdir )
7579 except URLError as e : # pragma: no cover
76- raise SkipTest (
80+ raise pytest . skip (
7781 f"Can't fetch test data: { e } ; skipping the test that depends on the Internet."
7882 )
7983 txt = tmppath / "ej-fra.txt"
@@ -90,31 +94,31 @@ def test_align_something(self):
9094 textgrid = tmppath / "ej-fra-16000.TextGrid"
9195 wav_out = tmppath / "ej-fra-16000.wav"
9296
93- with self . subTest ("ctc-segmenter align" ):
97+ with subtests . test ("ctc-segmenter align" ):
9498 result = self .runner .invoke (app , ["align" , str (txt ), str (wav )])
9599 if result .exit_code != 0 :
96100 os .system ("ls -la " + tmpdir )
97101 print (result .output )
98- self . assertEqual ( result .exit_code , 0 )
99- self . assertTrue ( textgrid .exists () )
100- self . assertTrue ( wav_out .exists () )
102+ assert result .exit_code == 0
103+ assert textgrid .exists ()
104+ assert wav_out .exists ()
101105
102- with self . subTest ("ctc-segmenter extract" ):
106+ with subtests . test ("ctc-segmenter extract" ):
103107 result = self .runner .invoke (
104108 app , ["extract" , str (textgrid ), str (wav_out ), str (tmppath / "out" )]
105109 )
106110 if result .exit_code != 0 :
107111 print (result .output )
108- self . assertEqual ( result .exit_code , 0 )
109- self . assertTrue (( tmppath / "out/metadata.psv" ).exists () )
112+ assert result .exit_code == 0
113+ assert ( tmppath / "out/metadata.psv" ).exists ()
110114 with open (txt , encoding = "utf8" ) as txt_f :
111115 non_blank_line_count = sum (1 for line in txt_f if line .strip ())
112116 for i in range (non_blank_line_count ):
113- self . assertTrue (( tmppath / f"out/wavs/segment{ i } .wav" ))
117+ assert ( tmppath / f"out/wavs/segment{ i } .wav" ). exists ( )
114118
115119
116- class MiscTests ( TestCase ) :
120+ class TestMisc :
117121 def test_segment (self ):
118122 segment = Segment ("text" , 500 , 700 , 0.42 )
119- self . assertEqual ( len (segment ), 200 )
120- self . assertEqual ( repr (segment ), "text (0.42): [ 500, 700)" )
123+ assert len (segment ) == 200
124+ assert repr (segment ) == "text (0.42): [ 500, 700)"
0 commit comments