Skip to content
← Back to Community
Running a file other than the main file for Unit Testing
Profile icon
LeonGrin

I created a test_main.py file to unit test the main.py file. But every time I click "Run", it only runs the main.py file. Is it possible to run a file other than the main file?

Voters
Profile icon
Lathryx
Profile icon
DavidEcklund
Profile icon
Chrisgopher
Profile icon
Nader213
Profile icon
pira245
Profile icon
prajitviswanadh
Profile icon
GIJoe143
Profile icon
JimLynch
Profile icon
emmadial
Profile icon
PatMyron
Comments
hotnewtop
Profile icon
mat1

You could import test_main at the beginning of main.py to run test_main.py.
Please upvote this comment if it was helpful or useful in any way

Profile icon
LeonGrin

@mat1 This import in the main file allows to run code of the test_main file when you call it on the main file. But what I want to do is to run the test_main file without writting calling it's code on the main file.

Profile icon
timmy_i_chen

@LeonGrin Right now all repls run the main file to start; there's no way around that. :(

Profile icon
LeonGrin

@timmy_i_chen thank you. Is it possible for 2 repls to interact with each other? Maybe this could be a workaround.

Profile icon
LeonGrin

Hi @timmy_i_chen
I am trying to run a unit testing code from the file test_main.py, as you can see here: https://repl.it/@LeonGrin/Unit-Testing-in-Python-with-Replit
This repl is very simple. I am trying to figure out a way to run unit testing on Repl.it without having to insert all the unit testing code on the main.py file. Do you know how can I do this?
I feel that it might exist a very simple solution to my problem that I am not seeing.

Profile icon
Aersum

I solved this problem like this:
Just use python os.system to get access to console commands

# main.py import os os.system('python3 -m pytest -v test_api.py')

In general case
os.system('python3 -m <name_of_file>.py')

Profile icon
abhijeetbhanjadeo

@Aersum , thanks that still works. Sample for others to refer https://replit.com/join/fredthut-abhijeetbhanjadeo

Profile icon
fandressouza

In case someone is looking to simply run a file use:

#main.py import os os.system('python3 <filename>.py')
Profile icon
m201713

@fandressouza
how did you do that?
if i click the run icon, it show me like this

sh: 1: cannot open Ex01: No such file

Profile icon
fandressouza

@m201713 make sure you remove "<>", if your file is called second.py you must write the following:

#main.py import os os.system('python3 second.py')
Profile icon
m201713

@fandressouza oh!!!
thank you

Profile icon
KarthikGunaseka

@fandressouza Thank you.... This solved my requirement...

Profile icon
Badal-and-Akash

¡help

Profile icon
MichaelShepansk

I ended up making my main look like this. Allows me to type run(<search>) in the console and it will execute the first filename that matches the search string.

# Replit doesn't let us just run any Python file in our project. So I # made a handy little function to us run files from the console on the # right. Just type `run(<search>)` in the console. import os import replit def find_script(search): search = str(search).strip().lower() for root, dirs, files in os.walk('.'): for file in files: filepath = os.path.join(root, file) if search in filepath.lower() and filepath.endswith('.py'): return filepath def run(search): filepath = find_script(search) if not filepath: raise SystemExit(f'No Python scripts match search: {search}') replit.clear() print(f'Running {os.path.basename(filepath)}\n') os.system(f'python3 {filepath}')
Profile icon
tarekben1

use
exec(open("filename.py").read())
in console

Profile icon
madhawag

Hi, I want to do the same thing for Java with Junit to seperate the Junit test running logic from main to seperate file. Could you please let me know the best approach to do it... This is for Java