6

I need to check if my python unittest script is run by Visual studio test explorer, or if it is run by command line. Is there anything more than if len(sys.argv) >= 1?

AlesKas
  • 91
  • 1
  • 5
  • I would start with checking env variables when the tests are running by VS test explorer. Checking sys.argv is not the right approach as you may run these tests from command line with several arguments. – awesoon Sep 02 '19 at 09:16
  • 2
    Why is it important to know? – jonrsharpe Sep 02 '19 at 09:21
  • another way to go is by os.getppid() get the pid of your unittest script instance parent, then you can try to do a little wrapping on [find process name via pid](https://stackoverflow.com/questions/27211427/find-process-name-by-its-process-id) – James Li Sep 02 '19 at 09:30
  • i need to run two test siutes, one when it is run by VS, and second, when tests are run from jenkins job – AlesKas Sep 02 '19 at 09:39
  • You shouldn't tightly couple your tests to your IDE or a particular CI environment. Sounds like you need a proper feature-flag/config-file – OneCricketeer Aug 25 '21 at 16:53

1 Answers1

3

Look in os.environ.keys() for Visual Studio specific environment variables that are only present when running inside VS. See Detect where Python code is running (e.g., in Spyder interpreter vs. IDLE vs. cmd)

matt wilkie
  • 17,268
  • 24
  • 80
  • 115