0

I hope it isn't a duplicate as I looked around and could not find an answer.

I have a batch script witch uses python coverage tool to execute tests and measure coverage.

the scripts traverse through all sub folders of a main unit tests folder and execute tests.

The thing is, I need to call coverage run command from the same folder where my tests files are so I need to first change the batch current directory to the directory of the current test, for each test.

How can it be done?

This is my current script

    @echo off

    for /r %%f in (Test_*.py) do (

        echo current test (full path) is %%f

        :: I need a way to change CD to the directory of %%f
        :: before running the test. the problem is that %%f contains the file 
        :: name so I cant just `cd %f%`

        coverage run -a "%%f"
    )
    coverage report > "coverage_results.txt" 
    coverage erase 
    pause

Thanks

Noam
  • 1,640
  • 4
  • 26
  • 55
  • 1
    have you tried `cd %f%\..` ? ugly but works very well. – Jean-François Fabre Nov 11 '17 at 21:25
  • @Jean-FrançoisFabre no, it doesn't, it gives the parent folder, I dont need the parent folder, I need the current folder where %%f is but without the file name so that I can cd to it before execution. – Noam Nov 11 '17 at 21:30
  • Noam, you have not tried what he said – Thomas Weller Nov 11 '17 at 21:30
  • @ThomasWeller ok.. so I guess I'm missing something.. can you write complete example? I tried to copy & paste this line but wasn't helpful. – Noam Nov 11 '17 at 21:33
  • ok I closed that using my python badge. Maybe it's abusing, but the linked answer explains a lot of methods (including my .. trick BTW :)) – Jean-François Fabre Nov 11 '17 at 21:34
  • Please try. If you try you'll also find out that your batch file has another problem before the CD statement can be executed – Thomas Weller Nov 11 '17 at 21:34
  • 1
    If `%%f` is `c:\mytests\subfolder\Test_A.py`, then `%%f\..` is `c:\mytests\subfolder\Test_A.py\..` which is equivant to `c:\mytests\subfolder`. You'll not go to the parent directory – Thomas Weller Nov 11 '17 at 21:38
  • You should open up a cmd prompt and read the help for the FOR the command. The very last section is an eye opener. – Squashman Nov 11 '17 at 23:46
  • Noam, please also consider replying to your question from two days ago! – Compo Nov 12 '17 at 03:12
  • Thanks everyone, using `cd %%f\..` really does what I need. – Noam Nov 12 '17 at 12:35

0 Answers0