logo
Tags down

shadow

pytest: How to test project-dependent directory creation?


By : Plumes
Date : October 18 2020, 08:10 PM
will help you Since you have already thought about providing a custom save path, you don't even need to mock anything if you don't want to; just pass the custom path derived from the tmp_path fixture:
code :
def test_library_creation(tmp_path):
    app = weather.WeatherApp()
    temp_data = app.get_data()
    lib_dir = tmp_path / 'library'
    # this check is somewhat redundant because pytest will ensure
    # the temp dir is created from scratch and is empty
    assert not lib_dir.is_dir()
    app.save(temp_data, save_path=str(lib_dir / 'data.json'))
    # Library should exist after save.
    assert lib_dir.is_dir()
    assert (lib_dir / 'data.json').is_file()
    shutil.rmtree(str(lib_dir))
app.save(temp_data, save_path=lib_dir / 'data.json')
shutil.rmtree(lib_dir)
import os
import shutil
import pytest

from weather import weather


@pytest.fixture
def lib_dir(tmp_path):
    d = tmp_path / 'library'
    yield d
    shutil.rmtree(str(d))


def test_library_creation(lib_dir):
    app = weather.WeatherApp()
    temp_data = app.get_data()
    assert not lib_dir.is_dir()
    app.save(temp_data, save_path=str(lib_dir))
    # Library should exist after save.
    assert lib_dir.is_dir()
    assert (lib_dir / 'data.json').is_file()
def test_library_creation(monkeypatch, lib_dir):
    app = weather.WeatherApp()
    temp_data = app.get_data()
    assert not lib_dir.is_dir()
    with monkeypatch.context() as m:
        m.setattr(weather, 'DEFAULT_SAVE_PATH', os.path.join(lib_dir, 'data.json'))
        app.save(temp_data)
    # Library should exist after save.
    assert lib_dir.is_dir()
    assert (lib_dir / 'data.json').is_file()


Share : facebook icon twitter icon

pytest How to locate input file for conftest.py in test directory when run test from parent directory


By : Werner Mayer
Date : March 29 2020, 07:55 AM
With these it helps Assuming that your syntax is correct (you are I think transposing the name and extension of the file) you just need to specify the location of the file correctly including relative or absolute path:
Instead of:
code :
py.test --test_option=input.conf
py.test --test_option=api/input.conf 

pytest: how to make dedicated test directory


By : Sridhar
Date : March 29 2020, 07:55 AM
Hope this helps An alternative way is making tests a module as well, by adding __init__.py file in folder tests. One of the most popular python lib requests https://github.com/kennethreitz/requests is doing in this way in their unit tests folder tests. You don't have to export PYTHONPATH to your project to execute tests.
The limitation is you have to run py.test command in directory 'test_pytest' in your example project.
code :
from t_pytest.sum import my_sum

Missing Directory src/test/java after creation of maven project in IntelliJ


By : Milan Prochazka
Date : March 29 2020, 07:55 AM
hop of those help? You should still be able to add src/test/java folder, and then right click on src/test/java and choose Mark Directory As > Test Sources Root.

How to run pytest with a specified test directory on a file that uses argparse?


By : user1588865
Date : March 29 2020, 07:55 AM
it fixes the issue parse_args() without argument reads the sys.argv[1:] list. That will include the 'tests' string.
pytests also uses that sys.argv[1:] with its own parser.
code :
def parse_args(argv=None):
    parser = argparse.ArgumentParser(description="description")

    parser.add_argument("--a", type=int, default=3)
    parser.add_argument("--b", type=int, default=5)

    return parser.parse_args(argv)
parse_args(['-a', '4'])
parse_args()

When pytest run all tests in a directory, how can it decide which test to run last?


By : goutham
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can easily change the default test execution order by implementing your own pytest_collection_modifyitems hook. Create a file conftest.py with the following contents:
code :
def pytest_collection_modifyitems(items):
    test_name = 'test_1'
    test_index = next((i for i, item in enumerate(items) if item.name == test_name), -1)
    test_item = items.pop(test_index)
    items.append(test_item)
Related Posts Related Posts :
  • Get mongod rs.status() results from a python script
  • ImportError: C extension: No module named 'parsing' not built
  • python pandas update column values related to previous updated row during iteration over it
  • 3 nested loops: Optimizing a simple simulation for speed
  • Assign subset of values to pandas dataframe with MultiIndex
  • How to group two sets of buttons on each top corner of the screen using Tkinter?
  • django login using class based for custom user
  • MRJob sort reducer output
  • Python Pandas Counts using rolling time window
  • Getting or editing a string from a column in a csv file with pandas
  • Python - Delete row in matrix/array if row contains
  • Using dicom Images with OpenCV in Python
  • Odoo ghost record
  • Creating and assigning multiple variables in a tkinter application
  • Graph dictionary
  • No changes to original dataframe after applying loop
  • AUC of Random forest model is lower after tuning parameters using hypergrid search and CV with 10 folds
  • Python: Reading multiple CSV files, and assigning each to a different variable
  • How to identify empty rectangle using OpenCV
  • How to iterate multilevel dataframe in python
  • How to limit the contour plot with a line plot?
  • Why subclassing a str or int behaves differently from subclising a list or dict?
  • Python decode with translation table
  • i need to click unordered links in the below URL using selenium, python
  • How to join pandas dataframe with itself?
  • How to apply a color cast to a video frame in OpenCV Python?
  • Is there any existing library for median filtering with kernel size greater then 5 using OpenCL acceleration in python?
  • Changing the color of points in scatter plot for different dummy values
  • Calculate center for each polygon in a list efficiently
  • Loading modules in the same Python package
  • replacing pixels in an imagewith pixels from another image python
  • Suggestion on picking the best options of two lists (minimum and maximum )python
  • Resetting Index in a Dataframe drops the Indexed column by 1 row
  • Convert number which are str from readlines to digits - python
  • Unable to authenitcate with python minds api
  • Print variables from a query in python
  • Ipython does not see the installed library
  • Javascript-like array-method chaining in Python?
  • PyQT: Get contents CustFormWidgetIem inside QListWidgetItem
  • Bottle server: HTTPResponse vs bottle.response
  • pytorch vgg model test on one image
  • Runtime scope and `main` symbol is different inside or outside a function
  • Use anaconda in pycharm (Import libraries error, updating anaconda and virtual environment)
  • how to get the sum of a CSV column list to print
  • Python plot drop lines with repeating value in column
  • receive binary file from POST request with BaseHTTPRequestHandler
  • D-Bus - 'ServiceUnknown' exception encountered while calling a remote procedure
  • Pandas .min() method doesn't seem fastest
  • Pandas: How to reference columns of structure: ('Name', n) ('Name', n+1)
  • Read a text file and remove all characters except alphabets & spaces in Python
  • Compute all powerset intersections of two lists
  • Applying literal_eval on string of lists of POS tags gives ValueError
  • Modelling a logic puzzle
  • What is the meaning of Copy_X in sklearn linear models
  • selenium.common.exceptions.ElementNotInteractableException: Message: Element is not displayed
  • pydev debugger does not stop in breakpoint
  • Python windows path regex
  • Flask and selenium-hub are not communicating when dockerised
  • How to use groupby on a single column and perform comparisons for multiple columns in Pandas?
  • Locate a python script without absolute path
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org