I have a program (file1.py) with functions and I want to test these functions from the file test1.py. When I import the first function I don’t know why the terminal tells me that I need to introduce the arguments that are required when I run file1.py. Is beyond my understanding why this happens because as far as I know from test1.py I am taking the first function and not the complete file1.py.
file1.py (until the first function)
JavaScript
x
31
31
1
import os
2
import argparse
3
import pandas as pd
4
import numpy as np
5
6
# Enter the path/file names
7
8
parser = argparse.ArgumentParser()
9
parser.add_argument('--vcf1', type=str, required=True)
10
parser.add_argument('--vcf2', type=str, required=True)
11
args = parser.parse_args()
12
NAME_FILE_1 = args.vcf1
13
NAME_FILE_2 = args.vcf2
14
15
16
def load_sample (Name_file):
17
'''
18
Take the header of the body of the CSV file
19
'''
20
with open(Name_file, 'r') as f:
21
for line in f:
22
if line.startswith('#') and len(line)>2 and line[1] != '#':
23
columns = line[1:-1].split('t')
24
data = pd.read_csv(Name_file, comment='#', delimiter='t', names=columns)
25
break
26
return data
27
28
# The data of the VCF is here
29
dataA = load_sample (NAME_FILE_1)
30
dataB = load_sample (NAME_FILE_2)
31
And my test1.py
JavaScript
1
17
17
1
import os
2
3
import pandas as pd
4
import numpy as np
5
6
from VCF_matcher.app.run import load_sample
7
8
9
NAME_FILE_1 = "./test_sample.vcf"
10
11
# FIRST TEST
12
13
def test_load_sample():
14
'''Verify all rows of the body of the vcf file is taken'''
15
data_to_test = load_sample (NAME_FILE_1)
16
assert len(data_to_test) == 10425
17
The output:
JavaScript
1
25
25
1
======================================================== ERRORS ========================================================
2
_________________________________________ ERROR collecting test_vcf_matcher.py _________________________________________
3
test_vcf_matcher.py:13: in <module>
4
from VCF_matcher.app.run import load_sample
5
../app/run.py:26: in <module>
6
args = parser.parse_args()
7
../../../opt/anaconda3/lib/python3.8/argparse.py:1768: in parse_args
8
args, argv = self.parse_known_args(args, namespace)
9
../../../opt/anaconda3/lib/python3.8/argparse.py:1800: in parse_known_args
10
namespace, args = self._parse_known_args(args, namespace)
11
../../../opt/anaconda3/lib/python3.8/argparse.py:2034: in _parse_known_args
12
self.error(_('the following arguments are required: %s') %
13
../../../opt/anaconda3/lib/python3.8/argparse.py:2521: in error
14
self.exit(2, _('%(prog)s: error: %(message)sn') % args)
15
../../../opt/anaconda3/lib/python3.8/argparse.py:2508: in exit
16
_sys.exit(status)
17
E SystemExit: 2
18
--------------------------------------------------- Captured stderr ----------------------------------------------------
19
usage: pytest [-h] --vcf1 VCF1 --vcf2 VCF2
20
pytest: error: the following arguments are required: --vcf1, --vcf2
21
=============================================== short test summary info ================================================
22
ERROR test_vcf_matcher.py - SystemExit: 2
23
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24
25
Advertisement
Answer
You have to structure file1.py
as follows if you don’t want to run the “main” part every time you import this file from some other Python file:
JavaScript
1
30
30
1
import os
2
import argparse
3
import pandas as pd
4
import numpy as np
5
6
7
def load_sample (Name_file):
8
'''
9
Take the header of the body of the CSV file
10
'''
11
with open(Name_file, 'r') as f:
12
for line in f:
13
if line.startswith('#') and len(line)>2 and line[1] != '#':
14
columns = line[1:-1].split('t')
15
data = pd.read_csv(Name_file, comment='#', delimiter='t', names=columns)
16
break
17
return data
18
19
20
if __name__ == "__main__":
21
parser = argparse.ArgumentParser()
22
parser.add_argument('--vcf1', type=str, required=True)
23
parser.add_argument('--vcf2', type=str, required=True)
24
args = parser.parse_args()
25
NAME_FILE_1 = args.vcf1
26
NAME_FILE_2 = args.vcf2
27
28
dataA = load_sample(NAME_FILE_1)
29
dataB = load_sample(NAME_FILE_2)
30
For a better explanation, see.