GithubHelp home page GithubHelp logo

pytest-venv's Introduction

pytest-venv

https://github.com/mmerickel/pytest-venv/actions/workflows/ci-tests.yml/badge.svg?branch=main

pytest-venv is a simple pytest plugin that exposes a venv fixture. The fixture is used to create a new virtual environment which can be used to install packages and run commands inside tests.

Usage

import os

def test_it(venv):
    venv.install('pyramid', upgrade=True)
    subprocess.check_call(
        [os.path.join(venv.bin, 'pserve'), 'development.ini'],
    )

API

The venv fixture is an instance of pytest_venv.VirtualEnvironment(path) which exposes the following API:

path

The path to the virtualenv directory.

bin

The path to the bin / Scripts directory.

python

The path to the python executable in the virtualenv.

create(system_site_packages=False, python=None, *, extra_args=None)

Create a virtualenv. This is called automatically by the venv fixture. If python is None then the virtual environment will be created using the same executable as the active runtime.

install(pkg_name, editable=False, upgrade=False, *, extra_args=None)

Use pip to install a package into the virtualenv. pkg_name may be a path to a package on disk.

get_version(pkg_name, *, raises=True)

Returns a packaging.version.Version object which is sortable and convertable to a string.

pytest-venv's People

Contributors

dependabot[bot] avatar frenzymadness avatar mmerickel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pytest-venv's Issues

Do you want a help with maintenance?

Hello.

I like this project and I'd like to help you with maintenance. I also have some ideas about how to improve it.

If you still want to actively maintain it, I can open PRs and you can review them.

BTW, we are using this tool in micropipenv and I recently started with packaging it for Fedora.

Check that the package is not installed

Hi.

We use get_version to check if a package is installed and in which specific version. However, if I want to check that some package is intentionally missing, get_version is not useful because if a package does not exist, it raises subprocess.CalledProcessError.

I think that it might be better to return None if a package is not present in a virtual environment. Or we might create a new method for this, something like get_version_or_none to keep the API stable.

What do you think?

Problem with Python 3.12

Hello.

I've found an issue with this method and Python 3.12:

def get_version(self, pkg_name):
script = (
'import pkg_resources; '
'print(pkg_resources.get_distribution("%(pkg_name)s").version)'
) % dict(pkg_name=pkg_name)
version = subprocess.check_output([self.python, '-c', script]).strip()
return pkg_resources.parse_version(version.decode('utf8'))

The problem is that it uses pkg_resources (provided by setuptools) but the setuptools library is no longer a default part of virtual environments created by Python venv module and the same behavior is implemented in virtualenv: https://github.com/pypa/virtualenv/blob/main/docs/changelog.rst#features---20230

We can either use one of the proposed solutions in the mentioned changelog to install setuptools into the environment or try to replace pkg_resource with importlib.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.