Skip to content

Commit

Permalink
Rework testing suite files
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Jun 7, 2024
1 parent 70587b4 commit 36c25a7
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 64 deletions.
34 changes: 34 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import shutil
import tempfile

from facefusion.filesystem import is_file


def get_test_jobs_directory() -> str:
return os.path.join(tempfile.gettempdir(), 'test-jobs')


def get_test_examples_directory() -> str:
return os.path.join(tempfile.gettempdir(), 'test-examples')


def get_test_outputs_directory() -> str:
return os.path.join(tempfile.gettempdir(), 'test-outputs')


def prepare_test_output_directory() -> None:
shutil.rmtree(get_test_outputs_directory())
os.mkdir(get_test_outputs_directory())


def get_test_example_file(file : str) -> str:
return os.path.join(tempfile.gettempdir(), 'test-examples', file)


def get_test_output_file(file : str) -> str:
return os.path.join(tempfile.gettempdir(), 'test-outputs', file)


def is_test_output_file(file : str) -> bool:
return is_file(get_test_output_file(file))
13 changes: 7 additions & 6 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

from facefusion.audio import get_audio_frame, read_static_audio
from facefusion.download import conditional_download
from .helper import get_test_examples_directory, get_test_example_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.mp3'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/source.mp3', '.assets/examples/source.wav' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('source.mp3'), get_test_example_file('source.wav') ])


def test_get_audio_frame() -> None:
assert get_audio_frame('.assets/examples/source.mp3', 25) is not None
assert get_audio_frame('.assets/examples/source.wav', 25) is not None
assert get_audio_frame(get_test_example_file('source.mp3'), 25) is not None
assert get_audio_frame(get_test_example_file('source.wav'), 25) is not None
assert get_audio_frame('invalid', 25) is None


def test_read_static_audio() -> None:
assert len(read_static_audio('.assets/examples/source.mp3', 25)) == 280
assert len(read_static_audio('.assets/examples/source.wav', 25)) == 280
assert len(read_static_audio(get_test_example_file('source.mp3'), 25)) == 280
assert len(read_static_audio(get_test_example_file('source.wav'), 25)) == 280
assert read_static_audio('invalid', 25) is None
22 changes: 14 additions & 8 deletions tests/test_cli_face_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '.assets/examples/target-240p.jpg' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_debug_face_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_debugger', '-t', '.assets/examples/target-240p.jpg', '-o', '.assets/examples/test-debug-face-to-image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_debugger', '-t', get_test_example_file('target-240p.jpg'), '-o', get_test_output_file('test-debug-face-to-image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test-debug-face-to-image.jpg') is True


def test_debug_face_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_debugger', '-t', '.assets/examples/target-240p.mp4', '-o', '.assets/examples/test-debug-face-to-video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_debugger', '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test-debug-face-to-video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()
assert is_test_output_file('test-debug-face-to-video.mp4') is True
23 changes: 14 additions & 9 deletions tests/test_cli_face_enhancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '.assets/examples/target-240p.jpg' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_enhance_face_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_enhancer', '-t', '.assets/examples/target-240p.jpg', '-o', '.assets/examples/test-enhance-face-to-image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_enhancer', '-t', get_test_example_file('target-240p.jpg'), '-o', get_test_output_file('test-enhance-face-to-image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test-enhance-face-to-image.jpg') is True


def test_enhance_face_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_enhancer', '-t', '.assets/examples/target-240p.mp4', '-o', '.assets/examples/test-enhance-face-to-video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_enhancer', '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test-enhance-face-to-video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()

assert is_test_output_file('test-enhance-face-to-video.mp4') is True
22 changes: 14 additions & 8 deletions tests/test_cli_face_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '.assets/examples/target-240p.jpg' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_swap_face_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_swapper', '-s', '.assets/examples/source.jpg', '-t', '.assets/examples/target-240p.jpg', '-o', '.assets/examples/test-swap-face-to-image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_swapper', '-s', get_test_example_file('source.jpg'), '-t', get_test_example_file('target-240p.jpg'), '-o', get_test_output_file('test-swap-face-to-image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test-swap-face-to-image.jpg') is True


def test_swap_face_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_swapper', '-s', '.assets/examples/source.jpg', '-t', '.assets/examples/target-240p.mp4', '-o', '.assets/examples/test-swap-face-to-video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'face_swapper', '-s', get_test_example_file('source.jpg'), '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test-swap-face-to-video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()
assert is_test_output_file('test-swap-face-to-video.mp4') is True
24 changes: 15 additions & 9 deletions tests/test_cli_frame_colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '-vf', 'hue=s=0', '.assets/examples/target-240p-0sat.jpg' ])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vf', 'hue=s=0', '.assets/examples/target-240p-0sat.mp4' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', '-vf', 'hue=s=0', get_test_example_file('target-240p-0sat.jpg') ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vf', 'hue=s=0', get_test_example_file('target-240p-0sat.mp4') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_colorize_frame_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_colorizer', '-t', '.assets/examples/target-240p-0sat.jpg', '-o', '.assets/examples/test_colorize-frame-to-image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_colorizer', '-t', get_test_example_file('target-240p-0sat.jpg'), '-o', get_test_output_file('test_colorize-frame-to-image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test_colorize-frame-to-image.jpg') is True


def test_colorize_frame_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_colorizer', '-t', '.assets/examples/target-240p-0sat.mp4', '-o', '.assets/examples/test-colorize-frame-to-video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_colorizer', '-t', get_test_example_file('target-240p-0sat.mp4'), '-o', get_test_output_file('test-colorize-frame-to-video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()
assert is_test_output_file('test-colorize-frame-to-video.mp4') is True
22 changes: 14 additions & 8 deletions tests/test_cli_frame_enhancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '.assets/examples/target-240p.jpg' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_enhance_frame_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_enhancer', '-t', '.assets/examples/target-240p.jpg', '-o', '.assets/examples/test-enhance-frame-to-image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_enhancer', '-t', get_test_example_file('target-240p.jpg'), '-o', get_test_output_file('test-enhance-frame-to-image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test-enhance-frame-to-image.jpg') is True


def test_enhance_frame_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_enhancer', '-t', '.assets/examples/target-240p.mp4', '-o', '.assets/examples/test-enhance-frame-to-video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'frame_enhancer', '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test-enhance-frame-to-video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()
assert is_test_output_file('test-enhance-frame-to-video.mp4') is True
5 changes: 3 additions & 2 deletions tests/test_cli_jobs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest

from facefusion.job_manager import clear_jobs, init_jobs
from .helper import get_test_jobs_directory


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
clear_jobs('.jobs')
init_jobs('.jobs')
clear_jobs(get_test_jobs_directory())
init_jobs(get_test_jobs_directory())


@pytest.mark.skip()
Expand Down
22 changes: 14 additions & 8 deletions tests/test_cli_lip_syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
import pytest

from facefusion.download import conditional_download
from .helper import get_test_examples_directory, prepare_test_output_directory, get_test_example_file, get_test_output_file, is_test_output_file


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
conditional_download('.assets/examples',
conditional_download(get_test_examples_directory(),
[
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/source.mp3',
'https://github.com/facefusion/facefusion-assets/releases/download/examples/target-240p.mp4'
])
subprocess.run([ 'ffmpeg', '-i', '.assets/examples/target-240p.mp4', '-vframes', '1', '.assets/examples/target-240p.jpg' ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ])


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
prepare_test_output_directory()


def test_sync_lip_to_image() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'lip_syncer', '-s', '.assets/examples/source.mp3', '-t', '.assets/examples/target-240p.jpg', '-o', '.assets/examples/test_sync_lip_to_image.jpg', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'lip_syncer', '-s', get_test_example_file('source.mp3'), '-t', get_test_example_file('target-240p.jpg'), '-o', get_test_output_file('test_sync_lip_to_image.jpg'), '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'image succeed' in run.stdout.decode()
assert is_test_output_file('test_sync_lip_to_image.jpg') is True


def test_sync_lip_to_video() -> None:
commands = [ sys.executable, 'run.py', '--frame-processors', 'lip_syncer', '-s', '.assets/examples/source.mp3', '-t', '.assets/examples/target-240p.mp4', '-o', '.assets/examples/test_sync_lip_to_video.mp4', '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
commands = [ sys.executable, 'run.py', '--frame-processors', 'lip_syncer', '-s', get_test_example_file('source.mp3'), '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test_sync_lip_to_video.mp4'), '--trim-frame-end', '10', '--headless' ]
run = subprocess.run(commands)

assert run.returncode == 0
assert 'video succeed' in run.stdout.decode()
assert is_test_output_file('test_sync_lip_to_video.mp4') is True
5 changes: 3 additions & 2 deletions tests/test_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import pytest

from facefusion.job_manager import init_jobs, clear_jobs, create_job, delete_job, find_job_ids, move_job_file, add_step, remix_step, insert_step, remove_step, get_steps, set_step_status
from .helper import get_test_jobs_directory


@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
clear_jobs('.jobs')
init_jobs('.jobs')
clear_jobs(get_test_jobs_directory())
init_jobs(get_test_jobs_directory())


def test_create_job() -> None:
Expand Down
9 changes: 5 additions & 4 deletions tests/test_job_runner.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest

from facefusion.job_manager import init_jobs, clear_jobs
from .helper import get_test_jobs_directory


@pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None:
clear_jobs('.jobs')
init_jobs('.jobs')
@pytest.fixture(scope = 'function', autouse = True)
def before_each() -> None:
clear_jobs(get_test_jobs_directory())
init_jobs(get_test_jobs_directory())


@pytest.mark.skip()
Expand Down

0 comments on commit 36c25a7

Please sign in to comment.