mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-12-20 06:58:57 +00:00
[test] Add code coverage framework
Implement a comprehensive test coverage framework that integrates with pytest-cov to generate code coverage reports for the yt-dlp codebase. Key components: - Configuration file (.coveragerc) with appropriate include/exclude patterns - Helper script (run_coverage.py) with parallel report generation - GitHub Actions workflow for automatic coverage reporting on PRs and pushes - Support for Hatch testing environment and CLI integration - Testing documentation for running coverage reports - Sample test for demonstrating coverage reporting
This commit is contained in:
39
test/README.md
Normal file
39
test/README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# yt-dlp Tests
|
||||
|
||||
This directory contains tests for the yt-dlp codebase.
|
||||
|
||||
## Running Tests
|
||||
|
||||
### Using hatch (requires `pip install hatch`)
|
||||
|
||||
```bash
|
||||
# Run tests for a specific test file
|
||||
hatch run hatch-test:run test/test_utils.py
|
||||
|
||||
# Run a specific test class or method
|
||||
hatch run hatch-test:run test/test_utils.py::TestUtil
|
||||
hatch run hatch-test:run test/test_utils.py::TestUtil::test_url_basename
|
||||
|
||||
# Run with verbosity
|
||||
hatch run hatch-test:run -- test/test_utils.py -v
|
||||
```
|
||||
|
||||
### Using pytest directly
|
||||
|
||||
```bash
|
||||
# Run a specific test file
|
||||
python -m pytest test/test_utils.py
|
||||
|
||||
# Run a specific test class or method
|
||||
python -m pytest test/test_utils.py::TestUtil
|
||||
python -m pytest test/test_utils.py::TestUtil::test_url_basename
|
||||
|
||||
# Run with verbosity
|
||||
python -m pytest -v test/test_utils.py
|
||||
```
|
||||
|
||||
**Important:** Always run tests from the project root directory, not from a subdirectory.
|
||||
|
||||
## Code Coverage
|
||||
|
||||
For information on running tests with code coverage, see the documentation in `.coverage-reports/README.md`.
|
||||
0
test/devscripts/__init__.py
Normal file
0
test/devscripts/__init__.py
Normal file
62
test/devscripts/test_install_deps.py
Normal file
62
test/devscripts/test_install_deps.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from devscripts import install_deps
|
||||
|
||||
|
||||
class TestInstallDeps(unittest.TestCase):
|
||||
|
||||
@mock.patch('devscripts.install_deps.parse_toml')
|
||||
@mock.patch('devscripts.install_deps.read_file')
|
||||
@mock.patch('devscripts.install_deps.subprocess.call')
|
||||
def test_print_option(self, mock_call, mock_read_file, mock_parse_toml):
|
||||
# Mock the parse_toml function to return a project table with dependencies
|
||||
mock_parse_toml.return_value = {
|
||||
'project': {
|
||||
'name': 'yt-dlp',
|
||||
'dependencies': ['dep1', 'dep2'],
|
||||
'optional-dependencies': {
|
||||
'default': ['opt1', 'opt2'],
|
||||
'test': ['test1', 'test2'],
|
||||
'dev': ['dev1', 'dev2'],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Mock sys.argv to simulate command line arguments
|
||||
with mock.patch('sys.argv', ['install_deps.py', '--print']):
|
||||
# Redirect stdout to capture the output
|
||||
from io import StringIO
|
||||
import sys
|
||||
original_stdout = sys.stdout
|
||||
try:
|
||||
output = StringIO()
|
||||
sys.stdout = output
|
||||
|
||||
# Execute the main function
|
||||
install_deps.main()
|
||||
|
||||
# Get the captured output
|
||||
printed_deps = output.getvalue().strip().split('\n')
|
||||
|
||||
# Check that default dependencies are included
|
||||
# 2 from dependencies + default dependencies
|
||||
self.assertEqual(len(printed_deps), 4)
|
||||
self.assertIn('dep1', printed_deps)
|
||||
self.assertIn('dep2', printed_deps)
|
||||
self.assertIn('opt1', printed_deps)
|
||||
self.assertIn('opt2', printed_deps)
|
||||
|
||||
finally:
|
||||
sys.stdout = original_stdout
|
||||
|
||||
# Call was not made because we used --print
|
||||
mock_call.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user