Giter VIP home page Giter VIP logo

mwe-pytest-in-package's Introduction

Test case 1

In this case py.test fails to collect tests because import of modules fails. The failure comes in following steps:

  1. I'm on Windows, py.test is a part of Anaconda scientific installation. Cannot use pip for upgrades.
  2. My current folder is D:\mwe-pytest-in-package\setting_1
  3. Run py.test
  4. Getting the following:
>py.test
============================= test session starts =============================
platform win32 -- Python 3.4.3 -- py-1.4.26 -- pytest-2.6.4
collected 0 items / 1 errors

=================================== ERRORS ====================================
________________________ ERROR collecting test_foo.py _________________________
test_foo.py:1: in <module>
    from code import foo
E   ImportError: cannot import name 'foo'
=========================== 1 error in 0.08 seconds ===========================
  • For some reason py.test fails to import a module code while colleting a test test_foo.py.
  • Googling and StackOverflow were not much help yet.
  • Note that python code.py and python test_foo.py run without error.

##Solution Adding current working directory to PYTHONPATH helped starting py.test:

```set "PYTHONPATH=D:\mwe-pytest-in-package\setting_1"``

Remaining questions:

  • other way of making py.test do the imports?
  • why python interpreter does imports modules OK and py.test doesn't?

##Answer

  • One problem I found was that you're importing the module code if you type in another shell the following, there's actually a module called code in the standard library:

    C:\Users\Gabriele\Anaconda3\lib\code.py

  • One solution would be for example to rename the module to something else, for example code1.py, or hide it behind a package, so you can access it by its full name mypackage.code:

  mypackage/
    __init__.py
    code.py
  • As for why is this happening with pytest and not with calling the module directly has to do with absolute imports (import something). Apparently the "import priority" work differently when run as-a-script, and by setting the PYTHONPATH you're making the priority higher for the modules in the current directory.

You can test in the setting_2 directory if this is working properly.

mwe-pytest-in-package's People

Contributors

epogrebnyak avatar gabrielelanaro avatar

Watchers

 avatar

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.