Giter Club home page Giter Club logo

pytest-picked's Introduction

pytest-picked

See Test Status on Github Actions

PyPI version

Conda forge package

Supported Python versions

Run the tests related to the unstaged files or the current branch (according to Git)


Demo

Let's say you have the following output from git status:

$ git status

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  api.py
  tests/api/
  tests/test_board.py

nothing added to commit but untracked files present (use "git add" to track)

Running pytest --picked, the plugin will run all tests that come from this output.

$ pytest --picked

============================= test session starts =============================
platform darwin -- Python 3.6.4, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /Users/ana.gomes/personal-workspace/grandma, inifile:
plugins: picked-0.1.0, mock-1.10.0, flask-0.10.0, deadfixtures-2.0.1
collecting 34 items
Changed test files... 1. ['tests/test_board.py']
Changed test folders... 1. ['tests/api/']
collected 34 items

tests/test_board.py .                                                      [ 50%]
tests/api/test_new.py .                                                    [100%]

=========================== 2 passed in 0.07 seconds ===========================

All tests will be run from files and folders which are modified but not yet committed. No more copy and paste!

Usage

pytest --picked

pytest --picked=first

pytest --picked --mode=branch

pytest --picked --mode=unstaged  # default

pytest --picked --mode=branch --parent-branch=dev # if your parent branch differs from "main"

Features

Using git status, this plugin allows you to:

  • Run only tests from modified test files
  • Run tests from modified test files first, followed by all unmodified tests

Installation

You can install pytest-picked via PyPI:

pip install pytest-picked

Contributing

Contributions are very welcome. Tests can be run with tox, so we can guarantee that it is working in different python versions. Also, make sure to add tests and use pre-commit before you submit a pull request.

License

Distributed under the terms of the MIT license, pytest-picked is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

pytest-picked's People

Contributors

anapaulagomes avatar dependabot-preview[bot] avatar dsalisbury avatar iamkhush avatar mastersam07 avatar misterrios avatar nicoddemus avatar pschroeder89 avatar pyup-bot avatar relud avatar sobolevn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytest-picked's Issues

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Add support to Python 3.9+

It does not work. No errors, no warnings, no tests running.

 pytest --picked

Changed test files... 0. []
Changed test folders... 0. []
Test session starts (platform: darwin, Python 3.9.10, pytest 6.2.5, pytest-sugar 0.9.4)
django: settings: dmp.settings (from ini)
rootdir: /Users/hipertracker/dev/app, configfile: pytest.ini
plugins: Faker-11.3.0, picked-0.4.6, xdist-2.5.0, forked-1.4.0, html-3.1.1, django-3.10.0, sugar-0.9.4, metadata-1.11.0, notifier-1.0.4, testmon-1.2.2
collecting ...

Results (0.01s):

pip freeze > requirements.lock.txt

...
pytest==6.2.5
pytest-django==3.10.0
pytest-forked==1.4.0
pytest-html==3.1.1
pytest-metadata==1.11.0
pytest-notifier==1.0.4
pytest-picked==0.4.6
pytest-sugar==0.9.4
pytest-testmon==1.2.2
pytest-watch==4.2.0
pytest-xdist==2.5.0
python-dateutil==2.8.2
$ python -V
Python 3.9.10

Parse git raw message

Within the current implementation, it removes 8 leading chars from git diff --name-status result for each candidate. However on my Mac for some reason it is \t between the modification flag and the file name. For example, instead of
M <File Path>
it is actually M\t<File Path>
I guess it is something configurable but we'd better handle it since there are must be some people with same git settings as I do.

Fix PytestDeprecationWarning: TerminalReporter.writer

To reproduce it, run pytest in this repo:

tests/test_pytest_picked.py: 13 tests with warnings
  /home/ana/workspace/pytest-picked/venv/lib/python3.8/site-packages/pytest-5.4.1-py3.8.egg/_pytest/terminal.py:287: PytestDeprecationWarning: TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.
  See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information.
    warnings.warn(

-- Docs: https://docs.pytest.org/en/latest/warnings.html

FileNotFoundError: [Errno 2] No such file or directory: 'git': 'git'

I'm trying to run pytest in docker container. I always use pytest in my projects with docker.
Is the .git folder missing in the container?

This is my .dockerignore:


.*
!.coveragerc
!.env
!.pylintrc

This is traceback:


INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/_pytest/main.py", line 174, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/_pytest/config/__init__.py", line 593, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/hooks.py", line 306, in call_historic
INTERNALERROR>     res = self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 67, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 61, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pytest_picked/plugin.py", line 45, in pytest_configure
INTERNALERROR>     picked_files, picked_folders = mode.affected_tests()
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pytest_picked/modes.py", line 12, in affected_tests
INTERNALERROR>     raw_output = self.git_output()
INTERNALERROR>   File "/usr/local/lib/python3.6/site-packages/pytest_picked/modes.py", line 31, in git_output
INTERNALERROR>     output = subprocess.run(self.command(), stdout=subprocess.PIPE)
INTERNALERROR>   File "/usr/local/lib/python3.6/subprocess.py", line 403, in run
INTERNALERROR>     with Popen(*popenargs, **kwargs) as process:
INTERNALERROR>   File "/usr/local/lib/python3.6/subprocess.py", line 709, in __init__
INTERNALERROR>     restore_signals, start_new_session)
INTERNALERROR>   File "/usr/local/lib/python3.6/subprocess.py", line 1344, in _execute_child
INTERNALERROR>     raise child_exception_type(errno_num, err_msg, err_filename)
INTERNALERROR> FileNotFoundError: [Errno 2] No such file or directory: 'git': 'git'

Add a new mode "File"

There are certain scenarios that git is not available at the time of execution, so I guess it will be beneficial to have a new "File" mode, which means it reads change set from a text file instead of git diff.
For example, in my infra setup, tests are built into Docker images and deployed to K8s for execution. .git fold is dropped during image building. Even the .git folder is retained the K8s pod does not have permission to access git.
My current workaround is to run git diff at the time of image building and save the result to a text file in the image. If the plugin has the capability to read changes from that text file then I can directly use this plugin. Currently I have to write my own plugin.

Improve contributing documentation

Things that we need to do:

  • How to configure the development environment and run the tests
  • Explain usage of each mode

This can also be break down into different PRs.

after commit

Hi,
How can I run picked tests after commits ?
the scenario : commit on branch feature, and run tests pytest --picked --mode=branch --parent-branch=main

Thank you

run individual tests where code has changed

pytest-picked looks great, but I often have 10s or even hundreds of tests in a single file.

It would be great therefore if this library could pick specific test rather than whole files.

I imagine this would be possible by introspecting git diff or similar.

Module collects files with 'test' in the name, even if they are not test files.

Hi. I was just looking through the codebase to familiarize myself with how it works, and I found this bug- It collects files that have test in the name, including something with test in the middle of the word:

โ•ฐโ”€ touch sample/intestine.py 
โ•ฐโ”€ ls sample 
__init__.py  intestine.py  __pycache__  settings.py  urls.py  wsgi.py
โ•ฐโ”€ pytest --picked 
======================================================== test session starts =========================================================
platform linux -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/misterrios/Projects/sample/sample, inifile:
plugins: picked-0.1.0
collecting 0 items                                                                                                                   
Changed test files... 1. ['sample/intestine.py']
Changed test folders... 1. ['.pytest_cache/']
collected 0 items                                                                                                                    

==================================================== no tests ran in 0.05 seconds ====================================================

Pytest discovers tests by checking for test_*.py or *_test.py
See: https://docs.pytest.org/en/latest/goodpractices.html#test-discovery
So, a possible solution would be to use the built-in module pathlib to check the filenames and the file suffix combined with startswith and endswith. See: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.name

Filter picked files by folder/s if provided

Being able to run pytest --picked tests/unit/.
There are many use cases when it could be handy, in my case it is being able to run each test suite separately, or part of them: unit, integration and etc.
Easy one, I will be happy to contribute but would like to get feedback on the feature before spending time.

Run all other tests, after subset

When running with --cov and 100% coverage required, picked runs only a subset of tests, and coverage fails.

Long term it would be nice to make the coverage ignore all files not modified, so the coverage percentage is of the files being tested, however relationship between files modified and test coverage will make that very difficult.

But a quick fix is to have a mode that does run all of the tests, but reorders the tests to run the guessed modified test cases first, surfacing any errors sooner in the test suite.

"Guess" the test file from changed module

Let's say you've changed house/serializers.py. Would be nice to run the tests of house/test_serializers.py or even the tests that were related.

To keep in mind:

  • There is an option on pytest to specify test paths

exit code

Hello, thank you for making this library.
How can I set the exit code to 5 instead of 1 when no tests are collected exit-codes.
Thanks

image

Would you consider package design which enables extraction of core functionality?

I recently got interested in full-featured Test Impact Analysis in Python and I was thinking about creating a Python package python-tia which provides TIA functionality as new fun project. pytest is my Python test framework of choice as well. But I think TIA has a lot of potential which justifies the overhead of putting the core functionality of TIA into a separate package. This would enable integration of TIA with other test runners than pytest, other version control systems than git and probably even other coverage analysis tools than coverage. Are you interested in collaborating? If yes: Properly structured core functionality of pytest-picked could be refactored into python-tia leaving just the wrapping part in here...

Improve infos on README

Things to accomplish here:

  • Remove "TODO" session
  • Add gif with the demonstration of how to use it
  • Add some examples with git status output
  • Add badge for pypi
  • Replace _ picked_ for pytest-picked on LICENSE session

--mode=branch currently unusable with GitHub Actions checkout

Hi. It seems that at the moment this (very cool!) project isn't usable when run with --mode=branch using GitHub Actions checkout action (the start of pretty much every GHA workflow). If I attempt to use the something like the following

on:
  push:

jobs:
  test:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip setuptools wheel
        python -m pip install --ignore-installed -U -q --no-cache-dir -e .
        python -m pip list
    - name: Test with pytest
      run: |
        python -m pytest -r sx --picked=first --mode=branch

I'll get

fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

in the printed pytest logs. This is because the checkout action will checkout the current branch and then put the project under remotes/origin/. So adding in

    - name: Check what branches are available
      run: |
        git branch -a

would show something like

* name-of-branch-being-tested-in-pr
  remotes/origin/master

Would it be possible to look for some pattern that could match this if master isn't detected? Or allow for a branch name to given as an argument to test relative to?

Add a way to change the default parent branch

Hi, love the package, it's super useful!

I do have one feature proposal - at my org we've migrated all our repositories to use main as the primary branch (as opposed to master), meaning that by default using --mode=branch doesn't work, one has to always specify --parent-branch=main, which gets a bit old. I would love if there was a way to permanently set the default parent branch, maybe through an environment variable or configuration in pytest.ini or similar?

Thanks

Add option to run the changed files from the current branch

Let's say you are working on a feature branch and would like to run all the tests that were changed when compared with the master. To check it, you can run: git diff --name-only master.

Would be nice to have this option on picked. I'm wondering if something like pytest --picked=branch would be the best way to go.

Skip "Publish to Test PyPi" workflow when there is no change on setup.py

After merging a PR from dependencies or docs updates, for instance, the workflow "Publish to Test PyPi" will be triggered and it will fail because the version on setup.py is not bumped. This workflow is nice to have because it will generate a beta version so we can test and avoid surprises when the package is published to PyPi in production. Would be nice to skip the workflow when the setup.py is not updated.

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.