Ask coding questions

← Back to all posts
Running a file other than the main file for Unit Testing
LeonGrin (37)

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

mat1 (4381)

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

LeonGrin (37)

@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.

timmy_i_chen (1154)

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

LeonGrin (37)

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

LeonGrin (37)

Hi @timmy_i_chen
I am trying to run a unit testing code from the file, as you can see here:
This repl is very simple. I am trying to figure out a way to run unit testing on without having to insert all the unit testing code on the 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.

Aersum (7)

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

import os
os.system('python3 -m pytest -v')

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

fandressouza (5)

In case someone is looking to simply run a file use:
import os
os.system('python3 <filename>.py')
m201713 (0)

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

sh: 1: cannot open Ex01: No such file

fandressouza (5)

@m201713 make sure you remove "<>", if your file is called you must write the following:
import os
KarthikGunaseka (0)

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

tarekben1 (0)

in console

madhawag (1)

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