Usually, we draw manual Control Flow Graph using pen and paper by analyzing the control flow of the program. CFG helps us finding independent paths (Cyclomatic Complexity), which leads to the number of test cases required to test the program. We can automate the CFG task using a Python library called pycfg. This library takes a Python script as input and gives graph as output.
We can use pycfg in 2 following ways
By Directly using the file
- Download pycfg tar file
- UnZip it
Note: pycfg.py file location after unzipping is
Let’s take whiletest.py file to get CFG.
Run below command on terminal.
python path_to/pycfg.py path_to/whiletest.py -d
This approach gives output in the form of Graph having Nodes, labeled Sentences, edges between nodes.
By importing library in the program
With help of importing linrary and tkinter, we can do way batter than just using pycfg.py file alone.
- Representing CFG instead of terminal.
- Find cyclomatic complexity also
Run the below command
sudo pip install pycfg
Once Done, using same whiletest.py for testing. We can run the following python program on whiletest.py.
python /path_to/this_file.py /path_to/whiletest.py
Below is the code –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course