Name 'pytest' is not defined. how to run testpy? - python

I have installed python with conda.
pytest --version
This is pytest version 3.0.5, imported from /home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py
My test script
def tc1():
given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
assert base64.b64encode(bytes.fromhex(given)) == expected
I have imported pytest
import pytest
I am trying some stuff with pytest.But when I try from Python shell,I have problems like this
testset.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'testset' is not defined
In my shell
pytest
<module 'pytest' from '/home/fabiano/anaconda3/lib/python3.6/site-packages/pytest.py'>
Where should I save testset.py file?

pytest does test discovery. The basic steps are well listed in their documentation
Name the file containing the test test_*.py or *_test.py.
Name the test functions in those files def test_*
For you test, place the following code in the file test_set.py:
import base64
def test_tc1():
given="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
expected=b"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
assert base64.b64encode(bytes.fromhex(given)) == expected
Navigate to the directory containing the file test_set.py and execute the pytest command:
pytest
Expected output:
user#pc:/tmp/test_dir $ pytest .
============================= test session starts ==============================
platform linux -- Python 3.5.2+, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: /tmp/test_dir , inifile: collected 1 items
test_set.py .
=========================== 1 passed in 0.01 seconds ===========================

Related

(Pytest-Xdist) ModuleNotFoundError: No module named 'execnet.rsync'

I have been trying to bundle my tests written on pytest as .exe file.
I have tried the code mentioned in the pytest documentation for including the third party plugins:
https://docs.pytest.org/en/latest/example/simple.html#freezing-pytest
My code is as follows:
import pytest
import xdist
import pytest_rerunfailures
if len(sys.argv) > 1 and sys.argv[1] == "--pytest":
sys.exit(pytest.main(sys.argv[2:], plugins=[xdist, pytest_rerunfailures]))
Interestingly, the code works for pytest_rerunfailures but when I run the .exe using -n 2, it shows the following error:
ERROR: usage: mainfile [options] [file_or_dir] [file_or_dir] [...]
mainfile: error: unrecognized arguments: -n
inifile: None
rootdir: A:\New folder (2)\Scripted\dist
Here mainfile is the name of the exe file.
Also, it would be worth mentioning that xdist command-line options work when the tests are run normally but when bundled as exe, the create this issue.
ModuleNotFoundError: No module named 'execnet.rsync'
Could someone help me out here?

Pytest not detecting FizzBuzzTest.py

This is my code in FizzBuzzTest.py
import pytest
# content of test_sample.py
def fizzBuzz(value):
return value
def test_returns1With1PassedIn():
assert fizzBuzz(1) == 1
When I run pytest -v in the command line
================================= 1 passed in 0.05 seconds ===================================================================
PS C:\Users\pytest\FizzBuzz_Kata> pytest -v
======================================== test session starts ================================================================================================
platform win32 -- Python 3.5.2, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- c:\users\a606143\appdata\local\programs\python\python35\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\pytest\FizzBuzz_Kata
collected 0 items
Could somebody please explain why pytest is not able to detect this file and run tests?
I have fiddled a little around and I seem to have found a solution, a little odd but it is working.
To have pytest to detect the file, it needs to be name with test_ infront of the filename, so your file should be named: test_FizzBussTest.py and your function which pytest have to execute needs to have that exact same name, so your function needs to be named as such:
def test_FizzBussTest():
Edit: After further researched, the function doesnt need to be named exactly as the file, it just needs to have test_ infront of the function name so fx test_sum():

Unable to run coverage tool using embedded python using the -m option or when copied to different path locations in windows

I am trying to run the python coverage tool using embedded python downloaded from https://www.python.org/ftp/python/3.6.1/python-3.6.1-embed-amd64.zip
The following are the steps I followed to run a test code using coverage module.
1) After downloading python-3.6.1-embed-amd64.zip, I uncompress the files to D:\some_directory\python-3.6.1-embed-amd64
2) cd python-3.6.1-embed-amd64
3) python get-pip.py
4) python -mpip install coverage
5) python-3.6.1-embed-amd64\python36._pth is changed to
python36.zip
Lib\site-packages
.
# Uncomment to run site.main() automatically
#import site
6) In case if there is an internet proxy to used then use the following command
SET HTTP_PROXY=http://username:password#proxy_ip:proxy_port
7) python -mpip install coverage
Create test files as follows
The file module_prog.py is as follows
# -*- coding: utf-8 -*-
"""
Dummy method for testing
"""
def method_to_tested() -> None:
"""
Method To be tested
"""
print("Called method to be tested")
The file test_module.py is as follows
""" Unit test script for testing File : test_module.py
"""
import unittest
from module_prog import method_to_tested
class TestMethods(unittest.TestCase):
""" Testing the methods """
def test_method_to_tested(self):
method_to_tested()
if __name__ == "__main__":
unittest.main(warnings='ignore')
8) I run the following
D:\some_directory\python-3.6.1-embed-amd64>python test_module.py
Called method to be tested
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
9) But if I run the instruction as mentioned below using converage I get the following error.
D:\some_directory\python-3.6.1-embed-amd64>python -mcoverage run test_module.py
Traceback (most recent call last):
File "test_module.py", line 4, in <module>
import unittest
ModuleNotFoundError: No module named 'unittest'
If I run the command
Scripts\converage.exe run test_module.py
Then coverage run successfully,
But if I move the embedded python to another directory and try to run the same command as above then I get the following error
D:\some_other_directory\python-3.6.1-embed-amd64>Scripts\coverage.exe run test_module.py
Fatal error in launcher: Unable to create process using
'"d:\some_directory\python-3.6.1-embed-amd64\python.exe" "D:\some_other_directory\python-3.6.1-embed-amd64\Scripts\coverage.exe" run test_module.py'
How can I make coverage run as a python module

ImportError: No module named 'code.helper'

This is probably a question that has been asked multiple times, but I couldn't find a post with the answer I am looking for.
This is my structure:
/myproject
--/code
--/__init__.py
--/helper.py
--/tests
--/test_helper.py
--/docs
code/helper.py
def testFunction(x)
return x + 1
test/test_helper.py
import unittest
from code.helper import testFunction
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(testFunction(3), 4)
Whenever I run 'python3 -m unittest test_helper.py' from ~/home/user/Projects/myproject/tests - I get the error:
ImportError: No module named 'code.helper'; 'code' is not a package
I am not receiving any syntax errors.
You don't have myproject in your Python path. It works when the current directory is myproject, because the current directory is always on the Python path. When you change to tests, though, Python no longer knows where to find code.
192% pwd
/Users/chepner/myproject
192% python3 -m unittest tests/test_helper.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
192% cd tests
192% pwd
/Users/chepner/myproject/tests
192% python3 -m unittest test_helper.py
E
======================================================================
ERROR: test_helper (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_helper
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/chepner/myproject/tests/test_helper.py", line 2, in <module>
from code.helper import testFunction
ModuleNotFoundError: No module named 'code.helper'; 'code' is not a package
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
The simple fix is, don't run the tests from the tests directory! Another solution, though, is to add myproject to the path before running the tests:
% PYTHONPATH=.. python3 -m unittest test_helper.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
You can add anything to PYTHONPATH that will correctly resolve to myproject; I just used the trivial relative path .. here, but an absolute path like PYTHONPATH=/Users/chepner/myproject would work, too.

ImportError: No module named Binary with pytest

I'm working through http://blog.thedigitalcatonline.com/blog/2015/05/13/python-oop-tdd-example-part1/#.Vw0NojFJJ9n .
When I try:
$ py.test
============================= test session starts =============================
platform win32 -- Python 3.2.5, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: C:\envs\r3\binary, inifile:
plugins: capturelog-0.7
collected 0 items / 1 errors
=================================== ERRORS ====================================
____________________ ERROR collecting tests/test_binary.py ____________________
tests\test_binary.py:3: in <module>
import Binary
E ImportError: No module named Binary
================= 1 pytest-warnings, 1 error in 0.20 seconds ==================
What am I doing wrong?
Add current directory to PYTHONPATH environmental variable.
As you are on Windows:
$ set PYTHONPATH="."
This shall help py.test to find and import the module.
Checking the py.test tutorial I see, that at "Writing the class" section they use exactly the same trick.
In practice, you do not have to do this, as you usually test against installed Python module (typically with setup.py in project root directory and using develop mode), and it is accessible for import easily without playing with PYTHONPATH.

Categories

Resources