Wait for publication of pytester: https://github.com/databrickslabs/pytester. Then reuse its fixture instead of redefining the code here.