#πŸ”’ Struggling to import functions to run unittests

10 messages Β· Page 1 of 1 (latest)

keen bane
#

So here's my file structure
root

project
|-- src
    |-- example.py
|-- test
    |-- test_example.py

I know adding an __init__.py to each dir would help but I don't know what to put inside those

project
|-- src
    |-- __init__.py
    |-- example.py
|-- test
    |-- __init__.py
    |-- test_example.py

and for the sake of completeness, here are the contents of the files

example.py

def return_one():
    return 1

test_example.py

from src.example import return_one
import unittest


class TestExample(unittest.TestCase):
    def test_return_one(self):
        self.assertEqual(1, return_one())

I've also tried from from ..src.example import return_one

pallid rootBOT
#

@keen bane

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

chilly locust
#

Also, how are you running your unittests? What is the command you are using?

keen bane
#

I was using the vscode extension as well as just trying to discover tests using python -m unittest discover -s .\ -p test*

chilly locust
#

What error are you getting currently?

keen bane
#

mb, it started working. I tried switching to pytest for a minute then went back and it worked, I think it just wasn't quite updating. Also fixed the file path in an __init__.py in test/

Thank you!

#

!close

pallid rootBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.