add stream, studiomode tests

This commit is contained in:
onyx-and-iris 2025-04-26 15:53:29 +01:00
parent 5ca61d04c7
commit 7915547fca
6 changed files with 97 additions and 11 deletions

1
.gitignore vendored
View File

@ -131,6 +131,7 @@ ENV/
env.bak/ env.bak/
venv.bak/ venv.bak/
.hatch .hatch
.test.env
# Spyder project settings # Spyder project settings
.spyderproject .spyderproject

View File

@ -40,10 +40,9 @@ path = "obsws_cli/__about__.py"
[tool.hatch.envs.default.scripts] [tool.hatch.envs.default.scripts]
cli = "obsws-cli {args:}" cli = "obsws-cli {args:}"
test = "pytest {args:obsws_cli tests}"
[tool.hatch.envs.hatch-test] [tool.hatch.envs.hatch-test]
dependencies = ["pytest>=8.3.5", "pytest-dotenv"] dependencies = ["pytest>=8.3.5"]
[tool.hatch.envs.types] [tool.hatch.envs.types]
extra-dependencies = ["mypy>=1.0.0"] extra-dependencies = ["mypy>=1.0.0"]

View File

@ -3,6 +3,7 @@
import os import os
import obsws_python as obsws import obsws_python as obsws
from dotenv import find_dotenv, load_dotenv
def pytest_configure(config): def pytest_configure(config):
@ -19,9 +20,9 @@ def pytest_sessionstart(session):
""" """
# Initialize the OBS WebSocket client # Initialize the OBS WebSocket client
session.obsws = obsws.ReqClient( session.obsws = obsws.ReqClient(
host=os.environ['OBSWS_HOST'], host=os.environ['OBS_HOST'],
port=os.environ['OBSWS_PORT'], port=os.environ['OBS_PORT'],
password=os.environ['OBSWS_PASSWORD'], password=os.environ['OBS_PASSWORD'],
timeout=5, timeout=5,
) )
resp = session.obsws.get_version() resp = session.obsws.get_version()
@ -32,6 +33,17 @@ def pytest_sessionstart(session):
) )
print(' '.join(out)) print(' '.join(out))
load_dotenv(find_dotenv('.test.env'))
session.obsws.set_stream_service_settings(
'rtmp_common',
{
'service': 'Twitch',
'server': 'auto',
'key': os.environ['OBS_STREAM_KEY'],
},
)
session.obsws.set_current_scene_collection('test-collection') session.obsws.set_current_scene_collection('test-collection')
session.obsws.create_scene('pytest') session.obsws.create_scene('pytest')
@ -68,6 +80,10 @@ def pytest_sessionfinish(session, exitstatus):
""" """
session.obsws.remove_scene('pytest') session.obsws.remove_scene('pytest')
resp = session.obsws.get_stream_status()
if resp.output_active:
session.obsws.stop_stream()
# Close the OBS WebSocket client connection # Close the OBS WebSocket client connection
session.obsws.disconnect() session.obsws.disconnect()

View File

@ -9,35 +9,35 @@ runner = CliRunner()
def test_group_list(): def test_group_list():
"""Test the group list command.""" """Test the group list command."""
result = runner.invoke(app, ['group', 'list', 'pytest00']) result = runner.invoke(app, ['group', 'list', 'Scene'])
assert result.exit_code == 0 assert result.exit_code == 0
assert 'test_group' in result.stdout assert 'test_group' in result.stdout
def test_group_show(): def test_group_show():
"""Test the group show command.""" """Test the group show command."""
result = runner.invoke(app, ['group', 'show', 'pytest00', 'test_group']) result = runner.invoke(app, ['group', 'show', 'Scene', 'test_group'])
assert result.exit_code == 0 assert result.exit_code == 0
assert "Group 'test_group' is now visible." in result.stdout assert "Group 'test_group' is now visible." in result.stdout
def test_group_toggle(): def test_group_toggle():
"""Test the group toggle command.""" """Test the group toggle command."""
result = runner.invoke(app, ['group', 'hide', 'pytest00', 'test_group']) result = runner.invoke(app, ['group', 'hide', 'Scene', 'test_group'])
assert result.exit_code == 0 assert result.exit_code == 0
assert "Group 'test_group' is now hidden." in result.stdout assert "Group 'test_group' is now hidden." in result.stdout
result = runner.invoke(app, ['group', 'toggle', 'pytest00', 'test_group']) result = runner.invoke(app, ['group', 'toggle', 'Scene', 'test_group'])
assert result.exit_code == 0 assert result.exit_code == 0
assert "Group 'test_group' is now visible." in result.stdout assert "Group 'test_group' is now visible." in result.stdout
def test_group_status(): def test_group_status():
"""Test the group status command.""" """Test the group status command."""
result = runner.invoke(app, ['group', 'show', 'pytest00', 'test_group']) result = runner.invoke(app, ['group', 'show', 'Scene', 'test_group'])
assert result.exit_code == 0 assert result.exit_code == 0
assert "Group 'test_group' is now visible." in result.stdout assert "Group 'test_group' is now visible." in result.stdout
result = runner.invoke(app, ['group', 'status', 'pytest00', 'test_group']) result = runner.invoke(app, ['group', 'status', 'Scene', 'test_group'])
assert result.exit_code == 0 assert result.exit_code == 0
assert "Group 'test_group' is now visible." in result.stdout assert "Group 'test_group' is now visible." in result.stdout

43
tests/test_stream.py Normal file
View File

@ -0,0 +1,43 @@
"""Unit tests for the stream commands in the OBS WebSocket CLI."""
import time
from typer.testing import CliRunner
from obsws_cli.app import app
runner = CliRunner()
def test_stream_start():
"""Test the stream start command."""
result = runner.invoke(app, ['stream', 'status'])
assert result.exit_code == 0
active = 'Streaming is in progress' in result.stdout
result = runner.invoke(app, ['stream', 'start'])
assert result.exit_code == 0
time.sleep(1) # Wait for the stream to start
if active:
assert 'Streaming is already in progress, cannot start.' in result.stdout
else:
assert 'Streaming started successfully.' in result.stdout
def test_stream_stop():
"""Test the stream stop command."""
result = runner.invoke(app, ['stream', 'status'])
assert result.exit_code == 0
active = 'Streaming is in progress' in result.stdout
result = runner.invoke(app, ['stream', 'stop'])
assert result.exit_code == 0
time.sleep(1) # Wait for the stream to stop
if active:
assert 'Streaming stopped successfully.' in result.stdout
else:
assert 'Streaming is not in progress, cannot stop.' in result.stdout

27
tests/test_studiomode.py Normal file
View File

@ -0,0 +1,27 @@
"""Unit tests for the studio mode command in the OBS WebSocket CLI."""
from typer.testing import CliRunner
from obsws_cli.app import app
runner = CliRunner()
def test_studio_enable():
"""Test the studio enable command."""
result = runner.invoke(app, ['studiomode', 'enable'])
assert result.exit_code == 0
result = runner.invoke(app, ['studiomode', 'status'])
assert result.exit_code == 0
assert 'Studio mode is enabled.' in result.stdout
def test_studio_disable():
"""Test the studio disable command."""
result = runner.invoke(app, ['studiomode', 'disable'])
assert result.exit_code == 0
result = runner.invoke(app, ['studiomode', 'status'])
assert result.exit_code == 0
assert 'Studio mode is disabled.' in result.stdout