Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest throws typerror: positional arguments missing

I have taken code from official example:

import pytest
@pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

But it gives error:

.E
======================================================================
ERROR: test_config.test_eval
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: test_eval() missing 2 required positional arguments: 'test_input' and 'expected'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=1)

Error
Traceback (most recent call last):
  File "C:\Anaconda3\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Anaconda3\lib\unittest\case.py", line 601, in run
    testMethod()
  File "C:\Anaconda3\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: test_eval() missing 2 required positional arguments: 'test_input' and 'expected'


Process finished with exit code 1


pytest version 5.0.1

What is wrong here?

like image 845
Rocketq Avatar asked Aug 15 '19 14:08

Rocketq


2 Answers

I assume this is where OP has borrowed the code from: https://docs.pytest.org/en/latest/parametrize.html

You need to rename your file to test_expectation.py (as per documentation) and in the command line run command "pytest" (as per documentation), which will return expected output:

jonas@mint-vm /media/sf_Downloads $ mv mytest.py test_expectation.py
jonas@mint-vm /media/sf_Downloads $ pytest
============================================================================================================ test session starts =============================================================================================================
platform linux -- Python 3.5.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /media/sf_Downloads
collected 3 items                                                                                                                                                                                                                            

test_expectation.py ..F                                                                                                                                                                                                                [100%]

================================================================================================================== FAILURES ==================================================================================================================
_____________________________________________________________________________________________________________ test_eval[6*9-42] ______________________________________________________________________________________________________________

test_input = '6*9', expected = 42

    @pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6*9')

test_expectation.py:4: AssertionError
===================================================================================================== 1 failed, 2 passed in 0.54 seconds ======================================================================

Hope this helps.

Command "pytest" will run any python files (files ending with .py extension) with prefix "test", inside current directory.

like image 140
Jonas M Avatar answered Oct 22 '22 16:10

Jonas M


This is an example for Pytest whereas you are running it through Nose, which is a different (and incompatible) test framework for Python. To run tests in Pytest, you can call this from the command line:

pytest test_config.py

If you're trying to launch tests from an IDE, you'll need to configure it to use the correct test runner. For example, in PyCharm: Settings -> Python Integrated Tools -> Default Test Runner. Then update existing run configurations.

like image 1
Norrius Avatar answered Oct 22 '22 14:10

Norrius