0

I am unable to import the main file inside my tests. Here is what my directory structure looks like:

.
├── LICENSE
├── README.md
├── invoker.py
├── main.py
├── requirements.txt
├── script.py
├── template_generators
│   ├── __init__.py
│   ├── base.py
│   ├── flipper.py
│   └── psp22.py
├── templates
│   ├── flipper
│   │   ├── cargo.txt
│   │   └── flipper.txt
│   └── psp22
│       ├── cargo.txt
│       └── psp22.txt
└── tests
    ├── template_generators
    │   ├── test_flipper.py
    │   └── test_psp22.py
    └── test_cli.py

I want to import main inside test_cli.py

Here is how main.py looks like

import typer

from invoker import Invoker

def main() -> None:
    
    print("Welcome to Ink Wizard!")
    print("Type 1 to scaffold flipper contract")
    print("Type 2 to scaffold psp22 contract")

    contract_type = typer.prompt("Enter your choice to continue")

    if contract_type == "1":
        Invoker.flipper()
        print("flipper scaffolded")
    if contract_type == "2":
        contract_name = typer.prompt("Please enter name of contract")
        Invoker.psp22(contract_name=contract_name, mintable=mintable, metadata=metadata, burnable=burnable, wrapper=wrapper, flashmint=flashmint, pausable=pausable, capped=capped)
        print("psp22 contract scaffolded")


if __name__ == "__main__":
    typer.run(main)

Here is my test_cli.py

import typer
from typer.testing import CliRunner

import main

app = typer.Typer()
app.command()(main)

runner = CliRunner()

def test_cli() -> None:
    result = runner.invoke(app, [])
    assert result.exit_code == 0

When I run python test_cli.py, here is the error I am getting:

ModuleNotFoundError: No module named 'main'

If anyone can give me an example on how to import main inside test_cli. That would be great. Thanks

avirajkhare00
  • 79
  • 2
  • 11
  • Does your PYTHONPATH points to the root of the main folder? Are you able to open a python console and run `import main`? – Meny Issakov Jan 11 '23 at 08:41
  • @MenyIssakov I am able to import main inside root directory. In the tests/ directory, I am unable to do so. – avirajkhare00 Jan 11 '23 at 08:43
  • How are you running your tests? – dmontaner Jan 11 '23 at 08:52
  • @dmontaner Tests that are inside the package are running fine. Problem is, main does not belong to any package so I am unable to import it. One way to test it to move `test_cli.py` in the root directory. – avirajkhare00 Jan 11 '23 at 08:53

1 Answers1

-1

quite dumb answer, but whatever. If you want just test your main. you can manually paste your main inside same folder where your test is located and then it should work.

Unknown _
  • 17
  • 3