FAQ Database Discussion Community


Create automated tests for interactive shell based on Python's cmd module

python,python-3.x,tdd,py.test,python-cmd
I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. I'd like to create more comprehensive tests that actually interact with the shell itself by simulating a user's...

Does Allure handle xfail, xpass, skip, and error from pytest?

py.test,allure
From the front page of allure.qatools.ru, it looks like these categories are created: Pending Passed Canceled Broken Failed How does this map to pytest categories? I'd like to see skipped, xpass, xfail, and error. Are they supported? If so, what is the mapping to allure categories?...

Get a list of all custom markers

python,python-3.x,py.test
If I have a simple test case with my own custom markers like: class TestClass: @pytest.mark.first def test_first(self): assert True @pytest.mark.second def test_second(self): assert True @pytest.mark.third def test_third(self): assert True How can I get the list of whole custom markers, because $ py.test -v --markers returns a list of predefined...

python - Flask test_client() doesn't have request.authorization with pytest

python,unit-testing,flask,py.test,werkzeug
I have problem when testing my flask app with pytest. App is required basic auth which is parameters of request.authorization in flask. But with pytest, flask.test_client() doesn't have request.authorization. Here's a code of fixture: @pytest.yield_fixture(scope='session') def app() app = create_app() # some setup code ctx = app.app_context() ctx.push() yield app...

pytest: paramaterized tests vs explicit tests

python,unit-testing,py.test
I'm new to pytest, and I'm looking to clear up a bit of confusion I have regarding paramaterized tests. Is it better (i.e. clearer, more DRY, more maintainable) to use a single parameterized test function, or multiple explicit tests that only differ in their 'input'? Let me give an example...

How to test that Flask is using test_client rather than client?

python,python-3.x,flask,py.test
I'm using Pytest fixtures with Flask. My application is instantiated using an application factory. #conftest.py @pytest.fixture(scope='session') def app(request): '''Session-wide test application''' app = create_app('testing') app.client = app.test_client() app_context = app.app_context() app_context.push() def teardown(): app_context.pop() request.addfinalizer(teardown) return app I wanted to verify that the app created by my fixture uses Flask's...

combine httpretty with pytest tmpdir

python,py.test,httpretty
The following pytest-test uses httpretty, to mock a request. It writes the fetched data to a file: import requests import httpretty import json from os import listdir from os.path import join @httpretty.activate def test_write_file_from_datasource(): tmpdir = './mytestdir' # mock the connection concert_url = 'http://apis.is/concerts' httpretty.register_uri(httpretty.GET, concert_url, body = json.dumps({'results': []}),...

Using mock to patch a non-existing attribute

python,unit-testing,testing,mocking,py.test
I'm trying to test a context manager that makes use of a class that uses some __getattr__ magic to resolve several attributes which don't actually exist on the class. I'm running into a problem where mock is raising an AttributeError when trying to patch the class. Simplified example of objects...

AttributeError: 'module' object has no attribute 'ensuretemp'

python,python-3.4,py.test
Following the example at http://py.readthedocs.org/en/latest/path.html#basic-interactive-example import py temppath = py.test.ensuretemp('py.path_documentation') Raises the error AttributeError: 'module' object has no attribute 'ensuretemp' Python version 3.4.3, py version 1.4.26. In [1]: import py In [2]: dir(py.test) Out[2]: ['Class', 'Collector', 'File', 'Function', 'Generator', 'Instance', 'Item', 'Module', 'Session', 'UsageError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',...

How to add a screenshot to allure report with python?

python,webdriver,py.test,allure
i have this code: # coding: utf-8 from selenium import webdriver import pytest import allure @pytest.yield_fixture(scope='session') def driver(): _driver = webdriver.PhantomJS() yield _driver _driver.quit() def test_ya(driver): with allure.step('open ya.ru and take screenshot'): driver.get('http://ya.ru/') allure.attach('screenshot', driver.get_screenshot_as_png(), type='png') and with this one i try to take a screenshot and save it to...

How can I show verbose py.test diffs without verbose test progress?

python,py.test
py.test's --verbose option is required to show full diffs on assertion failures, but this also displays the full name of each test during execution (which is noisy). I'd like full diffs to show when an assertion fails, but I only want single .'s to appear when the tests are running....

pytest test class calling class methods, Type error takes exactly 2 arguments (1 given)

python,unit-testing,py.test
I have a test class to test my methods but i have some issue with passing self, both of them are inside class and test class. my method: def get_all_links(self): """return all the links inside an html :return: list of links from an html page :rtype: list """ return self.html.find_all('a')...

pytest.mark.parameterize not “finding” fixtures

python,py.test
I'm writing tests for a small library and I decided to use py.test after hearing so many good things about it. However, pytest.mark.parameterize is giving me some issues. At first, I thought maybe I just mismatched some parens and it went off looking for a fixture elsewhere. So I decided...

Anyway to specify the type of report on .coveragerc?

xml,py.test,coverage.py
I'm trying to move as much as I can the configuration of pytest-cov on .coveragerc I need to replicate the --cov-report xml argument passed on the command line inside the file. I tried to use [xml] tag and output = whatever, then i found outfile and i used that one...

Changing the second result of a function call with mock

python,mocking,py.test,python-mock
I have a loop that looks like this: for i in range(len(some_list)): response = requests.post(some_url, some_params) if response.status_code != HTTPOk: # do something What I would like to do is change response of requests.post in the second iteration of the loop. From within my test, I know I can do...

How to run pytest.main(“-v test_script2.py”) when test_script2.py needs some command line arguments in order for the test functions to run?

python,command-line-arguments,py.test
I have two scripts. One script parses all the arguments and decides the control flow of the script. Another script has all the test functions defined and I wanted to use pytest module to invoke the 'test_script2.py' and run all the tests. But there are some arguments that I need...

Running one test with different versions of fixture

python,py.test
Let's say I have an application which must work with two different backends: backend_1 and backend_2. I have two test modules. Module test_backend_1.py: @py.test.fixture(scope="session") def backend_1(): return connect_to_backend_1() def test_contract_method_1(backend_1): result = run_contract_method_1() assert result == backend_1_method_1_get_data() def test_contract_method_2(backend_1): result = run_contract_method_2() assert result == backend_1_method_2_get_data() and test_backend_2.py: @py.test.fixture(scope="session") def...

Trying to understand pytest-xdist failures for parametrized tests

py.test
I have some parametrized tests that use a fixture defined in conftest. My tests are failing when pytest-xdist is used but pass with standard pytest. The fixture creates a directory which is then used by the test and I'm getting IOErrors. From the output, it looks like the two failing...

Using py.test --cov from inside setup.py pytest.main

python,py.test,test-coverage
I'm developing a package with some testing. Working with CMD: py.test --cov my_pkg I get the results with covarage: --------------- coverage: platform win32, python 2.7.9-final-0 ---------------- Name Stmts Miss Cover --------------------------------------------------- my_pkg\__init__ 8 0 100% my_pkg\general 2 0 100% --------------------------------------------------- TOTAL 10 0 100% Fails: when trying to integrate it...

Concatenate a list of series into a uid

python,python-2.7,pandas,py.test
I have a Pandas data frame with several columns that together make up a unique identifier. I want to write a generic test case that allows me to concatenate those columns together into a single column (uid) and test that column for uniqueness. I have the following code as a...

How to skip a pytest using an external fixture?

python,decorator,py.test,python-decorators
Background I am running a py.test with a fixture in a conftest file. You can see the code below(this all works fine): example_test.py import pytest @pytest.fixture def platform(): return "ios" @pytest.mark.skipif("platform == 'ios'") def test_ios(platform): if platform != 'ios': raise Exception('not ios') def test_android_external(platform_external): if platform_external != 'android': raise Exception('not...

py.test mixing fixtures and asyncio coroutines

postgresql,fixtures,py.test,python-decorators,python-asyncio
I am building some tests for python3 code using py.test. The code accesses a Postgresql Database using aiopg (Asyncio based interface to postgres). My main expectations: Every test case should have access to a new asyncio event loop. A test that runs too long will stop with a timeout exception....

Does @ddt work with py.test?

python,selenium,py.test,ddt
Does @ddt work with py.test or it must use unittest format? I have a tests where the setup fixture is in the conftest.py file. When I run the test, it error out because it didn't run the setup fixture. E.g: @ddt class Test_searchProd: @data(['clothes': 3],['shoes': 4]) @unpack def test_searchAllProduct(setup,productType): ........

Why can't test function see my py.test fixtures?

python,unit-testing,python-2.7,py.test
I have the following structure: demo/ conftest.py test_1.py test_2.py with conftest.py content being: import pytest @pytest.fixture() def my_fixture(): print "testing"* and test_1.py content being: def test_my_fixture(my_fixture): my_fixture and test_2.py content being: import pytest @pytest.mark.usefixtures('my_fixture') def test_my_fixture(): my_fixture When executed tests with py.test -s, I get NameError for the fixture in...

Include package in Spark local mode

python,apache-spark,py.test,pyspark
I'm writing some unit tests for my Spark code in python. My code depends on spark-csv. In production I use spark-submit --packages com.databricks:spark-csv_2.10:1.0.3 to submit my python script. I'm using pytest to run my tests with Spark in local mode: conf = SparkConf().setAppName('myapp').setMaster('local[1]') sc = SparkContext(conf=conf) My question is, since...

unittest vs pytest vs nose [closed]

python,nose,py.test,python-unittest
I'm new in python testing and am having some diffiulties choosing between unittest, pytest and nose nose. I'm bulding a test lib that requires : installing a package changing the conf restarting deamons and unsintalling the packages on a linux box. Which test framewok will be the best between thoses...

How to test the execution order of several functions in python?

python,unit-testing,py.test
I have several update functions. They must be executed in strict order. For example def update1(a1): do_something... def update2(a1, a2): do_something... def update3(a1): do_something... def process(a,b): update1(a) update2(a,b) update3(a) In process, all update functions must be executed in that order. How to write a unit test to test the order...

Pytest: how to take action on test failure?

py.test
I'm using pytest. I would like to gather/save some data for postmortem analysis on a test failure. I can write a teardown_method, but I don't see a way to obtain test status in that context. Is it possible to take an action on any test (or assertion) failure?...

How to get the maximum width and height coordinates of an iOS device display in Appium?

python,ios,instruments,appium,py.test
I am using the appium python-client library to run a [py.]test on iOS and Android devices. I noticed that many iOS tests crashed with tap point is not within the bounds of the screen. Looking into the tests is seemed weird as the supplied coordinates were seemingly within the reported...

Pytest - How to pass an argument to setup_class?

python,py.test
I have some code as shown below. I am getting a too few args error when I run it. I am not calling setup_class explicitly, so not sure how to pass any parameter to it. I tried decorating the method with @classmethod, but still see the same error. The error...

Python testing: Simulate ImportError

python,import,mocking,py.test
I have the following code try: from foo_fast import bar except ImportError from foo import bar def some_function(a, b): return foo(a, b) I now want to test the two cases where foo_fast could be imported and where it couldn't. Using pytest and pytest-mock, I naturally want to encapsulate the two...

py.test fails due to missing module

python,py.test
I would like to write a test, but this test will pass only if the nlopt module is installed. Since this module is optional, I was wondering if there's a way to write a test that won't prevent py.test to fail completely if the module is not present. at this...

Accessing pytest assert message in finalizer

python,py.test
I am trying to generate a custom report with pytest and trying to access the assert message generated by pytest, in the case of a failure, in the finalizer of a global fixture in the conftest.py file. I am able to access the status of the test but I am...

Should I test helper functions or only the main function?

python,unit-testing,testing,py.test
I am writing a small command line tool in python, which has two subcommands (like: git init or git clone). Those subcommands use a few helper functions, which are not exposed on the command line. When writing tests with py.test, does it make sense to test every helper function separately...

Isolating py.test DB sessions in Flask-SQLAlchemy

python,unit-testing,flask,flask-sqlalchemy,py.test
I'm trying to build a Flask app with Flask-SQLAlchemy; I use pytest to test the DB. One of the problems seems to be creating isolated DB sessions between different tests. I cooked up a minimal, complete example to highlight the problem, note that test_user_schema1() and test_user_schema2() are the same. Filename:...

Printing inside a method in python 3.x

python,python-3.x,methods,py.test
I would like to print something inside the method. But the following code does not anything. But if I return the values I am able to print it outside the class. Why is it so? Any guidance would be great ! My code: import pytest class Test: def __init__(self, val_1,...

How do I run pytest on all-modules?

python,nose,py.test
nose has an all-modules=1 configuration value which makes its test collector look in all files, not just the ones following its naming conventions: .http://nose.readthedocs.org/en/latest/plugins/allmodules.html What is the equivalent way to do this with pytest? i.e. I do not want to rename all our test modules when switching to pytest....

How to create a Test class with parameters?

python,py.test
Here is what the current test code looks like: def do_simulation(speed): df = run_simulation(speed) # following is a lot of assert that checks the values in df assert df["X"].iloc[0] == 0 assert df["Y"].iloc[-1] == 2 assert ... .... def test_simulation(): do_simulation(100) do_simulation(200) I want to convert the test code to...

How to run tests in order of their appearance in test script using nose?

nose,py.test,nosetests,python-unittest,python-nose
I want run my tests in order of they are written not in alphabetical order that unittest does by default. import unittest class test2(unittest.TestCase): def test1(self): pass def test0(self): pass class test1(unittest.TestCase): def testB(self): pass def testA(self): pass In this example I want to set unittest or nosetests to run...

pytest error (newbie to pytest and TDD)

python,linux,module,typeerror,py.test
I'm having an issue trying to test the length of an instance's variable. I keep getting this error: ________________________________________ ERROR collecting test_Person.py _________________________________________ test_Person.py:7: in <module> person1 = Person("Tara", "Manderson", "F") E TypeError: 'module' object is not callable ------------------------------------------------ Captured stdout ------------------------------------------------- gender is: F last name is: Manderson first...

How does pytest do test selection?

py.test,discovery
I am trying to figure out the expression syntax for py.test selection using the '-k' option. I have seen the examples, but I am unclear of what the syntax options are when using the 'k' tag. I am trying to scan the py.test source code, but so far no luck....