passing sys.argv in pytest tests

58 Views Asked by At

I have this code in script.py:

...
async def main():
    max_requests = int(sys.argv[1]) if len(sys.argv) > 1 else 1
    txt_file = sys.argv[2] if len(sys.argv) > 2 else "urls.txt"
    ...

asyncio.run(main())

So I can run this script in CLI (with optional arguments) and in another modules. It has two arguments, and I trying to write unit tests with pytest for that script in test_script.py, but get an error:

    max_requests = int(sys.argv[1]) if len(sys.argv) > 1 else 1
E   ValueError: invalid literal for int() with base 10: 'test_script.py::TestScript::test_01'

I have read that pytest use its own arguments and I have tried to remove this arguments passing empty or slicing list to sys.argv in test:

    def test(self):
        sys.argv = []
        # or sys.argv = sys.argv[1:]

But it didn't help. Нow to get around pytest arguments and run tests with correct arguments from script?

0

There are 0 best solutions below